1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130:
<?php
namespace Nethgui\Test\Unit\Nethgui\Module;
class ModuleLoaderTest extends \PHPUnit_Framework_TestCase
{
protected $object;
protected function setUp()
{
spl_autoload_register(array($this, 'autoloader'));
$namespaceMap = new \ArrayObject(array(
'Vendor00' => '/usr/local/share',
'Vendor1B' => '/home/vendor1',
'Vendor1A' => '/home/vendor1',
));
$gfw = $this->getMockBuilder('Nethgui\Utility\PhpWrapper')
->disableOriginalConstructor()
->setMethods(array('scandir'))
->getMock();
$callback = function($fullPath) {
$directoryContents = array(
'/usr/local/share/Vendor00/Module' => array('TestModule1.php', 'README', 'NIL'),
'/home/vendor1/Vendor1B/Module' => array('TestModule1.php', 'TestModule2.php'),
'/home/vendor1/Vendor1A/Module' => array('TestModule3.php', 'TestModule4.php'),
);
return $directoryContents[$fullPath];
};
$gfw->expects($this->any())->method('scandir')->will($this->returnCallback($callback));
$di = $this->getMockBuilder('Nethgui\Component\DependencyInjector')->getMock();
$this->object = new \Nethgui\Module\ModuleLoader($di);
foreach ($namespaceMap as $nsPrefix => $nsRoot) {
$this->object->setNamespace($nsPrefix . '\\Module', $nsRoot);
}
$this->object->setPhpWrapper($gfw);
}
public function autoloader($className)
{
$parts = explode('\\', $className);
$rootNs = $parts[0];
$unqClass = $parts[2];
if (substr($rootNs, 0, 6) == 'Vendor' && substr($unqClass, 0, 10) == 'TestModule') {
eval("namespace ${rootNs}\\Module;\n class ${unqClass} extends \Nethgui\Controller\AbstractController {}");
}
}
public function testGetIterator1()
{
$moduleIdentifier = NULL;
foreach ($this->object as $moduleIdentifier => $moduleInstance) {
$this->assertInstanceOf('Nethgui\Module\ModuleInterface', $moduleInstance);
$this->assertEquals($moduleIdentifier, $moduleInstance->getIdentifier());
}
$this->assertEquals('TestModule4', $moduleIdentifier);
}
public function testGetIterator2()
{
$moduleMap = iterator_to_array($this->object, TRUE);
$this->assertEquals(array('TestModule1', 'TestModule2', 'TestModule3', 'TestModule4'), array_keys($moduleMap));
}
public function testGetIterator3()
{
$gfw = $this->getMockBuilder('Nethgui\Utility\PhpWrapper')
->disableOriginalConstructor()
->setMethods(array('scandir'))
->getMock();
$gfw->expects($this->any())->method('scandir')->will($this->returnValue(FALSE));
$di = $this->getMockBuilder('Nethgui\Component\DependencyInjector')->getMock();
$object = new \Nethgui\Module\ModuleLoader($di);
$object->setPhpWrapper($gfw);
try {
$object->getIterator();
} catch (\Exception $ex) {
$this->assertInstanceOf('UnexpectedValueException', $ex);
$this->assertEquals(1322649822, $ex->getCode());
}
}
public function testGetModule1()
{
$this->assertInstanceOf('Nethgui\Module\ModuleInterface', $this->object->getModule('TestModule1'));
}
public function testGetModule2()
{
try {
$this->object->getModule('NullModule');
} catch (\Exception $ex) {
$this->assertInstanceOf('RuntimeException', $ex);
$this->assertEquals(1322231262, $ex->getCode());
}
}
public function testGetModule3()
{
for ($i = 1; $i <= 4; $i ++ ) {
$identifier = sprintf('TestModule%d', $i);
$this->assertSame($this->object->getModule($identifier), $this->object->getModule($identifier));
}
}
public function testSetLog()
{
$this->object->setLog(new \Nethgui\Log\Nullog());
}
}