1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/nnx-framework/container |
4
|
|
|
* @author Malofeykin Andrey <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
namespace Nnx\Container\PhpUnit\Test; |
7
|
|
|
|
8
|
|
|
use Nnx\Container\PhpUnit\TestData\TestPaths; |
9
|
|
|
use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase; |
10
|
|
|
use Nnx\Container\ContainerInterface; |
11
|
|
|
use Nnx\Container\PhpUnit\TestData\ContextResolver\Custom\Service as ServiceApp; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class ContainerTest |
15
|
|
|
* |
16
|
|
|
* @package Nnx\Container\PhpUnit\Test |
17
|
|
|
*/ |
18
|
|
|
class ContainerTest extends AbstractHttpControllerTestCase |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Тестирование получения службы по контексту |
23
|
|
|
* |
24
|
|
|
* @return array |
25
|
|
|
*/ |
26
|
|
|
public function dataTestGetEntryByContext() |
27
|
|
|
{ |
28
|
|
|
return [ |
29
|
|
|
['testResolveServiceByConfig', ServiceApp\Module3\Module::class, ServiceApp\Module3\ResolvedServiceByConfigFromModule3Context::class], |
30
|
|
|
['testResolveServiceByConfig', ServiceApp\Module2\Module::class, ServiceApp\Module2\ResolvedServiceByConfigFromModule2Context::class], |
31
|
|
|
[ServiceApp\Module3\Service\ComponentInterface::class, ServiceApp\Module3\Module::class, ServiceApp\Module3\ResolvedServiceByConfigFromModule3Context::class], |
32
|
|
|
[ServiceApp\Module3\Service\ComponentInterface::class, ServiceApp\Module2\Module::class, ServiceApp\Module2\Service\Component::class] |
33
|
|
|
]; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Проверка ситуации когда пытаемся получить сервис, который: |
39
|
|
|
* 1) Не может быть разрезолвен в другой, в зависимости от контекста |
40
|
|
|
* 2) Не зарегестрирован в ServiceLocator приложения |
41
|
|
|
* |
42
|
|
|
* |
43
|
|
|
* @expectedException \Zend\ServiceManager\Exception\ServiceNotFoundException |
44
|
|
|
* @expectedExceptionMessage Nnx\Container\Container::get was unable to fetch or create an instance for |
45
|
|
|
* |
46
|
|
|
* |
47
|
|
|
* @return void |
48
|
|
|
* |
49
|
|
|
* @throws \Zend\ServiceManager\Exception\ServiceNotFoundException |
50
|
|
|
* @throws \Zend\Stdlib\Exception\LogicException |
51
|
|
|
*/ |
52
|
|
|
public function testLoadModule() |
53
|
|
|
{ |
54
|
|
|
/** @noinspection PhpIncludeInspection */ |
55
|
|
|
$this->setApplicationConfig( |
56
|
|
|
include TestPaths::getPathToContextResolverAppConfig() |
57
|
|
|
); |
58
|
|
|
|
59
|
|
|
/** @var ContainerInterface $container */ |
60
|
|
|
$container = $this->getApplicationServiceLocator()->get(ContainerInterface::class); |
61
|
|
|
|
62
|
|
|
$entryName = 'UnknownService'; |
63
|
|
|
$container->get($entryName); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Тестирование получени службы, в зависимости от контекста |
69
|
|
|
* |
70
|
|
|
* @dataProvider dataTestGetEntryByContext |
71
|
|
|
* |
72
|
|
|
* @param $entryName |
73
|
|
|
* @param $context |
74
|
|
|
* @param $expectedEntryClassName |
75
|
|
|
* |
76
|
|
|
* @throws \Zend\Stdlib\Exception\LogicException |
77
|
|
|
* @throws \Zend\ServiceManager\Exception\ServiceNotFoundException |
78
|
|
|
*/ |
79
|
|
|
public function testGetEntryByContext($entryName, $context, $expectedEntryClassName) |
80
|
|
|
{ |
81
|
|
|
/** @noinspection PhpIncludeInspection */ |
82
|
|
|
$this->setApplicationConfig( |
83
|
|
|
include TestPaths::getPathToContextResolverAppConfig() |
84
|
|
|
); |
85
|
|
|
|
86
|
|
|
/** @var ContainerInterface $container */ |
87
|
|
|
$container = $this->getApplicationServiceLocator()->get(ContainerInterface::class); |
88
|
|
|
|
89
|
|
|
static::assertInstanceOf($expectedEntryClassName, $container->get($entryName, [], null, $context)); |
|
|
|
|
90
|
|
|
static::assertInstanceOf($expectedEntryClassName, $container->getByContext($entryName, [], $context)); |
91
|
|
|
static::assertTrue($container->has($entryName, true, true, $context)); |
92
|
|
|
static::assertTrue($container->hasByContext($entryName, $context)); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: