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..bccf861da --- /dev/null +++ b/tests/ORM/Fixtures/SequenceDefaultValueMapper.php @@ -0,0 +1,23 @@ +register('user_code', new Fragment('nextval(\'user_code_seq\')'), true); + + return $command; + } +} diff --git a/tests/ORM/Fixtures/User.php b/tests/ORM/Fixtures/User.php index 3c2dc60ff..73f032981 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; /** @var Profile */ public $profile;