public function setUp() { Adapter\AdapterFactory::resetAdapterPluginManager(); $this->sm = new ServiceManager(); $this->sm->setService('Config', ['stakhanovist' => ['queue_adapters' => ['ArrayAdapter' => ['adapter' => 'array', 'options' => ['dummyOption' => 'dummyValue']], 'Foo' => ['adapter' => 'array', 'options' => ['dummyOption' => 'dummyValue']]]]]); $this->sm->addAbstractFactory(QueueAdapterAbstractServiceFactory::class); }
public function setUp() { $adapter = new ArrayAdapter(); $adapter->setOptions(['dummyOption' => 'dummyValue']); $this->sm = new ServiceManager(); $this->sm->setInvokableClass('custom', Adapter\NullAdapter::class); $this->sm->setService('Config', ['stakhanovist' => ['queues' => ['queueA' => ['name' => 'A', 'adapter' => ['adapter' => 'array', 'options' => ['dummyOption' => 'dummyValue']], 'options' => ['messageClass' => ZendMessage::class]], 'queueB' => ['name' => 'B', 'adapter' => $adapter, 'options' => ['messageClass' => ZendMessage::class]], 'queueC' => ['name' => 'C', 'adapter' => 'custom', 'options' => ['messageClass' => ZendMessage::class]]]]]); $this->sm->addAbstractFactory(QueueAbstractServiceFactory::class); }
public function testCreateConsumerWithCustomLogger() { $config = array('log' => array('consumer-logger' => array('writers' => array(array('name' => 'stream', 'priority' => 1000, 'options' => array('stream' => sys_get_temp_dir() . '/consumers.log'))))), 'humus_amqp_module' => array('default_connection' => 'default', 'exchanges' => array('demo-exchange' => array('name' => 'demo-exchange', 'type' => 'direct')), 'queues' => array('demo-queue' => array('name' => 'demo-queue', 'exchange' => 'demo-exchange')), 'consumers' => array('test-consumer' => array('connection' => 'default', 'queues' => ['demo-queue'], 'auto_setup_fabric' => false, 'logger' => 'consumer-logger', 'callback' => 'test-callback', 'qos' => array('prefetchCount' => 10))))); $this->prepare($config); $this->services->addAbstractFactory(new \Zend\Log\LoggerAbstractServiceFactory()); $this->components->createServiceWithName($this->services, 'test-consumer', 'test-consumer'); }
/** * @covers Zend\ServiceManager\ServiceManager::create */ public function testCreateWithCallableAbstractFactory() { $this->serviceManager->addAbstractFactory(function () { return new TestAsset\Foo(); }); $this->assertInstanceOf('ZendTest\\ServiceManager\\TestAsset\\Foo', $this->serviceManager->get('foo')); }
/** * @covers Zend\ServiceManager\ServiceManager::get */ public function testDuplicateNewInstanceMultipleAbstractFactories() { $this->serviceManager->setAllowOverride(true); $this->serviceManager->setShareByDefault(false); $this->serviceManager->addAbstractFactory('ZendTest\ServiceManager\TestAsset\BarAbstractFactory'); $this->serviceManager->addAbstractFactory('ZendTest\ServiceManager\TestAsset\FooAbstractFactory'); $this->assertInstanceOf('ZendTest\ServiceManager\TestAsset\Bar', $this->serviceManager->get('bar')); $this->assertInstanceOf('ZendTest\ServiceManager\TestAsset\Bar', $this->serviceManager->get('bar')); }
public function testPeeringService() { $di = new Di(); $di->instanceManager()->setParameters('ZendTest\\ServiceManager\\TestAsset\\Bar', array('foo' => array('a'))); $this->serviceManager->addAbstractFactory(new DiAbstractServiceFactory($di)); $sm = $this->serviceManager->createScopedServiceManager(ServiceManager::SCOPE_PARENT); $sm->setFactory('di', new DiFactory()); $bar = $sm->get('ZendTest\\ServiceManager\\TestAsset\\Bar', true); $this->assertInstanceOf('ZendTest\\ServiceManager\\TestAsset\\Bar', $bar); }
/** * @covers \DoctrineModule\Service\CacheFactory::createService * @group 547 */ public function testCreateZendCache() { $factory = new CacheFactory('phpunit'); $serviceManager = new ServiceManager(); $serviceManager->setAlias('Config', 'Configuration'); $serviceManager->setService('Configuration', ['doctrine' => ['cache' => ['phpunit' => ['class' => 'DoctrineModule\\Cache\\ZendStorageCache', 'instance' => 'my-zend-cache', 'namespace' => 'DoctrineModule']]], 'caches' => ['my-zend-cache' => ['adapter' => ['name' => 'blackhole']]]]); $serviceManager->addAbstractFactory('Zend\\Cache\\Service\\StorageCacheAbstractServiceFactory'); $cache = $factory->createService($serviceManager); $this->assertInstanceOf('DoctrineModule\\Cache\\ZendStorageCache', $cache); }
/** * Set up the service manager * * @return ServiceManager */ public function init() { $options = $this->getOptions(); $di = $this->getBootstrap()->bootstrap('Di')->getResource('Di'); $doctrineContainer = $this->getBootstrap()->bootstrap('Doctrine')->getResource('Doctrine'); $serviceManager = new ZendServiceManager\ServiceManager(new ZendServiceManager\Config($options)); $serviceManager->setService('Di', $di); $serviceManager->setService('doctrine.entitymanager.orm_default', $doctrineContainer->getEntityManager()); $serviceManager->addAbstractFactory(new ZendServiceManager\Di\DiAbstractServiceFactory($di)); return $serviceManager; }
/** * @covers Zend\ServiceManager\ServiceManager::canCreateFromAbstractFactory * @covers Zend\ServiceManager\ServiceManager::create */ public function testAbstractFactoryNotUsedIfNotAbleToCreate() { $service = new \stdClass(); $af1 = $this->getMock('Zend\\ServiceManager\\AbstractFactoryInterface'); $af1->expects($this->any())->method('canCreateServiceWithName')->will($this->returnValue(true)); $af1->expects($this->any())->method('createServiceWithName')->will($this->returnValue($service)); $af2 = $this->getMock('Zend\\ServiceManager\\AbstractFactoryInterface'); $af2->expects($this->any())->method('canCreateServiceWithName')->will($this->returnValue(false)); $af2->expects($this->never())->method('createServiceWithName'); $this->serviceManager->addAbstractFactory($af1); $this->serviceManager->addAbstractFactory($af2); $this->assertSame($service, $this->serviceManager->create('test')); }
/** * @dataProvider getServiceOfVariousTypes * @param $service */ public function testAbstractFactoriesCanReturnAnyTypeButNull($service) { $abstractFactory = $this->getMock('Zend\\ServiceManager\\AbstractFactoryInterface'); $abstractFactory->expects($this->any())->method('canCreateServiceWithName')->with($this->serviceManager, 'something', 'something')->will($this->returnValue(true)); $abstractFactory->expects($this->any())->method('createServiceWithName')->with($this->serviceManager, 'something', 'something')->will($this->returnValue($service)); $this->serviceManager->addAbstractFactory($abstractFactory); if ($service === null) { try { $this->serviceManager->get('something'); $this->fail('ServiceManager::get() successfully returned null'); } catch (\Exception $e) { $this->assertInstanceOf('Zend\\ServiceManager\\Exception\\ServiceNotCreatedException', $e); } } else { $this->assertSame($service, $this->serviceManager->get('something')); } }
/** * Configure the provided service manager instance with the configuration * in this class. * * In addition to using each of the internal properties to configure the * service manager, also adds an initializer to inject ServiceManagerAware * and ServiceLocatorAware classes with the service manager. * * @param ServiceManager $serviceManager */ public function configureServiceManager(ServiceManager $serviceManager) { foreach ($this->invokables as $name => $class) { $serviceManager->setInvokableClass($name, $class); } foreach ($this->factories as $name => $factoryClass) { $serviceManager->setFactory($name, $factoryClass); } foreach ($this->abstractFactories as $factoryClass) { $serviceManager->addAbstractFactory($factoryClass); } foreach ($this->aliases as $name => $service) { $serviceManager->setAlias($name, $service); } foreach ($this->shared as $name => $value) { $serviceManager->setShared($name, $value); } $serviceManager->addInitializer(function ($instance) use($serviceManager) { if ($instance instanceof EventManagerAwareInterface) { if ($instance->getEventManager() instanceof EventManagerInterface) { $instance->getEventManager()->setSharedManager($serviceManager->get('SharedEventManager')); } else { $instance->setEventManager($serviceManager->get('EventManager')); } } }); $serviceManager->addInitializer(function ($instance) use($serviceManager) { if ($instance instanceof ServiceManagerAwareInterface) { $instance->setServiceManager($serviceManager); } }); $serviceManager->addInitializer(function ($instance) use($serviceManager) { if ($instance instanceof ServiceLocatorAwareInterface) { $instance->setServiceLocator($serviceManager); } }); $serviceManager->setService('ServiceManager', $serviceManager); }
/** * @covers Zend\ServiceManager\ServiceManager::createFromAbstractFactory * @covers Zend\ServiceManager\ServiceManager::has */ public function testWillNotCreateCircularReferences() { $abstractFactory = new TestAsset\CircularDependencyAbstractFactory(); $sm = new ServiceManager(); $sm->addAbstractFactory($abstractFactory); $foo = $sm->get('foo'); $this->assertSame($abstractFactory->expectedInstance, $foo); }