Skip to content

Commit 92f0fdd

Browse files
committed
tests: improvements
1 parent 7960c8d commit 92f0fdd

File tree

4 files changed

+80
-18
lines changed

4 files changed

+80
-18
lines changed

tests/DI/Helpers.autowireArguments.errors.phpt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,8 @@ Assert::exception(function () use ($container) {
4242
Assert::exception(function () use ($container) {
4343
Helpers::autowireArguments(new ReflectionFunction(function ($x) {}), [], $container);
4444
}, Nette\DI\ServiceCreationException::class, 'Parameter $x in {closure}() has no class type hint or default value, so its value must be specified.');
45+
46+
47+
Assert::exception(function () use ($container) {
48+
Helpers::autowireArguments(new ReflectionFunction(function (int $x) {}), [], $container);
49+
}, Nette\DI\ServiceCreationException::class, 'Parameter $x in {closure}() has no class type hint or default value, so its value must be specified.');
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\DI\Config\Helpers::autowireArguments()
5+
*/
6+
7+
use Nette\DI\Helpers;
8+
use Tester\Assert;
9+
10+
11+
require __DIR__ . '/../bootstrap.php';
12+
13+
14+
class Container
15+
{
16+
function getByType($type)
17+
{
18+
return $type === 'Test' ? new Test : NULL;
19+
}
20+
}
21+
22+
class Test
23+
{
24+
function method(Test $class, Undefined $nullable1 = NULL, int $nullable2 = NULL)
25+
{}
26+
}
27+
28+
$container = new Container;
29+
30+
Assert::equal(
31+
[new Test],
32+
Helpers::autowireArguments(new ReflectionMethod('Test', 'method'), [], $container)
33+
);

tests/DI/PhpReflection.getReturnType.php7.phpt

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
/**
44
* Test: Nette\DI\PhpReflection::getReturnType
5-
* @phpversion >= 7
5+
* @phpversion 7
66
*/
77

88
namespace NS
@@ -14,22 +14,34 @@ namespace NS
1414
function noType()
1515
{}
1616

17+
function classType(): B
18+
{}
19+
20+
function nativeType(): string
21+
{}
22+
23+
function selfType(): self
24+
{}
25+
1726
/** @return B */
18-
function annotationSingle()
27+
function annotationClassType()
1928
{}
2029

2130
/** @return B|string */
22-
function annotationComplex()
31+
function annotationUnionType()
2332
{}
2433

25-
function nativeType(): string
34+
/** @return String */
35+
function annotationNativeType()
2636
{}
2737

28-
function selfType(): self
38+
/** @return self */
39+
function annotationSelfType()
2940
{}
3041
}
3142

32-
function classType(): B
43+
/** @return B */
44+
function annotationClassType()
3345
{}
3446
}
3547

@@ -44,14 +56,20 @@ namespace
4456

4557
Assert::null(PhpReflection::getReturnType(new \ReflectionMethod(NS\A::class, 'noType')));
4658

47-
Assert::same('Test\B', PhpReflection::getReturnType(new \ReflectionMethod(NS\A::class, 'annotationSingle')));
48-
49-
Assert::same('Test\B', PhpReflection::getReturnType(new \ReflectionMethod(NS\A::class, 'annotationComplex')));
59+
Assert::same('Test\B', PhpReflection::getReturnType(new \ReflectionMethod(NS\A::class, 'classType')));
5060

5161
Assert::same('string', PhpReflection::getReturnType(new \ReflectionMethod(NS\A::class, 'nativeType')));
5262

5363
Assert::same('NS\A', PhpReflection::getReturnType(new \ReflectionMethod(NS\A::class, 'selfType')));
5464

65+
Assert::same('Test\B', PhpReflection::getReturnType(new \ReflectionMethod(NS\A::class, 'annotationClassType')));
66+
67+
Assert::same('Test\B', PhpReflection::getReturnType(new \ReflectionMethod(NS\A::class, 'annotationUnionType')));
68+
69+
Assert::same('string', PhpReflection::getReturnType(new \ReflectionMethod(NS\A::class, 'annotationNativeType')));
70+
71+
Assert::same('NS\A', PhpReflection::getReturnType(new \ReflectionMethod(NS\A::class, 'annotationSelfType')));
72+
5573
// class name expanding is NOT supported for global functions
56-
Assert::same('Test\B', PhpReflection::getReturnType(new \ReflectionFunction(NS\classType::class)));
74+
Assert::same('B', PhpReflection::getReturnType(new \ReflectionFunction('NS\annotationClassType')));
5775
}

tests/DI/PhpReflection.getReturnType.phpt

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,24 @@ namespace NS
1414
{}
1515

1616
/** @return B */
17-
function annotationSingle()
17+
function annotationClassType()
1818
{}
1919

2020
/** @return B|string */
21-
function annotationComplex()
21+
function annotationUnionType()
2222
{}
2323

2424
/** @return String */
25-
function nativeType()
25+
function annotationNativeType()
26+
{}
27+
28+
/** @return self */
29+
function annotationSelfType()
2630
{}
2731
}
2832

2933
/** @return B */
30-
function classType()
34+
function annotationClassType()
3135
{}
3236
}
3337

@@ -42,12 +46,14 @@ namespace
4246

4347
Assert::null(PhpReflection::getReturnType(new \ReflectionMethod(NS\A::class, 'noType')));
4448

45-
Assert::same('Test\B', PhpReflection::getReturnType(new \ReflectionMethod(NS\A::class, 'annotationSingle')));
49+
Assert::same('Test\B', PhpReflection::getReturnType(new \ReflectionMethod(NS\A::class, 'annotationClassType')));
50+
51+
Assert::same('Test\B', PhpReflection::getReturnType(new \ReflectionMethod(NS\A::class, 'annotationUnionType')));
4652

47-
Assert::same('Test\B', PhpReflection::getReturnType(new \ReflectionMethod(NS\A::class, 'annotationComplex')));
53+
Assert::same('string', PhpReflection::getReturnType(new \ReflectionMethod(NS\A::class, 'annotationNativeType')));
4854

49-
Assert::same('string', PhpReflection::getReturnType(new \ReflectionMethod(NS\A::class, 'nativeType')));
55+
Assert::same('NS\A', PhpReflection::getReturnType(new \ReflectionMethod(NS\A::class, 'annotationSelfType')));
5056

5157
// class name expanding is NOT supported for global functions
52-
Assert::same('B', PhpReflection::getReturnType(new \ReflectionFunction(NS\classType::class)));
58+
Assert::same('B', PhpReflection::getReturnType(new \ReflectionFunction('NS\annotationClassType')));
5359
}

0 commit comments

Comments
 (0)