From 9ec36836af04ff4c3315b8c045bc2de838bc3eed Mon Sep 17 00:00:00 2001 From: Francesco Danti Date: Thu, 15 Dec 2022 20:26:06 +0100 Subject: [PATCH 01/14] Add Join FakeModel idField as $foreignModelIdField --- src/Model/Join.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/Model/Join.php b/src/Model/Join.php index cb107794d..9b091f77d 100644 --- a/src/Model/Join.php +++ b/src/Model/Join.php @@ -85,6 +85,13 @@ abstract class Join /** @var array> Data indexed by spl_object_id(entity) which is populated here as the save/insert progresses. */ private array $saveBufferByOid = []; + /** + * Custom definition if $idField for of ForeignModel + * By Default ForeignModel idField is equal to 'id' + * To be used when the joined table doesn't have a field named 'id' + */ + public ?string $foreignModelIdField = null; + public function __construct(string $foreignTable) { $this->foreignTable = $foreignTable; @@ -107,6 +114,7 @@ protected function createFakeForeignModel(): Model { $fakeModel = new Model($this->getOwner()->getPersistence(), [ 'table' => $this->foreignTable, + 'idField' => $this->foreignModelIdField ]); foreach ($this->getOwner()->getFields() as $ownerField) { if ($ownerField->hasJoin() && $ownerField->getJoin()->shortName === $this->shortName) { From 2426c4d33836a6088f5ebbf1a94d197cf6aa4ade Mon Sep 17 00:00:00 2001 From: Francesco Danti Date: Thu, 15 Dec 2022 21:22:26 +0100 Subject: [PATCH 02/14] Add Tests for Joining tables on non-id fields --- tests/ConditionSqlTest.php | 57 ++++++++++ tests/JoinSqlTest.php | 227 +++++++++++++++++++++++++++++++++++++ 2 files changed, 284 insertions(+) diff --git a/tests/ConditionSqlTest.php b/tests/ConditionSqlTest.php index 3e9e97d87..d3e3f00d3 100644 --- a/tests/ConditionSqlTest.php +++ b/tests/ConditionSqlTest.php @@ -286,6 +286,63 @@ public function testExpressionJoin(): void static::assertSame('+123 smiths', $mm2->get('contact_phone')); } + public function testExpressionJoinForeignCustomIdField(): void + { + $this->setDb([ + 'user' => [ + 1 => ['id' => 1, 'name' => 'John', 'surname' => 'Smith', 'gender' => 'M', 'contact_id' => 1], + 2 => ['id' => 2, 'name' => 'Sue', 'surname' => 'Sue', 'gender' => 'F', 'contact_id' => 2], + 3 => ['id' => 3, 'name' => 'Peter', 'surname' => 'Smith', 'gender' => 'M', 'contact_id' => 1], + ], + 'contact' => [ + 1 => ['custom_id' => 1, 'contact_id' => 1, 'contact_phone' => '+123 smiths'], + 2 => ['custom_id' => 2, 'contact_id' => 2, 'contact_phone' => '+321 sues'], + ], + ]); + + $m = new Model($this->db, ['table' => 'user']); + $m->addField('name'); + $m->addField('gender'); + $m->addField('surname'); + + $m->join('contact', [ + 'masterField' => 'contact_id', + 'foreignField' => 'contact_id', + 'foreignModelIdField' => 'custom_id' + ])->addField('contact_phone'); + + $mm2 = $m->load(1); + static::assertSame('John', $mm2->get('name')); + static::assertSame('+123 smiths', $mm2->get('contact_phone')); + $mm2 = $m->load(2); + static::assertSame('Sue', $mm2->get('name')); + static::assertSame('+321 sues', $mm2->get('contact_phone')); + $mm2 = $m->load(3); + static::assertSame('Peter', $mm2->get('name')); + static::assertSame('+123 smiths', $mm2->get('contact_phone')); + + $mm = clone $m; + $mm->addCondition($mm->expr('[name] = [surname]')); + $mm2 = $mm->tryLoad(1); + static::assertNull($mm2); + $mm2 = $mm->load(2); + static::assertSame('Sue', $mm2->get('name')); + static::assertSame('+321 sues', $mm2->get('contact_phone')); + $mm2 = $mm->tryLoad(3); + static::assertNull($mm2); + + $mm = clone $m; + $mm->addCondition($mm->expr('\'+123 smiths\' = [contact_phone]')); + $mm2 = $mm->load(1); + static::assertSame('John', $mm2->get('name')); + static::assertSame('+123 smiths', $mm2->get('contact_phone')); + $mm2 = $mm->tryLoad(2); + static::assertNull($mm2); + $mm2 = $mm->load(3); + static::assertSame('Peter', $mm2->get('name')); + static::assertSame('+123 smiths', $mm2->get('contact_phone')); + } + public function testArrayCondition(): void { $this->setDb([ diff --git a/tests/JoinSqlTest.php b/tests/JoinSqlTest.php index 3e462f956..b4dda714b 100644 --- a/tests/JoinSqlTest.php +++ b/tests/JoinSqlTest.php @@ -6,6 +6,7 @@ use Atk4\Data\Exception; use Atk4\Data\Model; +use Atk4\Data\Persistence; use Atk4\Data\Schema\TestCase; use Doctrine\DBAL\Platforms\MySQLPlatform; @@ -762,4 +763,230 @@ public function testJoinActualFieldNamesAndPrefix(): void ], ], $this->getDb()); } + + public function testJoinSavingForeignCustomIdFieldRaiseException(): void + { + $this->expectException(\Atk4\Data\Persistence\Sql\ExecuteException::class); + + $master_model = new \Atk4\Data\Model($this->db, [ + 'table' => 'user_join', + ]); + $master_model->addField('name'); + $master_model->addField('test_id'); + + $joined_model = new \Atk4\Data\Model($this->db, [ + 'table' => 'contact_join', + 'idField' => 'not_named_id', + ]); + $joined_model->addField('contact_id'); + $joined_model->addField('contact_phone'); + + $this->createMigrator($master_model)->create(); + $this->createMigrator($joined_model)->create(); + + $master_model->import([ + [ + 'id' => 1, + 'name' => 'John', + 'test_id' => 21, + ], + ]); + + $joined_model->import([ + [ + 'not_named_id' => 1, + 'contact_id' => 21, + 'contact_phone' => '+123', + ], + ]); + + $user = new Model($this->db, ['table' => 'user_join']); + $user->addField('name'); + $j = $user->join('contact_join', [ + 'foreignTable' => 'contact_join', + 'masterField' => 'test_id', + 'foreignField' => 'contact_id', + //'foreignModelIdField' => 'not_named_id', + ]); + $this->createMigrator()->createForeignKey($j); + $j->addField('contact_phone'); + + $user = $user->load(1); + + $user->set('name', 'John'); + $user->set('contact_phone', '+321'); + + $user->save(); + } + + public function testJoinSavingForeignCustomIdField(): void + { + $master_model = new \Atk4\Data\Model($this->db, [ + 'table' => 'user_join', + ]); + $master_model->addField('name'); + $master_model->addField('test_id'); + + $joined_model = new \Atk4\Data\Model($this->db, [ + 'table' => 'contact_join', + 'idField' => 'not_named_id', + ]); + $joined_model->addField('contact_id'); + $joined_model->addField('contact_phone'); + + $this->createMigrator($master_model)->create(); + $this->createMigrator($joined_model)->create(); + + $master_model->import([ + [ + 'id' => 1, + 'name' => 'John', + 'test_id' => 21, + ], + ]); + + $joined_model->import([ + [ + 'not_named_id' => 1, + 'contact_id' => 21, + 'contact_phone' => '+123', + ], + ]); + + $user = new Model($this->db, ['table' => 'user_join']); + $user->addField('name'); + $j = $user->join('contact_join', [ + 'masterField' => 'test_id', + 'foreignField' => 'contact_id', + 'foreignModelIdField' => 'not_named_id', + ]); + $this->createMigrator()->createForeignKey($j); + $j->addField('contact_phone'); + + $user = $user->load(1); + + $user->set('name', 'John'); + $user->set('contact_phone', '+321'); + + $user->save(); + + static::assertSame([ + 'user_join' => [ + 1 => ['id' => 1, 'name' => 'John', 'test_id' => '21'], + ], + 'contact_join' => [ + 1 => ['not_named_id' => 1, 'contact_id' => '21', 'contact_phone' => '+321'], + ], + ], [ + 'user_join' => [ + 1 => $master_model->load(1)->get(), + ], + 'contact_join' => [ + 1 => $joined_model->load(1)->get(), + ], + ]); + } + + public function testJoinDeleteForeignCustomIdField(): void + { + $master_model = new \Atk4\Data\Model($this->db, [ + 'table' => 'user_join', + ]); + $master_model->addField('name'); + $master_model->addField('test_id'); + + $this->createMigrator($master_model)->create(); + + $master_model->import([ + [ + 'id' => 1, + 'name' => 'John', + 'test_id' => 21, + ], + ]); + + $joined_model = new \Atk4\Data\Model($this->db, [ + 'table' => 'contact_join', + 'idField' => 'not_named_id', + ]); + $joined_model->addField('contact_id'); + $joined_model->addField('contact_phone'); + + $this->createMigrator($joined_model)->create(); + + $joined_model->import([ + [ + 'not_named_id' => 1, + 'contact_id' => 21, + 'contact_phone' => '+123', + ], + ]); + + $user = new Model($this->db, ['table' => 'user_join']); + $user->addField('name'); + $j = $user->join('contact_join', [ + 'masterField' => 'test_id', + 'foreignField' => 'contact_id', + 'foreignModelIdField' => 'not_named_id', + ]); + $this->createMigrator()->createForeignKey($j); + $j->addField('contact_phone'); + + $user->delete(1); + + static::assertSame(null, $master_model->tryLoad(1)); + static::assertSame(null, $joined_model->tryLoad(1)); + } + + public function testJoinDeleteForeignCustomIdFieldReverse(): void + { + $master_model = new \Atk4\Data\Model($this->db, [ + 'table' => 'user_join', + ]); + $master_model->addField('name'); + $master_model->addField('test_id'); + + $this->createMigrator($master_model)->create(); + + $master_model->import([ + [ + 'id' => 1, + 'name' => 'John', + 'test_id' => 21, + ], + ]); + + $joined_model = new \Atk4\Data\Model($this->db, [ + 'table' => 'contact_join', + 'idField' => 'not_named_id', + ]); + $joined_model->addField('contact_id'); + $joined_model->addField('contact_phone'); + + $this->createMigrator($joined_model)->create(); + + $joined_model->import([ + [ + 'not_named_id' => 1, + 'contact_id' => 21, + 'contact_phone' => '+123', + ], + ]); + + $user = new Model($this->db, ['table' => 'user_join']); + $user->addField('name'); + $j = $user->join('contact_join', [ + 'masterField' => 'test_id', + 'foreignField' => 'contact_id', + 'foreignModelIdField' => 'not_named_id', + 'reverse' => true, + ]); + $this->createMigrator()->createForeignKey($j); + $j->addField('contact_phone'); + + $user->delete(1); + + static::assertSame(null, $master_model->tryLoad(1)); + static::assertSame(null, $joined_model->tryLoad(1)); + } } From eed8f7fa9b25db20716865da5532c561efdd708e Mon Sep 17 00:00:00 2001 From: Francesco Danti Date: Thu, 15 Dec 2022 23:02:19 +0100 Subject: [PATCH 03/14] Fix requested changes --- src/Model/Join.php | 16 ++--- tests/ConditionSqlTest.php | 57 ---------------- tests/JoinSqlTest.php | 132 ++++++++++++++++++------------------- 3 files changed, 74 insertions(+), 131 deletions(-) diff --git a/src/Model/Join.php b/src/Model/Join.php index 9b091f77d..c57e202bb 100644 --- a/src/Model/Join.php +++ b/src/Model/Join.php @@ -73,6 +73,13 @@ abstract class Join */ public ?string $foreignField = null; + /** + * Custom definition if $idField for of ForeignModel + * By Default ForeignModel idField is equal to 'id' + * To be used when the joined table doesn't have a field named 'id' + */ + public ?string $foreignIdField = null; + /** * When $prefix is set, then all the fields generated through * our wrappers will be automatically prefixed inside the model. @@ -85,13 +92,6 @@ abstract class Join /** @var array> Data indexed by spl_object_id(entity) which is populated here as the save/insert progresses. */ private array $saveBufferByOid = []; - /** - * Custom definition if $idField for of ForeignModel - * By Default ForeignModel idField is equal to 'id' - * To be used when the joined table doesn't have a field named 'id' - */ - public ?string $foreignModelIdField = null; - public function __construct(string $foreignTable) { $this->foreignTable = $foreignTable; @@ -114,7 +114,7 @@ protected function createFakeForeignModel(): Model { $fakeModel = new Model($this->getOwner()->getPersistence(), [ 'table' => $this->foreignTable, - 'idField' => $this->foreignModelIdField + 'idField' => $this->foreignIdField ]); foreach ($this->getOwner()->getFields() as $ownerField) { if ($ownerField->hasJoin() && $ownerField->getJoin()->shortName === $this->shortName) { diff --git a/tests/ConditionSqlTest.php b/tests/ConditionSqlTest.php index d3e3f00d3..3e9e97d87 100644 --- a/tests/ConditionSqlTest.php +++ b/tests/ConditionSqlTest.php @@ -286,63 +286,6 @@ public function testExpressionJoin(): void static::assertSame('+123 smiths', $mm2->get('contact_phone')); } - public function testExpressionJoinForeignCustomIdField(): void - { - $this->setDb([ - 'user' => [ - 1 => ['id' => 1, 'name' => 'John', 'surname' => 'Smith', 'gender' => 'M', 'contact_id' => 1], - 2 => ['id' => 2, 'name' => 'Sue', 'surname' => 'Sue', 'gender' => 'F', 'contact_id' => 2], - 3 => ['id' => 3, 'name' => 'Peter', 'surname' => 'Smith', 'gender' => 'M', 'contact_id' => 1], - ], - 'contact' => [ - 1 => ['custom_id' => 1, 'contact_id' => 1, 'contact_phone' => '+123 smiths'], - 2 => ['custom_id' => 2, 'contact_id' => 2, 'contact_phone' => '+321 sues'], - ], - ]); - - $m = new Model($this->db, ['table' => 'user']); - $m->addField('name'); - $m->addField('gender'); - $m->addField('surname'); - - $m->join('contact', [ - 'masterField' => 'contact_id', - 'foreignField' => 'contact_id', - 'foreignModelIdField' => 'custom_id' - ])->addField('contact_phone'); - - $mm2 = $m->load(1); - static::assertSame('John', $mm2->get('name')); - static::assertSame('+123 smiths', $mm2->get('contact_phone')); - $mm2 = $m->load(2); - static::assertSame('Sue', $mm2->get('name')); - static::assertSame('+321 sues', $mm2->get('contact_phone')); - $mm2 = $m->load(3); - static::assertSame('Peter', $mm2->get('name')); - static::assertSame('+123 smiths', $mm2->get('contact_phone')); - - $mm = clone $m; - $mm->addCondition($mm->expr('[name] = [surname]')); - $mm2 = $mm->tryLoad(1); - static::assertNull($mm2); - $mm2 = $mm->load(2); - static::assertSame('Sue', $mm2->get('name')); - static::assertSame('+321 sues', $mm2->get('contact_phone')); - $mm2 = $mm->tryLoad(3); - static::assertNull($mm2); - - $mm = clone $m; - $mm->addCondition($mm->expr('\'+123 smiths\' = [contact_phone]')); - $mm2 = $mm->load(1); - static::assertSame('John', $mm2->get('name')); - static::assertSame('+123 smiths', $mm2->get('contact_phone')); - $mm2 = $mm->tryLoad(2); - static::assertNull($mm2); - $mm2 = $mm->load(3); - static::assertSame('Peter', $mm2->get('name')); - static::assertSame('+123 smiths', $mm2->get('contact_phone')); - } - public function testArrayCondition(): void { $this->setDb([ diff --git a/tests/JoinSqlTest.php b/tests/JoinSqlTest.php index b4dda714b..976693f16 100644 --- a/tests/JoinSqlTest.php +++ b/tests/JoinSqlTest.php @@ -766,25 +766,23 @@ public function testJoinActualFieldNamesAndPrefix(): void public function testJoinSavingForeignCustomIdFieldRaiseException(): void { - $this->expectException(\Atk4\Data\Persistence\Sql\ExecuteException::class); - - $master_model = new \Atk4\Data\Model($this->db, [ + $masterModel = new \Atk4\Data\Model($this->db, [ 'table' => 'user_join', ]); - $master_model->addField('name'); - $master_model->addField('test_id'); + $masterModel->addField('name'); + $masterModel->addField('test_id'); - $joined_model = new \Atk4\Data\Model($this->db, [ + $joinedModel = new \Atk4\Data\Model($this->db, [ 'table' => 'contact_join', 'idField' => 'not_named_id', ]); - $joined_model->addField('contact_id'); - $joined_model->addField('contact_phone'); + $joinedModel->addField('contact_id'); + $joinedModel->addField('contact_phone'); - $this->createMigrator($master_model)->create(); - $this->createMigrator($joined_model)->create(); + $this->createMigrator($masterModel)->create(); + $this->createMigrator($joinedModel)->create(); - $master_model->import([ + $masterModel->import([ [ 'id' => 1, 'name' => 'John', @@ -792,7 +790,7 @@ public function testJoinSavingForeignCustomIdFieldRaiseException(): void ], ]); - $joined_model->import([ + $joinedModel->import([ [ 'not_named_id' => 1, 'contact_id' => 21, @@ -806,7 +804,7 @@ public function testJoinSavingForeignCustomIdFieldRaiseException(): void 'foreignTable' => 'contact_join', 'masterField' => 'test_id', 'foreignField' => 'contact_id', - //'foreignModelIdField' => 'not_named_id', + //'foreignIdField' => 'not_named_id', // Raise exception ]); $this->createMigrator()->createForeignKey($j); $j->addField('contact_phone'); @@ -816,28 +814,30 @@ public function testJoinSavingForeignCustomIdFieldRaiseException(): void $user->set('name', 'John'); $user->set('contact_phone', '+321'); + $this->expectException(\Atk4\Data\Persistence\Sql\ExecuteException::class); + $user->save(); } public function testJoinSavingForeignCustomIdField(): void { - $master_model = new \Atk4\Data\Model($this->db, [ + $masterModel = new \Atk4\Data\Model($this->db, [ 'table' => 'user_join', ]); - $master_model->addField('name'); - $master_model->addField('test_id'); + $masterModel->addField('name'); + $masterModel->addField('test_id'); - $joined_model = new \Atk4\Data\Model($this->db, [ + $joinedModel = new \Atk4\Data\Model($this->db, [ 'table' => 'contact_join', 'idField' => 'not_named_id', ]); - $joined_model->addField('contact_id'); - $joined_model->addField('contact_phone'); + $joinedModel->addField('contact_id'); + $joinedModel->addField('contact_phone'); - $this->createMigrator($master_model)->create(); - $this->createMigrator($joined_model)->create(); + $this->createMigrator($masterModel)->create(); + $this->createMigrator($joinedModel)->create(); - $master_model->import([ + $masterModel->import([ [ 'id' => 1, 'name' => 'John', @@ -845,7 +845,7 @@ public function testJoinSavingForeignCustomIdField(): void ], ]); - $joined_model->import([ + $joinedModel->import([ [ 'not_named_id' => 1, 'contact_id' => 21, @@ -856,9 +856,9 @@ public function testJoinSavingForeignCustomIdField(): void $user = new Model($this->db, ['table' => 'user_join']); $user->addField('name'); $j = $user->join('contact_join', [ - 'masterField' => 'test_id', - 'foreignField' => 'contact_id', - 'foreignModelIdField' => 'not_named_id', + 'masterField' => 'test_id', + 'foreignField' => 'contact_id', + 'foreignIdField' => 'not_named_id', ]); $this->createMigrator()->createForeignKey($j); $j->addField('contact_phone'); @@ -879,25 +879,33 @@ public function testJoinSavingForeignCustomIdField(): void ], ], [ 'user_join' => [ - 1 => $master_model->load(1)->get(), + 1 => $masterModel->load(1)->get(), ], 'contact_join' => [ - 1 => $joined_model->load(1)->get(), + 1 => $joinedModel->load(1)->get(), ], ]); } public function testJoinDeleteForeignCustomIdField(): void { - $master_model = new \Atk4\Data\Model($this->db, [ + $masterModel = new \Atk4\Data\Model($this->db, [ 'table' => 'user_join', ]); - $master_model->addField('name'); - $master_model->addField('test_id'); + $masterModel->addField('name'); + $masterModel->addField('test_id'); + + $joinedModel = new \Atk4\Data\Model($this->db, [ + 'table' => 'contact_join', + 'idField' => 'not_named_id', + ]); + $joinedModel->addField('contact_id'); + $joinedModel->addField('contact_phone'); - $this->createMigrator($master_model)->create(); + $this->createMigrator($masterModel)->create(); + $this->createMigrator($joinedModel)->create(); - $master_model->import([ + $masterModel->import([ [ 'id' => 1, 'name' => 'John', @@ -905,16 +913,7 @@ public function testJoinDeleteForeignCustomIdField(): void ], ]); - $joined_model = new \Atk4\Data\Model($this->db, [ - 'table' => 'contact_join', - 'idField' => 'not_named_id', - ]); - $joined_model->addField('contact_id'); - $joined_model->addField('contact_phone'); - - $this->createMigrator($joined_model)->create(); - - $joined_model->import([ + $joinedModel->import([ [ 'not_named_id' => 1, 'contact_id' => 21, @@ -925,30 +924,40 @@ public function testJoinDeleteForeignCustomIdField(): void $user = new Model($this->db, ['table' => 'user_join']); $user->addField('name'); $j = $user->join('contact_join', [ - 'masterField' => 'test_id', - 'foreignField' => 'contact_id', - 'foreignModelIdField' => 'not_named_id', + 'masterField' => 'test_id', + 'foreignField' => 'contact_id', + 'foreignIdField' => 'not_named_id', ]); + $this->createMigrator()->createForeignKey($j); + $j->addField('contact_phone'); $user->delete(1); - static::assertSame(null, $master_model->tryLoad(1)); - static::assertSame(null, $joined_model->tryLoad(1)); + static::assertSame(null, $masterModel->tryLoad(1)); + static::assertSame(null, $joinedModel->tryLoad(1)); } public function testJoinDeleteForeignCustomIdFieldReverse(): void { - $master_model = new \Atk4\Data\Model($this->db, [ + $masterModel = new \Atk4\Data\Model($this->db, [ 'table' => 'user_join', ]); - $master_model->addField('name'); - $master_model->addField('test_id'); + $masterModel->addField('name'); + $masterModel->addField('test_id'); + + $joinedModel = new \Atk4\Data\Model($this->db, [ + 'table' => 'contact_join', + 'idField' => 'not_named_id', + ]); + $joinedModel->addField('contact_id'); + $joinedModel->addField('contact_phone'); - $this->createMigrator($master_model)->create(); + $this->createMigrator($masterModel)->create(); + $this->createMigrator($joinedModel)->create(); - $master_model->import([ + $masterModel->import([ [ 'id' => 1, 'name' => 'John', @@ -956,16 +965,7 @@ public function testJoinDeleteForeignCustomIdFieldReverse(): void ], ]); - $joined_model = new \Atk4\Data\Model($this->db, [ - 'table' => 'contact_join', - 'idField' => 'not_named_id', - ]); - $joined_model->addField('contact_id'); - $joined_model->addField('contact_phone'); - - $this->createMigrator($joined_model)->create(); - - $joined_model->import([ + $joinedModel->import([ [ 'not_named_id' => 1, 'contact_id' => 21, @@ -978,7 +978,7 @@ public function testJoinDeleteForeignCustomIdFieldReverse(): void $j = $user->join('contact_join', [ 'masterField' => 'test_id', 'foreignField' => 'contact_id', - 'foreignModelIdField' => 'not_named_id', + 'foreignIdField' => 'not_named_id', 'reverse' => true, ]); $this->createMigrator()->createForeignKey($j); @@ -986,7 +986,7 @@ public function testJoinDeleteForeignCustomIdFieldReverse(): void $user->delete(1); - static::assertSame(null, $master_model->tryLoad(1)); - static::assertSame(null, $joined_model->tryLoad(1)); + static::assertSame(null, $masterModel->tryLoad(1)); + static::assertSame(null, $joinedModel->tryLoad(1)); } } From 073d16832b86d4eb02e218e7d64b1574a6be6c7e Mon Sep 17 00:00:00 2001 From: Francesco Danti Date: Fri, 16 Dec 2022 07:19:06 +0100 Subject: [PATCH 04/14] CleanUp CS --- src/Model/Join.php | 4 ++-- tests/JoinSqlTest.php | 26 +++++++++++++------------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/Model/Join.php b/src/Model/Join.php index c57e202bb..9b6e97abd 100644 --- a/src/Model/Join.php +++ b/src/Model/Join.php @@ -76,7 +76,7 @@ abstract class Join /** * Custom definition if $idField for of ForeignModel * By Default ForeignModel idField is equal to 'id' - * To be used when the joined table doesn't have a field named 'id' + * To be used when the joined table doesn't have a field named 'id'. */ public ?string $foreignIdField = null; @@ -114,7 +114,7 @@ protected function createFakeForeignModel(): Model { $fakeModel = new Model($this->getOwner()->getPersistence(), [ 'table' => $this->foreignTable, - 'idField' => $this->foreignIdField + 'idField' => $this->foreignIdField, ]); foreach ($this->getOwner()->getFields() as $ownerField) { if ($ownerField->hasJoin() && $ownerField->getJoin()->shortName === $this->shortName) { diff --git a/tests/JoinSqlTest.php b/tests/JoinSqlTest.php index 976693f16..92b020336 100644 --- a/tests/JoinSqlTest.php +++ b/tests/JoinSqlTest.php @@ -801,9 +801,9 @@ public function testJoinSavingForeignCustomIdFieldRaiseException(): void $user = new Model($this->db, ['table' => 'user_join']); $user->addField('name'); $j = $user->join('contact_join', [ - 'foreignTable' => 'contact_join', - 'masterField' => 'test_id', - 'foreignField' => 'contact_id', + 'foreignTable' => 'contact_join', + 'masterField' => 'test_id', + 'foreignField' => 'contact_id', //'foreignIdField' => 'not_named_id', // Raise exception ]); $this->createMigrator()->createForeignKey($j); @@ -856,8 +856,8 @@ public function testJoinSavingForeignCustomIdField(): void $user = new Model($this->db, ['table' => 'user_join']); $user->addField('name'); $j = $user->join('contact_join', [ - 'masterField' => 'test_id', - 'foreignField' => 'contact_id', + 'masterField' => 'test_id', + 'foreignField' => 'contact_id', 'foreignIdField' => 'not_named_id', ]); $this->createMigrator()->createForeignKey($j); @@ -924,8 +924,8 @@ public function testJoinDeleteForeignCustomIdField(): void $user = new Model($this->db, ['table' => 'user_join']); $user->addField('name'); $j = $user->join('contact_join', [ - 'masterField' => 'test_id', - 'foreignField' => 'contact_id', + 'masterField' => 'test_id', + 'foreignField' => 'contact_id', 'foreignIdField' => 'not_named_id', ]); @@ -935,8 +935,8 @@ public function testJoinDeleteForeignCustomIdField(): void $user->delete(1); - static::assertSame(null, $masterModel->tryLoad(1)); - static::assertSame(null, $joinedModel->tryLoad(1)); + static::assertNull($masterModel->tryLoad(1)); + static::assertNull($joinedModel->tryLoad(1)); } public function testJoinDeleteForeignCustomIdFieldReverse(): void @@ -976,8 +976,8 @@ public function testJoinDeleteForeignCustomIdFieldReverse(): void $user = new Model($this->db, ['table' => 'user_join']); $user->addField('name'); $j = $user->join('contact_join', [ - 'masterField' => 'test_id', - 'foreignField' => 'contact_id', + 'masterField' => 'test_id', + 'foreignField' => 'contact_id', 'foreignIdField' => 'not_named_id', 'reverse' => true, ]); @@ -986,7 +986,7 @@ public function testJoinDeleteForeignCustomIdFieldReverse(): void $user->delete(1); - static::assertSame(null, $masterModel->tryLoad(1)); - static::assertSame(null, $joinedModel->tryLoad(1)); + static::assertNull($masterModel->tryLoad(1)); + static::assertNull($joinedModel->tryLoad(1)); } } From ae3a38025e12f9e0ae86fa473719652de81be6d2 Mon Sep 17 00:00:00 2001 From: Francesco Danti Date: Fri, 16 Dec 2022 07:22:39 +0100 Subject: [PATCH 05/14] CleanUp CS --- tests/JoinSqlTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/JoinSqlTest.php b/tests/JoinSqlTest.php index 92b020336..f621cb491 100644 --- a/tests/JoinSqlTest.php +++ b/tests/JoinSqlTest.php @@ -804,7 +804,7 @@ public function testJoinSavingForeignCustomIdFieldRaiseException(): void 'foreignTable' => 'contact_join', 'masterField' => 'test_id', 'foreignField' => 'contact_id', - //'foreignIdField' => 'not_named_id', // Raise exception + // 'foreignIdField' => 'not_named_id', // Raise exception ]); $this->createMigrator()->createForeignKey($j); $j->addField('contact_phone'); From a2284dbdc44e531cac6b9286f667550d5f393514 Mon Sep 17 00:00:00 2001 From: Francesco Danti Date: Fri, 16 Dec 2022 07:33:26 +0100 Subject: [PATCH 06/14] CleanUp CS --- tests/JoinSqlTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/JoinSqlTest.php b/tests/JoinSqlTest.php index f621cb491..2b39fc3b9 100644 --- a/tests/JoinSqlTest.php +++ b/tests/JoinSqlTest.php @@ -6,7 +6,6 @@ use Atk4\Data\Exception; use Atk4\Data\Model; -use Atk4\Data\Persistence; use Atk4\Data\Schema\TestCase; use Doctrine\DBAL\Platforms\MySQLPlatform; From be7ebdd3459e146d3bc3f6a51b1df0ea713af285 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Mon, 13 Feb 2023 18:02:29 +0100 Subject: [PATCH 07/14] remove not much helpful test --- tests/JoinSqlTest.php | 55 ------------------------------------------- 1 file changed, 55 deletions(-) diff --git a/tests/JoinSqlTest.php b/tests/JoinSqlTest.php index 6eebad49d..530d6c76b 100644 --- a/tests/JoinSqlTest.php +++ b/tests/JoinSqlTest.php @@ -763,61 +763,6 @@ public function testJoinActualFieldNamesAndPrefix(): void ], $this->getDb()); } - public function testJoinSavingForeignCustomIdFieldRaiseException(): void - { - $masterModel = new \Atk4\Data\Model($this->db, [ - 'table' => 'user_join', - ]); - $masterModel->addField('name'); - $masterModel->addField('test_id'); - - $joinedModel = new \Atk4\Data\Model($this->db, [ - 'table' => 'contact_join', - 'idField' => 'not_named_id', - ]); - $joinedModel->addField('contact_id'); - $joinedModel->addField('contact_phone'); - - $this->createMigrator($masterModel)->create(); - $this->createMigrator($joinedModel)->create(); - - $masterModel->import([ - [ - 'id' => 1, - 'name' => 'John', - 'test_id' => 21, - ], - ]); - - $joinedModel->import([ - [ - 'not_named_id' => 1, - 'contact_id' => 21, - 'contact_phone' => '+123', - ], - ]); - - $user = new Model($this->db, ['table' => 'user_join']); - $user->addField('name'); - $j = $user->join('contact_join', [ - 'foreignTable' => 'contact_join', - 'masterField' => 'test_id', - 'foreignField' => 'contact_id', - // 'foreignIdField' => 'not_named_id', // Raise exception - ]); - $this->createMigrator()->createForeignKey($j); - $j->addField('contact_phone'); - - $user = $user->load(1); - - $user->set('name', 'John'); - $user->set('contact_phone', '+321'); - - $this->expectException(\Atk4\Data\Persistence\Sql\ExecuteException::class); - - $user->save(); - } - public function testJoinSavingForeignCustomIdField(): void { $masterModel = new \Atk4\Data\Model($this->db, [ From eb7baf36d3681946c06eb5863706b63e71b197e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Mon, 13 Feb 2023 18:39:52 +0100 Subject: [PATCH 08/14] dedup testing --- tests/JoinSqlTest.php | 173 ++++++++++-------------------------------- 1 file changed, 41 insertions(+), 132 deletions(-) diff --git a/tests/JoinSqlTest.php b/tests/JoinSqlTest.php index 530d6c76b..ec0c42361 100644 --- a/tests/JoinSqlTest.php +++ b/tests/JoinSqlTest.php @@ -41,6 +41,7 @@ public function testDirection(): void static::assertFalse($m->hasJoin('contact8')); $this->expectException(Exception::class); // TODO not implemented yet, see https://github.com/atk4/data/issues/803 + $this->expectExceptionMessage('Joining tables on non-id fields is not implemented yet'); $j4 = $m->join('contact4.foo_id', ['masterField' => 'test_id', 'reverse' => true]); // static::assertTrue($this->getProtected($j4, 'reverse')); // static::assertSame('test_id', $this->getProtected($j4, 'masterField')); @@ -52,6 +53,7 @@ public function testDirectionException(): void $m = new Model($this->db, ['table' => 'user']); $this->expectException(Exception::class); + $this->expectExceptionMessage('Joining tables on non-id fields is not implemented yet'); $m->join('contact.foo_id', ['masterField' => 'test_id']); } @@ -763,119 +765,65 @@ public function testJoinActualFieldNamesAndPrefix(): void ], $this->getDb()); } - public function testJoinSavingForeignCustomIdField(): void + /** + * @param array $joinDefaults + * + * @return array{ Model, Model, Model } + */ + protected function joinCustomForeignIdFieldSetup(array $joinDefaults = []): array { - $masterModel = new \Atk4\Data\Model($this->db, [ - 'table' => 'user_join', - ]); + $masterModel = new Model($this->db, ['table' => 'user']); $masterModel->addField('name'); - $masterModel->addField('test_id'); + $masterModel->addField('foo', ['type' => 'integer']); - $joinedModel = new \Atk4\Data\Model($this->db, [ - 'table' => 'contact_join', - 'idField' => 'not_named_id', - ]); - $joinedModel->addField('contact_id'); + $joinedModel = new Model($this->db, ['table' => 'contact', 'idField' => 'uid']); + $joinedModel->addField('bar', ['type' => 'integer']); $joinedModel->addField('contact_phone'); $this->createMigrator($masterModel)->create(); $this->createMigrator($joinedModel)->create(); $masterModel->import([ - [ - 'id' => 1, - 'name' => 'John', - 'test_id' => 21, - ], + ['id' => 1, 'name' => 'John', 'foo' => 21], ]); $joinedModel->import([ - [ - 'not_named_id' => 1, - 'contact_id' => 21, - 'contact_phone' => '+123', - ], + ['uid' => 1, 'bar' => 21, 'contact_phone' => '+123'], ]); - $user = new Model($this->db, ['table' => 'user_join']); + $user = new Model($this->db, ['table' => 'user']); $user->addField('name'); - $j = $user->join('contact_join', [ - 'masterField' => 'test_id', - 'foreignField' => 'contact_id', - 'foreignIdField' => 'not_named_id', - ]); + $j = $user->join('contact', array_merge([ + 'masterField' => 'foo', + 'foreignField' => 'bar', + 'foreignIdField' => 'uid', + ], $joinDefaults)); $this->createMigrator()->createForeignKey($j); $j->addField('contact_phone'); - $user = $user->load(1); + return [$masterModel, $joinedModel, $user]; + } + public function testJoinCustomForeignIdFieldSaving(): void + { + [$masterModel, $joinedModel, $user] = $this->joinCustomForeignIdFieldSetup(); + + $user = $user->load(1); $user->set('name', 'John'); $user->set('contact_phone', '+321'); - $user->save(); static::assertSame([ - 'user_join' => [ - 1 => ['id' => 1, 'name' => 'John', 'test_id' => '21'], - ], - 'contact_join' => [ - 1 => ['not_named_id' => 1, 'contact_id' => '21', 'contact_phone' => '+321'], - ], - ], [ - 'user_join' => [ - 1 => $masterModel->load(1)->get(), - ], - 'contact_join' => [ - 1 => $joinedModel->load(1)->get(), - ], - ]); + ['id' => 1, 'name' => 'John', 'foo' => 21], + ], $masterModel->export()); + static::assertSame([ + ['uid' => 1, 'bar' => 21, 'contact_phone' => '+321'], + ], $joinedModel->export()); } - public function testJoinDeleteForeignCustomIdField(): void + public function testJoinCustomForeignIdFieldDelete(): void { - $masterModel = new \Atk4\Data\Model($this->db, [ - 'table' => 'user_join', - ]); - $masterModel->addField('name'); - $masterModel->addField('test_id'); - - $joinedModel = new \Atk4\Data\Model($this->db, [ - 'table' => 'contact_join', - 'idField' => 'not_named_id', - ]); - $joinedModel->addField('contact_id'); - $joinedModel->addField('contact_phone'); - - $this->createMigrator($masterModel)->create(); - $this->createMigrator($joinedModel)->create(); - - $masterModel->import([ - [ - 'id' => 1, - 'name' => 'John', - 'test_id' => 21, - ], - ]); - - $joinedModel->import([ - [ - 'not_named_id' => 1, - 'contact_id' => 21, - 'contact_phone' => '+123', - ], - ]); - - $user = new Model($this->db, ['table' => 'user_join']); - $user->addField('name'); - $j = $user->join('contact_join', [ - 'masterField' => 'test_id', - 'foreignField' => 'contact_id', - 'foreignIdField' => 'not_named_id', - ]); - - $this->createMigrator()->createForeignKey($j); - - $j->addField('contact_phone'); + [$masterModel, $joinedModel, $user] = $this->joinCustomForeignIdFieldSetup(); $user->delete(1); @@ -883,54 +831,15 @@ public function testJoinDeleteForeignCustomIdField(): void static::assertNull($joinedModel->tryLoad(1)); } - public function testJoinDeleteForeignCustomIdFieldReverse(): void + public function testJoinCustomForeignIdFieldReverseDelete(): void { - $masterModel = new \Atk4\Data\Model($this->db, [ - 'table' => 'user_join', - ]); - $masterModel->addField('name'); - $masterModel->addField('test_id'); - - $joinedModel = new \Atk4\Data\Model($this->db, [ - 'table' => 'contact_join', - 'idField' => 'not_named_id', - ]); - $joinedModel->addField('contact_id'); - $joinedModel->addField('contact_phone'); - - $this->createMigrator($masterModel)->create(); - $this->createMigrator($joinedModel)->create(); - - $masterModel->import([ - [ - 'id' => 1, - 'name' => 'John', - 'test_id' => 21, - ], - ]); - - $joinedModel->import([ - [ - 'not_named_id' => 1, - 'contact_id' => 21, - 'contact_phone' => '+123', - ], - ]); - - $user = new Model($this->db, ['table' => 'user_join']); - $user->addField('name'); - $j = $user->join('contact_join', [ - 'masterField' => 'test_id', - 'foreignField' => 'contact_id', - 'foreignIdField' => 'not_named_id', - 'reverse' => true, - ]); - $this->createMigrator()->createForeignKey($j); - $j->addField('contact_phone'); + $this->expectException(Exception::class); + $this->expectExceptionMessage('Joining tables on non-id fields is not implemented yet'); + [$masterModel, $joinedModel, $user] = $this->joinCustomForeignIdFieldSetup(['reverse' => true]); - $user->delete(1); + // $user->delete(1); - static::assertNull($masterModel->tryLoad(1)); - static::assertNull($joinedModel->tryLoad(1)); + // static::assertNull($masterModel->tryLoad(1)); + // static::assertNull($joinedModel->tryLoad(1)); } } From b57a523ed1088004f2edf4ee1be19b8f8445e4ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Mon, 13 Feb 2023 18:40:35 +0100 Subject: [PATCH 09/14] reverse is tested but not supported --- tests/JoinSqlTest.php | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/tests/JoinSqlTest.php b/tests/JoinSqlTest.php index ec0c42361..cab960bda 100644 --- a/tests/JoinSqlTest.php +++ b/tests/JoinSqlTest.php @@ -830,16 +830,4 @@ public function testJoinCustomForeignIdFieldDelete(): void static::assertNull($masterModel->tryLoad(1)); static::assertNull($joinedModel->tryLoad(1)); } - - public function testJoinCustomForeignIdFieldReverseDelete(): void - { - $this->expectException(Exception::class); - $this->expectExceptionMessage('Joining tables on non-id fields is not implemented yet'); - [$masterModel, $joinedModel, $user] = $this->joinCustomForeignIdFieldSetup(['reverse' => true]); - - // $user->delete(1); - - // static::assertNull($masterModel->tryLoad(1)); - // static::assertNull($joinedModel->tryLoad(1)); - } } From 699891f090ced5be140cd36fbee43396cb4c03b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Mon, 13 Feb 2023 18:51:31 +0100 Subject: [PATCH 10/14] test more --- tests/JoinSqlTest.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/JoinSqlTest.php b/tests/JoinSqlTest.php index cab960bda..3021777f0 100644 --- a/tests/JoinSqlTest.php +++ b/tests/JoinSqlTest.php @@ -785,10 +785,12 @@ protected function joinCustomForeignIdFieldSetup(array $joinDefaults = []): arra $masterModel->import([ ['id' => 1, 'name' => 'John', 'foo' => 21], + ['id' => 2, 'name' => 'Roman', 'foo' => 22], ]); $joinedModel->import([ ['uid' => 1, 'bar' => 21, 'contact_phone' => '+123'], + ['uid' => 2, 'bar' => 22, 'contact_phone' => '+200'], ]); $user = new Model($this->db, ['table' => 'user']); @@ -809,15 +811,17 @@ public function testJoinCustomForeignIdFieldSaving(): void [$masterModel, $joinedModel, $user] = $this->joinCustomForeignIdFieldSetup(); $user = $user->load(1); - $user->set('name', 'John'); + $user->set('name', 'Karl'); $user->set('contact_phone', '+321'); $user->save(); static::assertSame([ - ['id' => 1, 'name' => 'John', 'foo' => 21], + ['id' => 1, 'name' => 'Karl', 'foo' => 21], + ['id' => 2, 'name' => 'Roman', 'foo' => 22], ], $masterModel->export()); static::assertSame([ ['uid' => 1, 'bar' => 21, 'contact_phone' => '+321'], + ['uid' => 2, 'bar' => 22, 'contact_phone' => '+200'], ], $joinedModel->export()); } From 2c5af258f4c4a1ad338a4205f2dd39cdd9992c55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Sat, 12 Aug 2023 15:51:34 +0200 Subject: [PATCH 11/14] fix cs --- tests/JoinSqlTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/JoinSqlTest.php b/tests/JoinSqlTest.php index 9b51b7d78..ffed75d17 100644 --- a/tests/JoinSqlTest.php +++ b/tests/JoinSqlTest.php @@ -815,11 +815,11 @@ public function testJoinCustomForeignIdFieldSaving(): void $user->set('contact_phone', '+321'); $user->save(); - static::assertSame([ + self::assertSame([ ['id' => 1, 'name' => 'Karl', 'foo' => 21], ['id' => 2, 'name' => 'Roman', 'foo' => 22], ], $masterModel->export()); - static::assertSame([ + self::assertSame([ ['uid' => 1, 'bar' => 21, 'contact_phone' => '+321'], ['uid' => 2, 'bar' => 22, 'contact_phone' => '+200'], ], $joinedModel->export()); @@ -831,7 +831,7 @@ public function testJoinCustomForeignIdFieldDelete(): void $user->delete(1); - static::assertNull($masterModel->tryLoad(1)); - static::assertNull($joinedModel->tryLoad(1)); + self::assertNull($masterModel->tryLoad(1)); + self::assertNull($joinedModel->tryLoad(1)); } } From 7b4d41cc14520088ae42bad208b3a81f1215232d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Sat, 12 Aug 2023 19:20:03 +0200 Subject: [PATCH 12/14] fix pgsql --- tests/JoinSqlTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/JoinSqlTest.php b/tests/JoinSqlTest.php index ffed75d17..00cf05922 100644 --- a/tests/JoinSqlTest.php +++ b/tests/JoinSqlTest.php @@ -815,11 +815,11 @@ public function testJoinCustomForeignIdFieldSaving(): void $user->set('contact_phone', '+321'); $user->save(); - self::assertSame([ + self::assertSameExportUnordered([ ['id' => 1, 'name' => 'Karl', 'foo' => 21], ['id' => 2, 'name' => 'Roman', 'foo' => 22], ], $masterModel->export()); - self::assertSame([ + self::assertSameExportUnordered([ ['uid' => 1, 'bar' => 21, 'contact_phone' => '+321'], ['uid' => 2, 'bar' => 22, 'contact_phone' => '+200'], ], $joinedModel->export()); From 7418d7fc4cfc1180e70c210643ea5e20bf1e5b74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Sun, 20 Aug 2023 11:05:29 +0200 Subject: [PATCH 13/14] unify comment --- src/Model/Join.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Model/Join.php b/src/Model/Join.php index 34b70432e..4ffcfa041 100644 --- a/src/Model/Join.php +++ b/src/Model/Join.php @@ -74,9 +74,8 @@ abstract class Join public ?string $foreignField = null; /** - * Custom definition if $idField for of ForeignModel - * By Default ForeignModel idField is equal to 'id' - * To be used when the joined table doesn't have a field named 'id'. + * Field to be used as foreign model ID field. + * By default it's 'id'. */ public ?string $foreignIdField = null; From 0425d5a8b0fadde8da724855ea21c0b5f56db6e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Sun, 20 Aug 2023 11:19:13 +0200 Subject: [PATCH 14/14] improve tests --- tests/JoinSqlTest.php | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/tests/JoinSqlTest.php b/tests/JoinSqlTest.php index 00cf05922..c52c1b772 100644 --- a/tests/JoinSqlTest.php +++ b/tests/JoinSqlTest.php @@ -40,8 +40,8 @@ public function testDirection(): void self::assertTrue($m->hasJoin('contact2')); self::assertFalse($m->hasJoin('contact8')); - $this->expectException(Exception::class); // TODO not implemented yet, see https://github.com/atk4/data/issues/803 - $this->expectExceptionMessage('Joining tables on non-id fields is not implemented yet'); + $this->expectException(Exception::class); + $this->expectExceptionMessage('Joining tables on non-id fields is not implemented yet'); // https://github.com/atk4/data/issues/803 $j4 = $m->join('contact4.foo_id', ['masterField' => 'test_id', 'reverse' => true]); // self::assertTrue($this->getProtected($j4, 'reverse')); // self::assertSame('test_id', $this->getProtected($j4, 'masterField')); @@ -770,7 +770,7 @@ public function testJoinActualFieldNamesAndPrefix(): void * * @return array{ Model, Model, Model } */ - protected function joinCustomForeignIdFieldSetup(array $joinDefaults = []): array + protected function setupJoinWithNonDefaultForeignIdField(array $joinDefaults = []): array { $masterModel = new Model($this->db, ['table' => 'user']); $masterModel->addField('name'); @@ -789,8 +789,8 @@ protected function joinCustomForeignIdFieldSetup(array $joinDefaults = []): arra ]); $joinedModel->import([ - ['uid' => 1, 'bar' => 21, 'contact_phone' => '+123'], - ['uid' => 2, 'bar' => 22, 'contact_phone' => '+200'], + ['uid' => 1, 'bar' => 22, 'contact_phone' => '+123'], + ['uid' => 2, 'bar' => 21, 'contact_phone' => '+200'], ]); $user = new Model($this->db, ['table' => 'user']); @@ -808,7 +808,7 @@ protected function joinCustomForeignIdFieldSetup(array $joinDefaults = []): arra public function testJoinCustomForeignIdFieldSaving(): void { - [$masterModel, $joinedModel, $user] = $this->joinCustomForeignIdFieldSetup(); + [$masterModel, $joinedModel, $user] = $this->setupJoinWithNonDefaultForeignIdField(); $user = $user->load(1); $user->set('name', 'Karl'); @@ -820,18 +820,22 @@ public function testJoinCustomForeignIdFieldSaving(): void ['id' => 2, 'name' => 'Roman', 'foo' => 22], ], $masterModel->export()); self::assertSameExportUnordered([ - ['uid' => 1, 'bar' => 21, 'contact_phone' => '+321'], - ['uid' => 2, 'bar' => 22, 'contact_phone' => '+200'], + ['uid' => 1, 'bar' => 22, 'contact_phone' => '+123'], + ['uid' => 2, 'bar' => 21, 'contact_phone' => '+321'], ], $joinedModel->export()); } public function testJoinCustomForeignIdFieldDelete(): void { - [$masterModel, $joinedModel, $user] = $this->joinCustomForeignIdFieldSetup(); + [$masterModel, $joinedModel, $user] = $this->setupJoinWithNonDefaultForeignIdField(); $user->delete(1); - self::assertNull($masterModel->tryLoad(1)); - self::assertNull($joinedModel->tryLoad(1)); + self::assertSameExportUnordered([ + ['id' => 2, 'name' => 'Roman', 'foo' => 22], + ], $masterModel->export()); + self::assertSameExportUnordered([ + ['uid' => 1, 'bar' => 22, 'contact_phone' => '+123'], + ], $joinedModel->export()); } }