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,74 @@ 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 ());
44
49
}
45
50
46
- public function testBuiltinReturnType ()
51
+ /**
52
+ * @dataProvider returnTypesProvider
53
+ */
54
+ public function testReturnTypes ($ method , $ returnType , $ hhvmSupport = true )
47
55
{
56
+ if (!$ hhvmSupport && defined ('HHVM_VERSION ' )) {
57
+ $ this ->markTestSkipped ('Special types not supported by hhvm. ' );
58
+ }
59
+
48
60
$ container = new ContainerBuilder ();
49
61
50
- $ service = $ container ->register ('builtin ' );
51
- $ service ->setFactory (array (FactoryDummy::class, 'createBuiltin ' ));
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
+ return array (
78
+ array ('createBuiltin ' , null , false ),
79
+ array ('createParent ' , FactoryParent::class),
80
+ array ('createSelf ' , FactoryDummy::class),
81
+ );
82
+ }
83
+
84
+ public function testCircularReference ()
85
+ {
86
+ $ container = new ContainerBuilder ();
87
+
88
+ $ factory = $ container ->register ('factory ' );
89
+ $ factory ->setFactory (array (new Reference ('factory2 ' ), 'createSelf ' ));
90
+
91
+ $ factory2 = $ container ->register ('factory2 ' );
92
+ $ factory2 ->setFactory (array (new Reference ('factory ' ), 'create ' ));
52
93
53
94
$ pass = new FactoryReturnTypePass ();
54
95
$ pass ->process ($ container );
55
96
56
- $ this ->assertNull ($ service ->getClass ());
97
+ $ this ->assertNull ($ factory ->getClass ());
98
+ $ this ->assertNull ($ factory2 ->getClass ());
57
99
}
58
100
}
0 commit comments