Skip to content

Commit 348a6fa

Browse files
committed
InjectExtension::getInjectMethods() added test
1 parent 6248029 commit 348a6fa

File tree

2 files changed

+67
-4
lines changed

2 files changed

+67
-4
lines changed

src/DI/Extensions/InjectExtension.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ private function updateDefinition($def)
4040
$injects[] = new DI\Statement('$' . $property, ['@\\' . ltrim($type, '\\')]);
4141
}
4242

43-
foreach (self::getInjectMethods($def->getClass()) as $method) {
43+
foreach (array_reverse(self::getInjectMethods($def->getClass())) as $method) {
4444
$injects[] = new DI\Statement($method);
4545
}
4646

@@ -65,9 +65,9 @@ private function updateDefinition($def)
6565
*/
6666
public static function getInjectMethods($class)
6767
{
68-
return array_values(array_filter(get_class_methods($class), function ($name) {
68+
return array_reverse(array_values(array_filter(get_class_methods($class), function ($name) {
6969
return substr($name, 0, 6) === 'inject';
70-
}));
70+
})));
7171
}
7272

7373

@@ -103,7 +103,7 @@ public static function callInjects(DI\Container $container, $service)
103103
throw new Nette\InvalidArgumentException(sprintf('Service must be object, %s given.', gettype($service)));
104104
}
105105

106-
foreach (array_reverse(self::getInjectMethods($service)) as $method) {
106+
foreach (self::getInjectMethods($service) as $method) {
107107
$container->callMethod([$service, $method]);
108108
}
109109

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\DI\Extensions\InjectExtension::getInjectMethods()
5+
*/
6+
7+
use Nette\DI\Extensions\InjectExtension;
8+
use Tester\Assert;
9+
10+
require __DIR__ . '/../bootstrap.php';
11+
12+
13+
trait Trait1
14+
{
15+
function injectT1()
16+
{}
17+
}
18+
19+
trait Trait2
20+
{
21+
function injectT2()
22+
{}
23+
}
24+
25+
class Class1
26+
{
27+
function inject1()
28+
{}
29+
}
30+
31+
class Class2 extends Class1
32+
{
33+
use Trait1;
34+
35+
function inject2()
36+
{}
37+
}
38+
39+
class Class3 extends Class2
40+
{
41+
use Trait2;
42+
43+
function inject3()
44+
{}
45+
}
46+
47+
Assert::same([
48+
'inject1',
49+
], InjectExtension::getInjectMethods('Class1'));
50+
51+
Assert::same([
52+
'injectT1',
53+
'inject1',
54+
'inject2',
55+
], InjectExtension::getInjectMethods('Class2'));
56+
57+
Assert::same([
58+
'injectT2',
59+
'injectT1',
60+
'inject1',
61+
'inject2',
62+
'inject3',
63+
], InjectExtension::getInjectMethods('Class3'));

0 commit comments

Comments
 (0)