Skip to content

Commit

Permalink
Update tst with many generated fields
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Feb 7, 2024
1 parent 6006ab6 commit ec0df10
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/cycle "
"url": "https://github.com/sponsors/cycle"
}
],
"require": {
Expand All @@ -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": {
Expand Down
8 changes: 7 additions & 1 deletion tests/ORM/Functional/Driver/Common/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ public function setUp(): void
null,
new DoctrineCollectionFactory()
))->withCollectionFactory('array', new ArrayCollectionFactory()),
new Schema([])
new Schema([]),
$this->getCommandGenerator(),
);
}

Expand Down Expand Up @@ -400,4 +401,9 @@ protected function applyDriverOptions(DriverConfig $config, array $options): voi
$config->options['withDatetimeMicroseconds'] = $options['withDatetimeMicroseconds'];
}
}

protected function getCommandGenerator(): ?Transaction\CommandGeneratorInterface
{
return null;
}
}
39 changes: 34 additions & 5 deletions tests/ORM/Functional/Driver/Postgres/SerialColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@

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;
use Cycle\ORM\Tests\Fixtures\CyclicRef2\Document;
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;

Expand Down Expand Up @@ -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();

Expand All @@ -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,
],
],
Expand All @@ -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;
}
};
}
}

0 comments on commit ec0df10

Please sign in to comment.