php - ZF2 Unit Test Can't Find Service -


i'm unit testing zf2 controller, below test class:

<?php class restcontrollertest extends abstracthttpcontrollertestcase {     public function setup()     {         bootstrap::init();         $this->setapplicationconfig(bootstrap::getconfig());         parent::setup();     }      public function testcreate()     {         $paramsarr = array(/** params here **/);         $this->dispatch('/transcode', 'post', $paramsarr);         $this->assertresponsestatuscode(204);         $this->assertcontrollername('transcode\controller\rest');         $this->assertmatchedroutename('transcode');     } } 

my unit test failing, indicating servicenotfoundexception on logger.transcode, attached eventmanager onbootstrap of transcode module:

public function onbootstrap(mvcevent $e)     {         $eventmanager   = $e->getapplication()->geteventmanager();         $app            = $e->gettarget();         $servicemanager = $app->getservicemanager();         $eventmanager->attach($servicemanager->get('transcode\listener'));     } 

transcode\listener instantiated via factory:

public function createservice(servicelocatorinterface $servicelocator) {     $logger = $servicelocator->get('logger.transcode');     $service = new transcodelistener($logger);     return $service; } 

and finally, logger.transcode set via config file creates logger.transcode abstract factory in enlitemonolog module.

any idea why unit test can't attach listener workflow?

per request in comments, bootstrap file below, although it's standard zf2 test bootstrap file:

<?php  namespace transcodetest;  use zend\loader\autoloaderfactory; use zend\mvc\service\servicemanagerconfig; use zend\servicemanager\servicemanager; use zend\stdlib\arrayutils; use runtimeexception; use dotenv;  error_reporting(e_all | e_strict); chdir(__dir__);  class bootstrap {     protected static $servicemanager;     protected static $config;     protected static $bootstrap;      public static function init()     {         // load user-defined test configuration file, if exists; otherwise, load         if (is_readable(__dir__ . '/testconfiguration.php')) {             $testconfig = include __dir__ . '/testconfiguration.php';         }          $zf2modulepaths = array();          if (isset($testconfig['module_listener_options']['module_paths'])) {             $modulepaths = $testconfig['module_listener_options']['module_paths'];             foreach ($modulepaths $modulepath) {                 if (($path = static::findparentpath($modulepath)) ) {                     $zf2modulepaths[] = $path;                 }             }         }          $zf2modulepaths  = implode(path_separator, $zf2modulepaths) . path_separator;         $zf2modulepaths .= getenv('zf2_modules_test_paths') ?: (defined('zf2_modules_test_paths') ? zf2_modules_test_paths : '');          static::initautoloader();          // use modulemanager load module , it's dependencies         $baseconfig = array(             'module_listener_options' => array(                 'module_paths' => explode(path_separator, $zf2modulepaths),             ),         );          $config = arrayutils::merge($baseconfig, $testconfig);          $servicemanager = new servicemanager(new servicemanagerconfig());         $servicemanager->setservice('applicationconfig', $config);         $servicemanager->get('modulemanager')->loadmodules();          static::$servicemanager = $servicemanager;         static::$config = $config;     }      public static function getservicemanager()     {         return static::$servicemanager;     }      public static function getconfig()     {         return static::$config;     }      protected static function initautoloader()     {         $vendorpath = static::findparentpath('vendor');          if (is_readable($vendorpath . '/autoload.php')) {             $loader = include $vendorpath . '/autoload.php';         } else {             $zf2path = getenv('zf2_path') ?: (defined('zf2_path') ? zf2_path : (is_dir($vendorpath . '/zf2/library') ? $vendorpath . '/zf2/library' : false));              if (!$zf2path) {                 throw new runtimeexception('unable load zf2. run `php composer.phar install` or define zf2_path environment variable.');             }              include $zf2path . '/zend/loader/autoloaderfactory.php';          }          autoloaderfactory::factory(array(             'zend\loader\standardautoloader' => array(                 'autoregister_zf' => true,                 'namespaces' => array(                     __namespace__ => __dir__ . '/' . __namespace__,                 ),             ),         ));          if (getenv('application_env') != 'production') {             dotenv::load('../../../config');         }     }      protected static function findparentpath($path)     {         $dir = __dir__;         $previousdir = '.';         while (!is_dir($dir . '/' . $path)) {             $dir = dirname($dir);             if ($previousdir === $dir) return false;             $previousdir = $dir;         }         return $dir . '/' . $path;     } }  bootstrap::init(); 

embarrassingly, seems forgot add 'enlitemonolog' testconfiguration.php file. once added module test ran expected. seems can, in fact @ code long

<?php return array( 'modules' => array(     'transcode',     /** other modules needed **/     'enlitemonolog', ), 'module_listener_options' => array(     'config_glob_paths'    => array(         '../../../config/autoload/{,*.}{global,local}.php',     ),     'module_paths' => array(         'module',         'vendor',     ), ),); 

Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -