Anton Terekhov
12 years ago
43 changed files with 1023 additions and 522 deletions
-
5Load.php
-
70app/CliController.php
-
7app/FrontController.php
-
18app/iCli.php
-
5exception/ErrorHandler.php
-
2logger/Logger.php
-
1model/Db.php
-
3model/DbStatement.php
-
17model/Model.php
-
32model/MongoDbCommand.php
-
20model/MongoDriver.php
-
157model/MongoModel.php
-
7model/MongoStatement.php
-
52model/MySQLiDriver.php
-
5model/MySQLiStatement.php
-
14model/SqlDbDriver.php
-
26model/SqlModel.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
-
5tests/app/FrontControllerTest.php
-
25tests/app/PagerActionTest.php
-
10tests/app/StaticActionTest.php
-
10tests/classes/EnvTest.php
-
13tests/exception/ErrorHandlerTest.php
-
12tests/layout/ErrorLayoutTest.php
-
26tests/layout/LayoutTest.php
-
6tests/logger/CliLoggerTest.php
-
13tests/logger/FileLoggerTest.php
-
11tests/model/MongoDbCommandTest.php
-
95tests/model/MongoDriverTest.php
-
277tests/model/MongoModelTest.php
-
95tests/model/MongoStatementTest.php
-
32tests/model/MySQLiDriverTest.php
-
50tests/model/MySQLiStatementTest.php
-
14tests/redis/RedisDebugTest.php
-
7tests/redis/RedisManagerTest.php
-
223tests/util/profiler/ProfilerTest.php
-
27util/profiler/Profiler.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.'); |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue