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

Commit 9c3bccf

Browse files
committed
Updated tests to be more generic - using any factory type
1 parent 5ef6cd2 commit 9c3bccf

File tree

1 file changed

+74
-76
lines changed

1 file changed

+74
-76
lines changed

src/DelegatorTestTrait.php

Lines changed: 74 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -151,34 +151,6 @@ public function testDelegatorsDoNotApplyToAliasResolvingToServiceEntry() : void
151151
self::assertSame($instance, $container->get('foo-bar'));
152152
}
153153

154-
public function testDelegatorsDoNotTriggerForAliasTargetingFactoryBasedService() : void
155-
{
156-
$config = [
157-
'aliases' => [
158-
'alias' => 'foo-bar',
159-
],
160-
'factories' => [
161-
'foo-bar' => TestAsset\Factory::class,
162-
],
163-
'delegators' => [
164-
'alias' => [
165-
TestAsset\DelegatorFactory::class,
166-
],
167-
],
168-
];
169-
170-
$container = $this->createContainer($config);
171-
172-
self::assertTrue($container->has('alias'));
173-
$instance = $container->get('alias');
174-
self::assertInstanceOf(TestAsset\Service::class, $instance);
175-
self::assertNotInstanceOf(TestAsset\Delegator::class, $instance);
176-
177-
// Now ensure that the instance already retrieved by alias is the same
178-
// as that when fetched by the canonical service name.
179-
self::assertSame($instance, $container->get('foo-bar'));
180-
}
181-
182154
public function testDelegatorsDoNotTriggerForAliasTargetingInvokableService() : void
183155
{
184156
$config = [
@@ -207,38 +179,6 @@ public function testDelegatorsDoNotTriggerForAliasTargetingInvokableService() :
207179
self::assertSame($instance, $container->get(TestAsset\Service::class));
208180
}
209181

210-
public function testDelegatorsTriggerForFactoryServiceResolvedByAlias() : void
211-
{
212-
$config = [
213-
'aliases' => [
214-
'alias' => 'foo-bar',
215-
],
216-
'factories' => [
217-
'foo-bar' => TestAsset\Factory::class,
218-
],
219-
'delegators' => [
220-
'foo-bar' => [
221-
TestAsset\DelegatorFactory::class,
222-
],
223-
],
224-
];
225-
226-
$container = $this->createContainer($config);
227-
228-
self::assertTrue($container->has('alias'));
229-
$instance = $container->get('alias');
230-
self::assertInstanceOf(TestAsset\Delegator::class, $instance);
231-
self::assertInstanceOf(TestAsset\Service::class, ($instance->callback)());
232-
233-
// Now ensure that the instance already retrieved by alias is the same
234-
// as that when fetched by the canonical service name.
235-
self::assertSame($instance, $container->get('foo-bar'));
236-
237-
// Now ensure that subsequent retrievals by alias retrieve the same
238-
// instance.
239-
self::assertSame($instance, $container->get('alias'));
240-
}
241-
242182
public function delegatorService() : Generator
243183
{
244184
yield 'invokable' => [
@@ -257,14 +197,6 @@ public function delegatorService() : Generator
257197
TestAsset\Service::class,
258198
];
259199

260-
yield 'factory-service' => [
261-
[
262-
'factories' => ['foo-bar' => TestAsset\Factory::class],
263-
],
264-
'foo-bar',
265-
'foo-bar',
266-
];
267-
268200
yield 'alias-of-invokable' => [
269201
[
270202
'aliases' => ['foo-bar' => TestAsset\Service::class],
@@ -274,14 +206,19 @@ public function delegatorService() : Generator
274206
TestAsset\Service::class,
275207
];
276208

277-
yield 'alias-of-factory-service' => [
278-
[
279-
'aliases' => ['alias' => 'foo-bar'],
280-
'factories' => ['foo-bar' => TestAsset\Factory::class],
281-
],
282-
'alias',
283-
'foo-bar',
284-
];
209+
foreach ($this->factoriesForDelegators() as $name => $params) {
210+
yield 'factory-service-' . $name => [
211+
$params[0],
212+
'service',
213+
'service',
214+
];
215+
216+
yield 'alias-of-factory-service-' . $name => [
217+
$params[0] + ['aliases' => ['alias' => 'service']],
218+
'alias',
219+
'service',
220+
];
221+
}
285222
}
286223

287224
/**
@@ -456,6 +393,67 @@ public function testDelegatorFactoriesTriggerForFactoryBackedServicesUsingAnyFac
456393
// Retrieving a second time should retrieve the same instance.
457394
self::assertSame($instance, $container->get('service'));
458395
}
396+
397+
/**
398+
* @dataProvider factoriesForDelegators
399+
*/
400+
public function testDelegatorsTriggerForFactoryServiceResolvedByAlias(array $config) : void
401+
{
402+
$config += [
403+
'aliases' => [
404+
'alias' => 'service',
405+
],
406+
'delegators' => [
407+
'service' => [
408+
TestAsset\DelegatorFactory::class,
409+
],
410+
],
411+
];
412+
413+
$container = $this->createContainer($config);
414+
415+
self::assertTrue($container->has('alias'));
416+
$instance = $container->get('alias');
417+
self::assertInstanceOf(TestAsset\Delegator::class, $instance);
418+
self::assertInstanceOf(TestAsset\Service::class, ($instance->callback)());
419+
420+
// Now ensure that the instance already retrieved by alias is the same
421+
// as that when fetched by the canonical service name.
422+
self::assertSame($instance, $container->get('service'));
423+
424+
// Now ensure that subsequent retrievals by alias retrieve the same
425+
// instance.
426+
self::assertSame($instance, $container->get('alias'));
427+
}
428+
429+
/**
430+
* @dataProvider factoriesForDelegators
431+
*/
432+
public function testDelegatorsDoNotTriggerForAliasTargetingFactoryBasedServiceUsingAnyFactoryType(
433+
array $config
434+
) : void {
435+
$config += [
436+
'aliases' => [
437+
'alias' => 'service',
438+
],
439+
'delegators' => [
440+
'alias' => [
441+
TestAsset\DelegatorFactory::class,
442+
],
443+
],
444+
];
445+
446+
$container = $this->createContainer($config);
447+
448+
self::assertTrue($container->has('alias'));
449+
$instance = $container->get('alias');
450+
self::assertInstanceOf(TestAsset\Service::class, $instance);
451+
self::assertNotInstanceOf(TestAsset\Delegator::class, $instance);
452+
453+
// Now ensure that the instance already retrieved by alias is the same
454+
// as that when fetched by the canonical service name.
455+
self::assertSame($instance, $container->get('service'));
456+
}
459457

460458
// @codingStandardsIgnoreStart
461459
public function testWhenInvokableWithDelegatorsResolvesToNonExistentClassNoExceptionIsRaisedIfCallbackNeverInvoked()

0 commit comments

Comments
 (0)