Repository navigation
Fix configuring a metadata class that extends the bundle's Metadata #14
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
e91e517
122606c
16ede20
8cfd68a
b532e84
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Setono\ClientBundle\Tests\Functional\App\Entity; | ||
|
|
||
| use Setono\ClientBundle\Entity\Metadata; | ||
|
|
||
| /** | ||
| * An application's own metadata entity, configured with the metadata_class option | ||
| */ | ||
| class CustomMetadata extends Metadata | ||
| { | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping | ||
| http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd"> | ||
| <entity name="Setono\ClientBundle\Tests\Functional\App\Entity\CustomMetadata" table="app_client_metadata"/> | ||
| </doctrine-mapping> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Setono\ClientBundle\Tests\Functional; | ||
|
|
||
| use Setono\ClientBundle\Entity\Metadata; | ||
| use Setono\ClientBundle\Tests\Functional\App\Entity\CustomMetadata; | ||
| use Setono\ClientBundle\Tests\Functional\App\Kernel; | ||
|
|
||
| /** | ||
| * Runs the metadata tests with an application entity that extends the bundle's Metadata class | ||
| */ | ||
| final class CustomMetadataClassTest extends MetadataTest | ||
| { | ||
| protected static function getEnvironment(): string | ||
| { | ||
| return Kernel::ENVIRONMENT_CUSTOM_METADATA; | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function it_stores_the_metadata_in_the_configured_entity(): void | ||
| { | ||
| $this->browser->request('GET', '/metadata/set?key=foo&value=bar'); | ||
|
|
||
| self::assertResponseIsSuccessful(); | ||
| self::assertSame('app_client_metadata', self::getEntityManager()->getClassMetadata(CustomMetadata::class)->getTableName()); | ||
| self::assertSame(['foo' => 'bar'], self::fetchStoredMetadata($this->getClientId())); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function it_keeps_the_bundles_metadata_a_mapped_superclass(): void | ||
| { | ||
| self::assertTrue(self::getEntityManager()->getClassMetadata(Metadata::class)->isMappedSuperclass); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,7 +26,7 @@ abstract class FunctionalTestCase extends WebTestCase | |
| protected function setUp(): void | ||
| { | ||
| // Fixed, so APP_ENV and APP_DEBUG in the environment, e.g. from the Symfony CLI, don't change the application | ||
| $this->browser = static::createClient(['environment' => 'test', 'debug' => true]); | ||
| $this->browser = static::createClient(['environment' => static::getEnvironment(), 'debug' => true]); | ||
|
|
||
| // An exception fails the test instead of becoming an error response | ||
| $this->browser->catchExceptions(false); | ||
|
|
@@ -37,6 +37,11 @@ protected static function getKernelClass(): string | |
| return Kernel::class; | ||
| } | ||
|
|
||
| protected static function getEnvironment(): string | ||
| { | ||
| return 'test'; | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Before,
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
|
|
||
| /** | ||
| * Returns the cookie stored in the browser | ||
| */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Setono\ClientBundle\Tests\Unit\DependencyInjection\Fixture; | ||
|
|
||
| use Setono\ClientBundle\Entity\MetadataInterface; | ||
|
|
||
| /** | ||
| * A metadata class that implements the interface without extending the bundle's Metadata class | ||
| */ | ||
| final class InterfaceOnlyMetadata implements MetadataInterface | ||
| { | ||
| public function getClientId(): ?string | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| public function setClientId(string $clientId): void | ||
| { | ||
| } | ||
|
|
||
| public function getMetadata(): array | ||
| { | ||
| return []; | ||
| } | ||
|
|
||
| public function setMetadata(array $metadata): void | ||
| { | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extending
Metadatasilently moves the data to a new table, and the migration diff drops the old oneThe child entity inherits the fields from the mapped superclass but not its
table. Doctrine only copies$parent->tablefor single-table inheritance (ClassMetadataFactory::doLoadMetadata). So<entity name="App\Entity\ClientMetadata"/>maps toClientMetadata/client_metadata, notsetono_client__metadata. I checked this with a probe: the child's table name came out as the short class name.Take an existing app that switches from the default class to a subclass, as this text suggests.
doctrine:migrations:diffthen creates the new table and dropssetono_client__metadata, because that table is now only a mapped superclass. All stored client metadata is lost. Please say here, and in the "tablesetono_client__metadata" note near the top of the README, that the entity must settable="setono_client__metadata"to keep existing data.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 2cbad1f: the README says that the subclass doesn't inherit the table name, and to map it to
table="setono_client__metadata"to keep the stored metadata.