From de8d463453421bca4fb5f6f61f35d9299b5480c1 Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Thu, 24 Aug 2023 19:24:24 +1200 Subject: [PATCH] MNT Fix unit test --- .../DataObject/InheritanceBuilderTest.php | 48 ++++++++++++------- 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/tests/Schema/DataObject/InheritanceBuilderTest.php b/tests/Schema/DataObject/InheritanceBuilderTest.php index adffea71..66c9d144 100644 --- a/tests/Schema/DataObject/InheritanceBuilderTest.php +++ b/tests/Schema/DataObject/InheritanceBuilderTest.php @@ -25,6 +25,7 @@ use SilverStripe\GraphQL\Tests\Fake\Inheritance\MySubclass; use SilverStripe\Core\Config\Config; use SilverStripe\Core\Injector\Injector; +use SilverStripe\Core\Injector\SilverStripeServiceConfigurationLocator; class InheritanceBuilderTest extends SapphireTest { @@ -323,24 +324,35 @@ private function assertFields(array $fields, Type $type) public function testFillAncestryInjectorSubclass() { - Config::modify()->merge(Injector::class, MyOrig::class, ['class' => MySubclass::class]); - $obj = MyOrig::create(); - $this->assertSame(MySubclass::class, get_class($obj)); - $schema = new TestSchema(); - $schema->applyConfig([ - 'models' => [ - MyOrig::class => [ - 'fields' => [ - 'MyField' => true, - 'MySubclassField' => true, + // Update configLocator of the Injector inst() in order to clear the cache in + // SilverStripeServiceConfigurationLocator() which has no API to clear the cache. + // This is done to fix bleed over from the unit test UsedOnTableTest::getUsage() + // which will cache the config for MyOrig::class as that test will automatically + // cache config for most DataObjects + Injector::inst()->setConfigLocator(new SilverStripeServiceConfigurationLocator()); + try { + Config::modify()->set(Injector::class, MyOrig::class, ['class' => MySubclass::class]); + $obj = MyOrig::create(); + $this->assertSame(MySubclass::class, get_class($obj)); + $schema = new TestSchema(); + $schema->applyConfig([ + 'models' => [ + MyOrig::class => [ + 'fields' => [ + 'MyField' => true, + 'MySubclassField' => true, + ], ], - ], - ] - ]); - $schema->createStoreableSchema(); - $modelType = $schema->getModelByClassName(MySubclass::class); - $builder = new InheritanceBuilder($schema); - $builder->fillAncestry($modelType); - $this->assertFields(['id', 'MyField', 'MySubclassField'], $modelType); + ] + ]); + $schema->createStoreableSchema(); + $modelType = $schema->getModelByClassName(MySubclass::class); + $builder = new InheritanceBuilder($schema); + $builder->fillAncestry($modelType); + $this->assertFields(['id', 'MyField', 'MySubclassField'], $modelType); + } finally { + // reset the cache again to prevent bleed over into any other tests + Injector::inst()->setConfigLocator(new SilverStripeServiceConfigurationLocator()); + } } }