Browse Source

modified alutoload classes for tests isolation

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

63
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);
@ -148,16 +166,6 @@ class LoadTest extends PHPUnit_Framework_TestCase
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();
}
}

93
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 = PATH . '/' . APP . '/cache/autoload.php';
self::$file = self::$path . '/' . self::$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)) {
@ -55,9 +59,11 @@ 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);
@ -65,23 +71,18 @@ class AutoloadBuilderTest extends PHPUnit_Framework_TestCase
$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();
$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);
@ -89,16 +90,27 @@ class AutoloadBuilderTest extends PHPUnit_Framework_TestCase
/**
* @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();
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);
}
}
}
Loading…
Cancel
Save