modified Load and AutoloadBuilder tests for vfs
This commit is contained in:
@ -11,72 +11,111 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
require dirname(__FILE__) . '/../Load.php';
|
require dirname(__FILE__) . '/../Load.php';
|
||||||
|
require_once 'vfsStream/vfsStream.php';
|
||||||
|
|
||||||
class LoadTest extends PHPUnit_Framework_TestCase
|
class LoadTest extends PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
|
|
||||||
private static $inc_dirs = array();
|
private static $inc_dirs = array();
|
||||||
|
|
||||||
private static $file;
|
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() should recieve AutoloadBuilder as a parameter
|
||||||
* @TODO: Load->buildAutoload() - uses two paths - PATH . '/' . APP . '/src' and PATH . '/lib' those are not checked. Can couse error.
|
* @TODO: Load->buildAutoload() - uses two paths - PATH . '/' . APP . '/src' and PATH . '/lib' those are not checked. Can cause error.
|
||||||
* @expectedException UnexpectedValueException
|
|
||||||
* @expectedException PHPUnit_Framework_Error
|
|
||||||
*/
|
*/
|
||||||
public static function setUpBeforeClass()
|
public function setUp()
|
||||||
{
|
{
|
||||||
if (!defined('PATH')) {
|
self::$file_contents = '<?php return array("Db" => "/lib/core/model/Db.php", "RedisDebug" => "/lib/core/redis/RedisDebug.php"); ?>';
|
||||||
define('PATH', realpath(dirname(__FILE__) . '/../../..'));
|
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');
|
||||||
}
|
}
|
||||||
|
|
||||||
// default application name
|
if (!defined('PATH')) {
|
||||||
|
//define('PATH', realpath(dirname(__FILE__) . '/../../../'));
|
||||||
|
define('PATH', vfsStream::url('root'));
|
||||||
|
}
|
||||||
if (!defined('APP')) {
|
if (!defined('APP')) {
|
||||||
define('APP', 'lib/core/tests/face');
|
define('APP', 'lib/core/tests/face');
|
||||||
}
|
}
|
||||||
|
|
||||||
self::$file = PATH . '/' . APP . '/cache/autoload.php';
|
vfsStreamWrapper::setRoot($this->root);
|
||||||
|
|
||||||
// value - if dir exists (to delete it if it was created), default true
|
self::$file = vfsStream::url('root/autoload.php');
|
||||||
self::$inc_dirs[PATH . '/' . APP . '/src'] = true;
|
|
||||||
self::$inc_dirs[PATH . '/' . APP . '/cache'] = true;
|
|
||||||
self::$inc_dirs[PATH . '/lib'] = true;
|
|
||||||
|
|
||||||
foreach (self::$inc_dirs as $dir => &$is_exist) {
|
set_new_overload(array($this, 'newCallback'));
|
||||||
if (!file_exists($dir)) {
|
|
||||||
$is_exist = false;
|
|
||||||
mkdir($dir, 0777, true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSetAutoLoadFrom()
|
public function testSetAutoLoadFromExistingFile()
|
||||||
{
|
{
|
||||||
// autoload
|
|
||||||
Load::setAutoloadFrom(self::$file);
|
Load::setAutoloadFrom(self::$file);
|
||||||
|
|
||||||
$autoload = require(self::$file);
|
$autoload = require(self::$file);
|
||||||
$this->assertNotEmpty($autoload);
|
$this->assertEquals(self::$autoload_array, $autoload);
|
||||||
Load::autoload('Action');
|
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();
|
* @TODO: Load::autoload() needs self::$autoload = require(self::$file); after self::buildAutoload();
|
||||||
*/
|
*/
|
||||||
public function testDebugAutoload()
|
public function testDebugAutoload()
|
||||||
{
|
{
|
||||||
|
$this->setUp();
|
||||||
Load::setAutoloadFrom(self::$file);
|
Load::setAutoloadFrom(self::$file);
|
||||||
|
|
||||||
$autoload = require(self::$file);
|
$autoload = require(self::$file);
|
||||||
$this->assertNotEmpty($autoload);
|
$this->assertNotEmpty($autoload);
|
||||||
|
|
||||||
if(!defined('DEBUG')) {
|
if (!defined('DEBUG')) {
|
||||||
define('DEBUG', true);
|
define('DEBUG', true);
|
||||||
}
|
}
|
||||||
Load::autoload('Func');
|
Load::autoload('Some');
|
||||||
Load::autoload('Captcha');
|
Load::autoload('RedisDebug');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function testAutoloadArrayExists()
|
public function testAutoloadArrayExists()
|
||||||
{
|
{
|
||||||
$this->assertFileExists(self::$file);
|
$this->assertFileExists(self::$file);
|
||||||
@ -95,13 +134,13 @@ class LoadTest extends PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
$autoload = require(self::$file);
|
$autoload = require(self::$file);
|
||||||
$this->assertNotEmpty($autoload);
|
$this->assertNotEmpty($autoload);
|
||||||
$this->assertArrayHasKey('Action', $autoload);
|
$this->assertArrayHasKey('Db', $autoload);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testAutoloadGetFilePath()
|
public function testAutoloadGetFilePath()
|
||||||
{
|
{
|
||||||
$autoload = require(self::$file);
|
$autoload = require(self::$file);
|
||||||
$this->assertNotEmpty(Load::getFilePath('Registry'));
|
$this->assertNotEmpty(Load::getFilePath('RedisDebug'));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -114,18 +153,28 @@ class LoadTest extends PHPUnit_Framework_TestCase
|
|||||||
$this->assertNotEmpty(Load::getFilePath('anton'));
|
$this->assertNotEmpty(Load::getFilePath('anton'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function tearDownAfterClass()
|
protected function newCallback($className)
|
||||||
{
|
{
|
||||||
if (file_exists(self::$file)) {
|
switch ($className) {
|
||||||
unlink(self::$file);
|
case 'AutoloadBuilder':
|
||||||
|
return 'AutoloadBuilderMock';
|
||||||
|
default:
|
||||||
|
return $className;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (self::$inc_dirs as $dir => $is_exist) {
|
|
||||||
if (!$is_exist) {
|
|
||||||
rmdir($dir);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
rmdir(PATH . '/' . APP);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user