Skip to content

Commit

Permalink
Merge pull request #73: support for generated fields
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk authored Feb 8, 2024
2 parents 67da7e7 + 6ec2148 commit 8fbc463
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 2 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"prefer-stable": true,
"require": {
"php": ">=8.0",
"cycle/orm": "^2.0",
"cycle/orm": "^2.7",
"cycle/database": "^2.7.1",
"yiisoft/friendly-exception": "^1.1"
},
Expand Down
13 changes: 13 additions & 0 deletions src/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ private function compute(Registry $registry, Entity $entity): void
Schema::FIND_BY_KEYS => $this->renderReferences($entity),
Schema::TYPECAST => $this->renderTypecast($entity),
Schema::RELATIONS => [],
Schema::GENERATED_FIELDS => $this->renderGeneratedFields($entity),
];

// For table inheritance we need to fill specific schema segments
Expand Down Expand Up @@ -187,6 +188,18 @@ private function renderColumns(Entity $entity): array
return $schema;
}

private function renderGeneratedFields(Entity $entity): array
{
$schema = [];
foreach ($entity->getFields() as $name => $field) {
if ($field->getGenerated() !== null) {
$schema[$name] = $field->getGenerated();
}
}

return $schema;
}

private function renderTypecast(Entity $entity): array
{
$schema = [];
Expand Down
18 changes: 18 additions & 0 deletions src/Definition/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Cycle\Schema\Definition;

use Cycle\ORM\Schema\GeneratedField;
use Cycle\Schema\Definition\Map\OptionMap;
use Cycle\Schema\Exception\FieldException;

Expand Down Expand Up @@ -32,6 +33,8 @@ final class Field
*/
private array|string|null $typecast = null;

private ?int $generated = null;

private bool $referenced = false;
private ?string $entityClass = null;

Expand Down Expand Up @@ -138,6 +141,21 @@ public function getTypecast(): array|string|null
return $this->typecast;
}

/**
* @param int|null $type Generating type {@see GeneratedField*} constants.
*/
public function setGenerated(int|null $type): self
{
$this->generated = $type;

return $this;
}

public function getGenerated(): ?int
{
return $this->generated;
}

public function setReferenced(bool $indexed): self
{
$this->referenced = $indexed;
Expand Down
43 changes: 42 additions & 1 deletion tests/Schema/CompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Cycle\Database\DatabaseProviderInterface;
use Cycle\ORM\Parser\Typecast;
use Cycle\ORM\Schema\GeneratedField;
use Cycle\ORM\SchemaInterface;
use Cycle\Schema\Compiler;
use Cycle\Schema\Definition\Entity;
Expand All @@ -18,7 +19,7 @@
use Cycle\Schema\Tests\Fixtures\Typecaster;
use PHPUnit\Framework\TestCase;

class CompilerTest extends TestCase
final class CompilerTest extends TestCase
{
public function testWrongGeneratorShouldThrowAnException(): void
{
Expand Down Expand Up @@ -86,6 +87,46 @@ public function testRenderTypecast(mixed $expected, array $defaults, mixed $enti
$this->assertSame($expected, $schema['author'][SchemaInterface::TYPECAST_HANDLER]);
}

public function testRenderGeneratedFields(): void
{
$entity = new Entity();
$entity->setRole('author')->setClass(Author::class);
$entity->getFields()->set('id', (new Field())->setType('primary')->setColumn('id'));
$entity->getFields()->set('name', (new Field())->setType('string')->setColumn('name'));
$entity->getFields()->set(
'createdAt',
(new Field())
->setType('datetime')
->setColumn('created_at')
->setGenerated(GeneratedField::BEFORE_INSERT)
);
$entity->getFields()->set(
'updatedAt',
(new Field())
->setType('datetime')
->setColumn('created_at')
->setGenerated(GeneratedField::BEFORE_INSERT | GeneratedField::BEFORE_UPDATE)
);
$entity->getFields()->set(
'sequence',
(new Field())
->setType('serial')
->setColumn('some_sequence')
->setGenerated(GeneratedField::ON_INSERT)
);

$r = new Registry($this->createMock(DatabaseProviderInterface::class));
$r->register($entity);

$schema = (new Compiler())->compile($r);

$this->assertSame([
'createdAt' => GeneratedField::BEFORE_INSERT,
'updatedAt' => GeneratedField::BEFORE_INSERT | GeneratedField::BEFORE_UPDATE,
'sequence' => GeneratedField::ON_INSERT,
], $schema['author'][SchemaInterface::GENERATED_FIELDS]);
}

public static function renderTypecastDataProvider(): \Traversable
{
yield [null, []];
Expand Down
4 changes: 4 additions & 0 deletions tests/Schema/Generator/TableGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public function testCompiled(): void
Schema::TYPECAST => [],
Schema::SCHEMA => [],
Schema::TYPECAST_HANDLER => [Typecaster::class, 'default_typecaster'],
Schema::GENERATED_FIELDS => [],
],
], $schema);
}
Expand Down Expand Up @@ -103,6 +104,7 @@ public function testCompiledWithPassedDefaultTypecastHandler(): void
Schema::TYPECAST => [],
Schema::SCHEMA => [],
Schema::TYPECAST_HANDLER => [Typecaster::class],
Schema::GENERATED_FIELDS => [],
],
], $schema);
}
Expand Down Expand Up @@ -171,6 +173,7 @@ public function testCompiledUser(): void
Schema::TYPECAST_HANDLER => [
Typecaster::class,
],
Schema::GENERATED_FIELDS => [],
],
], $c->getSchema());
}
Expand Down Expand Up @@ -203,6 +206,7 @@ public function testCompiledPost(): void
Schema::TYPECAST => [],
Schema::SCHEMA => [],
Schema::TYPECAST_HANDLER => null,
Schema::GENERATED_FIELDS => [],
],
], $c->getSchema());
}
Expand Down

0 comments on commit 8fbc463

Please sign in to comment.