|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tests; |
| 6 | + |
| 7 | +use CodeIgniter\Test\DatabaseTestTrait; |
| 8 | +use Tests\Support\Database\Seeds\SeedTests; |
| 9 | +use Tests\Support\Entities\Country; |
| 10 | +use Tests\Support\Entities\Profile; |
| 11 | +use Tests\Support\Entities\StrictImage; |
| 12 | +use Tests\Support\Entities\StrictStudent; |
| 13 | +use Tests\Support\Entities\StrictUser; |
| 14 | +use Tests\Support\Entities\User; |
| 15 | +use Tests\Support\Models\CountryModel; |
| 16 | +use Tests\Support\Models\StrictImageModel; |
| 17 | +use Tests\Support\Models\StrictStudentModel; |
| 18 | +use Tests\Support\Models\StrictUserModel; |
| 19 | +use Tests\Support\TestCase; |
| 20 | + |
| 21 | +/** |
| 22 | + * @internal |
| 23 | + */ |
| 24 | +final class StrictEntityRelationsTest extends TestCase |
| 25 | +{ |
| 26 | + use DatabaseTestTrait; |
| 27 | + |
| 28 | + protected $refresh = true; |
| 29 | + protected $namespace; |
| 30 | + protected $seed = SeedTests::class; |
| 31 | + |
| 32 | + public function testEagerLoadsHasOneRelationOnStrictEntity(): void |
| 33 | + { |
| 34 | + $user = model(StrictUserModel::class)->with('profile')->find(1); |
| 35 | + |
| 36 | + $this->assertInstanceOf(StrictUser::class, $user); |
| 37 | + $this->assertInstanceOf(Profile::class, $user->profile); |
| 38 | + $this->assertSame('1', $user->profile->user_id); |
| 39 | + } |
| 40 | + |
| 41 | + public function testEagerLoadsHasManyRelationOnStrictEntity(): void |
| 42 | + { |
| 43 | + $user = model(StrictUserModel::class)->with('posts')->find(1); |
| 44 | + |
| 45 | + $this->assertInstanceOf(StrictUser::class, $user); |
| 46 | + $this->assertCount(2, $user->posts); |
| 47 | + $this->assertSame('Getting Started with CodeIgniter 4', $user->posts[0]->title); |
| 48 | + } |
| 49 | + |
| 50 | + public function testLazyLoadsHasOneRelationOnStrictEntity(): void |
| 51 | + { |
| 52 | + $user = model(StrictUserModel::class)->find(1); |
| 53 | + |
| 54 | + $this->assertInstanceOf(StrictUser::class, $user); |
| 55 | + $this->assertInstanceOf(Profile::class, $user->profile); |
| 56 | + $this->assertSame('1', $user->profile->user_id); |
| 57 | + } |
| 58 | + |
| 59 | + public function testLazyLoadsHasManyRelationOnStrictEntity(): void |
| 60 | + { |
| 61 | + $user = model(StrictUserModel::class)->find(1); |
| 62 | + |
| 63 | + $this->assertInstanceOf(StrictUser::class, $user); |
| 64 | + $this->assertCount(2, $user->posts); |
| 65 | + $this->assertSame('Getting Started with CodeIgniter 4', $user->posts[0]->title); |
| 66 | + } |
| 67 | + |
| 68 | + public function testAssociateUpdatesLoadedBelongsToRelationOnStrictEntity(): void |
| 69 | + { |
| 70 | + $user = model(StrictUserModel::class)->find(1); |
| 71 | + $country = model(CountryModel::class)->find(2); |
| 72 | + |
| 73 | + $this->assertInstanceOf(StrictUser::class, $user); |
| 74 | + $this->assertInstanceOf(Country::class, $country); |
| 75 | + |
| 76 | + $result = $user->country()->associate($country); |
| 77 | + |
| 78 | + $this->assertTrue($result); |
| 79 | + $this->assertSame('2', (string) $user->country_id); |
| 80 | + $this->assertInstanceOf(Country::class, $user->country); |
| 81 | + $this->assertSame('2', $user->country->id); |
| 82 | + } |
| 83 | + |
| 84 | + public function testEagerLoadsMorphToRelationOnStrictEntity(): void |
| 85 | + { |
| 86 | + $image = model(StrictImageModel::class)->with('imageable')->find(1); |
| 87 | + |
| 88 | + $this->assertInstanceOf(StrictImage::class, $image); |
| 89 | + $this->assertInstanceOf(User::class, $image->imageable); |
| 90 | + $this->assertSame('1', $image->imageable->id); |
| 91 | + } |
| 92 | + |
| 93 | + public function testLazyLoadsMorphToRelationOnStrictEntity(): void |
| 94 | + { |
| 95 | + $image = model(StrictImageModel::class)->find(1); |
| 96 | + |
| 97 | + $this->assertInstanceOf(StrictImage::class, $image); |
| 98 | + $this->assertInstanceOf(User::class, $image->imageable); |
| 99 | + $this->assertSame('1', $image->imageable->id); |
| 100 | + } |
| 101 | + |
| 102 | + public function testEagerLoadsBelongsToManyRelationOnStrictEntity(): void |
| 103 | + { |
| 104 | + $student = model(StrictStudentModel::class)->with('courses')->find(1); |
| 105 | + |
| 106 | + $this->assertInstanceOf(StrictStudent::class, $student); |
| 107 | + $this->assertCount(3, $student->courses); |
| 108 | + $this->assertSame('Introduction to Programming', $student->courses[0]->title); |
| 109 | + } |
| 110 | + |
| 111 | + public function testLazyLoadsBelongsToManyRelationOnStrictEntity(): void |
| 112 | + { |
| 113 | + $student = model(StrictStudentModel::class)->find(1); |
| 114 | + |
| 115 | + $this->assertInstanceOf(StrictStudent::class, $student); |
| 116 | + $this->assertCount(3, $student->courses); |
| 117 | + $this->assertSame('Introduction to Programming', $student->courses[0]->title); |
| 118 | + } |
| 119 | +} |
0 commit comments