Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for generated fields #73

Merged
merged 4 commits into from Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
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
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
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
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
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