Skip to content

Commit

Permalink
Remove enum, rename constants
Browse files Browse the repository at this point in the history
  • Loading branch information
msmakouz committed Feb 8, 2024
1 parent ca54ddf commit 343bc8c
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 175 deletions.
31 changes: 19 additions & 12 deletions src/Annotation/Generated.php
Expand Up @@ -4,27 +4,34 @@

namespace Cycle\Annotated\Annotation;

use Cycle\Annotated\Enum\GeneratedType;
use Cycle\ORM\Schema\GeneratedField;
use Spiral\Attributes\NamedArgumentConstructor;

/**
* @Annotation
* @NamedArgumentConstructor
* @Target({"PROPERTY"})
*/
#[\Attribute(\Attribute::TARGET_PROPERTY)]
#[NamedArgumentConstructor]
class Generated
{
protected int $type = 0;
public function __construct(
protected bool $beforeInsert = false,
protected bool $onInsert = false,
protected bool $beforeUpdate = false,
) {
}

/**
* @param GeneratedType|int ...$type Generating type {@see GeneratedType}.
*/
public function __construct(GeneratedType|int ...$type)
public function getType(): ?int
{
foreach ($type as $value) {
$this->type |= $value instanceof GeneratedType ? $value->value : $value;
if (!$this->beforeInsert && !$this->onInsert && !$this->beforeUpdate) {
return null;

Check warning on line 29 in src/Annotation/Generated.php

View check run for this annotation

Codecov / codecov/patch

src/Annotation/Generated.php#L29

Added line #L29 was not covered by tests
}
}

public function getType(): int
{
return $this->type;
return
($this->beforeInsert ? GeneratedField::BEFORE_INSERT : 0) |
($this->onInsert ? GeneratedField::ON_INSERT : 0) |
($this->beforeUpdate ? GeneratedField::BEFORE_UPDATE : 0);
}
}
8 changes: 4 additions & 4 deletions src/Configurator.php
Expand Up @@ -10,11 +10,11 @@
use Cycle\Annotated\Annotation\ForeignKey;
use Cycle\Annotated\Annotation\Generated;
use Cycle\Annotated\Annotation\Relation as RelationAnnotation;
use Cycle\Annotated\Enum\GeneratedType;
use Cycle\Annotated\Exception\AnnotationException;
use Cycle\Annotated\Exception\AnnotationRequiredArgumentsException;
use Cycle\Annotated\Exception\AnnotationWrongTypeArgumentException;
use Cycle\Annotated\Utils\EntityUtils;
use Cycle\ORM\Schema\GeneratedField;
use Cycle\Schema\Definition\Entity as EntitySchema;
use Cycle\Schema\Definition\Field;
use Cycle\Schema\Definition\ForeignKey as ForeignKeySchema;
Expand Down Expand Up @@ -234,8 +234,8 @@ public function initField(string $name, Column $column, \ReflectionClass $class,
$field->setColumn($columnName);

$field->setPrimary($column->isPrimary());
if ($this->isDbGeneratedField($field)) {
$field->setGenerated(GeneratedType::Db->value);
if ($this->isOnInsertGeneratedField($field)) {
$field->setGenerated(GeneratedField::ON_INSERT);
}

$field->setTypecast($this->resolveTypecast($column->getTypecast(), $class));
Expand Down Expand Up @@ -406,7 +406,7 @@ private function getPropertyMetadata(\ReflectionProperty $property, string $name
}
}

private function isDbGeneratedField(Field $field): bool
private function isOnInsertGeneratedField(Field $field): bool
{
return $field->isPrimary()
|| $field->getType() === 'serial'
Expand Down
12 changes: 0 additions & 12 deletions src/Enum/GeneratedType.php

This file was deleted.

29 changes: 0 additions & 29 deletions tests/Annotated/Fixtures/Fixtures25/Php82/WithGenerated.php

This file was deleted.

Expand Up @@ -7,18 +7,33 @@
use Cycle\Annotated\Annotation\Column;
use Cycle\Annotated\Annotation\Entity;

#[Entity(role: 'generatedFieldsSerial', table: 'generated_fields_serial')]
class WithSerial
/**
* @Entity(role="withGeneratedSerial", table="with_generated_serial")
*/
#[Entity(role: 'withGeneratedSerial', table: 'with_generated_serial')]
class WithGeneratedSerial
{
/**
* @Column(type="primary")
*/
#[Column(type: 'primary')]
public int $id;

/**
* @Column(type="smallserial", name="small_serial")
*/
#[Column(type: 'smallserial', name: 'small_serial')]
public int $smallSerial;

/**
* @Column(type="serial")
*/
#[Column(type: 'serial')]
public int $serial;

/**
* @Column(type="bigserial", name="big_serial")
*/
#[Column(type: 'bigserial', name: 'big_serial')]
public int $bigSerial;
}
29 changes: 0 additions & 29 deletions tests/Annotated/Fixtures/Fixtures25/WithGeneratedEnum.php

This file was deleted.

52 changes: 52 additions & 0 deletions tests/Annotated/Fixtures/Fixtures25/WithGeneratedFields.php
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace Cycle\Annotated\Tests\Fixtures\Fixtures25;

use Cycle\Annotated\Annotation\Column;
use Cycle\Annotated\Annotation\Entity;
use Cycle\Annotated\Annotation\Generated;

/**
* @Entity(role="withGeneratedFields", table="with_generated_fields")
*/
#[Entity(role: 'withGeneratedFields', table: 'with_generated_fields')]
class WithGeneratedFields
{
/**
* @Column(type="primary")
*/
#[Column(type: 'primary')]
public int $id;

/**
* @Column(type="datetime", name="created_at")
* @Generated(beforeInsert=true)
*/
#[
Column(type: 'datetime', name: 'created_at'),
Generated(beforeInsert: true)
]
public \DateTimeImmutable $createdAt;

/**
* @Column(type="datetime", name="created_at_generated_by_database")
* @Generated(onInsert=true)
*/
#[
Column(type: 'datetime', name: 'created_at_generated_by_database'),
Generated(onInsert: true)
]
public \DateTimeImmutable $createdAtGeneratedByDatabase;

/**
* @Column(type="datetime", name="created_at")
* @Generated(beforeInsert=true, beforeUpdate=true)
*/
#[
Column(type: 'datetime', name: 'updated_at'),
Generated(beforeInsert: true, beforeUpdate: true)
]
public \DateTimeImmutable $updatedAt;
}
28 changes: 0 additions & 28 deletions tests/Annotated/Fixtures/Fixtures25/WithGeneratedInt.php

This file was deleted.

Expand Up @@ -6,81 +6,40 @@

use Cycle\Annotated\Entities;
use Cycle\Annotated\Locator\TokenizerEntityLocator;
use Cycle\ORM\Schema\GeneratedField;
use Cycle\ORM\SchemaInterface;
use Cycle\Schema\Compiler;
use Cycle\Schema\Registry;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\RequiresPhp;
use Spiral\Attributes\AttributeReader;
use Spiral\Attributes\ReaderInterface;
use Spiral\Tokenizer\Config\TokenizerConfig;
use Spiral\Tokenizer\Tokenizer;

abstract class GeneratedFieldsTestCase extends BaseTestCase
{
protected readonly ReaderInterface $reader;

public function setUp(): void
{
$this->reader = new AttributeReader();

parent::setUp();
}

#[DataProvider('generatedFieldsDataProvider')]
public function testGeneratedFields(string $role): void
#[DataProvider('allReadersProvider')]
public function testGeneratedFields(ReaderInterface $reader): void
{
$tokenizer = new Tokenizer(new TokenizerConfig([
'directories' => [__DIR__ . '/../../../Fixtures/Fixtures25'],
'exclude' => ['Php82', 'PostgreSQL'],
'exclude' => ['PostgreSQL'],
]));

$locator = $tokenizer->classLocator();

$r = new Registry($this->dbal);
$schema = (new Compiler())->compile($r, [
new Entities(new TokenizerEntityLocator($locator, $this->reader), $this->reader),
new Entities(new TokenizerEntityLocator($locator, $reader), $reader),
]);

$this->assertSame(
[
'id' => SchemaInterface::GENERATED_DB,
'createdAt' => SchemaInterface::GENERATED_PHP_INSERT,
'updatedAt' => SchemaInterface::GENERATED_PHP_INSERT | SchemaInterface::GENERATED_PHP_UPDATE,
'id' => GeneratedField::ON_INSERT,
'createdAt' => GeneratedField::BEFORE_INSERT,
'createdAtGeneratedByDatabase' => GeneratedField::ON_INSERT,
'updatedAt' => GeneratedField::BEFORE_INSERT | GeneratedField::BEFORE_UPDATE,
],
$schema[$role][SchemaInterface::GENERATED_FIELDS]
$schema['withGeneratedFields'][SchemaInterface::GENERATED_FIELDS]
);
}

#[RequiresPhp('^8.2')]
public function testGeneratedFieldsEnumValues(): void
{
$tokenizer = new Tokenizer(new TokenizerConfig([
'directories' => [__DIR__ . '/../../../Fixtures/Fixtures25/Php82'],
'exclude' => [],
]));

$locator = $tokenizer->classLocator();

$r = new Registry($this->dbal);

$schema = (new Compiler())->compile($r, [
new Entities(new TokenizerEntityLocator($locator, $this->reader), $this->reader),
]);

$this->assertSame(
[
'id' => SchemaInterface::GENERATED_DB,
'createdAt' => SchemaInterface::GENERATED_PHP_INSERT,
'updatedAt' => SchemaInterface::GENERATED_PHP_INSERT | SchemaInterface::GENERATED_PHP_UPDATE,
],
$schema['generatedFieldsEnumValue'][SchemaInterface::GENERATED_FIELDS]
);
}

public static function generatedFieldsDataProvider(): \Traversable
{
yield ['generatedFieldsEnum'];
yield ['generatedFieldsInt'];
}
}
Expand Up @@ -21,6 +21,7 @@
use Cycle\ORM\Relation;
use Cycle\ORM\Schema;
use Cycle\ORM\SchemaInterface;
use Cycle\ORM\Schema\GeneratedField;
use Cycle\ORM\Select\Repository;
use Cycle\ORM\Select\Source;
use Cycle\ORM\EntityManager;
Expand Down Expand Up @@ -307,7 +308,7 @@ public function testSingleTableInheritanceWithDifferentColumnDeclaration(
SchemaInterface::SCHEMA => [],
SchemaInterface::TYPECAST_HANDLER => null,
SchemaInterface::GENERATED_FIELDS => [
'id' => SchemaInterface::GENERATED_DB,
'id' => GeneratedField::ON_INSERT,
],
],
$schema['comment']
Expand Down

0 comments on commit 343bc8c

Please sign in to comment.