From ec0df10b7e9c9c25b6771b275017925c14fd08a2 Mon Sep 17 00:00:00 2001 From: roxblnfk Date: Wed, 7 Feb 2024 09:15:15 +0400 Subject: [PATCH] Update tst with many generated fields --- composer.json | 5 +-- .../ORM/Functional/Driver/Common/BaseTest.php | 8 +++- .../Driver/Postgres/SerialColumnTest.php | 39 ++++++++++++++++--- 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/composer.json b/composer.json index 4f6085ce..999eeef6 100644 --- a/composer.json +++ b/composer.json @@ -33,7 +33,7 @@ "funding": [ { "type": "github", - "url": "https://github.com/sponsors/cycle " + "url": "https://github.com/sponsors/cycle" } ], "require": { @@ -51,8 +51,7 @@ "phpunit/phpunit": "^9.5", "ramsey/uuid": "^4.0", "spiral/tokenizer": "^2.8 || ^3.0", - "vimeo/psalm": "5.21", - "buggregator/trap": "^1.4" + "vimeo/psalm": "5.21" }, "autoload": { "psr-4": { diff --git a/tests/ORM/Functional/Driver/Common/BaseTest.php b/tests/ORM/Functional/Driver/Common/BaseTest.php index ba446ae3..633f3132 100644 --- a/tests/ORM/Functional/Driver/Common/BaseTest.php +++ b/tests/ORM/Functional/Driver/Common/BaseTest.php @@ -97,7 +97,8 @@ public function setUp(): void null, new DoctrineCollectionFactory() ))->withCollectionFactory('array', new ArrayCollectionFactory()), - new Schema([]) + new Schema([]), + $this->getCommandGenerator(), ); } @@ -400,4 +401,9 @@ protected function applyDriverOptions(DriverConfig $config, array $options): voi $config->options['withDatetimeMicroseconds'] = $options['withDatetimeMicroseconds']; } } + + protected function getCommandGenerator(): ?Transaction\CommandGeneratorInterface + { + return null; + } } diff --git a/tests/ORM/Functional/Driver/Postgres/SerialColumnTest.php b/tests/ORM/Functional/Driver/Postgres/SerialColumnTest.php index eca5dc0c..85ba2fe7 100644 --- a/tests/ORM/Functional/Driver/Postgres/SerialColumnTest.php +++ b/tests/ORM/Functional/Driver/Postgres/SerialColumnTest.php @@ -4,7 +4,10 @@ namespace Cycle\ORM\Tests\Functional\Driver\Postgres; +use Cycle\Database\Schema\AbstractColumn; +use Cycle\ORM\Command\CommandInterface; use Cycle\ORM\Mapper\Mapper; +use Cycle\ORM\ORMInterface; use Cycle\ORM\Schema; use Cycle\ORM\SchemaInterface; use Cycle\ORM\Select; @@ -12,6 +15,7 @@ use Cycle\ORM\Tests\Fixtures\User; use Cycle\ORM\Tests\Functional\Driver\Common\BaseTest; use Cycle\ORM\Tests\Traits\TableTrait; +use Cycle\ORM\Transaction; use DateTimeImmutable; use Ramsey\Uuid\Uuid; @@ -45,7 +49,7 @@ public function setUp(): void $schema = $this->getDatabase()->table('document')->getSchema(); $schema->column('id')->primary(); $schema->column('body')->type('serial')->nullable(false); - $schema->column('created_at')->type('datetime')->nullable(false); + $schema->column('created_at')->type('datetime')->nullable(false)->defaultValue(AbstractColumn::DATETIME_NOW); $schema->column('updated_at')->type('datetime')->nullable(false); $schema->save(); @@ -72,10 +76,14 @@ public function setUp(): void SchemaInterface::COLUMNS => ['id', 'body', 'created_at', 'updated_at'], SchemaInterface::SCHEMA => [], SchemaInterface::RELATIONS => [], + SchemaInterface::TYPECAST => [ + 'created_at' => 'datetime', + 'updated_at' => 'datetime', + ], SchemaInterface::GENERATED_FIELDS => [ 'id' => SchemaInterface::GENERATED_DB, 'body' => SchemaInterface::GENERATED_DB, - 'created_at' => SchemaInterface::GENERATED_PHP_INSERT, + 'created_at' => SchemaInterface::GENERATED_DB, 'updated_at' => SchemaInterface::GENERATED_PHP_INSERT | SchemaInterface::GENERATED_PHP_UPDATE, ], ], @@ -102,23 +110,44 @@ public function testPersist(): void public function testPersistMultipleSerial(): void { $d1 = new Document(); - $d1->created_at = $d1->updated_at = new DateTimeImmutable(); $d2 = new Document(); $d2->body = 213; - $d2->created_at = $d2->updated_at = new DateTimeImmutable(); + $d2->created_at = $d2->updated_at = new DateTimeImmutable('2020-01-01'); $d3 = new Document(); - $d3->created_at = $d3->updated_at = new DateTimeImmutable(); + $d3->created_at = $d3->updated_at = new DateTimeImmutable('2020-01-01'); $this->save($d1, $d2, $d3); $this->assertSame(1, $d1->id); $this->assertSame(1, $d1->body); + $this->assertNotSame('2020-01-01', $d1->created_at->format('Y-m-d')); $this->assertSame(2, $d2->id); $this->assertSame(213, $d2->body); + $this->assertSame('2020-01-01', $d2->created_at->format('Y-m-d')); $this->assertSame(3, $d3->id); $this->assertSame(2, $d3->body); + $this->assertSame('2020-01-01', $d3->created_at->format('Y-m-d')); + } + + protected function getCommandGenerator(): ?Transaction\CommandGeneratorInterface + { + return new class extends Transaction\CommandGenerator { + protected function storeEntity(ORMInterface $orm, Transaction\Tuple $tuple, bool $isNew): ?CommandInterface + { + /** @var CommandInterface|null $command */ + $command = parent::storeEntity($orm, $tuple, $isNew); + + if ($command !== null && $tuple->entity instanceof Document && empty($tuple->entity->updated_at)) { + $now = new DateTimeImmutable(); + $tuple->state->register('updated_at', $now); + $tuple->entity->updated_at = $now; + } + + return $command; + } + }; } }