Skip to content
This repository was archived by the owner on Feb 6, 2020. It is now read-only.

Commit 5ab23c5

Browse files
committed
Merge branch 'hotfix/plugin-manager-with-config' into develop
Close #35
2 parents 3a911f7 + d70d21a commit 5ab23c5

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

src/ServiceManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public function __construct(array $config = [])
147147
public function withConfig(array $config)
148148
{
149149
$container = clone $this;
150-
$container->creationContext = $container;
150+
$container->creationContext = ($this === $this->creationContext) ? $container : $this->creationContext;
151151
$container->configure($config);
152152
return $container;
153153
}

test/AbstractPluginManagerTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,52 @@ function ($container, $name, $callback) {
164164
);
165165
$this->assertEquals('bar', $instance->foo);
166166
}
167+
168+
public function testWithConfigProperlySetsCreationContextToOriginalCreationContext()
169+
{
170+
$config = [
171+
'factories' => [
172+
InvokableObject::class => new InvokableFactory(),
173+
],
174+
];
175+
176+
$services = new ServiceManager();
177+
$plugins = new SimplePluginManager($services);
178+
179+
$revised = $plugins->withConfig(['factories' => [
180+
'foo' => function ($container, $name) {
181+
},
182+
]]);
183+
184+
$this->assertAttributeSame($services, 'creationContext', $revised);
185+
}
186+
187+
/**
188+
* Overrides test from CommonServiceLocatorBehaviorsTrait
189+
*
190+
* Behavior of creation context differs for plugin managers.
191+
*/
192+
public function testCanCreateNewLocatorWithMergedConfig()
193+
{
194+
$serviceManager = $this->createContainer([
195+
'factories' => [
196+
DateTime::class => InvokableFactory::class
197+
]
198+
]);
199+
200+
$newServiceManager = $serviceManager->withConfig([
201+
'factories' => [
202+
stdClass::class => InvokableFactory::class
203+
]
204+
]);
205+
206+
$this->assertTrue($serviceManager->has(DateTime::class));
207+
$this->assertFalse($serviceManager->has(stdClass::class));
208+
209+
$this->assertTrue($newServiceManager->has(DateTime::class));
210+
$this->assertTrue($newServiceManager->has(stdClass::class));
211+
212+
// Make sure the context has been updated for the new container
213+
$this->assertAttributeSame($this->creationContext, 'creationContext', $newServiceManager);
214+
}
167215
}

0 commit comments

Comments
 (0)