|
14 | 14 | use RecursiveIteratorIterator;
|
15 | 15 | use RecursiveRegexIterator;
|
16 | 16 | use RegexIterator;
|
| 17 | +use Zend\ServiceManager\Exception\ServiceNotCreatedException; |
| 18 | +use Zend\ServiceManager\Exception\ServiceNotFoundException; |
17 | 19 | use Zend\ServiceManager\Factory\InvokableFactory;
|
18 | 20 | use Zend\ServiceManager\Proxy\LazyServiceFactory;
|
19 | 21 | use Zend\ServiceManager\ServiceManager;
|
@@ -146,4 +148,102 @@ public function testCanUseLazyServiceFactoryFactoryToCreateLazyServiceFactoryToA
|
146 | 148 | );
|
147 | 149 | $this->assertEquals(['foo' => 'bar'], $options, 'Options returned do not match configuration');
|
148 | 150 | }
|
| 151 | + |
| 152 | + /** |
| 153 | + * @covers \Zend\ServiceManager\ServiceManager::createLazyServiceDelegatorFactory |
| 154 | + */ |
| 155 | + public function testMissingClassMapRaisesExceptionOnAttemptToRetrieveLazyService() |
| 156 | + { |
| 157 | + $config = [ |
| 158 | + 'lazy_services' => [ |
| 159 | + ], |
| 160 | + 'factories' => [ |
| 161 | + InvokableObject::class => InvokableFactory::class, |
| 162 | + ], |
| 163 | + 'delegators' => [ |
| 164 | + InvokableObject::class => [LazyServiceFactory::class], |
| 165 | + ], |
| 166 | + ]; |
| 167 | + |
| 168 | + $container = new ServiceManager($config); |
| 169 | + $this->setExpectedException(ServiceNotCreatedException::class, 'class_map'); |
| 170 | + $container->get(InvokableObject::class); |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * @covers \Zend\ServiceManager\ServiceManager::createLazyServiceDelegatorFactory |
| 175 | + */ |
| 176 | + public function testWillNotGenerateProxyClassFilesByDefault() |
| 177 | + { |
| 178 | + $config = [ |
| 179 | + 'lazy_services' => [ |
| 180 | + 'class_map' => [ |
| 181 | + InvokableObject::class => InvokableObject::class, |
| 182 | + ], |
| 183 | + 'proxies_namespace' => 'TestAssetProxy', |
| 184 | + ], |
| 185 | + 'factories' => [ |
| 186 | + InvokableObject::class => InvokableFactory::class, |
| 187 | + ], |
| 188 | + 'delegators' => [ |
| 189 | + InvokableObject::class => [LazyServiceFactory::class], |
| 190 | + ], |
| 191 | + ]; |
| 192 | + |
| 193 | + $this->assertProxyDirEmpty(); |
| 194 | + |
| 195 | + $container = new ServiceManager($config); |
| 196 | + $instance = $container->build(InvokableObject::class, ['foo' => 'bar']); |
| 197 | + |
| 198 | + // This is the important test |
| 199 | + $this->assertProxyDirEmpty('Expected proxy directory to remain empty when write_proxy_files disabled'); |
| 200 | + |
| 201 | + // Test we got a usable proxy |
| 202 | + $this->assertInstanceOf( |
| 203 | + InvokableObject::class, |
| 204 | + $instance, |
| 205 | + 'Service returned does not extend ' . InvokableObject::class |
| 206 | + ); |
| 207 | + $this->assertContains( |
| 208 | + 'TestAssetProxy', |
| 209 | + get_class($instance), |
| 210 | + 'Service returned does not contain expected namespace' |
| 211 | + ); |
| 212 | + |
| 213 | + // Test proxying works as expected |
| 214 | + $options = $instance->getOptions(); |
| 215 | + $this->assertInternalType( |
| 216 | + 'array', |
| 217 | + $options, |
| 218 | + sprintf( |
| 219 | + 'Expected an array of options; %s received', |
| 220 | + (is_object($options) ? get_class($options) : gettype($options)) |
| 221 | + ) |
| 222 | + ); |
| 223 | + $this->assertEquals(['foo' => 'bar'], $options, 'Options returned do not match configuration'); |
| 224 | + } |
| 225 | + |
| 226 | + public function testRaisesServiceNotFoundExceptionIfRequestedLazyServiceIsNotInClassMap() |
| 227 | + { |
| 228 | + $config = [ |
| 229 | + 'lazy_services' => [ |
| 230 | + 'class_map' => [ |
| 231 | + stdClass::class => stdClass::class, |
| 232 | + ], |
| 233 | + 'proxies_namespace' => 'TestAssetProxy', |
| 234 | + ], |
| 235 | + 'factories' => [ |
| 236 | + InvokableObject::class => InvokableFactory::class, |
| 237 | + ], |
| 238 | + 'delegators' => [ |
| 239 | + InvokableObject::class => [LazyServiceFactory::class], |
| 240 | + ], |
| 241 | + ]; |
| 242 | + |
| 243 | + $this->assertProxyDirEmpty(); |
| 244 | + |
| 245 | + $container = new ServiceManager($config); |
| 246 | + $this->setExpectedException(ServiceNotFoundException::class, 'not found in the provided services map'); |
| 247 | + $instance = $container->build(InvokableObject::class, ['foo' => 'bar']); |
| 248 | + } |
149 | 249 | }
|
0 commit comments