Skip to content

Commit 4799ee6

Browse files
committed
tests: improved
1 parent 8257f49 commit 4799ee6

File tree

4 files changed

+234
-1
lines changed

4 files changed

+234
-1
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/**
4+
* @phpVersion 8.0
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
use Nette\DI;
10+
use Nette\DI\Definitions\Statement;
11+
use Tester\Assert;
12+
13+
14+
class Factory
15+
{
16+
public function createUnion(): stdClass|array
17+
{
18+
return [];
19+
}
20+
}
21+
22+
23+
require __DIR__ . '/../bootstrap.php';
24+
25+
26+
Assert::exception(function () {
27+
$builder = new DI\ContainerBuilder;
28+
$builder->addDefinition('a')
29+
->setFactory([new Statement([Factory::class, 'createUnion']), 'next']);
30+
$container = createContainer($builder);
31+
}, Nette\DI\ServiceCreationException::class, "Service 'a': Return type of Factory::createUnion() is expected to not be nullable/built-in/complex, 'stdClass|array' given.");
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
<?php
2+
3+
/**
4+
* Test of chained resolving
5+
*
6+
* services:
7+
* - Factory::createClass()::next()
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
use Nette\DI;
13+
use Nette\DI\Definitions\Statement;
14+
use Tester\Assert;
15+
16+
17+
class Lorem
18+
{
19+
public function next(): stdClass
20+
{
21+
}
22+
}
23+
24+
25+
class Factory
26+
{
27+
/** @return Lorem */
28+
public function createClassPhpDoc()
29+
{
30+
return [];
31+
}
32+
33+
34+
public function createClass(): Lorem
35+
{
36+
return [];
37+
}
38+
39+
40+
/** @return Lorem|null */
41+
public function createNullableClassPhpDoc()
42+
{
43+
return [];
44+
}
45+
46+
47+
public function createNullableClass(): ?Lorem
48+
{
49+
return [];
50+
}
51+
52+
53+
/** @return array */
54+
public function createScalarPhpDoc()
55+
{
56+
return [];
57+
}
58+
59+
60+
public function createScalar(): array
61+
{
62+
return [];
63+
}
64+
65+
66+
/** @return object */
67+
public function createObjectPhpDoc()
68+
{
69+
return (object) null;
70+
}
71+
72+
73+
public function createObject(): object
74+
{
75+
return (object) null;
76+
}
77+
78+
79+
public function createObjectNullable(): ?object
80+
{
81+
return (object) null;
82+
}
83+
84+
85+
/** @return mixed */
86+
public function createMixedPhpDoc()
87+
{
88+
return (object) null;
89+
}
90+
91+
92+
public function createMixed(): mixed
93+
{
94+
return (object) null;
95+
}
96+
97+
98+
/**
99+
* @template T
100+
* @return T
101+
*/
102+
public function createGeneric()
103+
{
104+
return (object) null;
105+
}
106+
}
107+
108+
109+
require __DIR__ . '/../bootstrap.php';
110+
111+
112+
Assert::noError(function () {
113+
$builder = new DI\ContainerBuilder;
114+
$builder->addDefinition('a')
115+
->setFactory([new Statement([Factory::class, 'createClassPhpDoc']), 'next']);
116+
$container = @createContainer($builder); // @return is deprecated
117+
});
118+
119+
Assert::noError(function () {
120+
$builder = new DI\ContainerBuilder;
121+
$builder->addDefinition('a')
122+
->setFactory([new Statement([Factory::class, 'createClass']), 'next']);
123+
$container = createContainer($builder);
124+
});
125+
126+
Assert::noError(function () {
127+
$builder = new DI\ContainerBuilder;
128+
$builder->addDefinition('a')
129+
->setFactory([new Statement([Factory::class, 'createNullableClassPhpDoc']), 'next']);
130+
$container = @createContainer($builder); // @return is deprecated
131+
});
132+
133+
Assert::exception(function () {
134+
$builder = new DI\ContainerBuilder;
135+
$builder->addDefinition('a')
136+
->setFactory([new Statement([Factory::class, 'createNullableClass']), 'next']);
137+
$container = createContainer($builder);
138+
}, Nette\DI\ServiceCreationException::class, "Service 'a': Return type of Factory::createNullableClass() is expected to not be nullable/built-in/complex, '?Lorem' given.");
139+
140+
Assert::exception(function () {
141+
$builder = new DI\ContainerBuilder;
142+
$builder->addDefinition('a')
143+
->setFactory([new Statement([Factory::class, 'createScalarPhpDoc']), 'next']);
144+
$container = @createContainer($builder); // @return is deprecated
145+
}, Nette\DI\ServiceCreationException::class, "Service 'a': Return type of Factory::createScalarPhpDoc() is expected to not be nullable/built-in/complex, 'array' given.");
146+
147+
Assert::exception(function () {
148+
$builder = new DI\ContainerBuilder;
149+
$builder->addDefinition('a')
150+
->setFactory([new Statement([Factory::class, 'createScalar']), 'next']);
151+
$container = createContainer($builder);
152+
}, Nette\DI\ServiceCreationException::class, "Service 'a': Return type of Factory::createScalar() is expected to not be nullable/built-in/complex, 'array' given.");
153+
154+
Assert::exception(function () {
155+
$builder = new DI\ContainerBuilder;
156+
$builder->addDefinition('a')
157+
->setFactory([new Statement([Factory::class, 'createObjectPhpDoc']), 'next']);
158+
$container = createContainer($builder);
159+
}, Nette\DI\ServiceCreationException::class, "Service 'a': Unknown service type, specify it or declare return type of factory method.");
160+
161+
Assert::exception(function () {
162+
$builder = new DI\ContainerBuilder;
163+
$builder->addDefinition('a')
164+
->setFactory([new Statement([Factory::class, 'createObject']), 'next']);
165+
$container = createContainer($builder);
166+
}, Nette\DI\ServiceCreationException::class, "Service 'a': Unknown service type, specify it or declare return type of factory method.");
167+
168+
Assert::exception(function () {
169+
$builder = new DI\ContainerBuilder;
170+
$builder->addDefinition('a')
171+
->setFactory([new Statement([Factory::class, 'createObjectNullable']), 'next']);
172+
$container = createContainer($builder);
173+
}, Nette\DI\ServiceCreationException::class, "Service 'a': Unknown service type, specify it or declare return type of factory method.");
174+
175+
Assert::exception(function () {
176+
$builder = new DI\ContainerBuilder;
177+
$builder->addDefinition('a')
178+
->setFactory([new Statement([Factory::class, 'createMixedPhpDoc']), 'next']);
179+
$container = createContainer($builder);
180+
}, Nette\DI\ServiceCreationException::class, "Service 'a': Unknown service type, specify it or declare return type of factory method.");
181+
182+
Assert::exception(function () {
183+
$builder = new DI\ContainerBuilder;
184+
$builder->addDefinition('a')
185+
->setFactory([new Statement([Factory::class, 'createMixed']), 'next']);
186+
$container = createContainer($builder);
187+
}, Nette\DI\ServiceCreationException::class, "Service 'a': Unknown service type, specify it or declare return type of factory method.");
188+
189+
Assert::exception(function () {
190+
$builder = new DI\ContainerBuilder;
191+
$builder->addDefinition('a')
192+
->setFactory([new Statement([Factory::class, 'createGeneric']), 'next']);
193+
$container = @createContainer($builder); // @return is deprecated
194+
}, Nette\DI\ServiceCreationException::class, "Service 'a': Class 'T' not found.
195+
Check the return type of Factory::createGeneric().");

tests/DI/ContainerBuilder.resolveTypes.phpt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
<?php
22

3+
/**
4+
* Test of simple resolving
5+
*
6+
* services:
7+
* - Factory::createClass()
8+
*/
9+
310
declare(strict_types=1);
411

512
use Nette\DI;

tests/SearchExtension/batches.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ search:
2222
2323
third:
2424
in: fixtures
25-
files: foo*
25+
files: Foo*
2626
tags:
2727
- foo
2828
');

0 commit comments

Comments
 (0)