|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Tempest\Integration\Database\ModelInspector; |
| 4 | + |
| 5 | +use Tempest\Database\Config\DatabaseDialect; |
| 6 | +use Tempest\Database\HasManyThrough; |
| 7 | +use Tempest\Database\PrimaryKey; |
| 8 | +use Tempest\Database\Table; |
| 9 | +use Tests\Tempest\Integration\FrameworkIntegrationTestCase; |
| 10 | + |
| 11 | +use function Tempest\Database\inspect; |
| 12 | + |
| 13 | +final class HasManyThroughTest extends FrameworkIntegrationTestCase |
| 14 | +{ |
| 15 | + public function test_has_many_through(): void |
| 16 | + { |
| 17 | + $model = inspect(model: HasManyThroughOwnerModel::class); |
| 18 | + $relation = $model->getRelation(name: 'targets'); |
| 19 | + |
| 20 | + $this->assertInstanceOf( |
| 21 | + expected: HasManyThrough::class, |
| 22 | + actual: $relation, |
| 23 | + ); |
| 24 | + $this->assertSame( |
| 25 | + expected: 'LEFT JOIN intermediate ON intermediate.owner_id = owner.id LEFT JOIN target ON target.intermediate_id = intermediate.id', |
| 26 | + actual: $relation |
| 27 | + ->getJoinStatement() |
| 28 | + ->compile(dialect: DatabaseDialect::SQLITE), |
| 29 | + ); |
| 30 | + } |
| 31 | + |
| 32 | + public function test_has_many_through_select_fields(): void |
| 33 | + { |
| 34 | + $model = inspect(model: HasManyThroughOwnerModel::class); |
| 35 | + $relation = $model->getRelation(name: 'targets'); |
| 36 | + |
| 37 | + $fields = $relation->getSelectFields(); |
| 38 | + |
| 39 | + $this->assertSame( |
| 40 | + expected: 'target.id AS `targets.id`', |
| 41 | + actual: $fields[0]->compile(DatabaseDialect::SQLITE), |
| 42 | + ); |
| 43 | + $this->assertSame( |
| 44 | + expected: 'target.intermediate_id AS `targets.intermediate_id`', |
| 45 | + actual: $fields[1]->compile(DatabaseDialect::SQLITE), |
| 46 | + ); |
| 47 | + $this->assertSame( |
| 48 | + expected: 'target.data AS `targets.data`', |
| 49 | + actual: $fields[2]->compile(DatabaseDialect::SQLITE), |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + public function test_has_many_through_with_parent(): void |
| 54 | + { |
| 55 | + $model = inspect(model: HasManyThroughOwnerModel::class); |
| 56 | + $relation = $model |
| 57 | + ->getRelation(name: 'targets') |
| 58 | + ->setParent(name: 'parent'); |
| 59 | + |
| 60 | + $this->assertSame( |
| 61 | + expected: 'target.data AS `parent.targets.data`', |
| 62 | + actual: $relation->getSelectFields()[2]->compile(DatabaseDialect::SQLITE), |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + public function test_has_many_through_with_custom_joins(): void |
| 67 | + { |
| 68 | + $model = inspect(model: HasManyThroughCustomOwnerModel::class); |
| 69 | + $relation = $model->getRelation(name: 'targets'); |
| 70 | + |
| 71 | + $this->assertInstanceOf( |
| 72 | + expected: HasManyThrough::class, |
| 73 | + actual: $relation, |
| 74 | + ); |
| 75 | + $this->assertSame( |
| 76 | + expected: 'LEFT JOIN custom_intermediate ON custom_intermediate.custom_owner_fk = custom_owner.custom_pk LEFT JOIN target ON target.custom_intermediate_fk = custom_intermediate.custom_intermediate_pk', |
| 77 | + actual: $relation |
| 78 | + ->getJoinStatement() |
| 79 | + ->compile(dialect: DatabaseDialect::SQLITE), |
| 80 | + ); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +#[Table(name: 'owner')] |
| 85 | +final class HasManyThroughOwnerModel |
| 86 | +{ |
| 87 | + public PrimaryKey $id; |
| 88 | + |
| 89 | + /** @var \Tests\Tempest\Integration\Database\ModelInspector\HasManyThroughTargetModel[] */ |
| 90 | + #[HasManyThrough(through: HasManyThroughIntermediateModel::class)] |
| 91 | + public array $targets = []; |
| 92 | + |
| 93 | + public string $name; |
| 94 | +} |
| 95 | + |
| 96 | +#[Table(name: 'intermediate')] |
| 97 | +final class HasManyThroughIntermediateModel |
| 98 | +{ |
| 99 | + public PrimaryKey $id; |
| 100 | + |
| 101 | + public HasManyThroughOwnerModel $owner; |
| 102 | + |
| 103 | + public string $value; |
| 104 | +} |
| 105 | + |
| 106 | +#[Table(name: 'target')] |
| 107 | +final class HasManyThroughTargetModel |
| 108 | +{ |
| 109 | + public PrimaryKey $id; |
| 110 | + |
| 111 | + public HasManyThroughIntermediateModel $intermediate; |
| 112 | + |
| 113 | + public string $data; |
| 114 | +} |
| 115 | + |
| 116 | +#[Table(name: 'custom_owner')] |
| 117 | +final class HasManyThroughCustomOwnerModel |
| 118 | +{ |
| 119 | + public PrimaryKey $custom_pk; |
| 120 | + |
| 121 | + /** @var \Tests\Tempest\Integration\Database\ModelInspector\HasManyThroughTargetModel[] */ |
| 122 | + #[HasManyThrough( |
| 123 | + through: HasManyThroughCustomIntermediateModel::class, |
| 124 | + ownerJoin: 'custom_owner_fk', |
| 125 | + relationJoin: 'custom_pk', |
| 126 | + throughOwnerJoin: 'custom_intermediate_fk', |
| 127 | + throughRelationJoin: 'custom_intermediate_pk', |
| 128 | + )] |
| 129 | + public array $targets = []; |
| 130 | + |
| 131 | + public string $name; |
| 132 | +} |
| 133 | + |
| 134 | +#[Table(name: 'custom_intermediate')] |
| 135 | +final class HasManyThroughCustomIntermediateModel |
| 136 | +{ |
| 137 | + public PrimaryKey $custom_intermediate_pk; |
| 138 | + |
| 139 | + public string $value; |
| 140 | +} |
0 commit comments