-
Notifications
You must be signed in to change notification settings - Fork 11
/
CompilerTest.php
146 lines (123 loc) · 5.01 KB
/
CompilerTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
declare(strict_types=1);
namespace Cycle\Schema\Tests;
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;
use Cycle\Schema\Definition\Field;
use Cycle\Schema\Exception\CompilerException;
use Cycle\Schema\Exception\SchemaModifierException;
use Cycle\Schema\Registry;
use Cycle\Schema\Tests\Fixtures\Author;
use Cycle\Schema\Tests\Fixtures\BrokenSchemaModifier;
use Cycle\Schema\Tests\Fixtures\Typecaster;
use PHPUnit\Framework\TestCase;
final class CompilerTest extends TestCase
{
public function testWrongGeneratorShouldThrowAnException(): void
{
$this->expectException(CompilerException::class);
$this->expectExceptionMessage(
'Invalid generator `\'Cycle\\\\Schema\\\\Tests\\\\Fixtures\\\\Author\'`. '
. 'It should implement `Cycle\Schema\GeneratorInterface` interface.'
);
$r = new Registry(
$this->createMock(DatabaseProviderInterface::class)
);
$author = new Entity();
$author->setRole('author')->setClass(Author::class);
$author->getFields()->set('id', (new Field())->setType('primary')->setColumn('id'));
$r->register($author);
(new Compiler())->compile($r, [
Author::class,
]);
}
public function testWrongEntitySchemaModifierShouldThrowAnException(): void
{
$this->expectException(SchemaModifierException::class);
$this->expectExceptionMessage(
'Unable to apply schema modifier `Cycle\Schema\Tests\Fixtures\BrokenSchemaModifier` '
. 'for the `author` role. Something went wrong'
);
$r = new Registry(
$this->createMock(DatabaseProviderInterface::class)
);
$author = new Entity();
$author->setRole('author')->setClass(Author::class);
$author->getFields()->set('id', (new Field())->setType('primary')->setColumn('id'));
$author->addSchemaModifier(new BrokenSchemaModifier(BrokenSchemaModifier::class . '::modifySchema'));
$r->register($author);
(new Compiler())->compile($r);
}
/**
* @dataProvider renderTypecastDataProvider
*/
public function testRenderTypecast(mixed $expected, array $defaults, mixed $entityTypecast = null): void
{
$entity = new Entity();
$entity->setRole('author')->setClass(Author::class);
$entity->getFields()->set('id', (new Field())->setType('primary')->setColumn('id'));
if ($entityTypecast) {
$entity->setTypecast($entityTypecast);
}
$r = new Registry($this->createMock(DatabaseProviderInterface::class));
$r->register($entity);
$schema = (new Compiler())->compile($r, [], $defaults);
$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, []];
yield [Typecaster::class, [], Typecaster::class];
yield [[Typecaster::class], [SchemaInterface::TYPECAST_HANDLER => Typecaster::class]];
yield [
[Typecaster::class, Typecast::class],
[SchemaInterface::TYPECAST_HANDLER => Typecast::class],
Typecaster::class,
];
yield [
[Typecaster::class, Typecast::class],
[SchemaInterface::TYPECAST_HANDLER => [Typecaster::class, Typecast::class]],
Typecaster::class,
];
}
}