Anton Grebnev
12 years ago
44 changed files with 913 additions and 395 deletions
-
11Load.php
-
70app/CliController.php
-
21app/FrontController.php
-
18app/iCli.php
-
5app/router/Route.php
-
52app/router/Router.php
-
16exception/ErrorHandler.php
-
17layout/Error.layout.php
-
2logger/Logger.php
-
12model/MongoDbCommand.php
-
2model/MongoStatement.php
-
2model/MySQLiStatement.php
-
2redis/RedisManager.php
-
6tests/LoadTest.php
-
17tests/app/ActionTest.php
-
2tests/app/Action_TestCase.php
-
15tests/app/AjaxActionTest.php
-
101tests/app/CliControllerTest.php
-
10tests/app/ErrorActionTest.php
-
13tests/app/FrontControllerTest.php
-
25tests/app/PagerActionTest.php
-
10tests/app/StaticActionTest.php
-
13tests/app/router/RouteTest.php
-
58tests/app/router/RouterTest.php
-
10tests/classes/EnvTest.php
-
13tests/exception/ErrorHandlerTest.php
-
82tests/layout/ErrorLayoutTest.php
-
26tests/layout/LayoutTest.php
-
6tests/logger/CliLoggerTest.php
-
13tests/logger/FileLoggerTest.php
-
22tests/model/MongoDbCommandTest.php
-
64tests/model/MongoDriverTest.php
-
40tests/model/MongoModelTest.php
-
95tests/model/MongoStatementTest.php
-
32tests/model/MySQLiDriverTest.php
-
50tests/model/MySQLiStatementTest.php
-
14tests/redis/RedisDebugTest.php
-
7tests/redis/RedisManagerTest.php
-
30tests/util/AutoloadBuilderTest.php
-
221tests/util/profiler/ProfilerTest.php
-
9tests/view/helpers/GetViewHelperTest.php
-
21util/AutoloadBuilder.php
-
27util/profiler/Profiler.php
-
4view/helpers/GetViewHelper.php
@ -0,0 +1,70 @@ |
|||
<?php |
|||
/** |
|||
* @copyright NetMonsters <team@netmonsters.ru> |
|||
* @link http://netmonsters.ru |
|||
* @package Majestic |
|||
* @subpackage App |
|||
* @since 27.06.12 |
|||
* |
|||
*/ |
|||
|
|||
/** |
|||
* @desc CliController (run cli_class, end profiler) |
|||
* @author Aleksandr Demidov |
|||
*/ |
|||
class CliController |
|||
{ |
|||
/** |
|||
* @var CliController |
|||
*/ |
|||
protected static $instance; |
|||
|
|||
protected $error_stream; |
|||
|
|||
protected function __construct() |
|||
{ |
|||
ErrorHandler::init(); |
|||
$this->error_stream = Config::get('ErrorStream', 'php://stderr'); |
|||
} |
|||
|
|||
/** |
|||
* @static |
|||
* @return CliController |
|||
*/ |
|||
public static function getInstance() |
|||
{ |
|||
if (!isset(self::$instance)) { |
|||
self::$instance = new self(); |
|||
} |
|||
return self::$instance; |
|||
} |
|||
|
|||
/** |
|||
* @param iCli $cli_class |
|||
* @throws ErrorException |
|||
*/ |
|||
public function execute($cli_class) |
|||
{ |
|||
try { |
|||
if (!in_array('iCli', class_implements($cli_class))) { |
|||
throw new ErrorException('Runner "' . get_class($cli_class) . '" need implement of "iCli" interface.'); |
|||
} |
|||
$cli_class->run(); |
|||
if (Config::get('PROFILER')) { |
|||
$profile = Profiler::getInstance()->getCli(); |
|||
if (Config::get('LOGGING')) { |
|||
Logger::getInstance()->log($profile); |
|||
} else { |
|||
echo $profile; |
|||
} |
|||
} |
|||
} catch (Exception $e) { |
|||
$code = $e->getCode(); |
|||
if ($e instanceof ErrorException) { |
|||
$code = $e->getSeverity(); |
|||
} |
|||
file_put_contents($this->error_stream, PHP_EOL . 'Error ' . '#' . $code . ': ' . $e->getMessage() . PHP_EOL, FILE_APPEND); |
|||
file_put_contents($this->error_stream, PHP_EOL . 'Stack trace: ' . PHP_EOL . $e->getTraceAsString() . PHP_EOL, FILE_APPEND); |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,18 @@ |
|||
<?php |
|||
/** |
|||
* @copyright NetMonsters <team@netmonsters.ru> |
|||
* @link http://netmonsters.ru |
|||
* @package Majestic |
|||
* @subpackage App |
|||
* @since 10.07.12 |
|||
* |
|||
*/ |
|||
|
|||
/** |
|||
* @desc Starter cli point need implement iCli |
|||
* @author Aleksandr Demidov |
|||
*/ |
|||
interface iCli |
|||
{ |
|||
public function run(); |
|||
} |
@ -0,0 +1,101 @@ |
|||
<?php |
|||
/** |
|||
* @copyright NetMonsters <team@netmonsters.ru> |
|||
* @link http://netmonsters.ru |
|||
* @package Majestic |
|||
* @subpackage Tests app |
|||
* @since 10.07.12 |
|||
* |
|||
*/ |
|||
|
|||
require_once __DIR__ . '/../../util/profiler/Profiler.php'; |
|||
require_once __DIR__ . '/../../exception/GeneralException.php'; |
|||
require_once __DIR__ . '/../../exception/Error404Exception.php'; |
|||
require_once __DIR__ . '/../../exception/ErrorHandler.php'; |
|||
require_once __DIR__ . '/../../app/CliController.php'; |
|||
require_once __DIR__ . '/../../Registry.php'; |
|||
require_once __DIR__ . '/../../Config.php'; |
|||
require_once __DIR__ . '/../../app/iCli.php'; |
|||
|
|||
/** |
|||
* @desc CliController tests |
|||
* @author Aleksandr Demidov |
|||
*/ |
|||
class CliControllerTest extends PHPUnit_Framework_TestCase |
|||
{ |
|||
protected $stream; |
|||
|
|||
public function testGetInstance() |
|||
{ |
|||
$instance = CliController::getInstance(); |
|||
$this->assertInstanceOf('CliController', $instance); |
|||
} |
|||
|
|||
public function testExecute() |
|||
{ |
|||
Config::set('PROFILER', false); |
|||
$cli_class = $this->getMockForAbstractClass('iCli', array(), '', '', '', '', array('run')); |
|||
$cli_class->expects($this->once()) |
|||
->method('run') |
|||
->with(); |
|||
CliController::getInstance()->execute($cli_class); |
|||
} |
|||
|
|||
public function testExecuteWithProfiler() |
|||
{ |
|||
ob_start(); |
|||
Config::set('PROFILER', true); |
|||
$cli_class = $this->getMockForAbstractClass('iCli', array(), '', '', '', '', array('run')); |
|||
$cli_class->expects($this->once()) |
|||
->method('run') |
|||
->with(); |
|||
CliController::getInstance()->execute($cli_class); |
|||
$output = ob_get_clean(); |
|||
$this->assertContains('Elapsed time:', $output); |
|||
} |
|||
|
|||
/** |
|||
* @runInSeparateProcess |
|||
*/ |
|||
public function testExecuteImplementErrorToFile() |
|||
{ |
|||
Config::set('ErrorStream', __DIR__ . '/temp.txt'); |
|||
touch(Config::get('ErrorStream')); |
|||
$cli_class = new StdClass(); |
|||
CliController::getInstance()->execute($cli_class); |
|||
$this->assertContains('Runner "' . get_class($cli_class) . '" need implement of "iCli" interface.', file_get_contents(Config::get('ErrorStream'))); |
|||
unlink(Config::get('ErrorStream')); |
|||
} |
|||
|
|||
/** |
|||
* @runInSeparateProcess |
|||
*/ |
|||
public function testExecuteImplementErrorToConsole() |
|||
{ |
|||
Config::set('ErrorStream', 'php://output'); |
|||
$cli_class = new StdClass(); |
|||
$this->expectOutputRegex('/.*Runner "' . get_class($cli_class) . '" need implement of "iCli" interface\..*/'); |
|||
CliController::getInstance()->execute($cli_class); |
|||
} |
|||
/** |
|||
* @runInSeparateProcess |
|||
*/ |
|||
public function testExecuteWithRunThrowError() |
|||
{ |
|||
Config::set('ErrorStream', 'php://output'); |
|||
Config::set('PROFILER', false); |
|||
$cli_class = $this->getMockForAbstractClass('iCli', array(), '', '', '', '', array('run')); |
|||
$cli_class->expects($this->once()) |
|||
->method('run') |
|||
->with() |
|||
->will($this->returnCallback(array($this, 'callbackWithThrow'))); |
|||
|
|||
$this->expectOutputRegex('/.*Error from callback\..*/'); |
|||
CliController::getInstance()->execute($cli_class); |
|||
} |
|||
|
|||
public function callbackWithThrow() |
|||
{ |
|||
throw new ErrorException('Error from callback.'); |
|||
} |
|||
} |
@ -0,0 +1,82 @@ |
|||
<?php |
|||
/** |
|||
* @copyright NetMonsters <team@netmonsters.ru> |
|||
* @link http://netmonsters.ru |
|||
* @package Majestic |
|||
* @subpackage UnitTests |
|||
* @since 04-06-2012 |
|||
* @user: agrebnev |
|||
*/ |
|||
|
|||
require_once dirname(__FILE__) . '/../../Registry.php'; |
|||
require_once dirname(__FILE__) . '/../../Config.php'; |
|||
require_once dirname(__FILE__) . '/../../exception/ErrorHandler.php'; |
|||
require_once dirname(__FILE__) . '/../../exception/GeneralException.php'; |
|||
require_once dirname(__FILE__) . '/../../app/FrontController.php'; |
|||
require_once dirname(__FILE__) . '/../../layout/Layout.php'; |
|||
require_once dirname(__FILE__) . '/../../layout/Error.layout.php'; |
|||
|
|||
/** |
|||
* @runTestsInSeparateProcesses |
|||
*/ |
|||
class ErrorLayoutTest extends PHPUnit_Framework_TestCase |
|||
{ |
|||
|
|||
public function run(PHPUnit_Framework_TestResult $result = NULL) |
|||
{ |
|||
$this->setPreserveGlobalState(false); |
|||
return parent::run($result); |
|||
} |
|||
|
|||
public function setUp() |
|||
{ |
|||
if (!class_exists('RouterMock')) { |
|||
$this->getMock('Router', array(), array(), 'RouterMock', false); |
|||
} |
|||
if (!class_exists('PHPViewMock')) { |
|||
$this->getMock('PHPView', array('fetch', 'append', 'prepend', 'assign', 'getTemplate'), array(), 'PHPViewMock', false); |
|||
} |
|||
|
|||
set_new_overload(array($this, 'newCallback')); |
|||
} |
|||
|
|||
public function testSetException() |
|||
{ |
|||
|
|||
Config::set('DEBUG', false); |
|||
$layout = new ErrorLayout(); |
|||
$layout->setException(new GeneralException()); |
|||
$this->assertAttributeInstanceOf('GeneralException', 'exception', $layout); |
|||
} |
|||
|
|||
public function testExecute() |
|||
{ |
|||
|
|||
Config::set('DEBUG', false); |
|||
$action = $this->getMock('Action', array('fetch')); |
|||
$action->expects($this->once()) |
|||
->method('fetch') |
|||
->will($this->returnValue('this is the result of Action::fetch()')); |
|||
|
|||
$layout = new ErrorLayout(); |
|||
$layout->fetch($action); |
|||
} |
|||
|
|||
|
|||
protected function newCallback($className) |
|||
{ |
|||
switch ($className) { |
|||
case 'Router': |
|||
return 'RouterMock'; |
|||
case 'PHPView': |
|||
return 'PHPViewMock'; |
|||
default: |
|||
return $className; |
|||
} |
|||
} |
|||
|
|||
public function tearDown() |
|||
{ |
|||
unset_new_overload(); |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue