Add namespace.

This commit is contained in:
2014-06-02 18:58:49 +04:00
parent aec1a60985
commit 1ba341b064
159 changed files with 265 additions and 264 deletions

View File

@ -0,0 +1,176 @@
<?php
/*
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage UnitTests
* @since 2011-10-28
*
* Unit tests for AutoloadBuilder class
*/
require_once dirname(__FILE__) . '/../../util/AutoloadBuilder.php';
/**
* @TODO: AutoloadBuilder - fix writing to file: construct array first, write to file next - atomic operation
*/
class AutoloadBuilderTest extends PHPUnit_Framework_TestCase
{
private static $inc_dirs = array();
private static $file;
private static $path;
private static $lib_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.
*/
public static function setUpBeforeClass()
{
self::$path = realpath(dirname(__FILE__) . '/../../../..');
self::$lib_path = realpath(dirname(__FILE__) . '/../..');
self::$app = 'lib/core/tests/face';
self::$file = self::$path . '/' . self::$app . '/cache/autoload.php';
self::$inc_dirs[self::$path . '/' . self::$app . '/src'] = true;
self::$inc_dirs[self::$path . '/' . self::$app . '/cache'] = true;
self::$inc_dirs[self::$lib_path . '/'] = true;
foreach (self::$inc_dirs as $dir => &$is_exist) {
if (!file_exists($dir)) {
$is_exist = false;
mkdir($dir, 0777, true);
}
}
}
/**
* @TODO: AutoloadBuilder - check input params: string for filename, array for dirs
* @runInSeparateProcess
*/
public function testBuildParams()
{
$this->setConstants();
$autoload = self::$file;
$dirs = 'string';
$builder = new AutoloadBuilder($autoload, $dirs);
$this->setExpectedException('PHPUnit_Framework_Error');
$builder->build();
}
/**
* @runInSeparateProcess
*/
public function testBuild()
{
$this->setConstants();
$builder = new AutoloadBuilder(self::$file, array_keys(self::$inc_dirs));
$this->assertFileNotExists(self::$file);
$builder->build();
$this->assertFileExists(self::$file);
$array = require self::$file;
$this->assertFileExists(self::$file);
$this->assertInternalType('array', $array);
$this->assertNotEmpty($array);
$this->assertArrayHasKey('AutoloadBuilder', $array);
$this->assertArrayHasKey('Load', $array);
}
/**
* @runInSeparateProcess
*/
public function testBuildWithExcluded()
{
$this->setConstants();
$builder = new AutoloadBuilder(self::$file, array_keys(self::$inc_dirs), array(self::$lib_path . '/app/'));
$this->assertFileNotExists(self::$file);
$builder->build();
$this->assertFileExists(self::$file);
$array = require self::$file;
$this->assertInternalType('array', $array);
$this->assertNotEmpty($array);
$this->assertArrayHasKey('AutoloadBuilder', $array);
$this->assertArrayNotHasKey('FrontController', $array);
$this->assertArrayNotHasKey('AjaxAction', $array);
}
/**
* @runInSeparateProcess
*/
public function testAccessDenied()
{
$this->setConstants();
$this->assertFileNotExists(self::$file);
$path = dirname(self::$file);
chmod($path, 0400);
$builder = new AutoloadBuilder(self::$file, array(self::$path . '/' . self::$app . '/src', self::$path . '/' . self::$app . '/cache', self::$path . '/lib'));
$this->setExpectedException('PHPUnit_Framework_Error', 'Permission denied');
$builder->build();
chmod(self::$file, 0777);
}
public static function tearDownAfterClass()
{
if (file_exists(self::$file)) {
unlink(self::$file);
}
foreach (self::$inc_dirs as $dir => $is_exist) {
if (!$is_exist) {
self::rrmdir($dir);
}
}
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);
}
}
}

View File

@ -0,0 +1,128 @@
<?php
/*
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage UnitTests
* @since 2011-10-..
*
* Unit tests for AutoloadBuilder class
*/
require_once 'vfsStream/vfsStream.php';
require_once dirname(__FILE__) . '/../../util/AutoloadBuilder.php';
/**
* @TODO: AutoloadBuilder - fix writing to file: construct array first, write to file next - atomic operation
* @TODO: vfsStream doesn't work with SPLFIleObject->getRealPath - $file->getPath .'/'. $file->getFIleName instead
*/
class AutoloadBuilderTestVFS extends PHPUnit_Framework_TestCase
{
private static $inc_dirs = array();
private static $file;
private $root;
private $array = array();
/**
* @TODO: Load->buildAutoload() - uses two paths - PATH . '/' . APP . '/src' and PATH . '/lib' those are not checked. Can couse error.
*/
public function setUp()
{
if (!defined('PATH')) {
define('PATH', realpath(dirname(__FILE__) . '/../../../..'));
}
if (!defined('APP')) {
define('APP', 'lib/core/tests/face');
}
vfsStreamWrapper::register();
$this->root = vfsStream::create(
array(
'cache' => array(
'face' => array()
),
'face' => array(
'src' => array(
'Registry.php' => ' class Registry'
)
),
'lib' => array(
'Registry.php' => ' class Registry',
'Load.php' => 'class Load extends Registry',
'devel.config' => ' development config file'
)
)
);
vfsStreamWrapper::setRoot($this->root);
}
public function testVFS()
{
$builder = new AutoloadBuilder(vfsStream::url('root/cache/face/autoload.php'), array(vfsStream::url('root/lib'), vfsStream::url('root/face/src')));
$this->assertTrue($this->root->hasChild('lib'));
$this->assertFalse($this->root->hasChild('nochild'));
$this->assertFalse($this->root->hasChild('cache/face/autoload.php'));
$handle = fopen(vfsStream::url('root/cache/face/autoload.php'), 'w');
fwrite($handle, 'new string');
fclose($handle);
$this->assertTrue($this->root->hasChild('cache/face/autoload.php'));
$this->assertSame('new string', $this->root->getChild('cache/face/autoload.php')->getContent());
}
public function testBuild()
{
$this->markTestSkipped(
'AutoloadBuilder uses $file->getRealPath method, that doesn`t work properly with vfsStream.'
);
$builder = new AutoloadBuilder(vfsStream::url('root/cache/face/autoload.php'), array(vfsStream::url('root/lib'), vfsStream::url('root/face/src')));
$builder->build();
$this->assertTrue($this->root->hasChild('cache/face/autoload.php'));
$this->assertFileExists(vfsStream::url('root/cache/face/autoload.php'));
}
public function testAutoloadArray()
{
$this->markTestSkipped(
'AutoloadBuilder uses $file->getRealPath method, that doesn`t work properly with vfsStream.'
);
$this->assertFileNotExists(vfsStream::url('root/cache/face/autoload.php'));
$builder = new AutoloadBuilder(vfsStream::url('root/cache/face/autoload.php'), array(vfsStream::url('root/lib'), vfsStream::url('root/face/src')));
$builder->build();
$this->assertFileExists(vfsStream::url('root/cache/face/autoload.php'));
$this->assertEmpty($this->array);
$this->array = require vfsStream::url('root/cache/face/autoload.php');
$this->assertInternalType('array', $this->array);
$this->assertArrayHasKey('Load', $this->array);
$this->assertArrayNotHasKey('Key', $this->array);
$this->assertSame(2, count($this->array));
}
public function testAutoloadHasNoAccess()
{
$this->markTestSkipped(
'AutoloadBuilder uses $file->getRealPath method, that doesn`t work properly with vfsStream.'
);
$this->assertFileNotExists(vfsStream::url('root/cache/face/autoload.php'));
$autoloadFIle = new vfsStreamFile('cache/face/autoload.php', 0400);
$this->root->addChild($autoloadFIle);
$builder = new AutoloadBuilder(vfsStream::url('root/cache/face/autoload.php'), array(vfsStream::url('root/lib'), vfsStream::url('root/face/src')));
$builder->build();
$this->assertFileExists(vfsStream::url('root/cache/face/autoload.php'));
$this->assertEmpty($this->root->getChild('cache/face/autoload.php')->getContent());
$this->array = require vfsStream::url('root/cache/face/autoload.php');
$this->assertInternalType('integer', $this->array);
$autoloadFIle->chmod(0777);
$builder = new AutoloadBuilder(vfsStream::url('root/cache/face/autoload.php'), array(vfsStream::url('root/lib'), vfsStream::url('root/face/src')));
$builder->build();
$this->assertFileExists(vfsStream::url('root/cache/face/autoload.php'));
$this->assertNotEmpty($this->root->getChild('cache/face/autoload.php')->getContent());
$this->array = require vfsStream::url('root/cache/face/autoload.php');
$this->assertInternalType('array', $this->array);
$this->assertNotEmpty($this->array);
}
}

View File

@ -0,0 +1,42 @@
<?php
/*
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage UnitTests
* @since 2011-10-28
*
* Unit tests for CommandProfiler class
*/
require_once dirname(__FILE__) . '/../../../util/profiler/CommandProfiler.php';
class CommandProfilerTest extends PHPUnit_Framework_TestCase
{
private $profiler;
public function setUp()
{
$this->profiler = new CommandProfiler('method', 'exec');
$this->profiler->end();
}
public function testGetEllapsed()
{
$this->assertGreaterThan(0, $this->profiler->getElapsed());
}
public function testGetType()
{
$this->assertSame('method', $this->profiler->getType());
$this->assertNotEquals('argument', $this->profiler->getType());
}
public function testGetCommand()
{
$this->assertSame('exec', $this->profiler->getCommand());
$this->assertNotEquals('grep', $this->profiler->getCommand());
}
}

View File

@ -0,0 +1,359 @@
<?php
/*
* @copyright NetMonsters <team@netmonsters.ru>
* @link http://netmonsters.ru
* @package Majestic
* @subpackage UnitTests
* @since 2011-10-28
*
* Unit tests for CommandProfiler class
*/
require_once dirname(__FILE__) . '/../../../Registry.php';
require_once dirname(__FILE__) . '/../../../Config.php';
require_once dirname(__FILE__) . '/../../../exception/GeneralException.php';
require_once dirname(__FILE__) . '/../../../util/FirePHPCore-0.3.2/lib/FirePHPCore/fb.php';
require_once dirname(__FILE__) . '/../../../util/profiler/Profiler.php';
/**
* @TODO: Profiler->getJson() hardly depends on FB library
*/
class ProfilerTest extends PHPUnit_Framework_TestCase
{
public function run(PHPUnit_Framework_TestResult $result = NULL)
{
$this->setPreserveGlobalState(false);
return parent::run($result);
}
public function setUp()
{
set_new_overload(array($this, 'newCallback'));
}
/**
* @runInSeparateProcess
*/
public function testGetInstaceNoDebug()
{
Config::set('PROFILER', false);
$this->setExpectedException('GeneralException', 'Need turn PROFILER before use.');
Profiler::getInstance();
}
/**
* @runInSeparateProcess
*/
public function testGetInstance()
{
Config::set('PROFILER', true);
$profiler = Profiler::getInstance();
$this->assertInstanceOf('Profiler', $profiler);
$this->assertSame($profiler, Profiler::getInstance());
}
/**
* @runInSeparateProcess
*/
public function testProfilerCommand()
{
Config::set('PROFILER', true);
$this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
$profiler = Profiler::getInstance();
$cmdProfiler = $profiler->profilerCommand('command', 'type');
$this->assertInstanceOf('CommandProfiler', $cmdProfiler);
}
/**
* @runInSeparateProcess
*/
public function testStartEndNoCommandProfiler()
{
Config::set('PROFILER', true);
Config::set('PROFILER_DETAILS', true);
$profiler = Profiler::getInstance();
$profiler->start();
$result = $profiler->end('<body></body>');
$this->assertNotEquals('<body></body>', $result);
$this->assertStringStartsWith('<body>', $result);
$this->assertStringEndsWith(']<br/></div></body>', $result);
$this->assertContains('<div style="clear:both; font:12px monospace; margin: 5px; white-space: pre;">', $result);
$this->assertSame('html', $profiler->end('html'));
}
/**
* @runInSeparateProcess
*/
public function testStartEndWithCommandProfiler()
{
Config::set('PROFILER', true);
Config::set('PROFILER_DETAILS', true);
$this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
$profiler = Profiler::getInstance();
$profiler->start();
$cmdProfiler = $profiler->profilerCommand('command', 'type');
$this->assertNotNull($cmdProfiler);
$result = $profiler->end('<body></body>');
$this->assertNotEquals('<body></body>', $result);
$this->assertStringEndsNotWith(']<br/></div></body>', $result);
$this->assertContains('Queries: 1 [0 ms]<br/>() [0ms] <br/>', $result);
}
/**
* @runInSeparateProcess
*/
public function testStartEndWithCommandProfilerWithNonDetails()
{
Config::set('PROFILER', true);
Config::set('PROFILER_DETAILS', false);
$this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
$profiler = Profiler::getInstance();
$profiler->start();
$result = $profiler->end('<body></body>');
$this->assertNotEquals('<body></body>', $result);
$this->assertContains('Queries not counted.', $result);
}
/**
* @runInSeparateProcess
*/
public function testStartEndWithCommandProfilerWithNonDetailsButExistingQueries()
{
Config::set('PROFILER', true);
Config::set('PROFILER_DETAILS', false);
$this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
$profiler = Profiler::getInstance();
$profiler->start();
$cmdProfiler = $profiler->profilerCommand('command', 'type');
$result = $profiler->end('<body></body>');
$this->assertNotEquals('<body></body>', $result);
$this->assertStringEndsNotWith(']<br/></div></body>', $result);
$this->assertContains('Queries: 1', $result);
}
/**
* @runInSeparateProcess
*/
public function testStartEndWithCommandProfilerWithDetailsButNonQueries()
{
Config::set('PROFILER', true);
Config::set('PROFILER_DETAILS', true);
$this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
$profiler = Profiler::getInstance();
$profiler->start();
$result = $profiler->end('<body></body>');
$this->assertNotEquals('<body></body>', $result);
$this->assertStringEndsWith(']<br/></div></body>', $result);
$this->assertContains('Queries: 0', $result);
}
/**
* @runInSeparateProcess
*/
public function testGetJSON()
{
Config::set('PROFILER', true);
Config::set('PROFILER_DETAILS', true);
$this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
$profiler = Profiler::getInstance();
$cmdProfiler = $profiler->profilerCommand('command', 'type');
$fire_php_mock = $this->getMockBuilder('FirePHP')
->disableOriginalConstructor()
->setMethods(array('__construct', 'fb'))
->getMock();
$fire_php_mock->expects($this->exactly(2))
->method('fb')
->with($this->anything(), $this->logicalAnd($this->logicalXor($this->anything(), $this->stringContains('Queries not'))), $this->logicalXor($this->anything(), $this->stringContains('Queries: 0'))); // Expected "Queries: 1"
$reflection_property = new ReflectionProperty('FirePHP', 'instance');
$reflection_property->setAccessible(true);
$reflection_property->setValue($fire_php_mock);
$this->assertNull($profiler->getJson());
}
/**
* @runInSeparateProcess
*/
public function testGetJSONWithNonDetails()
{
Config::set('PROFILER', true);
Config::set('PROFILER_DETAILS', false);
$this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
$profiler = Profiler::getInstance();
$fire_php_mock = $this->getMockBuilder('FirePHP')
->disableOriginalConstructor()
->setMethods(array('__construct', 'fb'))
->getMock();
$fire_php_mock->expects($this->exactly(2))
->method('fb')
->with($this->anything(), $this->logicalAnd($this->logicalXor($this->anything(), $this->stringContains('Queries: 1'))), $this->logicalXor($this->anything(), $this->stringContains('Queries: 0'))); // expected "Queries not counted"
$reflection_property = new ReflectionProperty('FirePHP', 'instance');
$reflection_property->setAccessible(true);
$reflection_property->setValue($fire_php_mock);
$this->assertNull($profiler->getJson());
}
/**
* @runInSeparateProcess
*/
public function testGetJSONWithNonDetailsButExistingQueries()
{
Config::set('PROFILER', true);
Config::set('PROFILER_DETAILS', false);
$this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
$profiler = Profiler::getInstance();
$cmdProfiler = $profiler->profilerCommand('command', 'type');
$fire_php_mock = $this->getMockBuilder('FirePHP')
->disableOriginalConstructor()
->setMethods(array('__construct', 'fb'))
->getMock();
$fire_php_mock->expects($this->exactly(2))
->method('fb')
->with($this->anything(), $this->logicalAnd($this->logicalXor($this->anything(), $this->stringContains('Queries not counted'))), $this->logicalXor($this->anything(), $this->stringContains('Queries: 0'))); // expected "Queries: 1"
$reflection_property = new ReflectionProperty('FirePHP', 'instance');
$reflection_property->setAccessible(true);
$reflection_property->setValue($fire_php_mock);
$this->assertNull($profiler->getJson());
}
/**
* @runInSeparateProcess
*/
public function testGetJSONWithDetailsButNonQueries()
{
Config::set('PROFILER', true);
Config::set('PROFILER_DETAILS', true);
$this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
$profiler = Profiler::getInstance();
$fire_php_mock = $this->getMockBuilder('FirePHP')
->disableOriginalConstructor()
->setMethods(array('__construct', 'fb'))
->getMock();
$fire_php_mock->expects($this->exactly(2))
->method('fb')
->with($this->anything(), $this->logicalAnd($this->logicalXor($this->anything(), $this->stringContains('Queries not counted'))), $this->logicalXor($this->anything(), $this->stringContains('Queries: 1'))); // expected "Queries: 0"
$reflection_property = new ReflectionProperty('FirePHP', 'instance');
$reflection_property->setAccessible(true);
$reflection_property->setValue($fire_php_mock);
$this->assertNull($profiler->getJson());
}
/**
* @runInSeparateProcess
*/
public function testGetCLI()
{
Config::set('PROFILER', true);
Config::set('PROFILER_DETAILS', true);
$this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
$profiler = Profiler::getInstance();
$cmdProfiler = $profiler->profilerCommand('command', 'type');
$result = $profiler->getCli();
$this->assertNotNull($result);
$this->assertContains('Queries: 1', Profiler::getInstance()->getCli());
}
/**
* @runInSeparateProcess
*/
public function testGetCLIWithNonDetails()
{
Config::set('PROFILER', true);
Config::set('PROFILER_DETAILS', false);
$this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
$profiler = Profiler::getInstance();
$result = $profiler->getCli();
$this->assertNotNull($result);
$this->assertContains('Queries not counted', Profiler::getInstance()->getCli());
}
/**
* @runInSeparateProcess
*/
public function testGetCLIWithNonDetailsButExistingQueries()
{
Config::set('PROFILER', true);
Config::set('PROFILER_DETAILS', false);
$this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
$profiler = Profiler::getInstance();
$cmdProfiler = $profiler->profilerCommand('command', 'type');
$result = $profiler->getCli();
$this->assertNotNull($result);
$this->assertContains('Queries: 1', Profiler::getInstance()->getCli());
}
/**
* @runInSeparateProcess
*/
public function testGetCLIWithDetailsButNonQueries()
{
Config::set('PROFILER', true);
Config::set('PROFILER_DETAILS', true);
$this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false);
$profiler = Profiler::getInstance();
$result = $profiler->getCli();
$this->assertNotNull($result);
$this->assertContains('Queries: 0', Profiler::getInstance()->getCli());
}
public function tearDown()
{
// if (!is_null(Config::get('DEBUG'))) {
// $debug = Config::get('DEBUG') ? 'TRUE' : 'FALSE';
// echo PHP_EOL . __CLASS__ . ' ' . $debug . PHP_EOL;
// } else {
// echo PHP_EOL . __CLASS__ . ' ' . 'DEBUG NOT DEFINED' . PHP_EOL;
// }
unset_new_overload();
}
protected function newCallback($className)
{
switch ($className) {
case 'CommandProfiler':
return 'CommandProfilerMock';
default:
return $className;
}
}
}
//
//
//class CommandProfilerMock
//{
//
// public function __construct($type, $command)
// {
// }
//
// public function getCommand()
// {
// return 'command';
// }
//
// public function getType()
// {
// return 'type';
// }
//
// public function getElapsed()
// {
// return 10;
// }
//}