|
6 | 6 | use Matthias\SymfonyServiceDefinitionValidator\ResultingClassResolver;
|
7 | 7 | use Symfony\Component\DependencyInjection\ContainerBuilder;
|
8 | 8 | use Symfony\Component\DependencyInjection\Definition;
|
| 9 | +use Symfony\Component\DependencyInjection\Reference; |
9 | 10 |
|
10 | 11 | class ConstructorResolverTest extends \PHPUnit_Framework_TestCase
|
11 | 12 | {
|
@@ -55,55 +56,172 @@ public function ifConstructorIsNotPublicItFails()
|
55 | 56 |
|
56 | 57 | /**
|
57 | 58 | * @test
|
| 59 | + * @dataProvider getFactoryServiceAndFactoryMethodAreDefinedData |
58 | 60 | */
|
59 |
| - public function ifFactoryClassAndFactoryMethodAreDefinedResolvedConstructorIsFactoryMethod() |
| 61 | + public function ifFactoryServiceAndFactoryMethodAreDefinedResolvedConstructorIsFactoryMethod($factoryClass, Definition $definition, \ReflectionMethod $expectedConstructor) |
60 | 62 | {
|
61 | 63 | $containerBuilder = new ContainerBuilder();
|
| 64 | + $containerBuilder->register('factory', $factoryClass); |
62 | 65 | $resolver = new ConstructorResolver($containerBuilder, new ResultingClassResolver($containerBuilder));
|
63 | 66 |
|
64 |
| - $definition = new Definition(); |
| 67 | + $this->assertEquals($expectedConstructor, $resolver->resolve($definition)); |
| 68 | + } |
| 69 | + |
| 70 | + public function getFactoryServiceAndFactoryMethodAreDefinedData() |
| 71 | + { |
| 72 | + $factoryService = 'factory'; |
65 | 73 | $factoryClass = 'Matthias\SymfonyServiceDefinitionValidator\Tests\Fixtures\FactoryClass';
|
66 |
| - $definition->setFactoryClass($factoryClass); |
67 | 74 | $factoryMethod = 'create';
|
| 75 | + $expectedConstructor = new \ReflectionMethod($factoryClass, $factoryMethod); |
| 76 | + |
| 77 | + $definition = new Definition(); |
| 78 | + $definition->setFactoryService($factoryService); |
68 | 79 | $definition->setFactoryMethod($factoryMethod);
|
69 | 80 |
|
70 |
| - $expectedConstructor = new \ReflectionMethod($factoryClass, $factoryMethod); |
71 |
| - $this->assertEquals($expectedConstructor, $resolver->resolve($definition)); |
| 81 | + $data = array(array($factoryClass, $definition, $expectedConstructor)); |
| 82 | + |
| 83 | + if (method_exists($definition, 'setFactory')) { |
| 84 | + $definition = new Definition(); |
| 85 | + $definition->setFactory(array(new Reference($factoryService), $factoryMethod)); |
| 86 | + $data[] = array($factoryClass, $definition, $expectedConstructor); |
| 87 | + } |
| 88 | + |
| 89 | + return $data; |
72 | 90 | }
|
73 | 91 |
|
74 | 92 | /**
|
75 | 93 | * @test
|
| 94 | + * @dataProvider getFactoryClassAndFactoryMethodAreDefinedData |
76 | 95 | */
|
77 |
| - public function ifFactoryClassDoesNotExistFails() |
| 96 | + public function ifFactoryClassAndFactoryMethodAreDefinedResolvedConstructorIsFactoryMethod(Definition $definition, \ReflectionMethod $expectedConstructor) |
78 | 97 | {
|
79 | 98 | $containerBuilder = new ContainerBuilder();
|
80 | 99 | $resolver = new ConstructorResolver($containerBuilder, new ResultingClassResolver($containerBuilder));
|
81 | 100 |
|
| 101 | + $this->assertEquals($expectedConstructor, $resolver->resolve($definition)); |
| 102 | + } |
| 103 | + |
| 104 | + public function getFactoryClassAndFactoryMethodAreDefinedData() |
| 105 | + { |
| 106 | + $factoryClass = 'Matthias\SymfonyServiceDefinitionValidator\Tests\Fixtures\FactoryClass'; |
| 107 | + $factoryMethod = 'create'; |
| 108 | + $expectedConstructor = new \ReflectionMethod($factoryClass, $factoryMethod); |
| 109 | + |
82 | 110 | $definition = new Definition();
|
83 |
| - $factoryClass = 'NonExistingClass'; |
84 | 111 | $definition->setFactoryClass($factoryClass);
|
85 |
| - $factoryMethod = 'create'; |
86 | 112 | $definition->setFactoryMethod($factoryMethod);
|
87 | 113 |
|
| 114 | + $data = array(array($definition, $expectedConstructor)); |
| 115 | + |
| 116 | + if (method_exists($definition, 'setFactory')) { |
| 117 | + $definition = new Definition(); |
| 118 | + $definition->setFactory(array($factoryClass, $factoryMethod)); |
| 119 | + $data[] = array($definition, $expectedConstructor); |
| 120 | + } |
| 121 | + |
| 122 | + return $data; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * @test |
| 127 | + * @dataProvider getFactoryClassDoesNotExistData |
| 128 | + */ |
| 129 | + public function ifFactoryClassDoesNotExistFails(Definition $definition) |
| 130 | + { |
| 131 | + $containerBuilder = new ContainerBuilder(); |
| 132 | + $resolver = new ConstructorResolver($containerBuilder, new ResultingClassResolver($containerBuilder)); |
| 133 | + |
88 | 134 | $this->setExpectedException('Matthias\SymfonyServiceDefinitionValidator\Exception\ClassNotFoundException');
|
89 | 135 | $resolver->resolve($definition);
|
90 | 136 | }
|
91 | 137 |
|
| 138 | + public function getFactoryClassDoesNotExistData() |
| 139 | + { |
| 140 | + $factoryClass = 'NonExistingClass'; |
| 141 | + $factoryMethod = 'create'; |
| 142 | + |
| 143 | + $definition = new Definition(); |
| 144 | + $definition->setFactoryClass($factoryClass); |
| 145 | + $definition->setFactoryMethod($factoryMethod); |
| 146 | + |
| 147 | + $data = array(array($definition)); |
| 148 | + |
| 149 | + if (method_exists($definition, 'setFactory')) { |
| 150 | + $definition = new Definition(); |
| 151 | + $definition->setFactory(array($factoryClass, $factoryMethod)); |
| 152 | + $data[] = array($definition); |
| 153 | + } |
| 154 | + |
| 155 | + return $data; |
| 156 | + } |
| 157 | + |
92 | 158 | /**
|
93 | 159 | * @test
|
| 160 | + * @dataProvider getFactoryMethodIsNotStaticData |
94 | 161 | */
|
95 |
| - public function ifFactoryMethodIsNotStaticItFails() |
| 162 | + public function ifFactoryMethodIsNotStaticItFails(Definition $definition) |
96 | 163 | {
|
97 | 164 | $containerBuilder = new ContainerBuilder();
|
98 | 165 | $resolver = new ConstructorResolver($containerBuilder, new ResultingClassResolver($containerBuilder));
|
99 | 166 |
|
100 |
| - $definition = new Definition(); |
| 167 | + $this->setExpectedException('Matthias\SymfonyServiceDefinitionValidator\Exception\NonStaticFactoryMethodException'); |
| 168 | + $resolver->resolve($definition); |
| 169 | + } |
| 170 | + |
| 171 | + public function getFactoryMethodIsNotStaticData() |
| 172 | + { |
101 | 173 | $factoryClass = 'Matthias\SymfonyServiceDefinitionValidator\Tests\Fixtures\FactoryClass';
|
102 |
| - $definition->setFactoryClass($factoryClass); |
103 | 174 | $factoryMethod = 'createNonStatic';
|
| 175 | + |
| 176 | + $definition = new Definition(); |
| 177 | + $definition->setFactoryClass($factoryClass); |
104 | 178 | $definition->setFactoryMethod($factoryMethod);
|
105 | 179 |
|
106 |
| - $this->setExpectedException('Matthias\SymfonyServiceDefinitionValidator\Exception\NonStaticFactoryMethodException'); |
| 180 | + $data = array(array($definition)); |
| 181 | + |
| 182 | + if (method_exists($definition, 'setFactory')) { |
| 183 | + $definition = new Definition(); |
| 184 | + $definition->setFactory(array($factoryClass, $factoryMethod)); |
| 185 | + $data[] = array($definition); |
| 186 | + } |
| 187 | + |
| 188 | + return $data; |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * @test |
| 193 | + */ |
| 194 | + public function ifFactoryIsStringIsFactoryCallback() |
| 195 | + { |
| 196 | + if (!method_exists('Symfony\Component\DependencyInjection\Definition', 'getFactory')) { |
| 197 | + $this->markTestSkipped('Support for callables as factories was introduced in Symfony 2.6'); |
| 198 | + } |
| 199 | + |
| 200 | + $containerBuilder = new ContainerBuilder(); |
| 201 | + $resolver = new ConstructorResolver($containerBuilder, new ResultingClassResolver($containerBuilder)); |
| 202 | + |
| 203 | + $definition = new Definition(); |
| 204 | + $definition->setFactory('factoryCallback'); |
| 205 | + |
| 206 | + $this->assertSame('factoryCallback', $resolver->resolve($definition)); |
| 207 | + } |
| 208 | + |
| 209 | + /** |
| 210 | + * @test |
| 211 | + */ |
| 212 | + public function ifFactoryFunctionDoesNotExistFails() |
| 213 | + { |
| 214 | + if (!method_exists('Symfony\Component\DependencyInjection\Definition', 'getFactory')) { |
| 215 | + $this->markTestSkipped('Support for callables as factories was introduced in Symfony 2.6'); |
| 216 | + } |
| 217 | + |
| 218 | + $containerBuilder = new ContainerBuilder(); |
| 219 | + $resolver = new ConstructorResolver($containerBuilder, new ResultingClassResolver($containerBuilder)); |
| 220 | + |
| 221 | + $definition = new Definition(); |
| 222 | + $definition->setFactory('NotExistingFactoryCallback'); |
| 223 | + |
| 224 | + $this->setExpectedException('Matthias\SymfonyServiceDefinitionValidator\Exception\FunctionNotFoundException'); |
107 | 225 | $resolver->resolve($definition);
|
108 | 226 | }
|
109 | 227 | }
|
0 commit comments