Skip to content

Commit

Permalink
Merge branch 'feature/3942' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed Mar 12, 2013
2 parents dfe1dd4 + b319113 commit 35b513b
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 7 deletions.
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;
}
}
Expand Up @@ -24,14 +24,39 @@
*/
class AbstractControllerTestCaseTest extends AbstractHttpControllerTestCase
{
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();
Console::overrideIsConsole(null);
$this->setApplicationConfig(
include __DIR__ . '/../../_files/application.config.php'
);
parent::setUp();
}

public function tearDown()
{
$this->tearDownCacheDir();
parent::tearDown();
}

public function testModuleCacheIsDisabled()
{
$config = $this->getApplicationConfig();
Expand Down Expand Up @@ -64,22 +89,22 @@ public function testApplicationClass()

public function testApplicationClassAndTestRestoredConsoleFlag()
{
$this->assertTrue(Console::isConsole());
$this->assertTrue(Console::isConsole(), '1. Console::isConsole returned false in initial test');
$this->getApplication();
$this->assertFalse(Console::isConsole());
$this->assertFalse(Console::isConsole(), '2. Console::isConsole returned true after retrieving application');
$this->tearDown();
$this->assertTrue(Console::isConsole());
$this->assertTrue(Console::isConsole(), '3. Console::isConsole returned false after tearDown');

Console::overrideIsConsole(false);
parent::setUp();

$this->assertFalse(Console::isConsole());
$this->assertFalse(Console::isConsole(), '4. Console::isConsole returned true after parent::setUp');
$this->getApplication();
$this->assertFalse(Console::isConsole());
$this->assertFalse(Console::isConsole(), '5. Console::isConsole returned true after retrieving application');

parent::tearDown();

$this->assertFalse(Console::isConsole());
$this->assertFalse(Console::isConsole(), '6. Console.isConsole returned true after parent::tearDown');
}

public function testApplicationServiceLocatorClass()
Expand Down
113 changes: 113 additions & 0 deletions tests/ZendTest/Test/PHPUnit/Util/ModuleLoaderTest.php
@@ -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()
);
}
}
7 changes: 6 additions & 1 deletion tests/ZendTest/Test/_files/application.config.php
@@ -1,11 +1,16 @@
<?php
$cacheDir = sys_get_temp_dir() . '/zf2-module-test';
if (!is_dir($cacheDir)) {
mkdir($cacheDir);
}

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

0 comments on commit 35b513b

Please sign in to comment.