forked from zendframework/zendframework
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/3942' into develop
Close zendframework#3942
- Loading branch information
Showing
4 changed files
with
236 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
<?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 tearDownCacheDir() | ||
{ | ||
$cacheDir = sys_get_temp_dir() . '/zf2-module-test'; | ||
if (is_dir($cacheDir)) { | ||
static::rmdir($cacheDir); | ||
} | ||
} | ||
|
||
public static function rmdir($dir) | ||
{ | ||
$files = array_diff(scandir($dir), array('.','..')); | ||
foreach ($files as $file) { | ||
(is_dir("$dir/$file")) ? static::rmdir("$dir/$file") : unlink("$dir/$file"); | ||
} | ||
return rmdir($dir); | ||
} | ||
|
||
public function setUp() | ||
{ | ||
$this->tearDownCacheDir(); | ||
} | ||
|
||
public function tearDown() | ||
{ | ||
$this->tearDownCacheDir(); | ||
} | ||
|
||
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() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters