|
3 | 3 | namespace Spatie\PrefixedIds\Tests; |
4 | 4 |
|
5 | 5 | use Spatie\PrefixedIds\Exceptions\NoPrefixConfiguredForModel; |
| 6 | +use Spatie\PrefixedIds\Exceptions\NoPrefixedModelFound; |
6 | 7 | use Spatie\PrefixedIds\PrefixedIds; |
7 | 8 | use Spatie\PrefixedIds\Tests\TestClasses\Models\OtherTestModel; |
8 | 9 | use Spatie\PrefixedIds\Tests\TestClasses\Models\TestModel; |
@@ -78,4 +79,44 @@ public function it_can_find_the_right_model_for_the_given_prefixed_id() |
78 | 79 | $nonExistingModel = PrefixedIds::find('non-existing-id'); |
79 | 80 | $this->assertNull($nonExistingModel); |
80 | 81 | } |
| 82 | + |
| 83 | + /** @test */ |
| 84 | + public function a_model_can_find_or_fail_a_record_with_the_given_prefixed_id() |
| 85 | + { |
| 86 | + $testModel = TestModel::create(); |
| 87 | + |
| 88 | + $foundModel = TestModel::findByPrefixedIdOrFail($testModel->prefixed_id); |
| 89 | + $this->assertEquals($testModel->id, $foundModel->id); |
| 90 | + } |
| 91 | + |
| 92 | + /** @test */ |
| 93 | + public function it_throws_exception_on_invalid_prefixed_id() |
| 94 | + { |
| 95 | + $this->expectException(NoPrefixedModelFound::class); |
| 96 | + |
| 97 | + TestModel::findByPrefixedIdOrFail('non_existing'); |
| 98 | + } |
| 99 | + |
| 100 | + // /** @test */ |
| 101 | + public function it_can_find_or_fail_the_right_model_for_the_given_prefixed_id() |
| 102 | + { |
| 103 | + $testModel = TestModel::create(); |
| 104 | + $otherTestModel = OtherTestModel::create(); |
| 105 | + |
| 106 | + $foundModel = PrefixedIds::findOrFail($testModel->prefixed_id); |
| 107 | + $this->assertInstanceOf(TestModel::class, $foundModel); |
| 108 | + $this->assertEquals($testModel->id, $foundModel->id); |
| 109 | + |
| 110 | + $otherFoundModel = PrefixedIds::findOrFail($otherTestModel->prefixed_id); |
| 111 | + $this->assertInstanceOf(OtherTestModel::class, $otherFoundModel); |
| 112 | + $this->assertEquals($testModel->id, $otherFoundModel->id); |
| 113 | + } |
| 114 | + |
| 115 | + // /** @test */ |
| 116 | + public function it_throws_exception_on_invalid_given_model_prefixed_id() |
| 117 | + { |
| 118 | + $this->expectException(NoPrefixedModelFound::class); |
| 119 | + |
| 120 | + PrefixedIds::findOrFail('non-existing-id'); |
| 121 | + } |
81 | 122 | } |
0 commit comments