Skip to content

Commit 079c004

Browse files
Added UUID test cases, now using ramsey/identifier
1 parent 6b0f189 commit 079c004

30 files changed

+2125
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cycle\ORM\Entity\Behavior\Identifier\Tests\Fixtures\Uuid;
6+
7+
use Cycle\Annotated\Annotation\Column;
8+
use Cycle\Annotated\Annotation\Entity;
9+
use Cycle\ORM\Entity\Behavior\Identifier\Uuid1;
10+
use Cycle\ORM\Entity\Behavior\Identifier\Uuid7;
11+
use Ramsey\Identifier\Uuid;
12+
13+
/**
14+
* @Entity
15+
* @Uuid1
16+
* @Uuid1(field="otherUuid", column="other_uuid")
17+
* @Uuid7(field="uuid7")
18+
* @Uuid7(field="otherUuid7", column="other_uuid7")
19+
*/
20+
#[Entity]
21+
#[Uuid1]
22+
#[Uuid1(field: 'otherUuid', column: 'other_uuid')]
23+
#[Uuid7(field: 'uuid7')]
24+
#[Uuid7(field: 'otherUuid7', column: 'other_uuid7')]
25+
final class MultipleUuid
26+
{
27+
/**
28+
* @Column(type="uuid", primary=true)
29+
*/
30+
#[Column(type: 'uuid', primary: true)]
31+
public Uuid $uuid;
32+
33+
/**
34+
* @Column(type="uuid", name="other_uuid")
35+
*/
36+
#[Column(type: 'uuid', name: 'other_uuid')]
37+
public Uuid $otherUuid;
38+
39+
/**
40+
* @Column(type="uuid")
41+
*/
42+
#[Column(type: 'uuid')]
43+
public Uuid $uuid7;
44+
45+
/**
46+
* @Column(type="uuid", name="other_uuid7")
47+
*/
48+
#[Column(type: 'uuid', name: 'other_uuid7')]
49+
public Uuid $otherUuid7;
50+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cycle\ORM\Entity\Behavior\Identifier\Tests\Fixtures\Uuid;
6+
7+
use Cycle\Annotated\Annotation\Column;
8+
use Cycle\Annotated\Annotation\Entity;
9+
use Cycle\ORM\Entity\Behavior\Identifier\Uuid1;
10+
use Ramsey\Identifier\Uuid;
11+
12+
/**
13+
* @Entity
14+
* @Uuid1
15+
* @Uuid1(field="notDefinedUuid", column="not_defined_uuid", nullable=true)
16+
*/
17+
#[Entity]
18+
#[Uuid1]
19+
#[Uuid1(field: 'notDefinedUuid', column: 'not_defined_uuid', nullable: true)]
20+
final class NullableUuid
21+
{
22+
/**
23+
* @Column(type="uuid", primary=true)
24+
*/
25+
#[Column(type: 'uuid', primary: true)]
26+
public Uuid $uuid;
27+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cycle\ORM\Entity\Behavior\Identifier\Tests\Fixtures\Uuid;
6+
7+
use Cycle\Annotated\Annotation\Column;
8+
use Cycle\Annotated\Annotation\Entity;
9+
use Cycle\ORM\Entity\Behavior\Identifier\Uuid4;
10+
11+
/**
12+
* @Entity
13+
* @Uuid4(field="customUuid", column="custom_uuid")
14+
*/
15+
#[Entity]
16+
#[Uuid4(field: 'customUuid', column: 'custom_uuid')]
17+
class Post
18+
{
19+
/**
20+
* @Column(type="primary")
21+
*/
22+
#[Column(type: 'primary')]
23+
public int $id;
24+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cycle\ORM\Entity\Behavior\Identifier\Tests\Fixtures\Uuid;
6+
7+
use Cycle\Annotated\Annotation\Column;
8+
use Cycle\Annotated\Annotation\Entity;
9+
use Cycle\ORM\Entity\Behavior\Identifier\Uuid1;
10+
use Ramsey\Identifier\Uuid;
11+
12+
/**
13+
* @Entity
14+
* @Uuid1
15+
*/
16+
#[Entity]
17+
#[Uuid1]
18+
class User
19+
{
20+
/**
21+
* @Column(type="uuid", primary=true)
22+
*/
23+
#[Column(type: 'uuid', primary: true)]
24+
public Uuid $uuid;
25+
}
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Cycle\ORM\Entity\Behavior\Identifier\Tests\Functional\Driver\Common;
6+
7+
use Cycle\Annotated\Entities;
8+
use Cycle\Annotated\Locator\EntityLocatorInterface;
9+
use Cycle\Annotated\MergeColumns;
10+
use Cycle\Annotated\MergeIndexes;
11+
use Cycle\Database\Config\DatabaseConfig;
12+
use Cycle\Database\Database;
13+
use Cycle\Database\DatabaseManager;
14+
use Cycle\Database\Driver\DriverInterface;
15+
use Cycle\Database\Driver\Handler;
16+
use Cycle\ORM\Collection\ArrayCollectionFactory;
17+
use Cycle\ORM\Config\RelationConfig;
18+
use Cycle\ORM\Entity\Behavior\EventDrivenCommandGenerator;
19+
use Cycle\ORM\Entity\Behavior\Identifier\Tests\Traits\Loggable;
20+
use Cycle\ORM\Entity\Behavior\Identifier\Tests\Utils\SimpleContainer;
21+
use Cycle\ORM\EntityManager;
22+
use Cycle\ORM\Factory;
23+
use Cycle\ORM\SchemaInterface;
24+
use Cycle\ORM\ORM;
25+
use Cycle\Schema\Compiler;
26+
use Cycle\Schema\Generator\GenerateModifiers;
27+
use Cycle\Schema\Generator\GenerateRelations;
28+
use Cycle\Schema\Generator\GenerateTypecast;
29+
use Cycle\Schema\Generator\RenderModifiers;
30+
use Cycle\Schema\Generator\RenderRelations;
31+
use Cycle\Schema\Generator\RenderTables;
32+
use Cycle\Schema\Generator\ResetTables;
33+
use Cycle\Schema\Generator\ValidateEntities;
34+
use Cycle\Schema\Registry;
35+
use PHPUnit\Framework\TestCase;
36+
use Spiral\Attributes\AnnotationReader;
37+
use Spiral\Attributes\AttributeReader;
38+
use Spiral\Attributes\Composite\SelectiveReader;
39+
use Spiral\Attributes\ReaderInterface;
40+
41+
abstract class BaseTest extends TestCase
42+
{
43+
use Loggable;
44+
45+
public const DRIVER = null;
46+
47+
public static array $config;
48+
protected ?DatabaseManager $dbal = null;
49+
protected ?ORM $orm = null;
50+
protected ?DriverInterface $driver = null;
51+
private static array $driverCache = [];
52+
53+
public static function readersDataProvider(): \Traversable
54+
{
55+
yield [new AnnotationReader()];
56+
yield [new AttributeReader()];
57+
yield [new SelectiveReader([new AttributeReader(), new AnnotationReader()])];
58+
}
59+
60+
public function getDriver(): DriverInterface
61+
{
62+
if (isset(static::$driverCache[static::DRIVER])) {
63+
return static::$driverCache[static::DRIVER];
64+
}
65+
66+
$config = self::$config[static::DRIVER];
67+
if (!isset($this->driver)) {
68+
$this->driver = $config->driver::create($config);
69+
}
70+
71+
return static::$driverCache[static::DRIVER] = $this->driver;
72+
}
73+
74+
public function withSchema(SchemaInterface $schema): ORM
75+
{
76+
$this->orm = new ORM(
77+
new Factory(
78+
$this->dbal,
79+
RelationConfig::getDefault(),
80+
null,
81+
new ArrayCollectionFactory(),
82+
),
83+
$schema,
84+
new EventDrivenCommandGenerator($schema, new SimpleContainer()),
85+
);
86+
87+
return $this->orm;
88+
}
89+
90+
public function compileWithTokenizer(EntityLocatorInterface $tokenizer, ReaderInterface $reader): void
91+
{
92+
(new Compiler())->compile($this->registry = new Registry($this->dbal), [
93+
new Entities($tokenizer, $reader),
94+
new ResetTables(),
95+
new MergeColumns($reader),
96+
new MergeIndexes($reader),
97+
new GenerateRelations(),
98+
new GenerateModifiers(),
99+
new ValidateEntities(),
100+
new RenderTables(),
101+
new RenderRelations(),
102+
new RenderModifiers(),
103+
new GenerateTypecast(),
104+
]);
105+
}
106+
107+
public function setUp(): void
108+
{
109+
$this->dbal = new DatabaseManager(new DatabaseConfig());
110+
$this->dbal->addDatabase(
111+
new Database(
112+
'default',
113+
'',
114+
$this->getDriver(),
115+
),
116+
);
117+
118+
if (self::$config['debug'] ?? false) {
119+
$this->setUpLogger($this->getDriver());
120+
$this->enableProfiling();
121+
}
122+
}
123+
124+
public function tearDown(): void
125+
{
126+
$this->dropDatabase($this->dbal->database('default'));
127+
128+
$this->orm = null;
129+
$this->dbal = null;
130+
131+
if (\function_exists('gc_collect_cycles')) {
132+
\gc_collect_cycles();
133+
}
134+
}
135+
136+
protected function dropDatabase(?Database $database = null): void
137+
{
138+
if ($database === null) {
139+
return;
140+
}
141+
142+
foreach ($database->getTables() as $table) {
143+
$schema = $table->getSchema();
144+
145+
foreach ($schema->getForeignKeys() as $foreign) {
146+
$schema->dropForeignKey($foreign->getColumns());
147+
}
148+
149+
$schema->save(Handler::DROP_FOREIGN_KEYS);
150+
}
151+
152+
foreach ($database->getTables() as $table) {
153+
$schema = $table->getSchema();
154+
$schema->declareDropped();
155+
$schema->save();
156+
}
157+
}
158+
159+
protected function getDatabase(): Database
160+
{
161+
return $this->dbal->database('default');
162+
}
163+
164+
protected function save(object ...$entities): void
165+
{
166+
$em = new EntityManager($this->orm);
167+
foreach ($entities as $entity) {
168+
$em->persist($entity);
169+
}
170+
$em->run();
171+
}
172+
}

0 commit comments

Comments
 (0)