15
15
use Symfony \Component \DependencyInjection \ContainerBuilder ;
16
16
use Symfony \Component \DependencyInjection \Reference ;
17
17
use Symfony \Component \DependencyInjection \Tests \Fixtures \FactoryDummy ;
18
+ use Symfony \Component \DependencyInjection \Tests \Fixtures \FactoryParent ;
18
19
19
20
/**
20
21
* @author Guilhem N. <egetick@gmail.com>
@@ -26,33 +27,75 @@ public function testProcess()
26
27
$ container = new ContainerBuilder ();
27
28
28
29
$ factory = $ container ->register ('factory ' );
29
- $ factory ->setFactory (array (FactoryDummy::class, 'createSelf ' ));
30
+ $ factory ->setFactory (array (FactoryDummy::class, 'createFactory ' ));
30
31
31
32
$ foo = $ container ->register ('foo ' );
32
33
$ foo ->setFactory (array (new Reference ('factory ' ), 'create ' ));
33
34
35
+ $ bar = $ container ->register ('bar ' , __CLASS__ );
36
+ $ bar ->setFactory (array (new Reference ('factory ' ), 'create ' ));
37
+
34
38
$ pass = new FactoryReturnTypePass ();
35
39
$ pass ->process ($ container );
36
40
37
- if (PHP_VERSION_ID >= 70000 ) {
41
+ if (method_exists (\ReflectionMethod::class, ' getReturnType ' ) ) {
38
42
$ this ->assertEquals (FactoryDummy::class, $ factory ->getClass ());
39
43
$ this ->assertEquals (\stdClass::class, $ foo ->getClass ());
40
44
} else {
41
45
$ this ->assertNull ($ factory ->getClass ());
42
46
$ this ->assertNull ($ foo ->getClass ());
43
47
}
48
+ $ this ->assertEquals (__CLASS__ , $ bar ->getClass ());
49
+ }
50
+
51
+ /**
52
+ * @dataProvider returnTypesProvider
53
+ */
54
+ public function testReturnTypes ($ method , $ returnType , $ hhvmSupport = true )
55
+ {
56
+ if (!$ hhvmSupport && defined ('HHVM_VERSION ' )) {
57
+ $ this ->markTestSkipped ('Special types not supported by hhvm. ' );
58
+ }
59
+
60
+ $ container = new ContainerBuilder ();
61
+
62
+ $ service = $ container ->register ('service ' );
63
+ $ service ->setFactory (array (FactoryDummy::class, $ method ));
64
+
65
+ $ pass = new FactoryReturnTypePass ();
66
+ $ pass ->process ($ container );
67
+
68
+ if (method_exists (\ReflectionMethod::class, 'getReturnType ' )) {
69
+ $ this ->assertEquals ($ returnType , $ service ->getClass ());
70
+ } else {
71
+ $ this ->assertNull ($ service ->getClass ());
72
+ }
73
+ }
74
+
75
+ public function returnTypesProvider ()
76
+ {
77
+
78
+ return array (
79
+ array ('createBuiltin ' , null , false ),
80
+ array ('createParent ' , FactoryParent::class),
81
+ array ('createSelf ' , FactoryDummy::class),
82
+ );
44
83
}
45
84
46
- public function testBuiltinReturnType ()
85
+ public function testCircularReference ()
47
86
{
48
87
$ container = new ContainerBuilder ();
49
88
50
- $ service = $ container ->register ('builtin ' );
51
- $ service ->setFactory (array (FactoryDummy::class, 'createBuiltin ' ));
89
+ $ factory = $ container ->register ('factory ' );
90
+ $ factory ->setFactory (array (new Reference ('factory2 ' ), 'createSelf ' ));
91
+
92
+ $ factory2 = $ container ->register ('factory2 ' );
93
+ $ factory2 ->setFactory (array (new Reference ('factory ' ), 'create ' ));
52
94
53
95
$ pass = new FactoryReturnTypePass ();
54
96
$ pass ->process ($ container );
55
97
56
- $ this ->assertNull ($ service ->getClass ());
98
+ $ this ->assertNull ($ factory ->getClass ());
99
+ $ this ->assertNull ($ factory2 ->getClass ());
57
100
}
58
101
}
0 commit comments