Question

I have created a Zend Framework Module which manages a set of DB and filesystem operations. I have managed to get a 100% Test Coverage (with mocked databases) and I would like to test the integration of this module into a testing environment.

I have a Service class in /modules/MyModule/src/MyModule/Service/MyModuleService.php which is currently not implementing any interfaces. The documentation doesn't hint that this is something I'd need to do.

class MyModuleService
{
    public function doA(){...}
    public function doB(){...}
.....
}

I have not changed anything in the application folder from the default application skeleton. I have coded up Model classes which do a lot of database and filesystem manipulation. The Service class is ultimately triggering these actions. Having done the basic configuration according to the tutorial it seems like ZF doesn't register my classes when I try to access them outside of the ZF filesystem.

//dependencies for auto loader
require_once 'Zend/Loader/AutoloaderFactory.php';
use Zend\Loader\AutoloaderFactory;

//NEW registering the autoloader (as per Tomdarkness' response)
Zend\Loader\AutoloaderFactory::factory(array(
        'Zend\Loader\StandardAutoloader' => array(
            'autoregister_zf' => TRUE,
            'namespaces'      => array(
                'MyModule'=> '/path/to/zendframework/module/src/MyModule',
            )
        )
    )
);

//what factories are auto-loaded?
var_export(AutoloaderFactory::getRegisteredAutoloaders()); 

The var_export now returns the following:

array(
    'Zend\\Loader\\StandardAutoloader' => Zend\Loader\StandardAutoloader::__set_state(
        array('namespaces'             =>
              array('Zend\\'         => '/usr/lib/zendframework/library/Zend/',
                    'MyModule\\'     => '/path/to/zendframework/module/src/MyModule/',
              ), 'prefixes'            => array(),
              'fallbackAutoloaderFlag' => FALSE,
        )
    )
);

In my module.php I do declare the service as follows:

   /* Configure DB Service Managers */
    public function getServiceConfig()
    {
        return array(
            // ...
            'invokables' => array(
                'myModuleService'=> function ($sm){
                    $myModuleTable = $sm->get('MyModuleTable\Model\MyModuleTable');
                    return new MyModuleService($myModuleTable);
                }
            ),
    }

How can I now instantiate a MyModuleService from this autoloaded class?

Was it helpful?

Solution

You call:

var_dump(AutoloaderFactory::getRegisteredAutoloaders()); 

But you've not actually registered any autoloaders. To register the standard autoloader you need to run:

Zend\Loader\AutoloaderFactory::factory(array(
    'Zend\Loader\StandardAutoloader' => array(
        'autoregister_zf' => true
    )
));

You can also register the Classmap autoloader via the factory method if you have generated classmaps.

Note, if you are using composer then you'll want to actually just include vendor/autoload.php in your file, rather than using the AutoloaderFactory. However, remember you need your module namespaces to be registered in the autoload section of composer.json if you use this method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top