From 2426c4d33836a6088f5ebbf1a94d197cf6aa4ade Mon Sep 17 00:00:00 2001 From: Francesco Danti Date: Thu, 15 Dec 2022 21:22:26 +0100 Subject: [PATCH] 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)); + } }