From cc51e7425e2b59823f0fec8eab8a48e34a575941 Mon Sep 17 00:00:00 2001 From: Anton Grebnev Date: Fri, 28 Oct 2011 15:51:49 +0400 Subject: [PATCH] modified alutoload classes for tests isolation --- tests/LoadTest.php | 63 ++++++++++++++------- tests/util/AutoloadBuilderTest.php | 111 +++++++++++++++++++++++++------------ 2 files changed, 118 insertions(+), 56 deletions(-) diff --git a/tests/LoadTest.php b/tests/LoadTest.php index b8826b2..fafafae 100644 --- a/tests/LoadTest.php +++ b/tests/LoadTest.php @@ -67,14 +67,6 @@ class LoadTest extends PHPUnit_Framework_TestCase $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'); @@ -82,8 +74,13 @@ class LoadTest extends PHPUnit_Framework_TestCase set_new_overload(array($this, 'newCallback')); } + /** + * @runInSeparateProcess + */ public function testSetAutoLoadFromExistingFile() { + $this->setConstants(); + $this->assertFileExists(self::$file); Load::setAutoloadFrom(self::$file); $autoload = require(self::$file); @@ -91,8 +88,12 @@ class LoadTest extends PHPUnit_Framework_TestCase Load::autoload('Db'); } + /** + * @runInSeparateProcess + */ public function testAutoloadFromNonExistingFile() { + $this->setConstants(); $this->assertTrue($this->root->removeChild('autoload.php')); $this->assertFileNotExists(self::$file); @@ -123,19 +124,36 @@ class LoadTest extends PHPUnit_Framework_TestCase $this->assertArrayHasKey('Db', $autoload); } + /** + * @runInSeparateProcess + */ public function testAutoloadGetFilePath() { - $autoload = require(self::$file); + $this->setConstants(); + Load::setAutoloadFrom(self::$file); $this->assertNotEmpty(Load::getFilePath('DbDriver')); } /** + * @TODO: Load::getFilePath - check for wrong index + * @expectedException PHPUnit_Framework_Error + * @runInSeparateProcess + */ + public function testAutoloadGetFilePathNullIndex() + { + $this->setConstants(); + Load::setAutoloadFrom(self::$file); + $autoload = require(self::$file); + $this->assertNotEmpty(Load::getFilePath('anton')); + } + + /** * @TODO: Load::autoload() needs self::$autoload = require(self::$file); after self::buildAutoload(); * @runInSeparateProcess */ public function testDebugAutoload() { - $this->setUp(); + $this->setConstants(); Load::setAutoloadFrom(self::$file); $autoload = require(self::$file); @@ -147,16 +165,6 @@ class LoadTest extends PHPUnit_Framework_TestCase Load::autoload('Some'); Load::autoload('DbDriver'); } - - /** - * @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) @@ -169,8 +177,23 @@ class LoadTest extends PHPUnit_Framework_TestCase } } + private function setConstants() + { + if (!defined('PATH')) { + define('PATH', vfsStream::url('root')); + } + if (!defined('APP')) { + define('APP', 'lib/core/tests/face'); + } + } + public function tearDown() { +// if (defined('PATH')) { +// echo PHP_EOL . __CLASS__ . ' ' . PATH . PHP_EOL; +// } else { +// echo PHP_EOL . __CLASS__ . ' ' . 'PATH NOT DEFINED' . PHP_EOL; +// } unset_new_overload(); } } diff --git a/tests/util/AutoloadBuilderTest.php b/tests/util/AutoloadBuilderTest.php index 05362db..d24f478 100644 --- a/tests/util/AutoloadBuilderTest.php +++ b/tests/util/AutoloadBuilderTest.php @@ -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); + } + } } \ No newline at end of file