-
Notifications
You must be signed in to change notification settings - Fork 719
fix: プラグインで Entity 拡張時に redeclare fatal になる問題を修正 (EccubeBundle の auto_mapping を無効化) #6963
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
Closed
nanasess
wants to merge
3
commits into
EC-CUBE:4.4
from
nanasess:fix/doctrine-eccube-bundle-auto-mapping
+107
−0
Closed
Changes from all 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
101 changes: 101 additions & 0 deletions
101
tests/Eccube/Tests/Doctrine/ORM/Mapping/EccubeEntityMetadataDriverTest.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,101 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /* | ||
| * This file is part of EC-CUBE | ||
| * | ||
| * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved. | ||
| * | ||
| * http://www.ec-cube.co.jp/ | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Eccube\Tests\Doctrine\ORM\Mapping; | ||
|
|
||
| use Doctrine\Bundle\DoctrineBundle\Mapping\MappingDriver as BundleMappingDriver; | ||
| use Doctrine\ORM\EntityManagerInterface; | ||
| use Doctrine\Persistence\Mapping\Driver\MappingDriver; | ||
| use Doctrine\Persistence\Mapping\Driver\MappingDriverChain; | ||
| use Eccube\Doctrine\ORM\Mapping\Driver\TraitProxyAttributeDriver; | ||
| use Eccube\Tests\EccubeTestCase; | ||
|
|
||
| /** | ||
| * src/Eccube/Entity のマッピングが TraitProxyAttributeDriver 以外から二重登録されないことの回帰テスト. | ||
| * | ||
| * Kernel::addEntityExtensionPass は src/Eccube/Entity を TraitProxyAttributeDriver で登録する. | ||
| * これは Proxy (app/proxy/entity) で宣言済みの Entity を再 require しない実装になっている. | ||
| * ところが doctrine.orm.auto_mapping が EccubeBundle を検出すると、同じ src/Eccube/Entity が | ||
| * 素の AttributeDriver でも登録される. 素のドライバは ColocatedMappingDriver::getAllClassNames() で | ||
| * Entity ソースを無条件に require_once するため、Kernel::loadEntityProxies が Proxy を | ||
| * 先にロードした状態では "Cannot redeclare class" で fatal になる | ||
| * (Entity の if (!class_exists()) ガード全廃前は、そのガードが吸収していた). | ||
| * | ||
| * @see https://github.com/EC-CUBE/ec-cube/pull/6895 Entity の if(!class_exists()) ガード全廃 | ||
| */ | ||
| final class EccubeEntityMetadataDriverTest extends EccubeTestCase | ||
| { | ||
| /** | ||
| * src/Eccube/Entity を担当するドライバが TraitProxyAttributeDriver だけであることを検証する. | ||
| */ | ||
| public function testCoreEntityPathIsMappedOnlyByTraitProxyAttributeDriver(): void | ||
| { | ||
| $entityManager = static::getContainer()->get(EntityManagerInterface::class); | ||
| $driver = $entityManager->getConfiguration()->getMetadataDriverImpl(); | ||
|
|
||
| $coreEntityDir = realpath(__DIR__.'/../../../../../../src/Eccube/Entity'); | ||
| $this->assertIsString($coreEntityDir); | ||
|
|
||
| $coreEntityDrivers = []; | ||
| foreach ($this->flattenDrivers($driver) as $each) { | ||
| if (!method_exists($each, 'getPaths')) { | ||
| continue; | ||
| } | ||
| foreach ($each->getPaths() as $path) { | ||
| if (realpath($path) !== $coreEntityDir) { | ||
| continue; | ||
| } | ||
| $coreEntityDrivers[] = $each; | ||
| $this->assertInstanceOf(TraitProxyAttributeDriver::class, $each, 'src/Eccube/Entity は TraitProxyAttributeDriver 以外から登録してはならない' | ||
| .' (素の AttributeDriver は Entity ソースを無条件に require_once するため、' | ||
| .'Proxy ロード済みの環境で "Cannot redeclare class" になる)'); | ||
| } | ||
| } | ||
|
|
||
| // 対象ドライバが 0 件でもループが素通りするため、明示登録そのものが失われた構成も検知する | ||
| $this->assertNotEmpty( | ||
| $coreEntityDrivers, | ||
| 'src/Eccube/Entity のマッピングドライバ (Kernel::addEntityExtensionPass の TraitProxyAttributeDriver) が登録されていない' | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * MappingDriverChain を再帰的に展開して、実際にマッピングを解決するドライバを列挙する. | ||
| * | ||
| * @return list<MappingDriver> | ||
| */ | ||
| private function flattenDrivers(?MappingDriver $driver): array | ||
| { | ||
| if ($driver === null) { | ||
| return []; | ||
| } | ||
|
|
||
| // doctrine-bundle の MappingDriver は実ドライバをラップしているため中身を取り出す | ||
| if ($driver instanceof BundleMappingDriver) { | ||
| return $this->flattenDrivers($driver->getDriver()); | ||
| } | ||
|
|
||
| if (!$driver instanceof MappingDriverChain) { | ||
| return [$driver]; | ||
| } | ||
|
|
||
| $drivers = []; | ||
| foreach ($driver->getDrivers() as $each) { | ||
| $drivers = [...$drivers, ...$this->flattenDrivers($each)]; | ||
| } | ||
|
|
||
| return [...$drivers, ...$this->flattenDrivers($driver->getDefaultDriver())]; | ||
| } | ||
| } | ||
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.