modified alutoload classes for tests isolation

This commit is contained in:
Anton Grebnev
2011-10-28 15:51:49 +04:00
parent b736d854c5
commit cc51e7425e
2 changed files with 118 additions and 56 deletions

View File

@ -5,7 +5,7 @@
* @link http://netmonsters.ru
* @package Majestic
* @subpackage UnitTests
* @since 2011-10-..
* @since 2011-10-28
*
* Unit tests for AutoloadBuilder class
*/
@ -19,8 +19,19 @@ class AutoloadBuilderTest extends PHPUnit_Framework_TestCase
{
private static $inc_dirs = array();
private static $file;
private static $path;
private static $app;
public function run(PHPUnit_Framework_TestResult $result = NULL)
{
$this->setPreserveGlobalState(false);
return parent::run($result);
}
/**
* @TODO: Load->buildAutoload() - uses two paths - PATH . '/' . APP . '/src' and PATH . '/lib' those are not checked. Can couse error.
* @expectedException UnexpectedValueException
@ -28,21 +39,14 @@ class AutoloadBuilderTest extends PHPUnit_Framework_TestCase
*/
public static function setUpBeforeClass()
{
if (!defined('PATH')) {
define('PATH', realpath(dirname(__FILE__) . '/../../../..'));
}
self::$path = realpath(dirname(__FILE__) . '/../../../..');
self::$app = 'lib/core/tests/face';
// default application name
if (!defined('APP')) {
define('APP', 'lib/core/tests/face');
}
self::$file = self::$path . '/' . self::$app . '/cache/autoload.php';
self::$file = PATH . '/' . APP . '/cache/autoload.php';
// value - if dir exists (to delete it if it was created), default true
self::$inc_dirs[PATH . '/' . APP . '/src'] = true;
self::$inc_dirs[PATH . '/' . APP . '/cache'] = true;
self::$inc_dirs[PATH . '/lib'] = true;
self::$inc_dirs[self::$path . '/' . self::$app . '/src'] = true;
self::$inc_dirs[self::$path . '/' . self::$app . '/cache'] = true;
self::$inc_dirs[self::$path . '/lib'] = true;
foreach (self::$inc_dirs as $dir => &$is_exist) {
if (!file_exists($dir)) {
@ -51,54 +55,62 @@ class AutoloadBuilderTest extends PHPUnit_Framework_TestCase
}
}
}
/**
* @TODO: AutoloadBuilder - check input params: string for filename, array for dirs
* @expectedException PHPUnit_Framework_Error
* @runInSeparateProcess
*/
public function testBuildParams()
{
$this->setConstants();
$autoload = self::$file;
$dirs = 'string';
$builder = new AutoloadBuilder($autoload, $dirs);
$builder->build();
$builder->build();
}
/**
* @runInSeparateProcess
*/
public function testBuild()
{
$builder = new AutoloadBuilder(self::$file, array(PATH . '/' . APP . '/src', PATH . '/' . APP . '/cache', PATH . '/lib'));
$this->setConstants();
$builder = new AutoloadBuilder(self::$file, array(self::$path . '/' . self::$app . '/src', self::$path . '/' . self::$app . '/cache', self::$path . '/lib'));
$builder->build();
$builder->build();
$this->assertFileExists(self::$file);
}
public function testAutoloadFileExists()
{
$this->assertFileExists(self::$file);
}
public function testAutoloadArray()
{
$this->assertFileExists(self::$file);
$array = require self::$file;
$this->assertInternalType('array', $array);
$this->assertNotEmpty($array);
}
/**
/**
* @expectedException PHPUnit_Framework_Error
* @runInSeparateProcess
*/
public function testAccessDenied()
{
$this->setConstants();
chmod(self::$file, 0400);
$builder = new AutoloadBuilder(self::$file, array(PATH . '/' . APP . '/src', PATH . '/' . APP . '/cache', PATH . '/lib'));
$builder = new AutoloadBuilder(self::$file, array(self::$path . '/' . self::$app . '/src', self::$path . '/' . self::$app . '/cache', self::$path . '/lib'));
$builder->build();
$builder->build();
chmod(self::$file, 0777);
}
public function tearDown()
{
// if (defined('PATH')) {
// echo PHP_EOL . __CLASS__ . ' ' . PATH . PHP_EOL;
// } else {
// echo PHP_EOL . __CLASS__ . ' ' . 'PATH NOT DEFINED' . PHP_EOL;
// }
}
public static function tearDownAfterClass()
{
if (file_exists(self::$file)) {
@ -107,10 +119,37 @@ class AutoloadBuilderTest extends PHPUnit_Framework_TestCase
foreach (self::$inc_dirs as $dir => $is_exist) {
if (!$is_exist) {
rmdir($dir);
self::rrmdir($dir);
}
}
rmdir(PATH . '/' . APP);
self::rrmdir(self::$path . '/' . self::$app);
}
private function setConstants()
{
if (!defined('PATH')) {
define('PATH', realpath(dirname(__FILE__) . '/../../../..'));
}
if (!defined('APP')) {
define('APP', 'lib/core/tests/face');
}
}
public static function rrmdir($dir)
{
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
if (filetype($dir . "/" . $object) == "dir") {
self::rrmdir($dir . "/" . $object);
} else {
unlink($dir . "/" . $object);
}
}
}
reset($objects);
rmdir($dir);
}
}
}