Browse Source

modified alutoload classes for tests isolation

master
Anton Grebnev 13 years ago
parent
commit
cc51e7425e
  1. 63
      tests/LoadTest.php
  2. 111
      tests/util/AutoloadBuilderTest.php

63
tests/LoadTest.php

@ -67,14 +67,6 @@ class LoadTest extends PHPUnit_Framework_TestCase
$this->getMock('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); vfsStreamWrapper::setRoot($this->root);
self::$file = vfsStream::url('root/autoload.php'); self::$file = vfsStream::url('root/autoload.php');
@ -82,8 +74,13 @@ class LoadTest extends PHPUnit_Framework_TestCase
set_new_overload(array($this, 'newCallback')); set_new_overload(array($this, 'newCallback'));
} }
/**
* @runInSeparateProcess
*/
public function testSetAutoLoadFromExistingFile() public function testSetAutoLoadFromExistingFile()
{ {
$this->setConstants();
$this->assertFileExists(self::$file);
Load::setAutoloadFrom(self::$file); Load::setAutoloadFrom(self::$file);
$autoload = require(self::$file); $autoload = require(self::$file);
@ -91,8 +88,12 @@ class LoadTest extends PHPUnit_Framework_TestCase
Load::autoload('Db'); Load::autoload('Db');
} }
/**
* @runInSeparateProcess
*/
public function testAutoloadFromNonExistingFile() public function testAutoloadFromNonExistingFile()
{ {
$this->setConstants();
$this->assertTrue($this->root->removeChild('autoload.php')); $this->assertTrue($this->root->removeChild('autoload.php'));
$this->assertFileNotExists(self::$file); $this->assertFileNotExists(self::$file);
@ -123,19 +124,36 @@ class LoadTest extends PHPUnit_Framework_TestCase
$this->assertArrayHasKey('Db', $autoload); $this->assertArrayHasKey('Db', $autoload);
} }
/**
* @runInSeparateProcess
*/
public function testAutoloadGetFilePath() public function testAutoloadGetFilePath()
{ {
$autoload = require(self::$file);
$this->setConstants();
Load::setAutoloadFrom(self::$file);
$this->assertNotEmpty(Load::getFilePath('DbDriver')); $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(); * @TODO: Load::autoload() needs self::$autoload = require(self::$file); after self::buildAutoload();
* @runInSeparateProcess * @runInSeparateProcess
*/ */
public function testDebugAutoload() public function testDebugAutoload()
{ {
$this->setUp();
$this->setConstants();
Load::setAutoloadFrom(self::$file); Load::setAutoloadFrom(self::$file);
$autoload = require(self::$file); $autoload = require(self::$file);
@ -147,16 +165,6 @@ class LoadTest extends PHPUnit_Framework_TestCase
Load::autoload('Some'); Load::autoload('Some');
Load::autoload('DbDriver'); 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) 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() 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(); unset_new_overload();
} }
} }

111
tests/util/AutoloadBuilderTest.php

@ -5,7 +5,7 @@
* @link http://netmonsters.ru * @link http://netmonsters.ru
* @package Majestic * @package Majestic
* @subpackage UnitTests * @subpackage UnitTests
* @since 2011-10-..
* @since 2011-10-28
* *
* Unit tests for AutoloadBuilder class * Unit tests for AutoloadBuilder class
*/ */
@ -19,8 +19,19 @@ class AutoloadBuilderTest extends PHPUnit_Framework_TestCase
{ {
private static $inc_dirs = array(); private static $inc_dirs = array();
private static $file; 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. * @TODO: Load->buildAutoload() - uses two paths - PATH . '/' . APP . '/src' and PATH . '/lib' those are not checked. Can couse error.
* @expectedException UnexpectedValueException * @expectedException UnexpectedValueException
@ -28,21 +39,14 @@ class AutoloadBuilderTest extends PHPUnit_Framework_TestCase
*/ */
public static function setUpBeforeClass() 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) { foreach (self::$inc_dirs as $dir => &$is_exist) {
if (!file_exists($dir)) { 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 * @TODO: AutoloadBuilder - check input params: string for filename, array for dirs
* @expectedException PHPUnit_Framework_Error * @expectedException PHPUnit_Framework_Error
* @runInSeparateProcess
*/ */
public function testBuildParams() public function testBuildParams()
{ {
$this->setConstants();
$autoload = self::$file; $autoload = self::$file;
$dirs = 'string'; $dirs = 'string';
$builder = new AutoloadBuilder($autoload, $dirs); $builder = new AutoloadBuilder($autoload, $dirs);
$builder->build();
$builder->build();
} }
/**
* @runInSeparateProcess
*/
public function testBuild() 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); $this->assertFileExists(self::$file);
}
public function testAutoloadArray()
{
$this->assertFileExists(self::$file);
$array = require self::$file; $array = require self::$file;
$this->assertInternalType('array', $array); $this->assertInternalType('array', $array);
$this->assertNotEmpty($array); $this->assertNotEmpty($array);
} }
/**
/**
* @expectedException PHPUnit_Framework_Error * @expectedException PHPUnit_Framework_Error
* @runInSeparateProcess
*/ */
public function testAccessDenied() public function testAccessDenied()
{ {
$this->setConstants();
chmod(self::$file, 0400); 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); 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() public static function tearDownAfterClass()
{ {
if (file_exists(self::$file)) { if (file_exists(self::$file)) {
@ -107,10 +119,37 @@ class AutoloadBuilderTest extends PHPUnit_Framework_TestCase
foreach (self::$inc_dirs as $dir => $is_exist) { foreach (self::$inc_dirs as $dir => $is_exist) {
if (!$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);
}
}
} }
Loading…
Cancel
Save