Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Feature/zend test load module #3942

Closed
wants to merge 9 commits into from
86 changes: 86 additions & 0 deletions library/Zend/Test/Util/ModuleLoader.php
@@ -0,0 +1,86 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Test\Util;

use Zend\Mvc\Service;
use Zend\ServiceManager\ServiceManager;

class ModuleLoader
{
/**
* @var ServiceManager
*/
protected $serviceManager;

/**
* Load list of modules or application configuration
* @param array $modules
*/
public function __construct(array $configuration)
{
if (!isset($configuration['modules'])) {
$modules = $configuration;
$configuration = array(
'module_listener_options' => array(
'module_paths' => array(),
),
'modules' => array(),
);
foreach ($modules as $key => $module) {
if (is_numeric($key)) {
$configuration['modules'][] = $module;
continue;
}
$configuration['modules'][] = $key;
$configuration['module_listener_options']['module_paths'][$key] = $module;
}
}

$smConfig = isset($configuration['service_manager']) ? $configuration['service_manager'] : array();
$this->serviceManager = new ServiceManager(new Service\ServiceManagerConfig($smConfig));
$this->serviceManager->setService('ApplicationConfig', $configuration);
$this->serviceManager->get('ModuleManager')->loadModules();
}

/**
* Get the application
* @return Zend\Mvc\Application
*/
public function getApplication()
{
return $this->getServiceManager()->get('Application');
}

/**
* Get the module manager
* @return Zend\ModuleManager\ModuleManager
*/
public function getModuleManager()
{
return $this->getServiceManager()->get('ModuleManager');
}

/**
* Get module
* @return mixed
*/
public function getModule($moduleName)
{
return $this->getModuleManager()->getModule($moduleName);
}

/**
* Get the service manager
* @var ServiceManager
*/
public function getServiceManager()
{
return $this->serviceManager;
}
}
86 changes: 86 additions & 0 deletions tests/ZendTest/Test/PHPUnit/Util/ModuleLoaderTest.php
@@ -0,0 +1,86 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace ZendTest\Test\PHPUnit\Util;

use PHPUnit_Framework_TestCase;
use Zend\Test\Util\ModuleLoader;

class ModuleLoaderTest extends PHPUnit_Framework_TestCase
{
public function testCanLoadModule()
{
require_once __DIR__ . '/../../_files/Baz/Module.php';

$loader = new ModuleLoader(array('Baz'));
$baz = $loader->getModule('Baz');
$this->assertTrue($baz instanceof \Baz\Module);
}

public function testCanNotLoadModule()
{
$this->setExpectedException('Zend\ModuleManager\Exception\RuntimeException', 'could not be initialized');
$loader = new ModuleLoader(array('FooBaz'));
}

public function testCanLoadModuleWithPath()
{
$loader = new ModuleLoader(array('Baz' => __DIR__ . '/../../_files/Baz'));
$baz = $loader->getModule('Baz');
$this->assertTrue($baz instanceof \Baz\Module);
}

public function testCanLoadModules()
{
require_once __DIR__ . '/../../_files/Baz/Module.php';
require_once __DIR__ . '/../../_files/modules-path/with-subdir/Foo/Module.php';

$loader = new ModuleLoader(array('Baz', 'Foo'));
$baz = $loader->getModule('Baz');
$this->assertTrue($baz instanceof \Baz\Module);
$foo = $loader->getModule('Foo');
$this->assertTrue($foo instanceof \Foo\Module);
}

public function testCanLoadModulesWithPath()
{
$loader = new ModuleLoader(array(
'Baz' => __DIR__ . '/../../_files/Baz',
'Foo' => __DIR__ . '/../../_files/modules-path/with-subdir/Foo',
));

$fooObject = $loader->getServiceManager()->get('FooObject');
$this->assertTrue($fooObject instanceof \stdClass);
}

public function testCanLoadModulesFromConfig()
{
$config = include __DIR__ . '/../../_files/application.config.php';
$loader = new ModuleLoader($config);
$baz = $loader->getModule('Baz');
$this->assertTrue($baz instanceof \Baz\Module);
}

public function testCanGetService()
{
$loader = new ModuleLoader(array('Baz' => __DIR__ . '/../../_files/Baz'));

$this->assertInstanceOf(
'Zend\ServiceManager\ServiceLocatorInterface',
$loader->getServiceManager()
);
$this->assertInstanceOf(
'Zend\ModuleManager\ModuleManager',
$loader->getModuleManager()
);
$this->assertInstanceOf(
'Zend\Mvc\ApplicationInterface',
$loader->getApplication()
);
}
}
3 changes: 2 additions & 1 deletion tests/ZendTest/Test/_files/application.config.php
@@ -1,11 +1,12 @@
<?php <?php

return array( return array(
'modules' => array( 'modules' => array(
'Baz', 'Baz',
), ),
'module_listener_options' => array( 'module_listener_options' => array(
'config_cache_enabled' => true, 'config_cache_enabled' => true,
'cache_dir' => __DIR__ . '/cache', 'cache_dir' => sys_get_temp_dir(),
'config_cache_key' => 'phpunit', 'config_cache_key' => 'phpunit',
'config_static_paths' => array(), 'config_static_paths' => array(),
'module_paths' => array( 'module_paths' => array(
Expand Down