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

Commit b275673

Browse files
committed
Added delegators configuration tests
1 parent 2915d9b commit b275673

File tree

5 files changed

+238
-0
lines changed

5 files changed

+238
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Then, depends what functionality you'd like to support, you can add the
3838
following traits into your test case:
3939

4040
- `Zend\ContainerTest\AliasTestTrait` - to support `aliases` configuration,
41+
- `Zend\ContainerTest\DelegatorTestTrait` - to support `delegators` configuration,
4142
- `Zend\ContainerTest\FactoryTestTrait` - to support `factories` configuration,
4243
- `Zend\ContainerTest\InvokableTestTrait` - to support `invokables` configuration,
4344
- `Zend\ContainerTest\ServiceTestTrait` - to support `services` configuration,

src/AllTestTrait.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
trait AllTestTrait
1313
{
1414
use AliasTestTrait;
15+
use DelegatorTestTrait;
1516
use FactoryTestTrait;
1617
use InvokableTestTrait;
1718
use ServiceTestTrait;

src/DelegatorTestTrait.php

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
<?php
2+
/**
3+
* @see https://github.com/zendframework/zend-container-test for the canonical source repository
4+
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (https://www.zend.com)
5+
* @license https://github.com/zendframework/zend-container-test/blob/master/LICENSE.md New BSD License
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Zend\ContainerTest;
11+
12+
trait DelegatorTestTrait
13+
{
14+
public function testDelegatorForInvokable()
15+
{
16+
$config = [
17+
'invokables' => [
18+
'foo-bar' => TestAsset\Service::class,
19+
],
20+
'delegators' => [
21+
'foo-bar' => [
22+
TestAsset\DelegatorFactory::class,
23+
],
24+
],
25+
];
26+
27+
$container = $this->createContainer($config);
28+
29+
self::assertTrue($container->has('foo-bar'));
30+
$delegator = $container->get('foo-bar');
31+
self::assertInstanceOf(TestAsset\Delegator::class, $delegator);
32+
self::assertInstanceOf(TestAsset\Service::class, ($delegator->callback)());
33+
}
34+
35+
public function testDelegatorForService()
36+
{
37+
$myService = new TestAsset\Service();
38+
$config = [
39+
'services' => [
40+
'foo-bar' => $myService,
41+
],
42+
'delegators' => [
43+
'foo-bar' => [
44+
TestAsset\DelegatorFactory::class,
45+
],
46+
],
47+
];
48+
49+
$container = $this->createContainer($config);
50+
51+
self::assertTrue($container->has('foo-bar'));
52+
$delegator = $container->get('foo-bar');
53+
self::assertInstanceOf(TestAsset\Delegator::class, $delegator);
54+
self::assertSame($myService, ($delegator->callback)());
55+
}
56+
57+
public function testDelegatorForFactory()
58+
{
59+
$config = [
60+
'factories' => [
61+
'foo-bar' => TestAsset\Factory::class,
62+
],
63+
'delegators' => [
64+
'foo-bar' => [
65+
TestAsset\DelegatorFactory::class,
66+
],
67+
],
68+
];
69+
70+
$container = $this->createContainer($config);
71+
72+
self::assertTrue($container->has('foo-bar'));
73+
$delegator = $container->get('foo-bar');
74+
self::assertInstanceOf(TestAsset\Delegator::class, $delegator);
75+
self::assertInstanceOf(TestAsset\Service::class, ($delegator->callback)());
76+
}
77+
78+
public function testDelegatorForAliasInvokable()
79+
{
80+
$config = [
81+
'aliases' => [
82+
'alias' => 'foo-bar',
83+
],
84+
'invokables' => [
85+
'foo-bar' => TestAsset\Service::class,
86+
],
87+
'delegators' => [
88+
'alias' => [
89+
TestAsset\DelegatorFactory::class,
90+
],
91+
],
92+
];
93+
94+
$container = $this->createContainer($config);
95+
96+
self::assertTrue($container->has('foo-bar'));
97+
$delegator = $container->get('foo-bar');
98+
self::assertInstanceOf(TestAsset\Delegator::class, $delegator);
99+
self::assertInstanceOf(TestAsset\Service::class, ($delegator->callback)());
100+
}
101+
102+
public function testDelegatorForAliasService()
103+
{
104+
$myService = new TestAsset\Service();
105+
$config = [
106+
'aliases' => [
107+
'alias' => 'foo-bar',
108+
],
109+
'services' => [
110+
'foo-bar' => $myService,
111+
],
112+
'delegators' => [
113+
'alias' => [
114+
TestAsset\DelegatorFactory::class,
115+
],
116+
],
117+
];
118+
119+
$container = $this->createContainer($config);
120+
121+
self::assertTrue($container->has('foo-bar'));
122+
$delegator = $container->get('foo-bar');
123+
self::assertInstanceOf(TestAsset\Delegator::class, $delegator);
124+
self::assertSame($myService, ($delegator->callback)());
125+
}
126+
127+
public function testDelegatorForAliasFactory()
128+
{
129+
$config = [
130+
'aliases' => [
131+
'alias' => 'foo-bar',
132+
],
133+
'factories' => [
134+
'foo-bar' => TestAsset\Factory::class,
135+
],
136+
'delegators' => [
137+
'alias' => [
138+
TestAsset\DelegatorFactory::class,
139+
],
140+
],
141+
];
142+
143+
$container = $this->createContainer($config);
144+
145+
self::assertTrue($container->has('foo-bar'));
146+
$delegator = $container->get('foo-bar');
147+
self::assertInstanceOf(TestAsset\Delegator::class, $delegator);
148+
self::assertInstanceOf(TestAsset\Service::class, ($delegator->callback)());
149+
}
150+
151+
public function delegatorService()
152+
{
153+
yield 'invokable' => [
154+
[
155+
'invokables' => ['foo-bar' => TestAsset\Service::class],
156+
],
157+
];
158+
159+
yield 'service' => [
160+
[
161+
'services' => ['foo-bar' => new TestAsset\Service()],
162+
],
163+
];
164+
165+
yield 'factory' => [
166+
[
167+
'factories' => ['foo-bar' => TestAsset\Factory::class],
168+
],
169+
];
170+
}
171+
172+
/**
173+
* @dataProvider delegatorService
174+
*/
175+
public function testDelegatorMultipleDelegators(array $config)
176+
{
177+
$config += [
178+
'delegators' => [
179+
'foo-bar' => [
180+
TestAsset\Delegator1Factory::class,
181+
TestAsset\Delegator2Factory::class,
182+
],
183+
],
184+
];
185+
186+
$container = $this->createContainer($config);
187+
188+
self::assertTrue($container->has('foo-bar'));
189+
$service = $container->get('foo-bar');
190+
self::assertInstanceOf(TestAsset\Service::class, $service);
191+
self::assertEquals(
192+
[
193+
TestAsset\Delegator1Factory::class,
194+
TestAsset\Delegator2Factory::class,
195+
],
196+
$service->injected
197+
);
198+
}
199+
}

src/TestAsset/Delegator1Factory.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/**
3+
* @see https://github.com/zendframework/zend-container-test for the canonical source repository
4+
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (https://www.zend.com)
5+
* @license https://github.com/zendframework/zend-container-test/blob/master/LICENSE.md New BSD License
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Zend\ContainerTest\TestAsset;
11+
12+
use Psr\Container\ContainerInterface;
13+
14+
class Delegator1Factory
15+
{
16+
public function __invoke(ContainerInterface $container, $name, callable $callback)
17+
{
18+
$service = $callback();
19+
$service->inject(static::class);
20+
21+
return $service;
22+
}
23+
}

src/TestAsset/Delegator2Factory.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
/**
3+
* @see https://github.com/zendframework/zend-container-test for the canonical source repository
4+
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (https://www.zend.com)
5+
* @license https://github.com/zendframework/zend-container-test/blob/master/LICENSE.md New BSD License
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Zend\ContainerTest\TestAsset;
11+
12+
class Delegator2Factory extends Delegator1Factory
13+
{
14+
}

0 commit comments

Comments
 (0)