-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
Container.getByType.phpt
68 lines (46 loc) 路 1.73 KB
/
Container.getByType.phpt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
/**
* Test: Nette\DI\Container::getByType() and findByType()
*/
declare(strict_types=1);
use Nette\DI;
use Tester\Assert;
require __DIR__ . '/../bootstrap.php';
class Service extends stdClass
{
}
class Child extends Service
{
}
class Service2 extends stdClass
{
}
$builder = new DI\ContainerBuilder;
$one = $builder->addDefinition('one')
->setType('Service');
$child = $builder->addDefinition('child')
->setType('Child')
->setAutowired(false);
$two = $builder->addDefinition('two')
->setType('Service2');
$three = $builder->addDefinition('three')
->setType('Service2')
->setAutowired(false);
$container = createContainer($builder);
Assert::type(Service::class, $container->getByType('Service'));
Assert::null($container->getByType('Child', false));
Assert::type(Service2::class, $container->getByType('Service2'));
Assert::exception(function () use ($container) {
$container->getByType(stdClass::class);
}, Nette\DI\MissingServiceException::class, 'Multiple services of type stdClass found: one, two.');
Assert::null($container->getByType('unknown', false));
Assert::exception(function () use ($container) {
$container->getByType('unknown');
}, Nette\DI\MissingServiceException::class, "Service of type 'unknown' not found. Check the class name because it cannot be found.");
Assert::exception(function () use ($container) {
$container->getByType('Exception');
}, Nette\DI\MissingServiceException::class, 'Service of type Exception not found. Did you add it to configuration file?');
Assert::same(['one', 'child'], $container->findByType('Service'));
Assert::same(['child'], $container->findByType('Child'));
Assert::same(['two', 'three'], $container->findByType('Service2'));
Assert::same([], $container->findByType('unknown'));