From c7d14fc73dc4280eef50872fde276cdf25e85ad6 Mon Sep 17 00:00:00 2001 From: thenotsoft Date: Fri, 20 Nov 2020 16:04:32 +0200 Subject: [PATCH 1/3] Add test to sequence default value --- .../Postgres/SequenceDefaultValueTest.php | 96 +++++++++++++++++++ .../Fixtures/SequenceDefaultValueMapper.php | 24 +++++ tests/ORM/Fixtures/User.php | 1 + tests/ORM/SoftDeletesTest.php | 1 - 4 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 tests/ORM/Driver/Postgres/SequenceDefaultValueTest.php create mode 100644 tests/ORM/Fixtures/SequenceDefaultValueMapper.php diff --git a/tests/ORM/Driver/Postgres/SequenceDefaultValueTest.php b/tests/ORM/Driver/Postgres/SequenceDefaultValueTest.php new file mode 100644 index 000000000..5c1be200f --- /dev/null +++ b/tests/ORM/Driver/Postgres/SequenceDefaultValueTest.php @@ -0,0 +1,96 @@ +clearSequence(); + $this->createSequence(); + + $this->makeTable( + 'user', + [ + 'id' => 'primary', + 'email' => 'string', + 'balance' => 'float', + 'user_code' => 'int', + 'deleted_at' => 'datetime,null', + ] + ); + + $this->orm = $this->withSchema( + new Schema( + [ + User::class => [ + Schema::ROLE => 'user', + Schema::MAPPER => SequenceDefaultValueMapper::class, + Schema::DATABASE => 'default', + Schema::TABLE => 'user', + Schema::PRIMARY_KEY => 'id', + Schema::COLUMNS => ['id', 'email', 'balance', 'user_code', 'deleted_at'], + Schema::TYPECAST => [ + 'id' => 'int', + 'balance' => 'float', + 'user_code' => 'int', + 'deleted_at' => 'datetime' + ], + Schema::SCHEMA => [], + Schema::RELATIONS => [], + Schema::CONSTRAIN => NotDeletedConstrain::class + ] + ] + ) + ); + } + + public function tearDown(): void + { + $this->clearSequence(); + + parent::tearDown(); + } + + public function testCreate(): void + { + $u = new User(); + $u->email = 'test@email.com'; + $u->balance = 199; + + (new Transaction($this->orm))->persist($u)->run(); + + $s = new Select($this->orm->withHeap(new Heap()), User::class); + $data = $s->fetchData(); + + $this->assertIsInt($data[0]['user_code']); + } + + private function createSequence(): void + { + $this->getDatabase()->query('CREATE SEQUENCE user_code_seq INCREMENT 1 START 1 MINVALUE 1'); + } + + private function clearSequence(): void + { + $this->getDatabase()->execute('DROP SEQUENCE IF EXISTS user_code_seq CASCADE'); + } +} diff --git a/tests/ORM/Fixtures/SequenceDefaultValueMapper.php b/tests/ORM/Fixtures/SequenceDefaultValueMapper.php new file mode 100644 index 000000000..5fa1bdc30 --- /dev/null +++ b/tests/ORM/Fixtures/SequenceDefaultValueMapper.php @@ -0,0 +1,24 @@ +register('user_code', new Fragment('nextval(\'user_code_seq\')'), true); + //$state->forward('user_code', $command, 'user_code'); + + return $command; + } +} diff --git a/tests/ORM/Fixtures/User.php b/tests/ORM/Fixtures/User.php index 3c2dc60ff..6f63f5652 100644 --- a/tests/ORM/Fixtures/User.php +++ b/tests/ORM/Fixtures/User.php @@ -20,6 +20,7 @@ class User implements ImagedInterface public $id; public $email; public $balance; + public $user_code=null; /** @var Profile */ public $profile; diff --git a/tests/ORM/SoftDeletesTest.php b/tests/ORM/SoftDeletesTest.php index 9817f23ef..3f24a0517 100644 --- a/tests/ORM/SoftDeletesTest.php +++ b/tests/ORM/SoftDeletesTest.php @@ -8,7 +8,6 @@ */ declare(strict_types=1); -declare(strict_types=1); namespace Cycle\ORM\Tests; From 1346b2f504f075906732d6a108fcd438d7cf4e03 Mon Sep 17 00:00:00 2001 From: thenotsoft Date: Fri, 20 Nov 2020 16:08:39 +0200 Subject: [PATCH 2/3] cleanup --- tests/ORM/Fixtures/SequenceDefaultValueMapper.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/ORM/Fixtures/SequenceDefaultValueMapper.php b/tests/ORM/Fixtures/SequenceDefaultValueMapper.php index 5fa1bdc30..bccf861da 100644 --- a/tests/ORM/Fixtures/SequenceDefaultValueMapper.php +++ b/tests/ORM/Fixtures/SequenceDefaultValueMapper.php @@ -17,7 +17,6 @@ public function queueCreate($entity, Node $node, State $state): ContextCarrierIn $command = parent::queueCreate($entity, $node, $state); $command->register('user_code', new Fragment('nextval(\'user_code_seq\')'), true); - //$state->forward('user_code', $command, 'user_code'); return $command; } From 01f195be4401abd30a58bd04c222a9480988ac4a Mon Sep 17 00:00:00 2001 From: thenotsoft Date: Fri, 20 Nov 2020 16:17:24 +0200 Subject: [PATCH 3/3] remove redundant code --- tests/ORM/Fixtures/User.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ORM/Fixtures/User.php b/tests/ORM/Fixtures/User.php index 6f63f5652..73f032981 100644 --- a/tests/ORM/Fixtures/User.php +++ b/tests/ORM/Fixtures/User.php @@ -20,7 +20,7 @@ class User implements ImagedInterface public $id; public $email; public $balance; - public $user_code=null; + public $user_code; /** @var Profile */ public $profile;