Skip to content

Commit

Permalink
Merge pull request #75: Use schema modifiers as mutable
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk authored Jul 10, 2024
2 parents 8fbc463 + e618563 commit 413af8f
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 16 deletions.
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
"Cycle\\Schema\\": "src/"
}
},
"extra": {
"branch-alias": {
"dev-master": "2.9.x-dev"
}
},
"autoload-dev": {
"psr-4": {
"Cycle\\Schema\\Tests\\": "tests/Schema/"
Expand Down
1 change: 1 addition & 0 deletions src/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ private function compute(Registry $registry, Entity $entity): void

// Apply modifiers
foreach ($entity->getSchemaModifiers() as $modifier) {
\assert($modifier instanceof SchemaModifierInterface);
try {
$modifier->modifySchema($schema);
} catch (Throwable $e) {
Expand Down
6 changes: 4 additions & 2 deletions src/Definition/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,10 @@ public function getForeignKeys(): ForeignKeyMap

public function addSchemaModifier(SchemaModifierInterface $modifier): self
{
$this->schemaModifiers[] = $modifier;
$this->schemaModifiers[] = $modifier->withRole($this->role ?? throw new EntityException(
'Entity must have a `role` to be able to add a modifier.'
));

return $this;
}

Expand All @@ -261,7 +264,6 @@ public function addSchemaModifier(SchemaModifierInterface $modifier): self
*/
public function getSchemaModifiers(): \Traversable
{
// yield from $this->getRelations();
yield from $this->schemaModifiers;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Generator/GenerateModifiers.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected function register(Registry $registry, Entity $entity): void
foreach ($entity->getSchemaModifiers() as $modifier) {
\assert($modifier instanceof SchemaModifierInterface);
try {
$modifier->withRole($role)->compute($registry);
$modifier->compute($registry);
} catch (SchemaModifierException $e) {
throw new SchemaException(
sprintf('Unable to compute modifier `%s` for the `%s` role.', $modifier::class, $role),
Expand Down
2 changes: 1 addition & 1 deletion src/Generator/RenderModifiers.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected function register(Registry $registry, Entity $entity): void
foreach ($entity->getSchemaModifiers() as $modifier) {
\assert($modifier instanceof SchemaModifierInterface);
try {
$modifier->withRole($role)->render($registry);
$modifier->render($registry);
} catch (SchemaModifierException $e) {
throw new SchemaException(
sprintf('Unable to render modifier `%s` for the `%s` role.', $modifier::class, $role),
Expand Down
19 changes: 17 additions & 2 deletions tests/Schema/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,14 +303,29 @@ public function testJoinedTableInheritance(): void
$this->assertSame($inheritance, $e->getInheritance());
}

public function testSchemaModifier(): void
public function testSchemaModifierWithRole(): void
{
$modifier = $this->createMock(SchemaModifierInterface::class);
$modifier->expects($this->once())->method('withRole')->with('my-entity')->willReturnSelf();

$e = new Entity();
$e->addSchemaModifier($modifier = $this->createMock(SchemaModifierInterface::class));
$e->setRole('my-entity');
$e->addSchemaModifier($modifier);

$this->assertSame([$modifier], iterator_to_array($e->getSchemaModifiers()));
}

public function testSchemaModifier(): void
{
$modifier = $this->createMock(SchemaModifierInterface::class);
$e = new Entity();

$this->expectException(EntityException::class);
$this->expectExceptionMessage('Entity must have a `role` to be able to add a modifier.');

$e->addSchemaModifier($modifier);
}

public function testMergeTwoEntities(): void
{
$e = new Entity();
Expand Down
1 change: 0 additions & 1 deletion tests/Schema/Generator/GenerateModifiersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ public function testErrorInsideModifierShouldThrowAnException(string $method)
public function brokenMethodsDataProvider()
{
return [
'withRole' => ['withRole'],
'compute' => ['compute'],
];
}
Expand Down
12 changes: 5 additions & 7 deletions tests/Schema/Generator/RenderModifiersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@ public function testEntityShouldBeRendered()
$user->setRole('user')->setClass(User::class);
$user->getFields()->set('foo_bar', (new Field())->setType('primary')->setColumn('id'));

$user->addSchemaModifier($mock = $this->createMock(SchemaModifierInterface::class));
$mock
->expects($this->atLeastOnce())
->method('withRole')
->with($this->equalTo('user'))
->willReturn($mock);
$mock = $this->createMock(SchemaModifierInterface::class);
$mock->expects(self::once())->method('withRole')->with('user')->willReturnSelf();

$user->addSchemaModifier($mock);

$mock
->expects($this->atLeastOnce())
Expand Down Expand Up @@ -73,7 +71,7 @@ public function testErrorInsideModifierShouldThrowAnException(string $method)
public function brokenMethodsDataProvider()
{
return [
'withRole' => ['withRole'],
// 'withRole' => ['withRole'],
'render' => ['render'],
];
}
Expand Down
4 changes: 2 additions & 2 deletions tests/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ services:
SA_PASSWORD: "SSpaSS__1"
ACCEPT_EULA: "Y"

mysql_latest:
image: mysql:latest
mysql:
image: mysql:8.0.37
restart: always
command: --default-authentication-plugin=mysql_native_password
ports:
Expand Down

0 comments on commit 413af8f

Please sign in to comment.