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 setUp() { $loaderFactory = new ControllerLoaderFactory(); $config = new ArrayObject(array('di' => array())); $this->services = new ServiceManager(); $this->services->setService('Zend\\ServiceManager\\ServiceLocatorInterface', $this->services); $this->services->setFactory('ControllerLoader', $loaderFactory); $this->services->setService('Config', $config); $this->services->setFactory('ControllerPluginManager', new ControllerPluginManagerFactory()); $this->services->setFactory('Di', new DiFactory()); $this->services->setFactory('EventManager', new EventManagerFactory()); $this->services->setInvokableClass('SharedEventManager', 'Zend\\EventManager\\SharedEventManager'); }
public function testWillGetListenerFromServiceLocator() { $this->services->setInvokableClass('TestListener', 'SynrgTest\\Twilio\\Controller\\TestAsset\\Listener'); $this->services->setAllowOverride(true); $this->services->setService('Config', array('synrg-twilio' => array('TestController' => array('listener' => 'TestListener')))); $controller = $this->controllers->get('TestController'); $this->assertInstanceOf('Synrg\\Twilio\\Controller\\TwilioController', $controller); $responder = $controller->getResponder(); $events = $responder->getEventManager(); $expected = array('SynrgTest\\Twilio\\Controller\\TestAsset\\Listener', 'Synrg\\Twilio\\Responder\\Responder'); $identifiers = $events->getIdentifiers(); $this->assertEquals(array_values($expected), array_values($identifiers)); }
public function testGet() { // Via container $this->container->foo = []; $this->assertEquals([], $this->container->get('foo')); // Via service manager $this->sm->setService('foo', new \stdClass()); $this->sm->setAlias('bar', 'foo'); $this->assertInstanceOf('stdClass', $this->container->get('bar')); $this->sm->setFactory('factory', function (ServiceManager $sm) { return $sm->get('bar'); }); $this->assertInstanceOf('stdClass', $this->container->get('factory')); $this->sm->setInvokableClass('invokable', 'stdClass'); $this->assertInstanceOf('stdClass', $this->container->get('invokable')); }
public function testIdentityFactoryCanInjectAuthenticationServiceIfInParentServiceManager() { $services = new ServiceManager(); $services->setInvokableClass('Zend\Authentication\AuthenticationService', 'Zend\Authentication\AuthenticationService'); $this->helpers->setServiceLocator($services); $identity = $this->helpers->get('identity'); $expected = $services->get('Zend\Authentication\AuthenticationService'); $this->assertSame($expected, $identity->getAuthenticationService()); }
/** * 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 * classes with the service manager. * * @param ServiceManager $serviceManager * @return void */ public function configureServiceManager(ServiceManager $serviceManager) { foreach ($this->invokables as $name => $service) { $serviceManager->setInvokableClass($name, $service); } $factories = $this->getFactories(); foreach ($factories as $name => $factory) { $serviceManager->setFactory($name, $factory); } }
public function testInstantiatesHalJsonRenderer() { $services = new ServiceManager(); $viewHelperManager = $this->getMockBuilder('Zend\\View\\HelperPluginManager')->disableOriginalConstructor()->getMock(); $services->setService('ViewHelperManager', $viewHelperManager); $services->setInvokableClass(ApiProblemRenderer::class, ApiProblemRenderer::class); $factory = new HalJsonRendererFactory(); $renderer = $factory($services, 'ZF\\Hal\\JsonRenderer'); $this->assertInstanceOf('ZF\\Hal\\View\\HalJsonRenderer', $renderer); }
public function testWillInstantiateFromFQCN() { $name = 'testFactory'; $factory = new StorageFactory($name); $objectManager = $this->getMock('Doctrine\\Common\\Persistence\\ObjectManager'); $serviceManager = new ServiceManager(); $serviceManager->setInvokableClass('DoctrineModule\\Authentication\\Storage\\Session', 'Zend\\Authentication\\Storage\\Session'); $serviceManager->setService('Configuration', array('doctrine' => array('authentication' => array($name => array('objectManager' => $objectManager, 'identityClass' => 'DoctrineModuleTest\\Authentication\\Adapter\\TestAsset\\IdentityObject', 'identityProperty' => 'username', 'credentialProperty' => 'password'))))); $adapter = $factory->createService($serviceManager); $this->assertInstanceOf('DoctrineModule\\Authentication\\Storage\\ObjectRepository', $adapter); }
/** * @covers Zend\ServiceManager\ServiceManager::create * @covers Zend\ServiceManager\ServiceManager::createDelegatorFromFactory * @covers Zend\ServiceManager\ServiceManager::createDelegatorCallback * @covers Zend\ServiceManager\ServiceManager::addDelegator */ public function testMultipleDelegatorFactoriesWhenNotRegisteredAsServices() { $fooDelegator = new MockSelfReturningDelegatorFactory(); $barDelegator = new MockSelfReturningDelegatorFactory(); $this->serviceManager->addDelegator('foo-service', $fooDelegator); $this->serviceManager->addDelegator('foo-service', $barDelegator); $this->serviceManager->setInvokableClass('foo-service', 'stdClass'); $this->assertSame($barDelegator, $this->serviceManager->create('foo-service')); $this->assertCount(1, $barDelegator->instances); $this->assertCount(1, $fooDelegator->instances); $this->assertInstanceOf('stdClass', array_shift($fooDelegator->instances)); $this->assertSame($fooDelegator, array_shift($barDelegator->instances)); }
public function testAutoGenerateAndEvaluateProxies() { $serviceManager = new ServiceManager(); $namespace = 'ZendTestProxy' . uniqid(); $serviceManager->setService('Config', array('lazy_services' => array('class_map' => array('foo' => __CLASS__), 'proxies_namespace' => $namespace))); $serviceManager->setFactory('foo-delegator', 'Zend\\ServiceManager\\Proxy\\LazyServiceFactoryFactory'); $serviceManager->setInvokableClass('foo', __CLASS__); $serviceManager->addDelegator('foo', 'foo-delegator'); /* @var $proxy self|\ProxyManager\Proxy\ValueHolderInterface|\ProxyManager\Proxy\LazyLoadingInterface */ $proxy = $serviceManager->create('foo'); $proxyClassName = get_class($proxy); $this->assertInstanceOf('ProxyManager\\Proxy\\LazyLoadingInterface', $proxy); $this->assertInstanceOf(__CLASS__, $proxy); $this->assertStringMatchesFormat($namespace . '\\__PM__\\ZendTest\\ServiceManager\\Proxy\\LazyServiceFactoryFactoryTest%s', $proxyClassName); $this->assertFileNotExists(sys_get_temp_dir() . '/' . str_replace('\\', '', $proxyClassName) . '.php'); $this->assertFalse($proxy->isProxyInitialized()); $this->assertEquals($this->invalidConfigProvider(), $proxy->invalidConfigProvider()); $this->assertTrue($proxy->isProxyInitialized()); }
/** * Generates a list of Action objects based on the given directory structure, handling * each found class as an invokable service * * @deprecated this logic is deprecated and uses per-directory scanning. Instead, please * map your defined service names in the 'services' config * @param array $modules * @return array * @throws InvalidArgumentException */ protected function buildDirectoryApi(array $modules) { $api = array(); foreach ($modules as $moduleName => $module) { if (!isset($module['directory']) || !is_dir($module['directory'])) { throw new InvalidArgumentException('Invalid directory given: "' . $module['directory'] . '"'); } if (!isset($module['namespace']) || !is_string($module['namespace'])) { throw new InvalidArgumentException('Invalid namespace provided for module "' . $moduleName . '"'); } $jsNamespace = rtrim(str_replace('\\', '.', $module['namespace']), '.') . '.'; $directoryScanner = new DirectoryScanner($module['directory']); /* @var $class \Zend\Code\Scanner\DerivedClassScanner */ foreach ($directoryScanner->getClasses(true) as $class) { // now building the service name as exposed client-side $className = $class->getName(); $jsClassName = str_replace('\\', '.', substr($className, strlen($module['namespace']) + 1)); $jsClassNames = explode('.', $jsClassName); $chunks = count($jsClassNames); // lcfirst all chunks except the last one for ($i = 1; $i < $chunks; $i += 1) { $jsClassNames[$i - 1] = lcfirst($jsClassNames[$i - 1]); } $serviceName = $jsNamespace . implode('.', $jsClassNames); if (!$this->serviceManager->has($serviceName)) { $this->serviceManager->setInvokableClass($serviceName, $className); } // invoking to check if nothing went wrong - this avoids setting invalid services $service = $this->serviceManager->get($serviceName); $action = $this->buildAction(get_class($service)); $action->setName($serviceName); $action->setObjectName($className); $api[$serviceName] = $action; } } return $api; }
public function createServiceManager(array $config) { // create ServiceManager $serviceManager = new ServiceManager(); // set an initializer for ServiceLocatorAwareInterface $serviceManager->addInitializer(function ($instance, ServiceLocatorInterface $serviceLocator) { if ($instance instanceof ServiceLocatorAwareInterface) { $instance->setServiceLocator($serviceLocator); } }); // add $serviceManger to the config keys so we can configure it $this->configKeys = ['service_manager' => $serviceManager] + $this->configKeys; // add the pluginManagers to the ServiceManager foreach ($this->pluginManagers as $key => $className) { $serviceManager->setInvokableClass($key, $className); } // add Zend\Form's view helpers to the ViewHelperManager $viewHelperManager = $serviceManager->get('ViewHelperManager'); $vhConfig = new \Zend\Form\View\HelperConfig(); $vhConfig->configureServiceManager($viewHelperManager); // apply configuration $this->applyConfig($serviceManager, $config); return $serviceManager; }
/** * Configure the provided service manager instance with the configuration * in this class. * * @param ServiceManager $serviceManager * @return void */ public function configureServiceManager(ServiceManager $serviceManager) { foreach ($this->invokables as $name => $service) { $serviceManager->setInvokableClass($name, $service); } }
/** * @covers Zend\ServiceManager\ServiceManager::setAlias */ public function testSetAlias() { $this->serviceManager->setInvokableClass('foo', 'bar'); $ret = $this->serviceManager->setAlias('bar', 'foo'); $this->assertSame($this->serviceManager, $ret); }
public function configureServiceManager(ServiceManager $serviceManager) { foreach ($this->helpers as $name => $fqcn) { $serviceManager->setInvokableClass($name, $fqcn); } }
/** * 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); }
public function getServiceLocator(array $config = array()) { $config = array('slm_locale' => $config + array('strategies' => array())); $serviceLocator = new ServiceManager(); $serviceLocator->setFactory('SlmLocale\\Locale\\Detector', 'SlmLocale\\Service\\DetectorFactory'); $serviceLocator->setInvokableClass('SlmLocale\\Strategy\\StrategyPluginManager', 'SlmLocale\\Strategy\\StrategyPluginManager'); $serviceLocator->setService('EventManager', new EventManager()); $serviceLocator->setService('config', $config); return $serviceLocator; }
public function testRender() { $renderer = new PhpRenderer(); $resolver = new Resolver\AggregateResolver(); $map = new Resolver\TemplateMapResolver(array('page' => __DIR__ . '/view/page.phtml', 'comments-list' => __DIR__ . '/view/comments-list.phtml', 'comment' => __DIR__ . '/view/comment.phtml', 'user' => __DIR__ . '/view/user.phtml')); $stack = new Resolver\TemplatePathStack(array('script_paths' => array(__DIR__ . '/view'))); $resolver->attach($map)->attach($stack)->attach(new Resolver\RelativeFallbackResolver($map))->attach(new Resolver\RelativeFallbackResolver($stack)); $renderer->setResolver($resolver); $view = new View(); $response = new Response(); $view->setResponse($response); $strategy = new PhpRendererStrategy($renderer); $strategy->attach($view->getEventManager()); ///////////////////////////////////////////////// $viewConfigExample = ['some-view' => ['template' => 'some-template', 'viewModel' => 'some-view-model|\\Some\\ViewModel::class', 'extend' => 'parent-view', 'capture' => 'some-capture', 'children' => ['child-view'], 'childrenDynamicLists' => ['child-view' => 'listVar'], 'data' => ['fromGlobal' => 'varName', 'fromParent' => 'varName', 'static' => ['key' => 'value']], 'dataProvider' => 'some-view-model|\\Some\\DataProvider::class']]; ////////////// $viewConfig = ['page' => ['template' => 'page', 'children' => ['comments-list', 'comment-create' => ['template' => 'comment-create', 'children' => ['myself-info' => ['viewModel' => \Sebaks\ViewTest\MyselfViewModel::class, 'template' => 'user'], 'comment-create-form' => ['template' => 'form', 'children' => ['form-element-textarea' => ['capture' => 'form-element', 'template' => 'form-element-textarea'], 'form-element-button' => ['capture' => 'form-element', 'template' => 'form-element-button']]]]], 'users-table' => ['template' => 'table', 'children' => ['table-head-rows' => ['template' => 'table-tr', 'data' => ['fromParent' => 'rows'], 'children' => ['table-th' => ['template' => 'table-th', 'capture' => 'table-td', 'data' => ['fromParent' => 'value']]], 'childrenDynamicLists' => ['table-th' => 'rows']], 'table-body-rows' => ['template' => 'table-tr', 'data' => ['fromParent' => 'rows'], 'children' => ['table-td' => ['template' => 'table-td', 'data' => ['fromParent' => 'value']]], 'childrenDynamicLists' => ['table-td' => 'rows']]], 'childrenDynamicLists' => ['table-body-rows' => 'bodyRows', 'table-head-rows' => 'headRows'], 'data' => ['static' => ['headRows' => [['Id', 'Name']], 'bodyRows' => [['1', 'John'], ['2', 'Helen']]]]]]], 'comments-list' => ['template' => 'comments-list', 'children' => ['comment' => ['viewModel' => \Sebaks\ViewTest\CommentViewModel::class, 'template' => 'comment', 'children' => ['user' => ['viewModel' => \Sebaks\ViewTest\UserViewModel::class, 'template' => 'user', 'data' => ['fromParent' => ['comment:userId' => 'userId'], 'static' => ['class' => 'user']]]], 'data' => ['fromParent' => ['comment' => 'comment']]]], 'childrenDynamicLists' => ['comment' => 'comments'], 'data' => ['fromGlobal' => 'comments']]]; $data = ['comments' => [['id' => 'c1', 'userId' => 'u1', 'text' => 'text of c1'], ['id' => 'c2', 'userId' => 'u2', 'text' => 'text of c2']]]; $serviceLocator = new \Zend\ServiceManager\ServiceManager(); $serviceLocator->setInvokableClass(\Sebaks\ViewTest\CommentViewModel::class, \Sebaks\ViewTest\CommentViewModel::class, false); $serviceLocator->setInvokableClass(\Sebaks\ViewTest\UserViewModel::class, \Sebaks\ViewTest\UserViewModel::class, false); $serviceLocator->setInvokableClass(\Sebaks\ViewTest\MyselfViewModel::class, \Sebaks\ViewTest\MyselfViewModel::class, false); ///////////////////// $config = new Config($viewConfig); $viewBuilder = new ViewBuilder($config, $serviceLocator); $pageViewModel = $viewBuilder->build($viewConfig['page'], $data); ///////////////////// $view->render($pageViewModel); $result = $response->getBody(); $expected = '<ul><li>text of c1 <div class="user">John</div></li><li>text of c2 <div class="user">Helen</div></li></ul> <div class="">Me</div><form><textarea></textarea><button type="submit">Submit</button></form><table> <thead> <tr><th>Id</th><th>Name</th></tr> </thead> <tbody> <tr><td>1</td><td>John</td></tr><tr><td>2</td><td>Helen</td></tr> </tbody> </table>'; $this->assertEquals($expected, $result); }
public function getServiceLocator() { $serviceLocator = new ServiceManager(); $serviceLocator->setInvokableClass('MQLocale\\Strategy\\StrategyManager', 'MQLocale\\Strategy\\StrategyManager'); return $serviceLocator; }
public function testChildrenData() { $config = ['someView' => ['viewModel' => CustomViewModel::class, 'data' => ['static' => ['parentVar' => 'parentVarValue']], 'children' => ['childrenView' => ['data' => ['static' => ['staticVar' => 'staticVarValue'], 'fromGlobal' => ['globalVar' => 'globalVar'], 'fromParent' => ['parentVar' => 'parentVar', 'parentCallVar' => 'parentVar2']]]]]]; $globalData = ['globalVar' => 'globalVarValue']; $serviceLocator = new ServiceManager(); $serviceLocator->setInvokableClass(CustomViewModel::class, CustomViewModel::class, false); $viewBuilder = new ViewBuilder(new Config($config), $serviceLocator); $viewModel = $viewBuilder->build($config['someView'], $globalData); $childrenView = $viewModel->getChildrenByCaptureTo('childrenView')[0]; $this->assertEquals('staticVarValue', $childrenView->getVariable('staticVar')); $this->assertEquals('globalVarValue', $childrenView->getVariable('globalVar')); $this->assertEquals('parentVarValue', $childrenView->getVariable('parentVar')); $this->assertEquals('parentCallVarValue', $childrenView->getVariable('parentVar2')); }
public function testTriggersRenderErrorEventInCaseOfRenderingException() { $resolver = new TemplateMapResolver(); $resolver->add('exception', __DIR__ . '/_files/exception.phtml'); $this->renderer->setResolver($resolver); $strategy = new PhpRendererStrategy($this->renderer); $this->view->getEventManager()->attach($strategy); $model = new ViewModel(); $model->setTemplate('exception'); $this->event->setViewModel($model); $services = new ServiceManager(); $services->setService('Request', $this->request); $services->setService('Response', $this->response); $services->setInvokableClass('SharedEventManager', 'Zend\\EventManager\\SharedEventManager'); $services->setFactory('EventManager', function ($services) { $sharedEvents = $services->get('SharedEventManager'); $events = new EventManager(); $events->setSharedManager($sharedEvents); return $events; }, false); $application = new Application(array(), $services); $this->event->setApplication($application); $test = (object) array('flag' => false); $application->getEventManager()->attach(MvcEvent::EVENT_RENDER_ERROR, function ($e) use($test) { $test->flag = true; $test->error = $e->getError(); $test->exception = $e->getParam('exception'); }); $this->strategy->render($this->event); $this->assertTrue($test->flag); $this->assertEquals(Application::ERROR_EXCEPTION, $test->error); $this->assertInstanceOf('Exception', $test->exception); $this->assertContains('script', $test->exception->getMessage()); }