-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
313 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cycle\Annotated\Annotation; | ||
|
||
use Cycle\ORM\Schema\GeneratedField; | ||
use Spiral\Attributes\NamedArgumentConstructor; | ||
|
||
/** | ||
* @Annotation | ||
* @NamedArgumentConstructor | ||
* @Target({"PROPERTY"}) | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_PROPERTY)] | ||
#[NamedArgumentConstructor] | ||
class GeneratedValue | ||
{ | ||
public function __construct( | ||
protected bool $beforeInsert = false, | ||
protected bool $onInsert = false, | ||
protected bool $beforeUpdate = false, | ||
) { | ||
} | ||
|
||
public function getFlags(): ?int | ||
{ | ||
if (!$this->beforeInsert && !$this->onInsert && !$this->beforeUpdate) { | ||
return null; | ||
} | ||
|
||
return | ||
($this->beforeInsert ? GeneratedField::BEFORE_INSERT : 0) | | ||
($this->onInsert ? GeneratedField::ON_INSERT : 0) | | ||
($this->beforeUpdate ? GeneratedField::BEFORE_UPDATE : 0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
tests/Annotated/Fixtures/Fixtures25/PostgreSQL/WithGeneratedSerial.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cycle\Annotated\Tests\Fixtures\Fixtures25\PostgreSQL; | ||
|
||
use Cycle\Annotated\Annotation\Column; | ||
use Cycle\Annotated\Annotation\Entity; | ||
|
||
/** | ||
* @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; | ||
} |
52 changes: 52 additions & 0 deletions
52
tests/Annotated/Fixtures/Fixtures25/WithGeneratedFields.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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\GeneratedValue; | ||
|
||
/** | ||
* @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") | ||
* @GeneratedValue(beforeInsert=true) | ||
*/ | ||
#[ | ||
Column(type: 'datetime', name: 'created_at'), | ||
GeneratedValue(beforeInsert: true) | ||
] | ||
public \DateTimeImmutable $createdAt; | ||
|
||
/** | ||
* @Column(type="datetime", name="created_at_generated_by_database") | ||
* @GeneratedValue(onInsert=true) | ||
*/ | ||
#[ | ||
Column(type: 'datetime', name: 'created_at_generated_by_database'), | ||
GeneratedValue(onInsert: true) | ||
] | ||
public \DateTimeImmutable $createdAtGeneratedByDatabase; | ||
|
||
/** | ||
* @Column(type="datetime", name="created_at") | ||
* @GeneratedValue(beforeInsert=true, beforeUpdate=true) | ||
*/ | ||
#[ | ||
Column(type: 'datetime', name: 'updated_at'), | ||
GeneratedValue(beforeInsert: true, beforeUpdate: true) | ||
] | ||
public \DateTimeImmutable $updatedAt; | ||
} |
43 changes: 43 additions & 0 deletions
43
tests/Annotated/Functional/Driver/Common/GeneratedFieldsTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cycle\Annotated\Tests\Functional\Driver\Common; | ||
|
||
use Cycle\Annotated\Entities; | ||
use Cycle\ORM\Schema\GeneratedField; | ||
use Cycle\ORM\SchemaInterface; | ||
use Cycle\Schema\Compiler; | ||
use Cycle\Schema\Registry; | ||
use Spiral\Attributes\ReaderInterface; | ||
use Spiral\Tokenizer\Config\TokenizerConfig; | ||
use Spiral\Tokenizer\Tokenizer; | ||
|
||
abstract class GeneratedFieldsTest extends BaseTest | ||
{ | ||
/** | ||
* @dataProvider allReadersProvider | ||
*/ | ||
public function testGeneratedFields(ReaderInterface $reader): void | ||
{ | ||
$tokenizer = new Tokenizer(new TokenizerConfig([ | ||
'directories' => [__DIR__ . '/../../../Fixtures/Fixtures25'], | ||
'exclude' => ['PostgreSQL'], | ||
])); | ||
|
||
$r = new Registry($this->dbal); | ||
$schema = (new Compiler())->compile($r, [ | ||
new Entities($tokenizer->classLocator(), $reader), | ||
]); | ||
|
||
$this->assertSame( | ||
[ | ||
'id' => GeneratedField::ON_INSERT, | ||
'createdAt' => GeneratedField::BEFORE_INSERT, | ||
'createdAtGeneratedByDatabase' => GeneratedField::ON_INSERT, | ||
'updatedAt' => GeneratedField::BEFORE_INSERT | GeneratedField::BEFORE_UPDATE, | ||
], | ||
$schema['withGeneratedFields'][SchemaInterface::GENERATED_FIELDS] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
tests/Annotated/Functional/Driver/MySQL/GeneratedFieldsTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cycle\Annotated\Tests\Functional\Driver\MySQL; | ||
|
||
// phpcs:ignore | ||
use Cycle\Annotated\Tests\Functional\Driver\Common\GeneratedFieldsTest as CommonClass; | ||
|
||
/** | ||
* @group driver | ||
* @group driver-mysql | ||
*/ | ||
final class GeneratedFieldsTest extends CommonClass | ||
{ | ||
public const DRIVER = 'mysql'; | ||
} |
52 changes: 52 additions & 0 deletions
52
tests/Annotated/Functional/Driver/Postgres/GeneratedFieldsTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cycle\Annotated\Tests\Functional\Driver\Postgres; | ||
|
||
// phpcs:ignore | ||
use Cycle\Annotated\Entities; | ||
use Cycle\Annotated\Tests\Functional\Driver\Common\GeneratedFieldsTest as CommonClass; | ||
use Cycle\ORM\Schema\GeneratedField; | ||
use Cycle\ORM\SchemaInterface; | ||
use Cycle\Schema\Compiler; | ||
use Cycle\Schema\Registry; | ||
use Spiral\Attributes\ReaderInterface; | ||
use Spiral\Tokenizer\Config\TokenizerConfig; | ||
use Spiral\Tokenizer\Tokenizer; | ||
|
||
/** | ||
* @group driver | ||
* @group driver-postgres | ||
*/ | ||
final class GeneratedFieldsTest extends CommonClass | ||
{ | ||
public const DRIVER = 'postgres'; | ||
|
||
/** | ||
* @dataProvider allReadersProvider | ||
*/ | ||
public function testSerialGeneratedFields(ReaderInterface $reader): void | ||
{ | ||
$tokenizer = new Tokenizer(new TokenizerConfig([ | ||
'directories' => [__DIR__ . '/../../../Fixtures/Fixtures25/PostgreSQL'], | ||
'exclude' => [], | ||
])); | ||
|
||
$r = new Registry($this->dbal); | ||
|
||
$schema = (new Compiler())->compile($r, [ | ||
new Entities($tokenizer->classLocator(), $reader), | ||
]); | ||
|
||
$this->assertSame( | ||
[ | ||
'id' => GeneratedField::ON_INSERT, | ||
'smallSerial' => GeneratedField::ON_INSERT, | ||
'serial' => GeneratedField::ON_INSERT, | ||
'bigSerial' => GeneratedField::ON_INSERT, | ||
], | ||
$schema['withGeneratedSerial'][SchemaInterface::GENERATED_FIELDS] | ||
); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
tests/Annotated/Functional/Driver/SQLServer/GeneratedFieldsTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cycle\Annotated\Tests\Functional\Driver\SQLServer; | ||
|
||
// phpcs:ignore | ||
use Cycle\Annotated\Tests\Functional\Driver\Common\GeneratedFieldsTest as CommonClass; | ||
|
||
/** | ||
* @group driver | ||
* @group driver-sqlserver | ||
*/ | ||
final class GeneratedFieldsTest extends CommonClass | ||
{ | ||
public const DRIVER = 'sqlserver'; | ||
} |
Oops, something went wrong.