You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
4.9 KiB

<?php
/*
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage UnitTests
* @since 2011-10-..
*
* Unit tests for Load class
*/
require dirname(__FILE__) . '/../Load.php';
require_once 'vfsStream/vfsStream.php';
class LoadTest extends PHPUnit_Framework_TestCase
{
private static $inc_dirs = array();
private static $file;
private $root;
public static $file_contents;
public static $autoload_array;
/**
* @TODO: Load->buildAutoload() should recieve AutoloadBuilder as a parameter
* @TODO: Load->buildAutoload() - uses two paths - PATH . '/' . APP . '/src' and PATH . '/lib' those are not checked. Can cause error.
*/
public function setUp()
{
self::$file_contents = '<?php return array("Db" => "/lib/core/model/Db.php", "RedisDebug" => "/lib/core/redis/RedisDebug.php"); ?>';
self::$autoload_array = array('Db' => '/lib/core/model/Db.php', 'RedisDebug' => '/lib/core/redis/RedisDebug.php');
vfsStreamWrapper::register();
$this->root = vfsStream::create(
array(
'lib' => array(
'core' =>
array(
'util' => array(
'AutoloadBuilder.php' => ''
),
'model' => array(
'Db.php' => '',
),
'redis' => array(
'RedisDebug.php' => ''
),
'Registry.php' => '',
'Load.php' => '',
'devel.config' => ' development config file'
)
),
'autoload.php' => self::$file_contents
)
);
if (!class_exists('AutoloadBuilder')) {
$this->getMock('AutoloadBuilder');
}
if (!defined('PATH')) {
//define('PATH', realpath(dirname(__FILE__) . '/../../../'));
define('PATH', vfsStream::url('root'));
}
if (!defined('APP')) {
define('APP', 'lib/core/tests/face');
}
vfsStreamWrapper::setRoot($this->root);
self::$file = vfsStream::url('root/autoload.php');
set_new_overload(array($this, 'newCallback'));
}
public function testSetAutoLoadFromExistingFile()
{
Load::setAutoloadFrom(self::$file);
$autoload = require(self::$file);
$this->assertEquals(self::$autoload_array, $autoload);
Load::autoload('Db');
}
public function testAutoloadFromNonExistingFile()
{
$this->assertTrue($this->root->removeChild('autoload.php'));
$this->assertFileNotExists(self::$file);
Load::setAutoloadFrom(self::$file);
$autoload = require(self::$file);
$this->assertEquals(self::$autoload_array, $autoload);
}
/**
* @TODO: Load::autoload() needs self::$autoload = require(self::$file); after self::buildAutoload();
*/
public function testDebugAutoload()
{
$this->setUp();
Load::setAutoloadFrom(self::$file);
$autoload = require(self::$file);
$this->assertNotEmpty($autoload);
if (!defined('DEBUG')) {
define('DEBUG', true);
}
Load::autoload('Some');
Load::autoload('RedisDebug');
}
public function testAutoloadArrayExists()
{
$this->assertFileExists(self::$file);
}
/**
* @TODO: Load - check if input file returns array
*/
public function testFileForArray()
{
$autoload = require(self::$file);
$this->assertInternalType('array', $autoload);
}
public function testAutoloadArrayNotEmpty()
{
$autoload = require(self::$file);
$this->assertNotEmpty($autoload);
$this->assertArrayHasKey('Db', $autoload);
}
public function testAutoloadGetFilePath()
{
$autoload = require(self::$file);
$this->assertNotEmpty(Load::getFilePath('RedisDebug'));
}
/**
* @TODO: Load::getFilePath - check for wrong index
* @expectedException PHPUnit_Framework_Error
*/
public function testAutoloadGetFilePathNullIndex()
{
$autoload = require(self::$file);
$this->assertNotEmpty(Load::getFilePath('anton'));
}
protected function newCallback($className)
{
switch ($className) {
case 'AutoloadBuilder':
return 'AutoloadBuilderMock';
default:
return $className;
}
}
public function tearDown()
{
unset_new_overload();
}
}
class AutoloadBuilderMock
{
public function build()
{
$file = new vfsStreamFile('autoload.php');
$file->setContent(LoadTest::$file_contents);
vfsStreamWrapper::getRoot()->addChild($file);
}
}