/** * @dataProvider servicesProvider */ public function testServices($service, $class) { // Create temporary service manager with identical configuration. $config = static::$_serviceManager->get('config'); $serviceManager = new \Zend\ServiceManager\ServiceManager($config['service_manager']); $serviceManager->setService('config', $config); $serviceManager->setService('Db', $this->createMock('Zend\\Db\\Adapter\\Adapter')); $this->assertEquals($class, get_class($serviceManager->get($service))); }
public function preDispatch() { $controller = $this->getActionController(); $controllerName = get_class($controller); if ($this->serviceManager->has($controllerName)) { $this->serviceManager->setService('controller', $controller); if (!$this->serviceManager->get($controllerName)) { throw new \Exception('Invalid Controller'); } } }
public function testCreateService() { $mockOptions = new \ZfcUser\Options\ModuleOptions(); $mockMapper = \Mockery::mock('ZfcUser\\Mapper\\UserInterface'); $serviceManager = new \Zend\ServiceManager\ServiceManager(); $serviceManager->setService('zfcuser_module_options', $mockOptions); $serviceManager->setService('zfcuser_user_mapper', $mockMapper); $factory = new \LdcUserProfile\Extensions\ZfcUser\ZfcUserInputFilterFactory(); $svc = $factory->createService($serviceManager); $this->assertInstanceOf('LdcUserProfile\\Extensions\\ZfcUser\\ZfcUserInputFilter', $svc); }
public function testCreateService() { $mockOptions = new \ZfcUser\Options\ModuleOptions(); $mockOptions->setUserEntityClass('ArrayObject'); $mockHydrator = \Mockery::mock('Zend\\Stdlib\\Hydrator\\HydratorInterface'); $serviceManager = new \Zend\ServiceManager\ServiceManager(); $serviceManager->setService('zfcuser_module_options', $mockOptions); $serviceManager->setService('zfcuser_user_hydrator', $mockHydrator); $factory = new \LdcUserProfile\Extensions\ZfcUser\ZfcUserFieldsetFactory(); $svc = $factory->createService($serviceManager); $this->assertInstanceOf('LdcUserProfile\\Extensions\\ZfcUser\\ZfcUserFieldset', $svc); $this->assertSame($mockHydrator, $svc->getHydrator()); $this->assertInstanceOf('ArrayObject', $svc->getObject()); }
public function testCreateService() { $mockUserService = \Mockery::mock('ZfcUser\\Service\\User'); $mockFieldset = \Mockery::mock('Zend\\Form\\FieldsetInterface'); $mockInputFilter = \Mockery::mock('Zend\\InputFilter\\InputFilterInterface'); $serviceManager = new \Zend\ServiceManager\ServiceManager(); $serviceManager->setService('zfcuser_user_service', $mockUserService); $serviceManager->setService('ldc-user-profile_extension_zfcuser_fieldset', $mockFieldset); $serviceManager->setService('ldc-user-profile_extension_zfcuser_inputfilter', $mockInputFilter); $factory = new \LdcUserProfile\Extensions\ZfcUser\ZfcUserExtensionFactory(); $svc = $factory->createService($serviceManager); $this->assertInstanceOf('LdcUserProfile\\Extensions\\ZfcUser\\ZfcUserExtension', $svc); $this->assertSame($mockUserService, $svc->getUserService()); $this->assertSame($mockFieldset, $svc->getFieldset()); $this->assertSame($mockInputFilter, $svc->getInputFilter()); }
protected function getRouterConfig() { $serviceManager = new \Zend\ServiceManager\ServiceManager(new \Zend\Mvc\Service\ServiceManagerConfig(array())); $serviceManager->setService('ApplicationConfig', include 'config/application.config.php'); $serviceManager->get('ModuleManager')->loadModules(); return $serviceManager->get('Config')['router']['routes']; }
public function providesClassWithConstructionParameters() { $serviceManager = new \Zend\ServiceManager\ServiceManager(); $serviceManager->setService('EventManager', new \Zend\EventManager\EventManager()); $serviceManager->setService('Request', new \stdClass()); $serviceManager->setService('Response', new \stdClass()); return array(array('Zend\\Config\\Config', array('array' => array())), array('Zend\\Db\\Adapter\\Adapter', array('driver' => array('driver' => 'Pdo_Sqlite'))), array('Zend\\Mvc\\Application', array('configuration' => array(), 'serviceManager' => $serviceManager))); }
function getServiceLocator() { $config = Zend\Config\Factory::fromFile(findParentPath('config') . "/module.config.php"); $serviceManagerConfig = new \Zend\ServiceManager\Config($config['service_manager']); $serviceManager = new \Zend\ServiceManager\ServiceManager($serviceManagerConfig); $serviceManager->setService('config', $config); return $serviceManager; }
public function testCreateServiceWithConfiguration() { $serviceManager = new \Zend\ServiceManager\ServiceManager(); $serviceManager->setService('Config', array('ldc-user-profile' => array('url_path' => '/foo/bar'))); $factory = new \LdcUserProfile\Options\ModuleOptionsFactory(); $svc = $factory->createService($serviceManager); $this->assertInstanceOf('LdcUserProfile\\Options\\ModuleOptions', $svc); $this->assertEquals('/foo/bar', $svc->getUrlPath()); }
/** * @return EntityManager */ protected function setupDoctrine() { $serviceManager = new \Zend\ServiceManager\ServiceManager(new \Zend\Mvc\Service\ServiceManagerConfig(array())); $serviceManager->setService('ApplicationConfig', $this->getTestConfig()); $serviceManager->get('ModuleManager')->loadModules(); $serviceManager->get('Application')->bootstrap(); $this->em = $serviceManager->get('doctrine.entitymanager.orm_default'); self::createDbSchema($this->em); return $this->em; }
/** * Call */ public function call() { //print_r('--middlewareServiceManager call()--'); $serviceManagerUtillConfigObject = new \Utill\Service\Manager\config(); $this->serviceManagerConfig = new \Zend\ServiceManager\Config($serviceManagerUtillConfigObject->getConfig()); $serviceManager = new \Zend\ServiceManager\ServiceManager($this->serviceManagerConfig); $serviceManager->setService('slimApp', $this->app); $this->app->setServiceManager($serviceManager); $this->next->call(); }
public function testCreateServiceWithUnregisterExtension() { $moduleOptions = new ModuleOptions(); $moduleOptions->setRegisteredExtensions(array('foo' => false)); $mockExtension = \Mockery::mock('LdcUserProfile\\Extensions\\AbstractExtension'); $mockExtension->shouldReceive('getName')->andReturn('foo'); $serviceManager = new \Zend\ServiceManager\ServiceManager(); $serviceManager->setService('ldc-user-profile_module_options', $moduleOptions); //$serviceManager->setService('foo', $mockExtension); $factory = new \LdcUserProfile\Service\ProfileServiceFactory(); $svc = $factory->createService($serviceManager); $this->assertInstanceOf('LdcUserProfile\\Service\\ProfileService', $svc); $this->assertFalse($svc->hasExtension('foo')); $this->assertFalse($svc->hasExtension($mockExtension)); }
/** * Boot Rails. * * It requires at least paths to libraries and/or Composer Autoloader * to autoload classes. An instance of Rails\Loader\Loader can also be * passed instead. * * @param array $config */ public static function boot(array $config) { if (self::$booted) { return; } /** * Loader. */ if (!isset($config['loader'])) { throw new BadMethodCallException('Requires at least loader instance or configuration'); } else { if (is_array($config['loader'])) { // require_once __DIR__ . '/Rails/Loader/Loader.php'; $loader = new Rails\Loader\Loader(); if (isset($config['loader']['paths'])) { $loader->addPaths($config['loader']['paths']); } if (isset($config['loader']['composerAutoloader'])) { $loader->setComposerAutoloader($config['loader']['composerAutoloader']); } else { $loader->addPath(__DIR__ . '/Rails'); } $loader->register(); } elseif ($config['loader'] instanceof Rails\Loader\Loader) { $loader = $config['loader']; } else { throw new InvalidArgumentException(sprintf('Loader must be either array or instance of Rails\\Loader\\Loader, %s passed', gettype($config['loader']))); } } /** * Service Manager. */ $sm = new Zend\ServiceManager\ServiceManager(); self::$serviceManager = $sm; $sm->setService('loader', $loader); /** * Global configuration. */ $sm->setService('rails.config', new Rails\ActiveSupport\ArrayObject(['use_cache' => false])); /** * Inflector. */ $sm->setFactory('inflector', function () { return new Rails\ActiveSupport\Inflector\Inflector(new Rails\ActiveSupport\Inflector\Inflections\EnglishInflections(), 'en'); }); /** * Translator. */ $sm->setFactory('i18n', function () { $tr = new Rails\I18n\LoadingTranslator(); $tr->setDefaultLocale('en'); $tr->setLoader(new Rails\I18n\Loader()); # Add Rails' locales paths to i18n loader $tr->loader()->addPaths([__DIR__ . '/Rails/ActiveSupport/Carbon/locales', __DIR__ . '/Rails/I18n/locales']); return $tr; }); self::$booted = true; }
public function test() { /* $viewConfigExample = [ 'some-view' => [ // view name 'template' => 'some-template', // html template //--- not required options 'viewModel' => 'some-view-model|\Some\ViewModel::class', // Instance of ViewModel 'extend' => 'parent-view', // extended view 'capture' => 'some-capture', // for grouping views 'children' => [ 'child-view'// array of views ], 'childrenDynamicLists' => [ // will be generated by list from one of child 'child-view' => 'listVar' // every entry in listVar will be setted to genereted child ], 'data' => [ // required data 'fromGlobal' => 'varName', // // will be set as variables from global data 'fromParent' => 'varName', // will be set as variables by calling getVariable('varName') from parent 'static' => [ // will be set as variables 'key' => 'value' ], ], ], ]; */ $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()); $viewConfig = ['layouts' => ['layout' => ['template' => 'layout']], 'contents' => ['page' => ['layout' => 'layout', 'template' => 'page', 'children' => ['comments-list' => ['extend' => 'comments-list', 'template' => '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']]]]]]]], 'blocks' => ['comments-list' => ['children' => ['comment' => ['viewModel' => \Sebaks\ViewTest\CommentViewModel::class, 'template' => 'comment', 'children' => ['user' => ['viewModel' => \Sebaks\ViewTest\UserViewModel::class, 'template' => 'user', 'data' => ['fromParent' => 'userId', 'static' => ['class' => 'user']], 'children' => ['location' => ['viewModel' => \Sebaks\ViewTest\LocationViewModel::class, 'template' => 'location', 'data' => ['fromParent' => 'countryId']]]]], 'data' => ['fromParent' => ['foo' => 'bar', 'comment' => 'comment']]]], 'childrenDynamicLists' => ['comment' => 'comments'], 'data' => ['fromGlobal' => ['foo' => 'bar', 'result' => 'comments']]]]]; $data = ['result' => [['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\LocationViewModel::class, \Sebaks\ViewTest\LocationViewModel::class, false); $serviceLocator->setInvokableClass(\Sebaks\ViewTest\MyselfViewModel::class, \Sebaks\ViewTest\MyselfViewModel::class, false); ///////////////////// $config = ['sebaks-view' => $viewConfig]; $serviceLocator->setService('config', $config); $serviceLocator->setService('EventManager', new EventManager()); $request = new Request(); $serviceLocator->setService('Request', $request); //$response = new Response(); $serviceLocator->setService('Response', $response); $e = new MvcEvent(); $e->setRequest($request); $e->setResponse($response); $dispatchResult = new ViewModel(); $dispatchResult->setVariables($data); $e->setResult($dispatchResult); $routeMatch = new RouteMatch([]); $routeMatch->setMatchedRouteName('page'); $e->setRouteMatch($routeMatch); $application = new Application([], $serviceLocator); $e->setApplication($application); ///////////////////// $viewBuilder = new BuildViewListener(); $viewBuilder->injectLayout($e); $pageViewModel = $e->getViewModel(); $view->render($pageViewModel); $result = $response->getBody(); $expected = '<body><ul><li>text of c1 <div class="user">John<span class="location">Ukraine</span></div></li><li>text of c2 <div class="user">Helen<span class="location">United States</span></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></body>'; $this->assertEquals($expected, $result); }
/** * Get mock backend manager * * @return BackendManager */ protected function getBackendManagerWithMockSolr() { $sm = new \Zend\ServiceManager\ServiceManager(); $pm = new BackendManager($sm); $mockBackend = $this->getMockBuilder('VuFindSearch\\Backend\\Solr\\Backend')->disableOriginalConstructor()->getMock(); $mockConnector = $this->getMockBuilder('VuFindSearch\\Backend\\Solr\\Connector')->disableOriginalConstructor()->setMethods(['getUrl', 'getTimeout', 'setTimeout', 'write'])->getMock(); $mockBackend->expects($this->any())->method('getConnector')->will($this->returnValue($mockConnector)); $mockConnector->expects($this->any())->method('getTimeout')->will($this->returnValue(30)); $mockConnector->expects($this->any())->method('getUrl')->will($this->returnValue('http://localhost:8080/solr/biblio')); $sm->setService('Solr', $mockBackend); return $pm; }
public function setup() { $sm = new \Zend\ServiceManager\ServiceManager(); $sm->setService('MongoDb', new MongoDbMock()); $this->_sm = $sm; }
<?php /* Check PHP */ if (version_compare(PHP_VERSION, '5.4.0') < 0) { throw new RuntimeException('PHP 5.4+ is required (currently running PHP ' . PHP_VERSION . ')'); } /* Setup PHP */ ini_set('error_reporting', E_ALL | E_STRICT); ini_set('default_charset', 'UTF-8'); ini_set('display_errors', 1); ini_set('display_startup_errors', 1); ini_set('log_errors', 0); ini_set('date.timezone', 'Europe/Berlin'); chdir(dirname(dirname(dirname(__DIR__)))); /* Setup constants */ define('EP3_BS_DEV', true); /* Setup autoloader */ $autoloaderFile = 'vendor/autoload.php'; if (!is_readable($autoloaderFile)) { throw new RuntimeException('Composer autoloader is required.'); } $autoloader = (require $autoloaderFile); /* Setup modules */ $moduleConfig = array('module_listener_options' => array('module_paths' => array('module', 'vendor')), 'modules' => array('User')); $serviceManager = new Zend\ServiceManager\ServiceManager(new Zend\Mvc\Service\ServiceManagerConfig()); $serviceManager->setService('ApplicationConfig', $moduleConfig); $serviceManager->get('ModuleManager')->loadModules();
/** * Get new model instance via service manager * * This method allows temporarily overriding services with manually supplied * instances. This is useful for injecting mock objects which will be passed * to the model's constructor by a factory. A clone of the service manager is * used to avoid interference with other tests. * * @param array $overrideServices Optional associative array (name => instance) with services to override * @return object Model instance */ protected function _getModel(array $overrideServices = array()) { if (empty($overrideServices)) { $serviceManager = static::$serviceManager; } else { // Create temporary service manager with identical configuration. $config = static::$serviceManager->get('config'); $serviceManager = new \Zend\ServiceManager\ServiceManager($config['service_manager']); // Clone 'config' service $serviceManager->setService('config', $config); // If not explicitly overridden, copy database services to avoid // expensive reconnect or table setup which has already been done. if (!isset($overrideServices['Db'])) { $serviceManager->setService('Db', static::$serviceManager->get('Db')); } if (!isset($overrideServices['Database\\Nada'])) { $serviceManager->setService('Database\\Nada', static::$serviceManager->get('Database\\Nada')); } // Override specified services foreach ($overrideServices as $name => $service) { $serviceManager->setService($name, $service); } } // Always build a new instance. return $serviceManager->build($this->_getClass()); }
<?php /** * Sample Zend Expressive Application for php[architect] * * This file is subject to the terms and conditions defined in * file 'LICENSE', which is part of this source code package. * * @link https://github.com/dragonmantank/phparch-zend-expressive for the canonical source repository * @copyright Copyright (c) 2015 Chris Tankersley * @license See LICENSE */ chdir(dirname(__DIR__)); require 'vendor/autoload.php'; $config = (require 'config.php'); $serviceManagerConfig = new Zend\ServiceManager\Config($config['dependencies']); $container = new Zend\ServiceManager\ServiceManager($serviceManagerConfig); $container->setService('config', $config); $app = $container->get('Zend\\Expressive\\Application'); $app->run();
/** * Bootstrap the tests. * * @return void */ public static function init() { // Module root directory path. $modulePath = dirname(__DIR__); // Module tests directory path. $testPath = __DIR__; // Application configuration. $configuration = []; // If a developer configuration file exists. if (is_file($configurationFile = $testPath . '/Configuration.php')) { // Retrieve the developer configuration. $configuration = (include $configurationFile); } else { if (is_file($configurationFile = $testPath . '/Configuration.php.dist')) { // Retrieve the distributed configuration. $configuration = (include $configurationFile); } else { // We cannot continue without a configuration file. throw new RuntimeException('Unable to locate a configuration file.'); } } // If a module autoload file exists. if (is_file($autoloadFile = $modulePath . '/vendor/autoload.php')) { // Retrieve the module autoloader $loader = (include $autoloadFile); } else { if (is_file($autoloadFile = $modulePath . '/../../vendor/autoload.php')) { // Retrieve the application autoloader. $loader = (include $autoloadFile); } else { // We cannot continue without a composer autoload file. throw new RuntimeException('Unable to locate a composer autoload file.'); } } // If additional autoload options are defined. if (isset($configuration['autoload'])) { // Define the additional autoload types and corresponding methods. $autoloadTypes = ['psr-0' => 'add', 'psr-4' => 'addPsr4']; // Iterate over each of the autoload types. foreach ($autoloadTypes as $type => $method) { // If the autoload type is defined in the application configuration. if (isset($configuration['autoload'][$type])) { // Iterate over each of the autoload type options. foreach ($configuration['autoload'][$type] as $name => $paths) { // Cast the path to an array encase a single path is given. $paths = (array) $paths; // Iterate over each of the paths. foreach ($paths as $index => $path) { // Set the path as the parent directory's absolute path. $paths[$index] = dirname($path); } // Register a set of paths for a given name. $loader->{$method}($name, $paths); } } } } // If we are unable to locate the Zend MVC Application class. if (!class_exists('Zend\\Mvc\\Application')) { // We cannot continue without the dependencies being loaded. throw new RuntimeException('Unable to load the Zend framework. Install dependencies using Composer.'); } // Create a service manager config object. $serviceManagerConfig = new Zend\Mvc\Service\ServiceManagerConfig(); // Create a server manager object and inject the configuration. $serviceManager = new Zend\ServiceManager\ServiceManager($serviceManagerConfig); // Set the configuration used when initializing the application. $serviceManager->setService('ApplicationConfig', $configuration); // Retrieve the module manager. $moduleManager = $serviceManager->get('ModuleManager'); // Load available modules. $moduleManager->loadModules(); // If a test case class is defined. if (isset($configuration['module_test_case_class'])) { // Retrieve the test case class. $moduleTestCaseClass = $configuration['module_test_case_class']; // If the test case class has a method named setServiceManager. if (method_exists($moduleTestCaseClass, 'setConfiguration')) { // Pass the configuration to the test case class. call_user_func_array($moduleTestCaseClass . '::setConfiguration', array($configuration)); } // If the test case class has a method named setServiceManager. if (method_exists($moduleTestCaseClass, 'setServiceManager')) { // Pass an instance of the service manager to the test case class. call_user_func_array($moduleTestCaseClass . '::setServiceManager', array($serviceManager)); } } }