-
-
Notifications
You must be signed in to change notification settings - Fork 81
Support BackedEnum for discriminator values in STI #566
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
10 changes: 10 additions & 0 deletions
10
tests/ORM/Functional/Driver/Common/Inheritance/Fixture/BossWithKind.php
This file contains hidden or 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,10 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Cycle\ORM\Tests\Functional\Driver\Common\Inheritance\Fixture; | ||
|
|
||
| class BossWithKind extends WorkerWithKind | ||
| { | ||
| public ?int $level = null; | ||
| } |
11 changes: 11 additions & 0 deletions
11
tests/ORM/Functional/Driver/Common/Inheritance/Fixture/EmployeeKind.php
This file contains hidden or 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,11 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Cycle\ORM\Tests\Functional\Driver\Common\Inheritance\Fixture; | ||
|
|
||
| enum EmployeeKind: int | ||
| { | ||
| case Employee = 1; | ||
| case Manager = 2; | ||
| } |
11 changes: 11 additions & 0 deletions
11
tests/ORM/Functional/Driver/Common/Inheritance/Fixture/EmployeeType.php
This file contains hidden or 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,11 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Cycle\ORM\Tests\Functional\Driver\Common\Inheritance\Fixture; | ||
|
|
||
| enum EmployeeType: string | ||
| { | ||
| case Employee = 'employee'; | ||
| case Manager = 'manager'; | ||
| } |
13 changes: 13 additions & 0 deletions
13
tests/ORM/Functional/Driver/Common/Inheritance/Fixture/WorkerWithKind.php
This file contains hidden or 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,13 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Cycle\ORM\Tests\Functional\Driver\Common\Inheritance\Fixture; | ||
|
|
||
| class WorkerWithKind extends Human | ||
| { | ||
| public ?EmployeeType $type = null; | ||
| public ?string $name = null; | ||
| public ?string $email = null; | ||
| public ?int $age = 0; | ||
| } |
163 changes: 163 additions & 0 deletions
163
tests/ORM/Functional/Driver/Common/Inheritance/STI/EnumDiscriminatorTest.php
This file contains hidden or 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,163 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Cycle\ORM\Tests\Functional\Driver\Common\Inheritance\STI; | ||
|
|
||
| use Cycle\ORM\Heap\Heap; | ||
| use Cycle\ORM\Mapper\Mapper; | ||
| use Cycle\ORM\Schema; | ||
| use Cycle\ORM\SchemaInterface; | ||
| use Cycle\ORM\Select; | ||
| use Cycle\ORM\Tests\Functional\Driver\Common\Inheritance\Fixture\Employee; | ||
| use Cycle\ORM\Tests\Functional\Driver\Common\Inheritance\Fixture\EmployeeType; | ||
| use Cycle\ORM\Tests\Functional\Driver\Common\Inheritance\Fixture\Manager; | ||
|
|
||
| abstract class EnumDiscriminatorTest extends StiBaseTest | ||
| { | ||
| protected const BASE_ROLE = 'employee'; | ||
| protected const MANAGER_ROLE = 'manager'; | ||
|
|
||
| protected static string $discriminator = 'discriminator_value'; | ||
|
|
||
| public function testChildClassResolvedFromScalarInDatabase(): void | ||
| { | ||
| $selector = new Select($this->orm, Employee::class); | ||
| [$first, $second] = $selector->orderBy('id')->fetchAll(); | ||
|
|
||
| $this->assertInstanceOf(Manager::class, $first); | ||
| $this->assertInstanceOf(Employee::class, $second); | ||
| $this->assertNotInstanceOf(Manager::class, $second); | ||
| } | ||
|
|
||
| public function testFetchedDataExposesEnumCaseAtDiscriminatorColumn(): void | ||
| { | ||
| $rows = (new Select($this->orm, Employee::class))->orderBy('id')->fetchData(); | ||
|
|
||
| $this->assertSame(EmployeeType::Manager, $rows[0]['_type']); | ||
| $this->assertSame(EmployeeType::Employee, $rows[1]['_type']); | ||
| } | ||
|
|
||
| public function testPersistedChildWritesScalarToDatabase(): void | ||
| { | ||
| $manager = new Manager(); | ||
| $manager->name = 'Manager'; | ||
| $manager->email = 'admin@email.com'; | ||
| $manager->age = 69; | ||
|
|
||
| $this->save($manager); | ||
|
|
||
| $row = $this->getDatabase() | ||
| ->table('employee_table') | ||
| ->select() | ||
| ->where('id', $manager->id) | ||
| ->fetchAll()[0]; | ||
|
|
||
| $this->assertSame('manager', $row[static::$discriminator]); | ||
| } | ||
|
|
||
| public function testRoundTripChildEntity(): void | ||
| { | ||
| $manager = new Manager(); | ||
| $manager->name = 'Manager'; | ||
| $manager->email = 'admin@email.com'; | ||
| $manager->age = 69; | ||
|
|
||
| $this->save($manager); | ||
|
|
||
| $loaded = (new Select($this->orm->withHeap(new Heap()), Employee::class)) | ||
| ->wherePK($manager->id) | ||
| ->fetchOne(); | ||
|
|
||
| $this->assertInstanceOf(Manager::class, $loaded); | ||
| } | ||
|
|
||
| public function testMakeResolvesChildClassFromEnumInputData(): void | ||
| { | ||
| // Defensive: explicit BackedEnum passed as discriminator value should still resolve. | ||
| $entity = $this->orm->make(static::BASE_ROLE, [ | ||
| '_type' => EmployeeType::Manager, | ||
| 'name' => 'Senya', | ||
| 'email' => 'sene4ka@hamster.me', | ||
| 'age' => 12, | ||
| ]); | ||
|
|
||
| $this->assertInstanceOf(Manager::class, $entity); | ||
| } | ||
|
|
||
| public function testNoExtraWritesAfterLoadAndResave(): void | ||
| { | ||
| /** @var Manager $manager */ | ||
| $manager = (new Select($this->orm, Employee::class))->orderBy('id')->fetchOne(); | ||
| $this->assertInstanceOf(Manager::class, $manager); | ||
|
|
||
| $this->captureWriteQueries(); | ||
| $this->save($manager); | ||
| $this->assertNumWrites(0); | ||
| } | ||
|
|
||
| public function testUpdateChildEntityKeepsScalarDiscriminatorInDatabase(): void | ||
| { | ||
| /** @var Manager $manager */ | ||
| $manager = (new Select($this->orm, Employee::class))->orderBy('id')->fetchOne(); | ||
| $this->assertInstanceOf(Manager::class, $manager); | ||
|
|
||
| $manager->name = 'Renamed'; | ||
| $this->save($manager); | ||
|
|
||
| $row = $this->getDatabase() | ||
| ->table('employee_table') | ||
| ->select() | ||
| ->where('id', $manager->id) | ||
| ->fetchAll()[0]; | ||
|
|
||
| $this->assertSame('manager', $row[static::$discriminator]); | ||
| $this->assertSame('Renamed', $row['name']); | ||
| } | ||
|
|
||
| public function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
|
|
||
| $this->makeTable('employee_table', [ | ||
| static::$discriminator => 'string,nullable', | ||
| 'id' => 'primary', | ||
| 'name' => 'string', | ||
| 'email' => 'string', | ||
| 'age' => 'int', | ||
| ]); | ||
|
|
||
| $this->getDatabase()->table('employee_table')->insertMultiple( | ||
| [static::$discriminator, 'name', 'email', 'age'], | ||
| [ | ||
| ['_type' => 'manager', 'name' => 'John', 'email' => 'captain@black.sea', 'age' => 38], | ||
| ['_type' => 'employee', 'name' => 'Anton', 'email' => 'antonio@mail.org', 'age' => 35], | ||
| ], | ||
| ); | ||
|
|
||
| $this->orm = $this->withSchema(new Schema($this->getSchemaArray())); | ||
| } | ||
|
|
||
| protected function getSchemaArray(): array | ||
| { | ||
| return [ | ||
| static::BASE_ROLE => [ | ||
| SchemaInterface::ENTITY => Employee::class, | ||
| SchemaInterface::CHILDREN => [ | ||
| 'manager' => Manager::class, | ||
| ], | ||
| SchemaInterface::MAPPER => Mapper::class, | ||
| SchemaInterface::DATABASE => 'default', | ||
| SchemaInterface::TABLE => 'employee_table', | ||
| SchemaInterface::PRIMARY_KEY => 'id', | ||
| SchemaInterface::COLUMNS => ['id', '_type' => static::$discriminator, 'name', 'email', 'age'], | ||
| SchemaInterface::TYPECAST => ['id' => 'int', 'age' => 'int', '_type' => EmployeeType::class], | ||
| SchemaInterface::SCHEMA => [], | ||
| SchemaInterface::RELATIONS => [], | ||
| ], | ||
| self::MANAGER_ROLE => [ | ||
| SchemaInterface::ENTITY => Manager::class, | ||
| ], | ||
| ]; | ||
| } | ||
| } |
152 changes: 152 additions & 0 deletions
152
tests/ORM/Functional/Driver/Common/Inheritance/STI/EnumPropertyDiscriminatorTest.php
This file contains hidden or 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,152 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Cycle\ORM\Tests\Functional\Driver\Common\Inheritance\STI; | ||
|
|
||
| use Cycle\ORM\Heap\Heap; | ||
| use Cycle\ORM\Mapper\Mapper; | ||
| use Cycle\ORM\Schema; | ||
| use Cycle\ORM\SchemaInterface; | ||
| use Cycle\ORM\Select; | ||
| use Cycle\ORM\Tests\Functional\Driver\Common\Inheritance\Fixture\BossWithKind; | ||
| use Cycle\ORM\Tests\Functional\Driver\Common\Inheritance\Fixture\EmployeeType; | ||
| use Cycle\ORM\Tests\Functional\Driver\Common\Inheritance\Fixture\WorkerWithKind; | ||
|
|
||
| /** | ||
| * STI scenario where the entity exposes the discriminator as a BackedEnum-typed property | ||
| * (so `extractData` returns an enum case in the discriminator slot). Exercises the | ||
| * scalarization branch in `Mapper::fetchFields`. | ||
| */ | ||
| abstract class EnumPropertyDiscriminatorTest extends StiBaseTest | ||
| { | ||
| protected const WORKER_ROLE = 'worker'; | ||
| protected const BOSS_ROLE = 'boss'; | ||
|
|
||
| public function testInsertChildWritesScalarDespiteEnumOnProperty(): void | ||
| { | ||
| $boss = new BossWithKind(); | ||
| $boss->type = EmployeeType::Manager; | ||
| $boss->name = 'Boss'; | ||
| $boss->email = 'boss@corp.example'; | ||
| $boss->age = 50; | ||
| $boss->level = 10; | ||
|
|
||
| $this->save($boss); | ||
|
|
||
| $row = $this->getDatabase() | ||
| ->table('worker_table') | ||
| ->select() | ||
| ->where('id', $boss->id) | ||
| ->fetchAll()[0]; | ||
|
|
||
| $this->assertSame('manager', $row['type']); | ||
| } | ||
|
|
||
| public function testInsertBaseWithEnumPropertyWritesScalar(): void | ||
| { | ||
| // Base entity does NOT match any child in CHILDREN -> getDiscriminatorValues() returns []. | ||
| // The only thing that scalarizes the enum from $entity->type is the fetchFields() branch. | ||
|
roxblnfk marked this conversation as resolved.
Outdated
|
||
| $worker = new WorkerWithKind(); | ||
| $worker->type = EmployeeType::Employee; | ||
| $worker->name = 'Plain Worker'; | ||
| $worker->email = 'plain@corp.example'; | ||
| $worker->age = 30; | ||
|
|
||
| $this->save($worker); | ||
|
|
||
| $row = $this->getDatabase() | ||
| ->table('worker_table') | ||
| ->select() | ||
| ->where('id', $worker->id) | ||
| ->fetchAll()[0]; | ||
|
|
||
| $this->assertSame('employee', $row['type']); | ||
| } | ||
|
|
||
| public function testRoundTripPreservesEnumOnEntity(): void | ||
| { | ||
| $boss = new BossWithKind(); | ||
| $boss->type = EmployeeType::Manager; | ||
| $boss->name = 'Boss'; | ||
| $boss->email = 'boss@corp.example'; | ||
| $boss->age = 50; | ||
| $boss->level = 10; | ||
| $this->save($boss); | ||
|
|
||
| /** @var BossWithKind $loaded */ | ||
| $loaded = (new Select($this->orm->withHeap(new Heap()), WorkerWithKind::class)) | ||
| ->wherePK($boss->id) | ||
| ->fetchOne(); | ||
|
|
||
| $this->assertInstanceOf(BossWithKind::class, $loaded); | ||
| $this->assertSame(EmployeeType::Manager, $loaded->type); | ||
| } | ||
|
|
||
| public function testNoExtraWritesAfterRoundTrip(): void | ||
| { | ||
| $boss = new BossWithKind(); | ||
| $boss->type = EmployeeType::Manager; | ||
| $boss->name = 'Boss'; | ||
| $boss->email = 'boss@corp.example'; | ||
| $boss->age = 50; | ||
| $boss->level = 10; | ||
| $this->save($boss); | ||
|
|
||
| $this->orm = $this->orm->withHeap(new Heap()); | ||
|
|
||
| /** @var BossWithKind $loaded */ | ||
| $loaded = (new Select($this->orm, WorkerWithKind::class)) | ||
| ->wherePK($boss->id) | ||
| ->fetchOne(); | ||
|
|
||
| $this->captureWriteQueries(); | ||
| $this->save($loaded); | ||
| $this->assertNumWrites(0); | ||
| } | ||
|
|
||
| public function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
|
|
||
| $this->makeTable('worker_table', [ | ||
| 'type' => 'string,nullable', | ||
| 'id' => 'primary', | ||
| 'name' => 'string', | ||
| 'email' => 'string', | ||
| 'age' => 'int', | ||
| 'level' => 'int,nullable', | ||
| ]); | ||
|
|
||
| $this->orm = $this->withSchema(new Schema($this->getSchemaArray())); | ||
| } | ||
|
|
||
| protected function getSchemaArray(): array | ||
| { | ||
| return [ | ||
| self::WORKER_ROLE => [ | ||
| SchemaInterface::ENTITY => WorkerWithKind::class, | ||
| SchemaInterface::CHILDREN => [ | ||
| 'manager' => BossWithKind::class, | ||
| ], | ||
| SchemaInterface::MAPPER => Mapper::class, | ||
| SchemaInterface::DATABASE => 'default', | ||
| SchemaInterface::TABLE => 'worker_table', | ||
| SchemaInterface::PRIMARY_KEY => 'id', | ||
| SchemaInterface::DISCRIMINATOR => 'type', | ||
| SchemaInterface::COLUMNS => ['id', 'type', 'name', 'email', 'age', 'level'], | ||
| SchemaInterface::TYPECAST => [ | ||
| 'id' => 'int', | ||
| 'age' => 'int', | ||
| 'level' => 'int', | ||
| 'type' => EmployeeType::class, | ||
| ], | ||
| SchemaInterface::SCHEMA => [], | ||
| SchemaInterface::RELATIONS => [], | ||
| ], | ||
| self::BOSS_ROLE => [ | ||
| SchemaInterface::ENTITY => BossWithKind::class, | ||
| ], | ||
| ]; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.