/
Compiler.generatedLocatorMulti.phpt
64 lines (44 loc) 路 1.2 KB
/
Compiler.generatedLocatorMulti.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
<?php
/**
* Test: Nette\DI\Compiler: generated services locators.
*/
declare(strict_types=1);
use Nette\DI;
use Tester\Assert;
require __DIR__ . '/../bootstrap.php';
class Lorem
{
}
interface Locator
{
public function getA(): Lorem;
public function getB(): ?Lorem;
public function createC(): Lorem;
public function createD(): ?Lorem;
}
$container = createContainer(new DI\Compiler, '
services:
lorem1: Lorem
one: Locator(a: Lorem, b: Lorem, c: Lorem, d: Lorem)
two: Locator(tagged: a)
');
// accessor
$one = $container->getService('one');
Assert::type(Lorem::class, $one->getA());
Assert::type(Lorem::class, $one->getB());
Assert::same($one->getA(), $one->getB());
// factory
Assert::type(Lorem::class, $one->createC());
Assert::type(Lorem::class, $one->createD());
Assert::notSame($one->createC(), $one->createD());
// nullable
$two = $container->getService('two');
Assert::null($two->getB());
Assert::null($two->createD());
// undefined
Assert::exception(function () use ($two) {
$two->getA();
}, Nette\DI\MissingServiceException::class, 'Service is not defined.');
Assert::exception(function () use ($two) {
$two->createC();
}, Nette\DI\MissingServiceException::class, 'Service is not defined.');