From bcc9be619d87130a826cbbd916003fad6205dd45 Mon Sep 17 00:00:00 2001 From: Alexander Demidov Date: Tue, 26 Jun 2012 17:52:37 +0400 Subject: [PATCH 01/25] Merge spl_autoload from init to Load::setAutoloadFrom. --- Load.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Load.php b/Load.php index 5c44b3f..46b54ab 100644 --- a/Load.php +++ b/Load.php @@ -39,6 +39,9 @@ class Load self::buildAutoload(); } self::$autoload = require(self::$file); + spl_autoload_register(array( + __CLASS__, + 'autoload')); } static public function autoload($class) From cfd6536c5f4c1aa585a394473047be48d733f8c5 Mon Sep 17 00:00:00 2001 From: Alexander Demidov Date: Wed, 27 Jun 2012 17:56:06 +0400 Subject: [PATCH 02/25] Replace constant DEBUG to use in Config. Remove start profiler from FrontController (moved to bootstrap). --- app/FrontController.php | 7 +--- logger/Logger.php | 2 +- model/MongoStatement.php | 2 +- model/MySQLiStatement.php | 2 +- redis/RedisManager.php | 2 +- tests/LoadTest.php | 4 +- tests/app/ActionTest.php | 17 +++----- tests/app/AjaxActionTest.php | 15 +++---- tests/app/ErrorActionTest.php | 10 ++--- tests/app/FrontControllerTest.php | 5 +-- tests/app/PagerActionTest.php | 25 +++++------- tests/app/StaticActionTest.php | 10 ++--- tests/classes/EnvTest.php | 10 ++--- tests/layout/ErrorLayoutTest.php | 10 ++--- tests/layout/LayoutTest.php | 20 ++++------ tests/logger/CliLoggerTest.php | 5 +-- tests/logger/FileLoggerTest.php | 12 ++---- tests/model/MongoDriverTest.php | 64 ++++++++---------------------- tests/model/MongoModelTest.php | 40 +++++-------------- tests/model/MongoStatementTest.php | 76 +++++++++--------------------------- tests/model/MySQLiDriverTest.php | 32 ++++----------- tests/model/MySQLiStatementTest.php | 40 +++++-------------- tests/redis/RedisDebugTest.php | 12 ++---- tests/redis/RedisManagerTest.php | 4 +- tests/util/profiler/ProfilerTest.php | 28 ++++--------- util/profiler/Profiler.php | 2 +- 26 files changed, 134 insertions(+), 322 deletions(-) diff --git a/app/FrontController.php b/app/FrontController.php index 4e79609..cb31776 100644 --- a/app/FrontController.php +++ b/app/FrontController.php @@ -32,9 +32,6 @@ class FrontController private function __construct() { ErrorHandler::init(); - if (DEBUG == true) { - Profiler::getInstance()->start(); - } $this->router = new Router(); } @@ -120,7 +117,7 @@ class FrontController $layout = new $layout_class(); $html = $layout->fetch($action); - if (DEBUG) { + if (Config::get('PROFILER')) { if (is_subclass_of($action, 'AjaxAction')) { Profiler::getInstance()->getJson(); } else { @@ -129,7 +126,7 @@ class FrontController } return $html; } catch (Exception $e) { - if (DEBUG == true) { + if (Config::get('DEBUG')) { if (!headers_sent()) { header('HTTP/1.0 500 Internal Server Error'); } diff --git a/logger/Logger.php b/logger/Logger.php index c8637c0..53be1de 100644 --- a/logger/Logger.php +++ b/logger/Logger.php @@ -43,7 +43,7 @@ abstract class Logger */ public function log($message) { - if (DEBUG) { + if (Config::get('LOGGING')) { $this->concreteLog($message); } } diff --git a/model/MongoStatement.php b/model/MongoStatement.php index 45d194c..49f4c53 100644 --- a/model/MongoStatement.php +++ b/model/MongoStatement.php @@ -138,7 +138,7 @@ class MongoStatement extends DbStatement $this->result = false; $mongo = $this->driver->getConnection(); if ($mongo instanceof Mongo) { - if (DEBUG) { + if (Config::get('PROFILER_DETAILS')) { $profiler = Profiler::getInstance()->profilerCommand('Mongo', $request); $result = $request->execute(); $profiler->end(); diff --git a/model/MySQLiStatement.php b/model/MySQLiStatement.php index 0176cfd..a89adb4 100644 --- a/model/MySQLiStatement.php +++ b/model/MySQLiStatement.php @@ -157,7 +157,7 @@ class MySQLiStatement extends DbStatement * @var MySQLi */ $mysqli = $this->driver->getConnection(); - if (DEBUG) { + if (Config::get('PROFILER_DETAILS')) { $profiler = Profiler::getInstance()->profilerCommand('MySQL', $request); $result = $mysqli->query($request); $profiler->end(); diff --git a/redis/RedisManager.php b/redis/RedisManager.php index 4a64f63..5da5651 100644 --- a/redis/RedisManager.php +++ b/redis/RedisManager.php @@ -49,7 +49,7 @@ class RedisManager * @var Redis */ $connection = new Redis(); - if (defined('DEBUG') && DEBUG == true) { + if (Config::get('DEBUG')) { $connection = new RedisDebug($connection); } if (!$connection->connect($host, $port)) { diff --git a/tests/LoadTest.php b/tests/LoadTest.php index 78e9e43..104f9ee 100644 --- a/tests/LoadTest.php +++ b/tests/LoadTest.php @@ -164,9 +164,7 @@ class LoadTest extends PHPUnit_Framework_TestCase $autoload = require(self::$file); $this->assertNotEmpty($autoload); - if (!defined('DEBUG')) { - define('DEBUG', true); - } + Config::set('DEBUG', 1); Load::autoload('Some'); Load::autoload('DbDriver'); } diff --git a/tests/app/ActionTest.php b/tests/app/ActionTest.php index da8e3fd..7fd03ed 100644 --- a/tests/app/ActionTest.php +++ b/tests/app/ActionTest.php @@ -20,9 +20,7 @@ class ActionTest extends Action_TestCase */ public function testActionConstructWithParams() { - if(!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); Env::setParams(array('param1' => 'value1', 'param2' => 'value2')); $action = $this->getMockForAbstractClass('Action' ); $this->assertSame('value1', $action->param1); @@ -33,9 +31,7 @@ class ActionTest extends Action_TestCase */ public function testFetch() { - if(!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $load = new ReflectionClass('Load'); $classes = $load->getProperty('autoload'); $classes->setAccessible(true); @@ -53,9 +49,7 @@ class ActionTest extends Action_TestCase */ public function testFetchNoTemplate() { - if(!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); Env::setParams(array('template' => '')); $controller = FrontController::getInstance(); $controller->setView('SomeView'); @@ -70,9 +64,8 @@ class ActionTest extends Action_TestCase public function testRedirect() { set_exit_overload(function() { return false; }); - if(!defined('DEBUG')) { - define('DEBUG', false); - } + + Config::set('DEBUG', 0); $load = new ReflectionClass('Action'); $redirect = $load->getMethod('redirect'); $redirect->setAccessible(true); diff --git a/tests/app/AjaxActionTest.php b/tests/app/AjaxActionTest.php index 65d9859..b8f22d2 100644 --- a/tests/app/AjaxActionTest.php +++ b/tests/app/AjaxActionTest.php @@ -21,9 +21,8 @@ class AjaxActionTest extends Action_TestCase */ public function testConstruct() { - if(!defined('DEBUG')) { - define('DEBUG', false); - } + + Config::set('DEBUG', 0); Env::setParams(array('ajax' => 'AjaxTemplate', 'param2' => 'value2')); $action = $this->getMockForAbstractClass('AjaxAction' ); $this->assertAttributeEquals('ajax', 'template', $action); @@ -34,9 +33,8 @@ class AjaxActionTest extends Action_TestCase */ public function testFetchWithEncode() { - if(!defined('DEBUG')) { - define('DEBUG', false); - } + + Config::set('DEBUG', 0); $controller = FrontController::getInstance(); $controller->setView('SomeView'); $action = $this->getMockForAbstractClass('AjaxAction' ); @@ -51,9 +49,8 @@ class AjaxActionTest extends Action_TestCase */ public function testFetchNoEncode() { - if(!defined('DEBUG')) { - define('DEBUG', false); - } + + Config::set('DEBUG', 0); Env::setParams(array('encode' => false)); $controller = FrontController::getInstance(); $controller->setView('SomeView'); diff --git a/tests/app/ErrorActionTest.php b/tests/app/ErrorActionTest.php index 045e7aa..466770a 100644 --- a/tests/app/ErrorActionTest.php +++ b/tests/app/ErrorActionTest.php @@ -80,9 +80,8 @@ class ErrorActionTest extends Action_TestCase */ public function testFetchNoTemplate() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + + Config::set('DEBUG', 0); $exception = $this->getMock('ErrorException'); $controller = FrontController::getInstance(); $controller->setView('SomeView'); @@ -123,9 +122,8 @@ class ErrorActionTest extends Action_TestCase private function setConstants($val = false) { - if (!defined('DEBUG')) { - define('DEBUG', $val); - } + + Config::set('DEBUG', $val); } private function header() diff --git a/tests/app/FrontControllerTest.php b/tests/app/FrontControllerTest.php index 59b0c8f..426cdfa 100644 --- a/tests/app/FrontControllerTest.php +++ b/tests/app/FrontControllerTest.php @@ -210,9 +210,8 @@ class FrontControllerTest extends PHPUnit_Framework_TestCase private function setConstants($val = false) { - if (!defined('DEBUG')) { - define('DEBUG', $val); - } + + Config::set('DEBUG', $val); } public function tearDown() diff --git a/tests/app/PagerActionTest.php b/tests/app/PagerActionTest.php index 80f9823..386a4e7 100644 --- a/tests/app/PagerActionTest.php +++ b/tests/app/PagerActionTest.php @@ -21,9 +21,8 @@ class PagerActionTest extends Action_TestCase */ public function testConstructWithParams() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + + Config::set('DEBUG', 0); $action = $this->getMockForAbstractClass('PagerAction'); $this->assertSame(20, $action->getLimit()); $action = $this->getMockForAbstractClass('PagerAction', array(50)); @@ -35,9 +34,8 @@ class PagerActionTest extends Action_TestCase */ public function testSetCount() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + + Config::set('DEBUG', 0); $action = $this->getMockForAbstractClass('PagerAction'); $action->setCount(50); $this->assertSame(1, $action->page); @@ -60,9 +58,8 @@ class PagerActionTest extends Action_TestCase */ public function testGetOffset() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + + Config::set('DEBUG', 0); $action = $this->getMockForAbstractClass('PagerAction'); $this->assertSame(0, $action->getOffset()); } @@ -72,9 +69,8 @@ class PagerActionTest extends Action_TestCase */ public function testFetchNoTemplate() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + + Config::set('DEBUG', 0); Env::setParams(array('template' => '')); $controller = FrontController::getInstance(); $controller->setView('SomeView'); @@ -88,9 +84,8 @@ class PagerActionTest extends Action_TestCase */ public function testFetchWithTemplate() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + + Config::set('DEBUG', 0); Env::setParams(array('template' => 'SomeTemplate')); $controller = FrontController::getInstance(); $controller->setView('SomeView'); diff --git a/tests/app/StaticActionTest.php b/tests/app/StaticActionTest.php index 979589b..283e482 100644 --- a/tests/app/StaticActionTest.php +++ b/tests/app/StaticActionTest.php @@ -21,9 +21,8 @@ class StaticActionTest extends Action_TestCase */ public function testFetchNoTemplate() { - if(!defined('DEBUG')) { - define('DEBUG', false); - } + + Config::set('DEBUG', 0); Env::setParams(array('template' => '')); $controller = FrontController::getInstance(); $controller->setView('SomeView'); @@ -37,9 +36,8 @@ class StaticActionTest extends Action_TestCase */ public function testFetchWithTemplate() { - if(!defined('DEBUG')) { - define('DEBUG', false); - } + + Config::set('DEBUG', 0); Env::setParams(array('template' => 'SomeTemplate')); $controller = FrontController::getInstance(); $controller->setView('SomeView'); diff --git a/tests/classes/EnvTest.php b/tests/classes/EnvTest.php index 542370c..89d3659 100644 --- a/tests/classes/EnvTest.php +++ b/tests/classes/EnvTest.php @@ -24,9 +24,8 @@ class EnvTest extends PHPUnit_Framework_TestCase */ public function testGetRequestUri() { - if(!defined('DEBUG')) { - define('DEBUG', false); - } + + Config::set('DEBUG', 0); $_SERVER['REQUEST_URI'] = '/test/index.php?id=1&test=wet&id_theme=512'; $this->assertSame('/test/index.php', Env::getRequestUri()); $_SERVER['REQUEST_URI'] = '/tes?t/index.php?id=1&test=wet&id_theme=512'; @@ -38,9 +37,8 @@ class EnvTest extends PHPUnit_Framework_TestCase */ public function testTrimBaseRequestUri() { - if(!defined('DEBUG')) { - define('DEBUG', false); - } + + Config::set('DEBUG', 0); $class = new ReflectionClass('Env'); $this->started = $class->getProperty('request'); $this->started->setAccessible(true); diff --git a/tests/layout/ErrorLayoutTest.php b/tests/layout/ErrorLayoutTest.php index 4efb16f..9affe12 100644 --- a/tests/layout/ErrorLayoutTest.php +++ b/tests/layout/ErrorLayoutTest.php @@ -40,9 +40,8 @@ class ErrorLayoutTest extends PHPUnit_Framework_TestCase public function testSetException() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + + Config::set('DEBUG', 0); $layout = new ErrorLayout(); $layout->setException(new GeneralException()); $this->assertAttributeInstanceOf('GeneralException', 'exception', $layout); @@ -50,9 +49,8 @@ class ErrorLayoutTest extends PHPUnit_Framework_TestCase public function testExecute() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + + Config::set('DEBUG', 0); $action = $this->getMock('Action', array('fetch')); $action->expects($this->once()) ->method('fetch') diff --git a/tests/layout/LayoutTest.php b/tests/layout/LayoutTest.php index 603a741..e7029ea 100644 --- a/tests/layout/LayoutTest.php +++ b/tests/layout/LayoutTest.php @@ -40,18 +40,16 @@ class LayoutTest extends PHPUnit_Framework_TestCase public function testConstruct() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + + Config::set('DEBUG', 0); $layout = $this->getMockForAbstractClass('Layout'); $this->assertAttributeInstanceOf('PHPView', 'view', $layout); } public function testFetch() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + + Config::set('DEBUG', 0); $layout = $this->getMockForAbstractClass('Layout'); $action = $this->getMock('Action', array('fetch')); @@ -64,9 +62,8 @@ class LayoutTest extends PHPUnit_Framework_TestCase public function testFetchWithTemplate() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + + Config::set('DEBUG', 0); $layout = $this->getMockForAbstractClass('Layout'); $class = new ReflectionClass('Layout'); @@ -89,9 +86,8 @@ class LayoutTest extends PHPUnit_Framework_TestCase public function testAppend() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + + Config::set('DEBUG', 0); $layout = $this->getMockForAbstractClass('Layout', array('append', 'prepend'), 'LayoutMock'); $action = $this->getMock('Action', array('fetch')); diff --git a/tests/logger/CliLoggerTest.php b/tests/logger/CliLoggerTest.php index 6ef0673..8874834 100644 --- a/tests/logger/CliLoggerTest.php +++ b/tests/logger/CliLoggerTest.php @@ -43,9 +43,8 @@ class CliLoggerTest extends PHPUnit_Framework_TestCase */ public function testLog() { - if(!defined('DEBUG')) { - define('DEBUG', true); - } + + Config::set('DEBUG', 1); $logger = Logger::getInstance(); ob_start(); $logger->setPid(123); diff --git a/tests/logger/FileLoggerTest.php b/tests/logger/FileLoggerTest.php index 889f985..cfe763e 100644 --- a/tests/logger/FileLoggerTest.php +++ b/tests/logger/FileLoggerTest.php @@ -54,9 +54,7 @@ class FileLoggerTest extends PHPUnit_Framework_TestCase */ public function testCannotWrite() { - if (!defined('DEBUG')) { - define('DEBUG', true); - } + Config::set('DEBUG', 1); $conf = array('logger' => 'FileLogger', 'filepath' => '/log.txt'); Config::set('Logger', $conf); @@ -71,9 +69,7 @@ class FileLoggerTest extends PHPUnit_Framework_TestCase */ public function testLog() { - if (!defined('DEBUG')) { - define('DEBUG', true); - } + Config::set('DEBUG', 1); $this->assertFileNotExists($this->conf['filepath']); $logger = Logger::getInstance(); $logger->setPid(123); @@ -86,9 +82,7 @@ class FileLoggerTest extends PHPUnit_Framework_TestCase */ public function testDestruct() { - if (!defined('DEBUG')) { - define('DEBUG', true); - } + Config::set('DEBUG', 1); $my_pid = posix_getpid(); $fd_command = 'lsof -n -p ' . $my_pid . ' | wc -l'; $fd_count_start = (int) `$fd_command`; diff --git a/tests/model/MongoDriverTest.php b/tests/model/MongoDriverTest.php index 26c6a1c..551a4fa 100644 --- a/tests/model/MongoDriverTest.php +++ b/tests/model/MongoDriverTest.php @@ -114,9 +114,7 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase */ public function testFind() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $mongo = new MongoDriver($this->conf); $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows()); @@ -145,9 +143,7 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase */ public function testOrderSkipLimit() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $mongo = new MongoDriver($this->conf); @@ -184,9 +180,7 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase */ public function testCount() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $mongo = new MongoDriver($this->conf); $this->assertEquals(5, $mongo->count('items')); $this->assertEquals(2, $mongo->count('items', array('name' => 'eggs'))); @@ -199,9 +193,7 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase */ public function testCursorCount() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $mongo = new MongoDriver($this->conf); $this->assertEquals(5, $mongo->find('items')->count(5)); $this->assertCount(3, $mongo->find('items')->limit(3)->fetchAll()); @@ -214,9 +206,7 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase */ public function testGet() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $mongo = new MongoDriver($this->conf); $eggs = $mongo->get('items', array('name' => 'eggs'))->fetchObject(); @@ -232,9 +222,7 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase */ public function testRemove() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $mongo = new MongoDriver($this->conf); $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows()); @@ -250,9 +238,7 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase */ public function testInsert() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $mongo = new MongoDriver($this->conf); $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows()); @@ -269,9 +255,7 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase */ public function testBatchInsert() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $data = array( array('name' => 'first object'), @@ -292,9 +276,7 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase */ public function testGetInsertId() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $mongo = new MongoDriver($this->conf); $this->assertEquals(0, $mongo->getInsertId()); @@ -316,9 +298,7 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase */ public function testUpdate() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $mongo = new MongoDriver($this->conf); $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows()); @@ -336,9 +316,7 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase */ public function testUpsert() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $mongo = new MongoDriver($this->conf); @@ -353,9 +331,7 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase */ public function testFindAndModify() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $mongo = new MongoDriver($this->conf); @@ -371,9 +347,7 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase */ public function testFindAndModifyNoItem() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $mongo = new MongoDriver($this->conf); @@ -389,9 +363,7 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase */ public function testEvalCommand() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $mongo = new MongoDriver($this->conf); $result = $mongo->command('items', array('$eval' => 'function() { return db.items.count();}')); $this->assertEquals(5, $result->fetch()); @@ -406,9 +378,7 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase */ public function testEval() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $mongo = new MongoDriver($this->conf); $result = $mongo->command('items', array('$eval' => 'function() {return true; }')); $this->assertTrue($result->fetch()); @@ -424,9 +394,7 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase */ public function testCommand() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $mongo = new MongoDriver($this->conf); $result = $mongo->command('items', array('distinct' => 'items', 'key' => 'name')); $this->assertEquals(4, count($result->fetch(DB::FETCH_ASSOC))); diff --git a/tests/model/MongoModelTest.php b/tests/model/MongoModelTest.php index af136a3..c15004e 100644 --- a/tests/model/MongoModelTest.php +++ b/tests/model/MongoModelTest.php @@ -64,9 +64,7 @@ class MongoModelTest extends PHPUnit_Framework_TestCase */ public function testFind() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $result = $this->model->find(); $this->assertInstanceOf('MongoStatement', $result); $this->assertEquals('milk', $result->limit(2)->order(array('name' => -1))->fetch()->name); @@ -83,9 +81,7 @@ class MongoModelTest extends PHPUnit_Framework_TestCase */ public function testGet() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $result = $this->model->find()->limit(1)->order(array('name' => 1)); $result = $result->fetch(); $this->assertEquals('bread', $result->name); @@ -99,9 +95,7 @@ class MongoModelTest extends PHPUnit_Framework_TestCase */ public function testDelete() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $result = $this->model->find()->limit(1)->order(array('name' => 1)); $id = $result->fetch()->_id; $this->assertEquals(1, $this->model->delete($id)); @@ -114,9 +108,7 @@ class MongoModelTest extends PHPUnit_Framework_TestCase */ public function testBatchInsert() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $data = array( array('name' => 'first object'), array('name' => 'second object'), @@ -135,9 +127,7 @@ class MongoModelTest extends PHPUnit_Framework_TestCase */ public function testDeleteAll() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $this->assertEquals(2, $this->model->count(array('name' => 'eggs'))); $this->assertEquals(0, $this->model->deleteAll(array('name' => 'eggs'))); @@ -150,9 +140,7 @@ class MongoModelTest extends PHPUnit_Framework_TestCase */ public function testCount() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $this->assertEquals(5, $this->model->count()); $this->assertEquals(2, $this->model->count(array('name' => 'eggs'))); } @@ -163,9 +151,7 @@ class MongoModelTest extends PHPUnit_Framework_TestCase */ public function testFetch() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $mock = $this->getMock('CacheKey', array('set', 'get')); $mock->expects($this->exactly(3)) @@ -204,9 +190,7 @@ class MongoModelTest extends PHPUnit_Framework_TestCase */ public function testUseMongoId() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $this->assertAttributeEquals(true, 'useMongoId', $this->model); } @@ -217,9 +201,7 @@ class MongoModelTest extends PHPUnit_Framework_TestCase */ public function testId() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $class = new ReflectionClass('MongoModel'); $prop = $class->getProperty('useMongoId'); $prop->setAccessible(true); @@ -239,9 +221,7 @@ class MongoModelTest extends PHPUnit_Framework_TestCase */ public function testIdToMongoId() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $this->model->insert(array('name' => 'testbread', 'price' => 3.2, 'quantity' => 10)); $this->model->insert(array('name' => 'testbread', 'price' => 12, 'quantity' => 2)); diff --git a/tests/model/MongoStatementTest.php b/tests/model/MongoStatementTest.php index b2415f1..a857051 100644 --- a/tests/model/MongoStatementTest.php +++ b/tests/model/MongoStatementTest.php @@ -61,9 +61,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testAffectedNumRowsNoResult() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $this->assertFalse($this->stmt->affectedRows()); $this->assertFalse($this->stmt->numRows()); @@ -82,9 +80,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testAffectedNumRows() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $this->setDriverGetConnectionMethod(); $this->request ->expects($this->once()) @@ -100,9 +96,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testGetInsertId() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $this->setDriverGetConnectionMethod(); $this->request = $this->getMockBuilder('InsertMongoCommand') @@ -131,9 +125,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testExecute() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $this->setDriverGetConnectionMethod()->setRequestExecuteMethod(); $this->assertTrue($this->stmt->execute()); @@ -145,9 +137,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testExecuteNoResult() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $this->setDriverGetConnectionMethod(); $this->request ->expects($this->any()) @@ -163,9 +153,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testExecuteNoConnection() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $this->driver ->expects($this->any()) ->method('getConnection') @@ -180,9 +168,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testExecuteWithDebug() { - if (!defined('DEBUG')) { - define('DEBUG', true); - } + Config::set('DEBUG', 1; $this->setDriverGetConnectionMethod()->setRequestExecuteMethod(); $this->assertTrue($this->stmt->execute()); $this->assertEquals(10, $this->stmt->numRows()); @@ -194,9 +180,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testBindParam() { - if (!defined('DEBUG')) { - define('DEBUG', true); - } + Config::set('DEBUG', 1; $this->request ->expects($this->once()) @@ -217,9 +201,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testFetch() { - if (!defined('DEBUG')) { - define('DEBUG', true); - } + Config::set('DEBUG', 1; $this->assertFalse($this->stmt->fetch()); $this->setDriverGetConnectionMethod()->setRequestForFetch(); @@ -236,9 +218,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testFetchWithInitialArray() { - if (!defined('DEBUG')) { - define('DEBUG', true); - } + Config::set('DEBUG', 1; $this->assertFalse($this->stmt->fetch()); $this->setDriverGetConnectionMethod(); @@ -257,9 +237,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testFetchAssocFromCursor() { - if (!defined('DEBUG')) { - define('DEBUG', true); - } + Config::set('DEBUG', 1; $this->assertFalse($this->stmt->fetch()); $this->setDriverGetConnectionMethod()->setRequestForFetch(); @@ -276,9 +254,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testFetchAssocFromArray() { - if (!defined('DEBUG')) { - define('DEBUG', true); - } + Config::set('DEBUG', 1; $this->assertFalse($this->stmt->fetch()); $this->setDriverGetConnectionMethod(); @@ -298,9 +274,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testFetchWrongMode() { - if (!defined('DEBUG')) { - define('DEBUG', true); - } + Config::set('DEBUG', 1; $this->assertFalse($this->stmt->fetch()); $this->setDriverGetConnectionMethod(); @@ -320,9 +294,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testSkipOrderLimit() { - if (!defined('DEBUG')) { - define('DEBUG', true); - } + Config::set('DEBUG', 1; $this->setDriverGetConnectionMethod()->setRequestForFetch(); $this->stmt->execute(); @@ -340,9 +312,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testCount() { - if (!defined('DEBUG')) { - define('DEBUG', true); - } + Config::set('DEBUG', 1; $this->setDriverGetConnectionMethod()->setRequestForFetch(); $this->stmt->execute(); @@ -357,9 +327,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testCountException() { - if (!defined('DEBUG')) { - define('DEBUG', true); - } + Config::set('DEBUG', 1; $this->setDriverGetConnectionMethod(); $this->request ->expects($this->once()) @@ -377,9 +345,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testOrderException() { - if (!defined('DEBUG')) { - define('DEBUG', true); - } + Config::set('DEBUG', 1; $this->setDriverGetConnectionMethod(); $this->request ->expects($this->once()) @@ -397,9 +363,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testSkipException() { - if (!defined('DEBUG')) { - define('DEBUG', true); - } + Config::set('DEBUG', 1; $this->setDriverGetConnectionMethod(); $this->request ->expects($this->once()) @@ -417,9 +381,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testLimitException() { - if (!defined('DEBUG')) { - define('DEBUG', true); - } + Config::set('DEBUG', 1; $this->setDriverGetConnectionMethod(); $this->request ->expects($this->once()) diff --git a/tests/model/MySQLiDriverTest.php b/tests/model/MySQLiDriverTest.php index d7ddb51..6321190 100644 --- a/tests/model/MySQLiDriverTest.php +++ b/tests/model/MySQLiDriverTest.php @@ -57,9 +57,7 @@ class MySQLiDriverTest extends PHPUnit_Extensions_Database_TestCase */ public function testDriver() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $driver = new MySQLiDriver($this->conf); @@ -78,9 +76,7 @@ class MySQLiDriverTest extends PHPUnit_Extensions_Database_TestCase */ public function testGetConnection() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $driver = new MySQLiDriver($this->conf); $this->assertInstanceOf('mysqli', $driver->getConnection()); @@ -91,9 +87,7 @@ class MySQLiDriverTest extends PHPUnit_Extensions_Database_TestCase */ public function testGetConnectionWrongConfig() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $this->conf['database'] = 'nodb'; $driver = new MySQLiDriver($this->conf); @@ -106,9 +100,7 @@ class MySQLiDriverTest extends PHPUnit_Extensions_Database_TestCase */ public function testDisconnect() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $driver = new MySQLiDriver($this->conf); @@ -125,9 +117,7 @@ class MySQLiDriverTest extends PHPUnit_Extensions_Database_TestCase */ public function testInsert() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $driver = new MySQLiDriver($this->conf); @@ -147,9 +137,7 @@ class MySQLiDriverTest extends PHPUnit_Extensions_Database_TestCase */ public function testGetInsertId() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $driver = new MySQLiDriver($this->conf); @@ -161,9 +149,7 @@ class MySQLiDriverTest extends PHPUnit_Extensions_Database_TestCase */ public function testTransaction() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $driver = new MySQLiDriver($this->conf); @@ -194,9 +180,7 @@ class MySQLiDriverTest extends PHPUnit_Extensions_Database_TestCase */ public function testRollback() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $driver = new MySQLiDriver($this->conf); diff --git a/tests/model/MySQLiStatementTest.php b/tests/model/MySQLiStatementTest.php index 83dfc96..e372c01 100644 --- a/tests/model/MySQLiStatementTest.php +++ b/tests/model/MySQLiStatementTest.php @@ -81,9 +81,7 @@ class MySQLiStatementTest extends PHPUnit_Framework_TestCase */ public function testExecute() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $this->driver ->expects($this->any()) @@ -102,9 +100,7 @@ class MySQLiStatementTest extends PHPUnit_Framework_TestCase */ public function testExecuteNoPlaceholders() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $this->setDriverGetConnectionMethod(); $this->sql = 'PLAIN SQL'; @@ -118,9 +114,7 @@ class MySQLiStatementTest extends PHPUnit_Framework_TestCase */ public function testFetchNoResult() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $this->assertFalse($this->stmt->fetch()); } @@ -130,9 +124,7 @@ class MySQLiStatementTest extends PHPUnit_Framework_TestCase */ public function testDriverExecuteNoResult() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $this->setDriverGetConnectionWrongResultMethod(); $this->setExpectedException('GeneralException', 'ERROR'); $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val')); @@ -144,9 +136,7 @@ class MySQLiStatementTest extends PHPUnit_Framework_TestCase */ public function testFetch() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $this->setDriverGetConnectionMethod(); $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val')); $this->assertSame('OBJECT', $this->stmt->fetch()); @@ -161,9 +151,7 @@ class MySQLiStatementTest extends PHPUnit_Framework_TestCase */ public function testFetchObject() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $this->setDriverGetConnectionMethod(); $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val')); $this->assertSame('OBJECT', $this->stmt->fetchObject()); @@ -175,9 +163,7 @@ class MySQLiStatementTest extends PHPUnit_Framework_TestCase */ public function testFetchPairs() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $resultMock = $this->getMockBuilder('mysqli_result') ->disableOriginalConstructor() @@ -232,9 +218,7 @@ class MySQLiStatementTest extends PHPUnit_Framework_TestCase */ public function testClose() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $this->assertAttributeEquals(null, 'result', $this->stmt); $this->setDriverGetConnectionMethod(); $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val')); @@ -275,9 +259,7 @@ class MySQLiStatementTest extends PHPUnit_Framework_TestCase public function testNumRows() { $this->markTestSkipped('Unable to execute a method or access a property of an incomplete object.'); - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $this->setDriverGetConnectionMethod(); $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val')); $this->assertNull($this->stmt->numRows()); @@ -289,9 +271,7 @@ class MySQLiStatementTest extends PHPUnit_Framework_TestCase */ public function testFetchInvalidMode() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $this->setDriverGetConnectionMethod(); $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val')); diff --git a/tests/redis/RedisDebugTest.php b/tests/redis/RedisDebugTest.php index 83642f7..e79a063 100644 --- a/tests/redis/RedisDebugTest.php +++ b/tests/redis/RedisDebugTest.php @@ -57,9 +57,7 @@ class RedisDebugTest extends PHPUnit_Framework_TestCase */ public function testCallSimpleParams() { - if (!defined('DEBUG')) { - define('DEBUG', true); - } + Config::set('DEBUG', 1); $mock = $this->getMock('Redis', array('connect')); $mock->expects($this->once()) @@ -77,9 +75,7 @@ class RedisDebugTest extends PHPUnit_Framework_TestCase */ public function testCallArrayParam() { - if (!defined('DEBUG')) { - define('DEBUG', true); - } + Config::set('DEBUG', 1); $mock = $this->getMock('Redis', array('connect')); $mock->expects($this->once()) @@ -97,9 +93,7 @@ class RedisDebugTest extends PHPUnit_Framework_TestCase */ public function testCallUndefinedMethod() { - if (!defined('DEBUG')) { - define('DEBUG', true); - } + Config::set('DEBUG', 1); $mock = $this->getMock('Redis', array('connect')); $redisDebug = new RedisDebug($mock); diff --git a/tests/redis/RedisManagerTest.php b/tests/redis/RedisManagerTest.php index fed1e28..a64769e 100644 --- a/tests/redis/RedisManagerTest.php +++ b/tests/redis/RedisManagerTest.php @@ -107,9 +107,7 @@ class RedisManagerTest extends PHPUnit_Framework_TestCase */ public function testConnectWithDebug() { - if (!defined('DEBUG')) { - define('DEBUG', true); - } + Config::set('DEBUG', 1); $this->getMock('RedisDebug'); Config::set('Redis', array('new' => array('host' => true, 'port' => 2023, 'database' => true))); diff --git a/tests/util/profiler/ProfilerTest.php b/tests/util/profiler/ProfilerTest.php index 554ecf2..3fef636 100644 --- a/tests/util/profiler/ProfilerTest.php +++ b/tests/util/profiler/ProfilerTest.php @@ -36,9 +36,7 @@ class ProfilerTest extends PHPUnit_Framework_TestCase */ public function testGetInstaceNoDebug() { - if (!defined('DEBUG')) { - define('DEBUG', false); - } + Config::set('DEBUG', 0); $this->setExpectedException('GeneralException', 'Need to turn on DEBUG before use.'); Profiler::getInstance(); } @@ -48,9 +46,7 @@ class ProfilerTest extends PHPUnit_Framework_TestCase */ public function testGetInstance() { - if (!defined('DEBUG')) { - define('DEBUG', true); - } + Config::set('DEBUG', 1); $profiler = Profiler::getInstance(); $this->assertInstanceOf('Profiler', $profiler); $this->assertSame($profiler, Profiler::getInstance()); @@ -61,9 +57,7 @@ class ProfilerTest extends PHPUnit_Framework_TestCase */ public function testProfilerCommand() { - if (!defined('DEBUG')) { - define('DEBUG', true); - } + Config::set('DEBUG', 1); $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false); $profiler = Profiler::getInstance(); $cmdProfiler = $profiler->profilerCommand('command', 'type'); @@ -75,9 +69,7 @@ class ProfilerTest extends PHPUnit_Framework_TestCase */ public function testStartEndNoCommandProfiler() { - if (!defined('DEBUG')) { - define('DEBUG', true); - } + Config::set('DEBUG', 1); $profiler = Profiler::getInstance(); $profiler->start(); $result = $profiler->end(''); @@ -95,9 +87,7 @@ class ProfilerTest extends PHPUnit_Framework_TestCase */ public function testStartEndWithCommandProfiler() { - if (!defined('DEBUG')) { - define('DEBUG', true); - } + Config::set('DEBUG', 1); $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false); $profiler = Profiler::getInstance(); @@ -115,9 +105,7 @@ class ProfilerTest extends PHPUnit_Framework_TestCase */ public function testGetJSON() { - if (!defined('DEBUG')) { - define('DEBUG', true); - } + Config::set('DEBUG', 1); $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false); $profiler = Profiler::getInstance(); @@ -130,9 +118,7 @@ class ProfilerTest extends PHPUnit_Framework_TestCase */ public function testGetCLI() { - if (!defined('DEBUG')) { - define('DEBUG', true); - } + Config::set('DEBUG', 1); $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false); $profiler = Profiler::getInstance(); diff --git a/util/profiler/Profiler.php b/util/profiler/Profiler.php index 0f423e2..68fa9dc 100644 --- a/util/profiler/Profiler.php +++ b/util/profiler/Profiler.php @@ -22,7 +22,7 @@ class Profiler private function __construct() { - if (DEBUG == false) { + if (Config::get('DEBUG') == false) { throw new GeneralException('Need to turn on DEBUG before use.'); } } From 7a26da1034aee2229797efe15d5cbceeba236915 Mon Sep 17 00:00:00 2001 From: Alexander Demidov Date: Thu, 28 Jun 2012 16:18:28 +0400 Subject: [PATCH 03/25] Fixed tests with inc Config and Registry classes. Use LOGGING in logger tests. --- tests/LoadTest.php | 2 ++ tests/app/Action_TestCase.php | 2 ++ tests/layout/ErrorLayoutTest.php | 2 ++ tests/layout/LayoutTest.php | 2 ++ tests/logger/CliLoggerTest.php | 3 ++- tests/logger/FileLoggerTest.php | 1 + tests/model/MongoStatementTest.php | 28 +++++++++++++++------------- tests/model/MySQLiStatementTest.php | 2 ++ tests/redis/RedisDebugTest.php | 2 ++ tests/util/profiler/ProfilerTest.php | 2 ++ 10 files changed, 32 insertions(+), 14 deletions(-) diff --git a/tests/LoadTest.php b/tests/LoadTest.php index 104f9ee..2ea7e76 100644 --- a/tests/LoadTest.php +++ b/tests/LoadTest.php @@ -10,6 +10,8 @@ * Unit tests for Load class */ +require_once dirname(__FILE__) . '/../Registry.php'; +require_once dirname(__FILE__) . '/../Config.php'; require_once dirname(__FILE__) . '/../Load.php'; require_once 'vfsStream/vfsStream.php'; diff --git a/tests/app/Action_TestCase.php b/tests/app/Action_TestCase.php index 44abf38..d552117 100644 --- a/tests/app/Action_TestCase.php +++ b/tests/app/Action_TestCase.php @@ -10,6 +10,8 @@ * Action_TestCase class for testing Actions */ +require_once dirname(__FILE__) . '/../../Registry.php'; +require_once dirname(__FILE__) . '/../../Config.php'; require_once dirname(__FILE__) . '/../../Load.php'; require_once dirname(__FILE__) . '/../../classes/Env.class.php'; require_once dirname(__FILE__) . '/../../exception/ErrorHandler.php'; diff --git a/tests/layout/ErrorLayoutTest.php b/tests/layout/ErrorLayoutTest.php index 9affe12..7ec97fd 100644 --- a/tests/layout/ErrorLayoutTest.php +++ b/tests/layout/ErrorLayoutTest.php @@ -8,6 +8,8 @@ * @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'; diff --git a/tests/layout/LayoutTest.php b/tests/layout/LayoutTest.php index e7029ea..44038a4 100644 --- a/tests/layout/LayoutTest.php +++ b/tests/layout/LayoutTest.php @@ -10,6 +10,8 @@ * Unit tests for Layout class */ +require_once dirname(__FILE__) . '/../../Registry.php'; +require_once dirname(__FILE__) . '/../../Config.php'; require_once dirname(__FILE__) . '/../../exception/ErrorHandler.php'; require_once dirname(__FILE__) . '/../../app/FrontController.php'; require_once dirname(__FILE__) . '/../../layout/Layout.php'; diff --git a/tests/logger/CliLoggerTest.php b/tests/logger/CliLoggerTest.php index 8874834..ae6fa45 100644 --- a/tests/logger/CliLoggerTest.php +++ b/tests/logger/CliLoggerTest.php @@ -44,7 +44,8 @@ class CliLoggerTest extends PHPUnit_Framework_TestCase public function testLog() { - Config::set('DEBUG', 1); + Config::set('LOGGING', 1); + Config::set('Logger', array('logger' => 'CliLogger')); $logger = Logger::getInstance(); ob_start(); $logger->setPid(123); diff --git a/tests/logger/FileLoggerTest.php b/tests/logger/FileLoggerTest.php index cfe763e..c108f3d 100644 --- a/tests/logger/FileLoggerTest.php +++ b/tests/logger/FileLoggerTest.php @@ -33,6 +33,7 @@ class FileLoggerTest extends PHPUnit_Framework_TestCase vfsStream::setup(); $root = vfsStream::create(array()); vfsStreamWrapper::setRoot($root); + Config::set('LOGGING', 1); $this->conf = array('logger' => 'FileLogger', 'filepath' => vfsStream::url('root/log.txt')); Config::set('Logger', $this->conf); if ($root->hasChild('log.txt')) { diff --git a/tests/model/MongoStatementTest.php b/tests/model/MongoStatementTest.php index a857051..6bcd833 100644 --- a/tests/model/MongoStatementTest.php +++ b/tests/model/MongoStatementTest.php @@ -10,6 +10,8 @@ * Unit tests for MySQLiStatement class */ +require_once dirname(__FILE__) . '/../../Registry.php'; +require_once dirname(__FILE__) . '/../../Config.php'; require_once dirname(__FILE__) . '/../../util/profiler/CommandProfiler.php'; require_once dirname(__FILE__) . '/../../util/profiler/Profiler.php'; require_once dirname(__FILE__) . '/../../model/Db.php'; @@ -168,7 +170,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testExecuteWithDebug() { - Config::set('DEBUG', 1; + Config::set('DEBUG', 1); $this->setDriverGetConnectionMethod()->setRequestExecuteMethod(); $this->assertTrue($this->stmt->execute()); $this->assertEquals(10, $this->stmt->numRows()); @@ -180,7 +182,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testBindParam() { - Config::set('DEBUG', 1; + Config::set('DEBUG', 1); $this->request ->expects($this->once()) @@ -201,7 +203,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testFetch() { - Config::set('DEBUG', 1; + Config::set('DEBUG', 1); $this->assertFalse($this->stmt->fetch()); $this->setDriverGetConnectionMethod()->setRequestForFetch(); @@ -218,7 +220,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testFetchWithInitialArray() { - Config::set('DEBUG', 1; + Config::set('DEBUG', 1); $this->assertFalse($this->stmt->fetch()); $this->setDriverGetConnectionMethod(); @@ -237,7 +239,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testFetchAssocFromCursor() { - Config::set('DEBUG', 1; + Config::set('DEBUG', 1); $this->assertFalse($this->stmt->fetch()); $this->setDriverGetConnectionMethod()->setRequestForFetch(); @@ -254,7 +256,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testFetchAssocFromArray() { - Config::set('DEBUG', 1; + Config::set('DEBUG', 1); $this->assertFalse($this->stmt->fetch()); $this->setDriverGetConnectionMethod(); @@ -274,7 +276,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testFetchWrongMode() { - Config::set('DEBUG', 1; + Config::set('DEBUG', 1); $this->assertFalse($this->stmt->fetch()); $this->setDriverGetConnectionMethod(); @@ -294,7 +296,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testSkipOrderLimit() { - Config::set('DEBUG', 1; + Config::set('DEBUG', 1); $this->setDriverGetConnectionMethod()->setRequestForFetch(); $this->stmt->execute(); @@ -312,7 +314,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testCount() { - Config::set('DEBUG', 1; + Config::set('DEBUG', 1); $this->setDriverGetConnectionMethod()->setRequestForFetch(); $this->stmt->execute(); @@ -327,7 +329,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testCountException() { - Config::set('DEBUG', 1; + Config::set('DEBUG', 1); $this->setDriverGetConnectionMethod(); $this->request ->expects($this->once()) @@ -345,7 +347,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testOrderException() { - Config::set('DEBUG', 1; + Config::set('DEBUG', 1); $this->setDriverGetConnectionMethod(); $this->request ->expects($this->once()) @@ -363,7 +365,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testSkipException() { - Config::set('DEBUG', 1; + Config::set('DEBUG', 1); $this->setDriverGetConnectionMethod(); $this->request ->expects($this->once()) @@ -381,7 +383,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testLimitException() { - Config::set('DEBUG', 1; + Config::set('DEBUG', 1); $this->setDriverGetConnectionMethod(); $this->request ->expects($this->once()) diff --git a/tests/model/MySQLiStatementTest.php b/tests/model/MySQLiStatementTest.php index e372c01..e360150 100644 --- a/tests/model/MySQLiStatementTest.php +++ b/tests/model/MySQLiStatementTest.php @@ -10,6 +10,8 @@ * Unit tests for MySQLiStatement class */ +require_once dirname(__FILE__) . '/../../Registry.php'; +require_once dirname(__FILE__) . '/../../Config.php'; require_once dirname(__FILE__) . '/../../util/profiler/CommandProfiler.php'; require_once dirname(__FILE__) . '/../../util/profiler/Profiler.php'; require_once dirname(__FILE__) . '/../../model/Db.php'; diff --git a/tests/redis/RedisDebugTest.php b/tests/redis/RedisDebugTest.php index e79a063..c479daa 100644 --- a/tests/redis/RedisDebugTest.php +++ b/tests/redis/RedisDebugTest.php @@ -10,6 +10,8 @@ * Unit tests for RedisDebug 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/profiler/CommandProfiler.php'; require_once dirname(__FILE__) . '/../../util/profiler/Profiler.php'; diff --git a/tests/util/profiler/ProfilerTest.php b/tests/util/profiler/ProfilerTest.php index 3fef636..1e7000d 100644 --- a/tests/util/profiler/ProfilerTest.php +++ b/tests/util/profiler/ProfilerTest.php @@ -10,6 +10,8 @@ * 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'; From 94b8fadbc45b0331d1013478c36224cac96bc6fa Mon Sep 17 00:00:00 2001 From: Alexander Demidov Date: Fri, 29 Jun 2012 13:00:29 +0400 Subject: [PATCH 04/25] Replace ob_end_clean to ob_clean in ErrorHandler class error_handler method. --- exception/ErrorHandler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exception/ErrorHandler.php b/exception/ErrorHandler.php index eec155e..6600de3 100644 --- a/exception/ErrorHandler.php +++ b/exception/ErrorHandler.php @@ -21,7 +21,7 @@ class ErrorHandler { $ob_handlers = ob_get_status(); if (!empty($ob_handlers)) { - ob_end_clean(); + ob_clean(); } throw new ErrorException($errstr, 0, $errno, $errfile, $errline); } From c836b5974e9010a17e0bdd0008883d975c3f0caf Mon Sep 17 00:00:00 2001 From: Alexander Demidov Date: Wed, 4 Jul 2012 17:30:05 +0400 Subject: [PATCH 05/25] Replace checking DEBUG in constructor Profiler to checking PROFILER is it true. --- util/profiler/Profiler.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/util/profiler/Profiler.php b/util/profiler/Profiler.php index 68fa9dc..e883b4c 100644 --- a/util/profiler/Profiler.php +++ b/util/profiler/Profiler.php @@ -22,8 +22,8 @@ class Profiler private function __construct() { - if (Config::get('DEBUG') == false) { - throw new GeneralException('Need to turn on DEBUG before use.'); + if (Config::get('PROFILER') == false) { + throw new GeneralException('Need to turn on PROFILER before use.'); } } From dc60d703bfbaaf6f499beae16e3713ba8043ae18 Mon Sep 17 00:00:00 2001 From: Alexander Demidov Date: Mon, 9 Jul 2012 12:52:52 +0400 Subject: [PATCH 06/25] Return ob_end_clean to ErroHandler. --- exception/ErrorHandler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exception/ErrorHandler.php b/exception/ErrorHandler.php index 6600de3..eec155e 100644 --- a/exception/ErrorHandler.php +++ b/exception/ErrorHandler.php @@ -21,7 +21,7 @@ class ErrorHandler { $ob_handlers = ob_get_status(); if (!empty($ob_handlers)) { - ob_clean(); + ob_end_clean(); } throw new ErrorException($errstr, 0, $errno, $errfile, $errline); } From b6a2c4dbc3a7dc0ca5e94cfde9926eaf7adb0132 Mon Sep 17 00:00:00 2001 From: Alexander Demidov Date: Mon, 9 Jul 2012 13:47:32 +0400 Subject: [PATCH 07/25] Replace 0 to false and 1 to true in Config:;set(DEBUG...) in majestic tests. --- tests/LoadTest.php | 2 +- tests/app/ActionTest.php | 8 ++++---- tests/app/AjaxActionTest.php | 6 +++--- tests/app/ErrorActionTest.php | 2 +- tests/app/PagerActionTest.php | 10 +++++----- tests/app/StaticActionTest.php | 4 ++-- tests/classes/EnvTest.php | 4 ++-- tests/layout/ErrorLayoutTest.php | 4 ++-- tests/layout/LayoutTest.php | 8 ++++---- tests/logger/FileLoggerTest.php | 6 +++--- tests/model/MongoDriverTest.php | 32 +++++++++++++++--------------- tests/model/MongoModelTest.php | 20 +++++++++---------- tests/model/MongoStatementTest.php | 38 ++++++++++++++++++------------------ tests/model/MySQLiDriverTest.php | 16 +++++++-------- tests/model/MySQLiStatementTest.php | 20 +++++++++---------- tests/redis/RedisDebugTest.php | 6 +++--- tests/redis/RedisManagerTest.php | 2 +- tests/util/profiler/ProfilerTest.php | 14 ++++++------- 18 files changed, 101 insertions(+), 101 deletions(-) diff --git a/tests/LoadTest.php b/tests/LoadTest.php index 2ea7e76..34f3097 100644 --- a/tests/LoadTest.php +++ b/tests/LoadTest.php @@ -166,7 +166,7 @@ class LoadTest extends PHPUnit_Framework_TestCase $autoload = require(self::$file); $this->assertNotEmpty($autoload); - Config::set('DEBUG', 1); + Config::set('DEBUG', true); Load::autoload('Some'); Load::autoload('DbDriver'); } diff --git a/tests/app/ActionTest.php b/tests/app/ActionTest.php index 7fd03ed..6e41b5f 100644 --- a/tests/app/ActionTest.php +++ b/tests/app/ActionTest.php @@ -20,7 +20,7 @@ class ActionTest extends Action_TestCase */ public function testActionConstructWithParams() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); Env::setParams(array('param1' => 'value1', 'param2' => 'value2')); $action = $this->getMockForAbstractClass('Action' ); $this->assertSame('value1', $action->param1); @@ -31,7 +31,7 @@ class ActionTest extends Action_TestCase */ public function testFetch() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $load = new ReflectionClass('Load'); $classes = $load->getProperty('autoload'); $classes->setAccessible(true); @@ -49,7 +49,7 @@ class ActionTest extends Action_TestCase */ public function testFetchNoTemplate() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); Env::setParams(array('template' => '')); $controller = FrontController::getInstance(); $controller->setView('SomeView'); @@ -65,7 +65,7 @@ class ActionTest extends Action_TestCase { set_exit_overload(function() { return false; }); - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $load = new ReflectionClass('Action'); $redirect = $load->getMethod('redirect'); $redirect->setAccessible(true); diff --git a/tests/app/AjaxActionTest.php b/tests/app/AjaxActionTest.php index b8f22d2..b8c7c82 100644 --- a/tests/app/AjaxActionTest.php +++ b/tests/app/AjaxActionTest.php @@ -22,7 +22,7 @@ class AjaxActionTest extends Action_TestCase public function testConstruct() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); Env::setParams(array('ajax' => 'AjaxTemplate', 'param2' => 'value2')); $action = $this->getMockForAbstractClass('AjaxAction' ); $this->assertAttributeEquals('ajax', 'template', $action); @@ -34,7 +34,7 @@ class AjaxActionTest extends Action_TestCase public function testFetchWithEncode() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $controller = FrontController::getInstance(); $controller->setView('SomeView'); $action = $this->getMockForAbstractClass('AjaxAction' ); @@ -50,7 +50,7 @@ class AjaxActionTest extends Action_TestCase public function testFetchNoEncode() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); Env::setParams(array('encode' => false)); $controller = FrontController::getInstance(); $controller->setView('SomeView'); diff --git a/tests/app/ErrorActionTest.php b/tests/app/ErrorActionTest.php index 466770a..15a8896 100644 --- a/tests/app/ErrorActionTest.php +++ b/tests/app/ErrorActionTest.php @@ -81,7 +81,7 @@ class ErrorActionTest extends Action_TestCase public function testFetchNoTemplate() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $exception = $this->getMock('ErrorException'); $controller = FrontController::getInstance(); $controller->setView('SomeView'); diff --git a/tests/app/PagerActionTest.php b/tests/app/PagerActionTest.php index 386a4e7..1545665 100644 --- a/tests/app/PagerActionTest.php +++ b/tests/app/PagerActionTest.php @@ -22,7 +22,7 @@ class PagerActionTest extends Action_TestCase public function testConstructWithParams() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $action = $this->getMockForAbstractClass('PagerAction'); $this->assertSame(20, $action->getLimit()); $action = $this->getMockForAbstractClass('PagerAction', array(50)); @@ -35,7 +35,7 @@ class PagerActionTest extends Action_TestCase public function testSetCount() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $action = $this->getMockForAbstractClass('PagerAction'); $action->setCount(50); $this->assertSame(1, $action->page); @@ -59,7 +59,7 @@ class PagerActionTest extends Action_TestCase public function testGetOffset() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $action = $this->getMockForAbstractClass('PagerAction'); $this->assertSame(0, $action->getOffset()); } @@ -70,7 +70,7 @@ class PagerActionTest extends Action_TestCase public function testFetchNoTemplate() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); Env::setParams(array('template' => '')); $controller = FrontController::getInstance(); $controller->setView('SomeView'); @@ -85,7 +85,7 @@ class PagerActionTest extends Action_TestCase public function testFetchWithTemplate() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); Env::setParams(array('template' => 'SomeTemplate')); $controller = FrontController::getInstance(); $controller->setView('SomeView'); diff --git a/tests/app/StaticActionTest.php b/tests/app/StaticActionTest.php index 283e482..cbd34f5 100644 --- a/tests/app/StaticActionTest.php +++ b/tests/app/StaticActionTest.php @@ -22,7 +22,7 @@ class StaticActionTest extends Action_TestCase public function testFetchNoTemplate() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); Env::setParams(array('template' => '')); $controller = FrontController::getInstance(); $controller->setView('SomeView'); @@ -37,7 +37,7 @@ class StaticActionTest extends Action_TestCase public function testFetchWithTemplate() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); Env::setParams(array('template' => 'SomeTemplate')); $controller = FrontController::getInstance(); $controller->setView('SomeView'); diff --git a/tests/classes/EnvTest.php b/tests/classes/EnvTest.php index 89d3659..60ca94a 100644 --- a/tests/classes/EnvTest.php +++ b/tests/classes/EnvTest.php @@ -25,7 +25,7 @@ class EnvTest extends PHPUnit_Framework_TestCase public function testGetRequestUri() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $_SERVER['REQUEST_URI'] = '/test/index.php?id=1&test=wet&id_theme=512'; $this->assertSame('/test/index.php', Env::getRequestUri()); $_SERVER['REQUEST_URI'] = '/tes?t/index.php?id=1&test=wet&id_theme=512'; @@ -38,7 +38,7 @@ class EnvTest extends PHPUnit_Framework_TestCase public function testTrimBaseRequestUri() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $class = new ReflectionClass('Env'); $this->started = $class->getProperty('request'); $this->started->setAccessible(true); diff --git a/tests/layout/ErrorLayoutTest.php b/tests/layout/ErrorLayoutTest.php index 7ec97fd..b8e8b5e 100644 --- a/tests/layout/ErrorLayoutTest.php +++ b/tests/layout/ErrorLayoutTest.php @@ -43,7 +43,7 @@ class ErrorLayoutTest extends PHPUnit_Framework_TestCase public function testSetException() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $layout = new ErrorLayout(); $layout->setException(new GeneralException()); $this->assertAttributeInstanceOf('GeneralException', 'exception', $layout); @@ -52,7 +52,7 @@ class ErrorLayoutTest extends PHPUnit_Framework_TestCase public function testExecute() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $action = $this->getMock('Action', array('fetch')); $action->expects($this->once()) ->method('fetch') diff --git a/tests/layout/LayoutTest.php b/tests/layout/LayoutTest.php index 44038a4..dd785b6 100644 --- a/tests/layout/LayoutTest.php +++ b/tests/layout/LayoutTest.php @@ -43,7 +43,7 @@ class LayoutTest extends PHPUnit_Framework_TestCase public function testConstruct() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $layout = $this->getMockForAbstractClass('Layout'); $this->assertAttributeInstanceOf('PHPView', 'view', $layout); } @@ -51,7 +51,7 @@ class LayoutTest extends PHPUnit_Framework_TestCase public function testFetch() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $layout = $this->getMockForAbstractClass('Layout'); $action = $this->getMock('Action', array('fetch')); @@ -65,7 +65,7 @@ class LayoutTest extends PHPUnit_Framework_TestCase public function testFetchWithTemplate() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $layout = $this->getMockForAbstractClass('Layout'); $class = new ReflectionClass('Layout'); @@ -89,7 +89,7 @@ class LayoutTest extends PHPUnit_Framework_TestCase public function testAppend() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $layout = $this->getMockForAbstractClass('Layout', array('append', 'prepend'), 'LayoutMock'); $action = $this->getMock('Action', array('fetch')); diff --git a/tests/logger/FileLoggerTest.php b/tests/logger/FileLoggerTest.php index c108f3d..12c905f 100644 --- a/tests/logger/FileLoggerTest.php +++ b/tests/logger/FileLoggerTest.php @@ -55,7 +55,7 @@ class FileLoggerTest extends PHPUnit_Framework_TestCase */ public function testCannotWrite() { - Config::set('DEBUG', 1); + Config::set('DEBUG', true); $conf = array('logger' => 'FileLogger', 'filepath' => '/log.txt'); Config::set('Logger', $conf); @@ -70,7 +70,7 @@ class FileLoggerTest extends PHPUnit_Framework_TestCase */ public function testLog() { - Config::set('DEBUG', 1); + Config::set('DEBUG', true); $this->assertFileNotExists($this->conf['filepath']); $logger = Logger::getInstance(); $logger->setPid(123); @@ -83,7 +83,7 @@ class FileLoggerTest extends PHPUnit_Framework_TestCase */ public function testDestruct() { - Config::set('DEBUG', 1); + Config::set('DEBUG', true); $my_pid = posix_getpid(); $fd_command = 'lsof -n -p ' . $my_pid . ' | wc -l'; $fd_count_start = (int) `$fd_command`; diff --git a/tests/model/MongoDriverTest.php b/tests/model/MongoDriverTest.php index 551a4fa..63cfd1e 100644 --- a/tests/model/MongoDriverTest.php +++ b/tests/model/MongoDriverTest.php @@ -114,7 +114,7 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase */ public function testFind() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $mongo = new MongoDriver($this->conf); $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows()); @@ -143,7 +143,7 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase */ public function testOrderSkipLimit() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $mongo = new MongoDriver($this->conf); @@ -180,7 +180,7 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase */ public function testCount() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $mongo = new MongoDriver($this->conf); $this->assertEquals(5, $mongo->count('items')); $this->assertEquals(2, $mongo->count('items', array('name' => 'eggs'))); @@ -193,7 +193,7 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase */ public function testCursorCount() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $mongo = new MongoDriver($this->conf); $this->assertEquals(5, $mongo->find('items')->count(5)); $this->assertCount(3, $mongo->find('items')->limit(3)->fetchAll()); @@ -206,7 +206,7 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase */ public function testGet() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $mongo = new MongoDriver($this->conf); $eggs = $mongo->get('items', array('name' => 'eggs'))->fetchObject(); @@ -222,7 +222,7 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase */ public function testRemove() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $mongo = new MongoDriver($this->conf); $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows()); @@ -238,7 +238,7 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase */ public function testInsert() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $mongo = new MongoDriver($this->conf); $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows()); @@ -255,7 +255,7 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase */ public function testBatchInsert() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $data = array( array('name' => 'first object'), @@ -276,7 +276,7 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase */ public function testGetInsertId() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $mongo = new MongoDriver($this->conf); $this->assertEquals(0, $mongo->getInsertId()); @@ -298,7 +298,7 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase */ public function testUpdate() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $mongo = new MongoDriver($this->conf); $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows()); @@ -316,7 +316,7 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase */ public function testUpsert() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $mongo = new MongoDriver($this->conf); @@ -331,7 +331,7 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase */ public function testFindAndModify() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $mongo = new MongoDriver($this->conf); @@ -347,7 +347,7 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase */ public function testFindAndModifyNoItem() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $mongo = new MongoDriver($this->conf); @@ -363,7 +363,7 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase */ public function testEvalCommand() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $mongo = new MongoDriver($this->conf); $result = $mongo->command('items', array('$eval' => 'function() { return db.items.count();}')); $this->assertEquals(5, $result->fetch()); @@ -378,7 +378,7 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase */ public function testEval() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $mongo = new MongoDriver($this->conf); $result = $mongo->command('items', array('$eval' => 'function() {return true; }')); $this->assertTrue($result->fetch()); @@ -394,7 +394,7 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase */ public function testCommand() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $mongo = new MongoDriver($this->conf); $result = $mongo->command('items', array('distinct' => 'items', 'key' => 'name')); $this->assertEquals(4, count($result->fetch(DB::FETCH_ASSOC))); diff --git a/tests/model/MongoModelTest.php b/tests/model/MongoModelTest.php index c15004e..848dc99 100644 --- a/tests/model/MongoModelTest.php +++ b/tests/model/MongoModelTest.php @@ -64,7 +64,7 @@ class MongoModelTest extends PHPUnit_Framework_TestCase */ public function testFind() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $result = $this->model->find(); $this->assertInstanceOf('MongoStatement', $result); $this->assertEquals('milk', $result->limit(2)->order(array('name' => -1))->fetch()->name); @@ -81,7 +81,7 @@ class MongoModelTest extends PHPUnit_Framework_TestCase */ public function testGet() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $result = $this->model->find()->limit(1)->order(array('name' => 1)); $result = $result->fetch(); $this->assertEquals('bread', $result->name); @@ -95,7 +95,7 @@ class MongoModelTest extends PHPUnit_Framework_TestCase */ public function testDelete() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $result = $this->model->find()->limit(1)->order(array('name' => 1)); $id = $result->fetch()->_id; $this->assertEquals(1, $this->model->delete($id)); @@ -108,7 +108,7 @@ class MongoModelTest extends PHPUnit_Framework_TestCase */ public function testBatchInsert() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $data = array( array('name' => 'first object'), array('name' => 'second object'), @@ -127,7 +127,7 @@ class MongoModelTest extends PHPUnit_Framework_TestCase */ public function testDeleteAll() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $this->assertEquals(2, $this->model->count(array('name' => 'eggs'))); $this->assertEquals(0, $this->model->deleteAll(array('name' => 'eggs'))); @@ -140,7 +140,7 @@ class MongoModelTest extends PHPUnit_Framework_TestCase */ public function testCount() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $this->assertEquals(5, $this->model->count()); $this->assertEquals(2, $this->model->count(array('name' => 'eggs'))); } @@ -151,7 +151,7 @@ class MongoModelTest extends PHPUnit_Framework_TestCase */ public function testFetch() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $mock = $this->getMock('CacheKey', array('set', 'get')); $mock->expects($this->exactly(3)) @@ -190,7 +190,7 @@ class MongoModelTest extends PHPUnit_Framework_TestCase */ public function testUseMongoId() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $this->assertAttributeEquals(true, 'useMongoId', $this->model); } @@ -201,7 +201,7 @@ class MongoModelTest extends PHPUnit_Framework_TestCase */ public function testId() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $class = new ReflectionClass('MongoModel'); $prop = $class->getProperty('useMongoId'); $prop->setAccessible(true); @@ -221,7 +221,7 @@ class MongoModelTest extends PHPUnit_Framework_TestCase */ public function testIdToMongoId() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $this->model->insert(array('name' => 'testbread', 'price' => 3.2, 'quantity' => 10)); $this->model->insert(array('name' => 'testbread', 'price' => 12, 'quantity' => 2)); diff --git a/tests/model/MongoStatementTest.php b/tests/model/MongoStatementTest.php index 6bcd833..5a854ac 100644 --- a/tests/model/MongoStatementTest.php +++ b/tests/model/MongoStatementTest.php @@ -63,7 +63,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testAffectedNumRowsNoResult() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $this->assertFalse($this->stmt->affectedRows()); $this->assertFalse($this->stmt->numRows()); @@ -82,7 +82,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testAffectedNumRows() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $this->setDriverGetConnectionMethod(); $this->request ->expects($this->once()) @@ -98,7 +98,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testGetInsertId() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $this->setDriverGetConnectionMethod(); $this->request = $this->getMockBuilder('InsertMongoCommand') @@ -127,7 +127,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testExecute() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $this->setDriverGetConnectionMethod()->setRequestExecuteMethod(); $this->assertTrue($this->stmt->execute()); @@ -139,7 +139,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testExecuteNoResult() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $this->setDriverGetConnectionMethod(); $this->request ->expects($this->any()) @@ -155,7 +155,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testExecuteNoConnection() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $this->driver ->expects($this->any()) ->method('getConnection') @@ -170,7 +170,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testExecuteWithDebug() { - Config::set('DEBUG', 1); + Config::set('DEBUG', true); $this->setDriverGetConnectionMethod()->setRequestExecuteMethod(); $this->assertTrue($this->stmt->execute()); $this->assertEquals(10, $this->stmt->numRows()); @@ -182,7 +182,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testBindParam() { - Config::set('DEBUG', 1); + Config::set('DEBUG', true); $this->request ->expects($this->once()) @@ -203,7 +203,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testFetch() { - Config::set('DEBUG', 1); + Config::set('DEBUG', true); $this->assertFalse($this->stmt->fetch()); $this->setDriverGetConnectionMethod()->setRequestForFetch(); @@ -220,7 +220,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testFetchWithInitialArray() { - Config::set('DEBUG', 1); + Config::set('DEBUG', true); $this->assertFalse($this->stmt->fetch()); $this->setDriverGetConnectionMethod(); @@ -239,7 +239,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testFetchAssocFromCursor() { - Config::set('DEBUG', 1); + Config::set('DEBUG', true); $this->assertFalse($this->stmt->fetch()); $this->setDriverGetConnectionMethod()->setRequestForFetch(); @@ -256,7 +256,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testFetchAssocFromArray() { - Config::set('DEBUG', 1); + Config::set('DEBUG', true); $this->assertFalse($this->stmt->fetch()); $this->setDriverGetConnectionMethod(); @@ -276,7 +276,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testFetchWrongMode() { - Config::set('DEBUG', 1); + Config::set('DEBUG', true); $this->assertFalse($this->stmt->fetch()); $this->setDriverGetConnectionMethod(); @@ -296,7 +296,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testSkipOrderLimit() { - Config::set('DEBUG', 1); + Config::set('DEBUG', true); $this->setDriverGetConnectionMethod()->setRequestForFetch(); $this->stmt->execute(); @@ -314,7 +314,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testCount() { - Config::set('DEBUG', 1); + Config::set('DEBUG', true); $this->setDriverGetConnectionMethod()->setRequestForFetch(); $this->stmt->execute(); @@ -329,7 +329,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testCountException() { - Config::set('DEBUG', 1); + Config::set('DEBUG', true); $this->setDriverGetConnectionMethod(); $this->request ->expects($this->once()) @@ -347,7 +347,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testOrderException() { - Config::set('DEBUG', 1); + Config::set('DEBUG', true); $this->setDriverGetConnectionMethod(); $this->request ->expects($this->once()) @@ -365,7 +365,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testSkipException() { - Config::set('DEBUG', 1); + Config::set('DEBUG', true); $this->setDriverGetConnectionMethod(); $this->request ->expects($this->once()) @@ -383,7 +383,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testLimitException() { - Config::set('DEBUG', 1); + Config::set('DEBUG', true); $this->setDriverGetConnectionMethod(); $this->request ->expects($this->once()) diff --git a/tests/model/MySQLiDriverTest.php b/tests/model/MySQLiDriverTest.php index 6321190..3f67c20 100644 --- a/tests/model/MySQLiDriverTest.php +++ b/tests/model/MySQLiDriverTest.php @@ -57,7 +57,7 @@ class MySQLiDriverTest extends PHPUnit_Extensions_Database_TestCase */ public function testDriver() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $driver = new MySQLiDriver($this->conf); @@ -76,7 +76,7 @@ class MySQLiDriverTest extends PHPUnit_Extensions_Database_TestCase */ public function testGetConnection() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $driver = new MySQLiDriver($this->conf); $this->assertInstanceOf('mysqli', $driver->getConnection()); @@ -87,7 +87,7 @@ class MySQLiDriverTest extends PHPUnit_Extensions_Database_TestCase */ public function testGetConnectionWrongConfig() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $this->conf['database'] = 'nodb'; $driver = new MySQLiDriver($this->conf); @@ -100,7 +100,7 @@ class MySQLiDriverTest extends PHPUnit_Extensions_Database_TestCase */ public function testDisconnect() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $driver = new MySQLiDriver($this->conf); @@ -117,7 +117,7 @@ class MySQLiDriverTest extends PHPUnit_Extensions_Database_TestCase */ public function testInsert() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $driver = new MySQLiDriver($this->conf); @@ -137,7 +137,7 @@ class MySQLiDriverTest extends PHPUnit_Extensions_Database_TestCase */ public function testGetInsertId() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $driver = new MySQLiDriver($this->conf); @@ -149,7 +149,7 @@ class MySQLiDriverTest extends PHPUnit_Extensions_Database_TestCase */ public function testTransaction() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $driver = new MySQLiDriver($this->conf); @@ -180,7 +180,7 @@ class MySQLiDriverTest extends PHPUnit_Extensions_Database_TestCase */ public function testRollback() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $driver = new MySQLiDriver($this->conf); diff --git a/tests/model/MySQLiStatementTest.php b/tests/model/MySQLiStatementTest.php index e360150..5539796 100644 --- a/tests/model/MySQLiStatementTest.php +++ b/tests/model/MySQLiStatementTest.php @@ -83,7 +83,7 @@ class MySQLiStatementTest extends PHPUnit_Framework_TestCase */ public function testExecute() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $this->driver ->expects($this->any()) @@ -102,7 +102,7 @@ class MySQLiStatementTest extends PHPUnit_Framework_TestCase */ public function testExecuteNoPlaceholders() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $this->setDriverGetConnectionMethod(); $this->sql = 'PLAIN SQL'; @@ -116,7 +116,7 @@ class MySQLiStatementTest extends PHPUnit_Framework_TestCase */ public function testFetchNoResult() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $this->assertFalse($this->stmt->fetch()); } @@ -126,7 +126,7 @@ class MySQLiStatementTest extends PHPUnit_Framework_TestCase */ public function testDriverExecuteNoResult() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $this->setDriverGetConnectionWrongResultMethod(); $this->setExpectedException('GeneralException', 'ERROR'); $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val')); @@ -138,7 +138,7 @@ class MySQLiStatementTest extends PHPUnit_Framework_TestCase */ public function testFetch() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $this->setDriverGetConnectionMethod(); $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val')); $this->assertSame('OBJECT', $this->stmt->fetch()); @@ -153,7 +153,7 @@ class MySQLiStatementTest extends PHPUnit_Framework_TestCase */ public function testFetchObject() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $this->setDriverGetConnectionMethod(); $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val')); $this->assertSame('OBJECT', $this->stmt->fetchObject()); @@ -165,7 +165,7 @@ class MySQLiStatementTest extends PHPUnit_Framework_TestCase */ public function testFetchPairs() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $resultMock = $this->getMockBuilder('mysqli_result') ->disableOriginalConstructor() @@ -220,7 +220,7 @@ class MySQLiStatementTest extends PHPUnit_Framework_TestCase */ public function testClose() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $this->assertAttributeEquals(null, 'result', $this->stmt); $this->setDriverGetConnectionMethod(); $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val')); @@ -261,7 +261,7 @@ class MySQLiStatementTest extends PHPUnit_Framework_TestCase public function testNumRows() { $this->markTestSkipped('Unable to execute a method or access a property of an incomplete object.'); - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $this->setDriverGetConnectionMethod(); $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val')); $this->assertNull($this->stmt->numRows()); @@ -273,7 +273,7 @@ class MySQLiStatementTest extends PHPUnit_Framework_TestCase */ public function testFetchInvalidMode() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $this->setDriverGetConnectionMethod(); $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val')); diff --git a/tests/redis/RedisDebugTest.php b/tests/redis/RedisDebugTest.php index c479daa..f6ba5eb 100644 --- a/tests/redis/RedisDebugTest.php +++ b/tests/redis/RedisDebugTest.php @@ -59,7 +59,7 @@ class RedisDebugTest extends PHPUnit_Framework_TestCase */ public function testCallSimpleParams() { - Config::set('DEBUG', 1); + Config::set('DEBUG', true); $mock = $this->getMock('Redis', array('connect')); $mock->expects($this->once()) @@ -77,7 +77,7 @@ class RedisDebugTest extends PHPUnit_Framework_TestCase */ public function testCallArrayParam() { - Config::set('DEBUG', 1); + Config::set('DEBUG', true); $mock = $this->getMock('Redis', array('connect')); $mock->expects($this->once()) @@ -95,7 +95,7 @@ class RedisDebugTest extends PHPUnit_Framework_TestCase */ public function testCallUndefinedMethod() { - Config::set('DEBUG', 1); + Config::set('DEBUG', true); $mock = $this->getMock('Redis', array('connect')); $redisDebug = new RedisDebug($mock); diff --git a/tests/redis/RedisManagerTest.php b/tests/redis/RedisManagerTest.php index a64769e..37fd50c 100644 --- a/tests/redis/RedisManagerTest.php +++ b/tests/redis/RedisManagerTest.php @@ -107,7 +107,7 @@ class RedisManagerTest extends PHPUnit_Framework_TestCase */ public function testConnectWithDebug() { - Config::set('DEBUG', 1); + Config::set('DEBUG', true); $this->getMock('RedisDebug'); Config::set('Redis', array('new' => array('host' => true, 'port' => 2023, 'database' => true))); diff --git a/tests/util/profiler/ProfilerTest.php b/tests/util/profiler/ProfilerTest.php index 1e7000d..b9426e3 100644 --- a/tests/util/profiler/ProfilerTest.php +++ b/tests/util/profiler/ProfilerTest.php @@ -38,7 +38,7 @@ class ProfilerTest extends PHPUnit_Framework_TestCase */ public function testGetInstaceNoDebug() { - Config::set('DEBUG', 0); + Config::set('DEBUG', false); $this->setExpectedException('GeneralException', 'Need to turn on DEBUG before use.'); Profiler::getInstance(); } @@ -48,7 +48,7 @@ class ProfilerTest extends PHPUnit_Framework_TestCase */ public function testGetInstance() { - Config::set('DEBUG', 1); + Config::set('DEBUG', true); $profiler = Profiler::getInstance(); $this->assertInstanceOf('Profiler', $profiler); $this->assertSame($profiler, Profiler::getInstance()); @@ -59,7 +59,7 @@ class ProfilerTest extends PHPUnit_Framework_TestCase */ public function testProfilerCommand() { - Config::set('DEBUG', 1); + Config::set('DEBUG', true); $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false); $profiler = Profiler::getInstance(); $cmdProfiler = $profiler->profilerCommand('command', 'type'); @@ -71,7 +71,7 @@ class ProfilerTest extends PHPUnit_Framework_TestCase */ public function testStartEndNoCommandProfiler() { - Config::set('DEBUG', 1); + Config::set('DEBUG', true); $profiler = Profiler::getInstance(); $profiler->start(); $result = $profiler->end(''); @@ -89,7 +89,7 @@ class ProfilerTest extends PHPUnit_Framework_TestCase */ public function testStartEndWithCommandProfiler() { - Config::set('DEBUG', 1); + Config::set('DEBUG', true); $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false); $profiler = Profiler::getInstance(); @@ -107,7 +107,7 @@ class ProfilerTest extends PHPUnit_Framework_TestCase */ public function testGetJSON() { - Config::set('DEBUG', 1); + Config::set('DEBUG', true); $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false); $profiler = Profiler::getInstance(); @@ -120,7 +120,7 @@ class ProfilerTest extends PHPUnit_Framework_TestCase */ public function testGetCLI() { - Config::set('DEBUG', 1); + Config::set('DEBUG', true); $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false); $profiler = Profiler::getInstance(); From a0010c37063d62eb647ab9dda7c54e4d46294a4b Mon Sep 17 00:00:00 2001 From: Alexander Demidov Date: Mon, 9 Jul 2012 13:49:11 +0400 Subject: [PATCH 08/25] Replace values 1 to (bool)true in Config::set(PROFILER...) in majestic tests. --- tests/logger/CliLoggerTest.php | 2 +- tests/logger/FileLoggerTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/logger/CliLoggerTest.php b/tests/logger/CliLoggerTest.php index ae6fa45..8074724 100644 --- a/tests/logger/CliLoggerTest.php +++ b/tests/logger/CliLoggerTest.php @@ -44,7 +44,7 @@ class CliLoggerTest extends PHPUnit_Framework_TestCase public function testLog() { - Config::set('LOGGING', 1); + Config::set('LOGGING', true); Config::set('Logger', array('logger' => 'CliLogger')); $logger = Logger::getInstance(); ob_start(); diff --git a/tests/logger/FileLoggerTest.php b/tests/logger/FileLoggerTest.php index 12c905f..35a4df9 100644 --- a/tests/logger/FileLoggerTest.php +++ b/tests/logger/FileLoggerTest.php @@ -33,7 +33,7 @@ class FileLoggerTest extends PHPUnit_Framework_TestCase vfsStream::setup(); $root = vfsStream::create(array()); vfsStreamWrapper::setRoot($root); - Config::set('LOGGING', 1); + Config::set('LOGGING', true); $this->conf = array('logger' => 'FileLogger', 'filepath' => vfsStream::url('root/log.txt')); Config::set('Logger', $this->conf); if ($root->hasChild('log.txt')) { From 69cd61121e1ed153901438da2e67799485db7b92 Mon Sep 17 00:00:00 2001 From: Alexander Demidov Date: Mon, 9 Jul 2012 14:28:02 +0400 Subject: [PATCH 09/25] Fixed tests RedisDebugTest, ProfilerTest with use PROFILER setting. --- tests/redis/RedisDebugTest.php | 6 +++--- tests/util/profiler/ProfilerTest.php | 16 ++++++++-------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/tests/redis/RedisDebugTest.php b/tests/redis/RedisDebugTest.php index f6ba5eb..97e0cc5 100644 --- a/tests/redis/RedisDebugTest.php +++ b/tests/redis/RedisDebugTest.php @@ -59,7 +59,7 @@ class RedisDebugTest extends PHPUnit_Framework_TestCase */ public function testCallSimpleParams() { - Config::set('DEBUG', true); + Config::set('PROFILER', true); $mock = $this->getMock('Redis', array('connect')); $mock->expects($this->once()) @@ -77,7 +77,7 @@ class RedisDebugTest extends PHPUnit_Framework_TestCase */ public function testCallArrayParam() { - Config::set('DEBUG', true); + Config::set('PROFILER', true); $mock = $this->getMock('Redis', array('connect')); $mock->expects($this->once()) @@ -95,7 +95,7 @@ class RedisDebugTest extends PHPUnit_Framework_TestCase */ public function testCallUndefinedMethod() { - Config::set('DEBUG', true); + Config::set('PROFILER', true); $mock = $this->getMock('Redis', array('connect')); $redisDebug = new RedisDebug($mock); diff --git a/tests/util/profiler/ProfilerTest.php b/tests/util/profiler/ProfilerTest.php index b9426e3..6c01510 100644 --- a/tests/util/profiler/ProfilerTest.php +++ b/tests/util/profiler/ProfilerTest.php @@ -38,8 +38,8 @@ class ProfilerTest extends PHPUnit_Framework_TestCase */ public function testGetInstaceNoDebug() { - Config::set('DEBUG', false); - $this->setExpectedException('GeneralException', 'Need to turn on DEBUG before use.'); + Config::set('PROFILER', false); + $this->setExpectedException('GeneralException', 'Need to turn on PROFILER before use.'); Profiler::getInstance(); } @@ -48,7 +48,7 @@ class ProfilerTest extends PHPUnit_Framework_TestCase */ public function testGetInstance() { - Config::set('DEBUG', true); + Config::set('PROFILER', true); $profiler = Profiler::getInstance(); $this->assertInstanceOf('Profiler', $profiler); $this->assertSame($profiler, Profiler::getInstance()); @@ -59,7 +59,7 @@ class ProfilerTest extends PHPUnit_Framework_TestCase */ public function testProfilerCommand() { - Config::set('DEBUG', true); + Config::set('PROFILER', true); $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false); $profiler = Profiler::getInstance(); $cmdProfiler = $profiler->profilerCommand('command', 'type'); @@ -71,7 +71,7 @@ class ProfilerTest extends PHPUnit_Framework_TestCase */ public function testStartEndNoCommandProfiler() { - Config::set('DEBUG', true); + Config::set('PROFILER', true); $profiler = Profiler::getInstance(); $profiler->start(); $result = $profiler->end(''); @@ -89,7 +89,7 @@ class ProfilerTest extends PHPUnit_Framework_TestCase */ public function testStartEndWithCommandProfiler() { - Config::set('DEBUG', true); + Config::set('PROFILER', true); $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false); $profiler = Profiler::getInstance(); @@ -107,7 +107,7 @@ class ProfilerTest extends PHPUnit_Framework_TestCase */ public function testGetJSON() { - Config::set('DEBUG', true); + Config::set('PROFILER', true); $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false); $profiler = Profiler::getInstance(); @@ -120,7 +120,7 @@ class ProfilerTest extends PHPUnit_Framework_TestCase */ public function testGetCLI() { - Config::set('DEBUG', true); + Config::set('PROFILER', true); $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false); $profiler = Profiler::getInstance(); From 2395adf7b875c6f32f7f0f25ac4d8f20ad0935e1 Mon Sep 17 00:00:00 2001 From: Alexander Demidov Date: Tue, 10 Jul 2012 13:49:49 +0400 Subject: [PATCH 10/25] Modified MySQLiStatementTest with use PROFILER_DETAILS set. --- tests/model/MySQLiStatementTest.php | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/tests/model/MySQLiStatementTest.php b/tests/model/MySQLiStatementTest.php index 5539796..38b0aba 100644 --- a/tests/model/MySQLiStatementTest.php +++ b/tests/model/MySQLiStatementTest.php @@ -83,7 +83,7 @@ class MySQLiStatementTest extends PHPUnit_Framework_TestCase */ public function testExecute() { - Config::set('DEBUG', false); + Config::set('PROFILER_DETAILS', false); $this->driver ->expects($this->any()) @@ -102,7 +102,7 @@ class MySQLiStatementTest extends PHPUnit_Framework_TestCase */ public function testExecuteNoPlaceholders() { - Config::set('DEBUG', false); + Config::set('PROFILER_DETAILS', false); $this->setDriverGetConnectionMethod(); $this->sql = 'PLAIN SQL'; @@ -116,7 +116,7 @@ class MySQLiStatementTest extends PHPUnit_Framework_TestCase */ public function testFetchNoResult() { - Config::set('DEBUG', false); + Config::set('PROFILER_DETAILS', false); $this->assertFalse($this->stmt->fetch()); } @@ -126,7 +126,7 @@ class MySQLiStatementTest extends PHPUnit_Framework_TestCase */ public function testDriverExecuteNoResult() { - Config::set('DEBUG', false); + Config::set('PROFILER_DETAILS', false); $this->setDriverGetConnectionWrongResultMethod(); $this->setExpectedException('GeneralException', 'ERROR'); $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val')); @@ -138,7 +138,7 @@ class MySQLiStatementTest extends PHPUnit_Framework_TestCase */ public function testFetch() { - Config::set('DEBUG', false); + Config::set('PROFILER_DETAILS', false); $this->setDriverGetConnectionMethod(); $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val')); $this->assertSame('OBJECT', $this->stmt->fetch()); @@ -153,7 +153,7 @@ class MySQLiStatementTest extends PHPUnit_Framework_TestCase */ public function testFetchObject() { - Config::set('DEBUG', false); + Config::set('PROFILER_DETAILS', false); $this->setDriverGetConnectionMethod(); $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val')); $this->assertSame('OBJECT', $this->stmt->fetchObject()); @@ -165,7 +165,7 @@ class MySQLiStatementTest extends PHPUnit_Framework_TestCase */ public function testFetchPairs() { - Config::set('DEBUG', false); + Config::set('PROFILER_DETAILS', false); $resultMock = $this->getMockBuilder('mysqli_result') ->disableOriginalConstructor() @@ -206,9 +206,8 @@ class MySQLiStatementTest extends PHPUnit_Framework_TestCase */ public function testFetchWithDebug() { - if (!defined('DEBUG')) { - define('DEBUG', true); - } + Config::set('PROFILER', true); + Config::set('PROFILER_DETAILS', true); $this->setDriverGetConnectionMethod(); $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val')); $this->assertSame('OBJECT', $this->stmt->fetch()); @@ -220,7 +219,7 @@ class MySQLiStatementTest extends PHPUnit_Framework_TestCase */ public function testClose() { - Config::set('DEBUG', false); + Config::set('PROFILER_DETAILS', false); $this->assertAttributeEquals(null, 'result', $this->stmt); $this->setDriverGetConnectionMethod(); $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val')); @@ -261,7 +260,7 @@ class MySQLiStatementTest extends PHPUnit_Framework_TestCase public function testNumRows() { $this->markTestSkipped('Unable to execute a method or access a property of an incomplete object.'); - Config::set('DEBUG', false); + Config::set('PROFILER_DETAILS', false); $this->setDriverGetConnectionMethod(); $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val')); $this->assertNull($this->stmt->numRows()); @@ -273,7 +272,7 @@ class MySQLiStatementTest extends PHPUnit_Framework_TestCase */ public function testFetchInvalidMode() { - Config::set('DEBUG', false); + Config::set('PROFILER_DETAILS', false); $this->setDriverGetConnectionMethod(); $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val')); From 34337efda4c254908cc2e63d4d40280bf439707d Mon Sep 17 00:00:00 2001 From: Alexander Demidov Date: Tue, 10 Jul 2012 13:50:04 +0400 Subject: [PATCH 11/25] Remove use constant DEBUG from Load.php --- Load.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Load.php b/Load.php index 46b54ab..51b160a 100644 --- a/Load.php +++ b/Load.php @@ -50,7 +50,7 @@ class Load require(PATH . self::$autoload[$class]); return; } - if (defined('DEBUG') && DEBUG == true) { + if (Config::get('DEBUG')) { if (!isset(self::$autoload[$class])) { self::buildAutoload(); } From 4e0b762ab0069bc4aea5a7e5c3051ee5faff5aa1 Mon Sep 17 00:00:00 2001 From: Alexander Demidov Date: Tue, 10 Jul 2012 17:03:25 +0400 Subject: [PATCH 12/25] Use PROFILER_DETAILS in RedisManager. --- redis/RedisManager.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/redis/RedisManager.php b/redis/RedisManager.php index 5da5651..ae5f3db 100644 --- a/redis/RedisManager.php +++ b/redis/RedisManager.php @@ -49,7 +49,7 @@ class RedisManager * @var Redis */ $connection = new Redis(); - if (Config::get('DEBUG')) { + if (Config::get('PROFILER_DETAILS')) { $connection = new RedisDebug($connection); } if (!$connection->connect($host, $port)) { From e59e270476800e87778135110c739b18cbae8dff Mon Sep 17 00:00:00 2001 From: Alexander Demidov Date: Tue, 10 Jul 2012 17:03:55 +0400 Subject: [PATCH 13/25] Modified tests Mongo, Mysqli, Redis with checking work Profiler (with PROFILER_DETAILS). --- tests/model/MongoStatementTest.php | 54 +++++++++++++++++++++++-------------- tests/model/MySQLiStatementTest.php | 1 + tests/redis/RedisManagerTest.php | 5 ++-- 3 files changed, 38 insertions(+), 22 deletions(-) diff --git a/tests/model/MongoStatementTest.php b/tests/model/MongoStatementTest.php index 5a854ac..bef39ef 100644 --- a/tests/model/MongoStatementTest.php +++ b/tests/model/MongoStatementTest.php @@ -51,7 +51,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase ->getMock(); $this->request = $this->getMockBuilder('MongoDbCommandMock') ->disableOriginalConstructor() - ->setMethods(array('execute', 'bindParam', 'getInsertId')) + ->setMethods(array('execute', 'bindParam', 'getInsertId', '__toString')) ->getMock(); $this->stmt = new MongoStatement($this->driver, $this->request); } @@ -63,7 +63,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testAffectedNumRowsNoResult() { - Config::set('DEBUG', false); + Config::set('PROFILER_DETAILS', false); $this->assertFalse($this->stmt->affectedRows()); $this->assertFalse($this->stmt->numRows()); @@ -82,7 +82,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testAffectedNumRows() { - Config::set('DEBUG', false); + Config::set('PROFILER_DETAILS', false); $this->setDriverGetConnectionMethod(); $this->request ->expects($this->once()) @@ -98,7 +98,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testGetInsertId() { - Config::set('DEBUG', false); + Config::set('PROFILER_DETAILS', false); $this->setDriverGetConnectionMethod(); $this->request = $this->getMockBuilder('InsertMongoCommand') @@ -127,7 +127,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testExecute() { - Config::set('DEBUG', false); + Config::set('PROFILER_DETAILS', false); $this->setDriverGetConnectionMethod()->setRequestExecuteMethod(); $this->assertTrue($this->stmt->execute()); @@ -139,7 +139,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testExecuteNoResult() { - Config::set('DEBUG', false); + Config::set('PROFILER_DETAILS', false); $this->setDriverGetConnectionMethod(); $this->request ->expects($this->any()) @@ -155,7 +155,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testExecuteNoConnection() { - Config::set('DEBUG', false); + Config::set('PROFILER_DETAILS', false); $this->driver ->expects($this->any()) ->method('getConnection') @@ -170,10 +170,12 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testExecuteWithDebug() { - Config::set('DEBUG', true); + Config::set('PROFILER', true); + Config::set('PROFILER_DETAILS', true); $this->setDriverGetConnectionMethod()->setRequestExecuteMethod(); $this->assertTrue($this->stmt->execute()); $this->assertEquals(10, $this->stmt->numRows()); + $this->assertContains('Queries: 1', Profiler::getInstance()->getCli()); } /** @@ -182,7 +184,8 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testBindParam() { - Config::set('DEBUG', true); + Config::set('PROFILER', true); + Config::set('PROFILER_DETAILS', true); $this->request ->expects($this->once()) @@ -203,7 +206,8 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testFetch() { - Config::set('DEBUG', true); + Config::set('PROFILER', true); + Config::set('PROFILER_DETAILS', true); $this->assertFalse($this->stmt->fetch()); $this->setDriverGetConnectionMethod()->setRequestForFetch(); @@ -220,7 +224,8 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testFetchWithInitialArray() { - Config::set('DEBUG', true); + Config::set('PROFILER', true); + Config::set('PROFILER_DETAILS', true); $this->assertFalse($this->stmt->fetch()); $this->setDriverGetConnectionMethod(); @@ -239,7 +244,8 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testFetchAssocFromCursor() { - Config::set('DEBUG', true); + Config::set('PROFILER', true); + Config::set('PROFILER_DETAILS', true); $this->assertFalse($this->stmt->fetch()); $this->setDriverGetConnectionMethod()->setRequestForFetch(); @@ -256,7 +262,8 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testFetchAssocFromArray() { - Config::set('DEBUG', true); + Config::set('PROFILER', true); + Config::set('PROFILER_DETAILS', true); $this->assertFalse($this->stmt->fetch()); $this->setDriverGetConnectionMethod(); @@ -276,7 +283,8 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testFetchWrongMode() { - Config::set('DEBUG', true); + Config::set('PROFILER', true); + Config::set('PROFILER_DETAILS', true); $this->assertFalse($this->stmt->fetch()); $this->setDriverGetConnectionMethod(); @@ -296,7 +304,8 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testSkipOrderLimit() { - Config::set('DEBUG', true); + Config::set('PROFILER', true); + Config::set('PROFILER_DETAILS', true); $this->setDriverGetConnectionMethod()->setRequestForFetch(); $this->stmt->execute(); @@ -314,7 +323,8 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testCount() { - Config::set('DEBUG', true); + Config::set('PROFILER', true); + Config::set('PROFILER_DETAILS', true); $this->setDriverGetConnectionMethod()->setRequestForFetch(); $this->stmt->execute(); @@ -329,7 +339,8 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testCountException() { - Config::set('DEBUG', true); + Config::set('PROFILER', true); + Config::set('PROFILER_DETAILS', true); $this->setDriverGetConnectionMethod(); $this->request ->expects($this->once()) @@ -347,7 +358,8 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testOrderException() { - Config::set('DEBUG', true); + Config::set('PROFILER', true); + Config::set('PROFILER_DETAILS', true); $this->setDriverGetConnectionMethod(); $this->request ->expects($this->once()) @@ -365,7 +377,8 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testSkipException() { - Config::set('DEBUG', true); + Config::set('PROFILER', true); + Config::set('PROFILER_DETAILS', true); $this->setDriverGetConnectionMethod(); $this->request ->expects($this->once()) @@ -383,7 +396,8 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase */ public function testLimitException() { - Config::set('DEBUG', true); + Config::set('PROFILER', true); + Config::set('PROFILER_DETAILS', true); $this->setDriverGetConnectionMethod(); $this->request ->expects($this->once()) diff --git a/tests/model/MySQLiStatementTest.php b/tests/model/MySQLiStatementTest.php index 38b0aba..22c13be 100644 --- a/tests/model/MySQLiStatementTest.php +++ b/tests/model/MySQLiStatementTest.php @@ -211,6 +211,7 @@ class MySQLiStatementTest extends PHPUnit_Framework_TestCase $this->setDriverGetConnectionMethod(); $this->stmt->execute(array('place' => 'place_val', 'new' => 'new_val')); $this->assertSame('OBJECT', $this->stmt->fetch()); + $this->assertContains('Queries: 1', Profiler::getInstance()->getCli()); } /** diff --git a/tests/redis/RedisManagerTest.php b/tests/redis/RedisManagerTest.php index 37fd50c..1e60837 100644 --- a/tests/redis/RedisManagerTest.php +++ b/tests/redis/RedisManagerTest.php @@ -1,4 +1,4 @@ - @@ -107,7 +107,8 @@ class RedisManagerTest extends PHPUnit_Framework_TestCase */ public function testConnectWithDebug() { - Config::set('DEBUG', true); + Config::set('PROFILER', true); + Config::set('PROFILER_DETAILS', true); $this->getMock('RedisDebug'); Config::set('Redis', array('new' => array('host' => true, 'port' => 2023, 'database' => true))); From 595c1b73ebb1f6e2289c4519cd037783cd06ba0a Mon Sep 17 00:00:00 2001 From: Alexander Demidov Date: Tue, 10 Jul 2012 19:37:03 +0400 Subject: [PATCH 14/25] Add CliController and iCli interface for cli applications. --- app/CliController.php | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++ app/iCli.php | 18 ++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 app/CliController.php create mode 100644 app/iCli.php diff --git a/app/CliController.php b/app/CliController.php new file mode 100644 index 0000000..89f5e5d --- /dev/null +++ b/app/CliController.php @@ -0,0 +1,59 @@ + + * @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 function __construct() + { + ErrorHandler::init(); + } + + /** + * @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')) { + echo PHP_EOL . Profiler::getInstance()->getCli(); + } + } catch (Exception $e) { + $stderr = fopen('php://stderr', 'w'); + fputs($stderr, PHP_EOL . 'Error: ' . $e->getMessage() . PHP_EOL); + fputs($stderr, PHP_EOL . 'Stack trace: ' . PHP_EOL . $e->getTraceAsString() . PHP_EOL); + } + } +} \ No newline at end of file diff --git a/app/iCli.php b/app/iCli.php new file mode 100644 index 0000000..82b9644 --- /dev/null +++ b/app/iCli.php @@ -0,0 +1,18 @@ + + * @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(); +} \ No newline at end of file From ac091ac1ed61443c59f71ca121c95f481fb423cc Mon Sep 17 00:00:00 2001 From: Alexander Demidov Date: Tue, 10 Jul 2012 19:38:10 +0400 Subject: [PATCH 15/25] Add CliControllerTest (not working with RuntimeException). --- tests/app/CliControllerTest.php | 50 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 tests/app/CliControllerTest.php diff --git a/tests/app/CliControllerTest.php b/tests/app/CliControllerTest.php new file mode 100644 index 0000000..da4ff64 --- /dev/null +++ b/tests/app/CliControllerTest.php @@ -0,0 +1,50 @@ + + * @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 +{ + public function _testGetInstance() + { + $instance = CliController::getInstance(); + $this->assertInstanceOf('CliController', $instance); + } + + public function _testExecute() + { + $cli_class = $this->getMockForAbstractClass('iCli', array(), '', '', '', '', array('run')); + $cli_class->expects($this->once()) + ->method('run') + ->with(); + CliController::getInstance()->execute($cli_class); + } + + /** + * @runInSeparateProcess + */ + public function testExecuteImplementError() + { + $cli_class = new StdClass(); + $this->setExpectedException('ErrorException', 'Runner "' . get_class($cli_class) . '" need implement of "iCli" interface.'); + CliController::getInstance()->execute($cli_class); + } +} \ No newline at end of file From ad9c6b0a0c9b0e633d6391519872002417ae8185 Mon Sep 17 00:00:00 2001 From: Alexander Demidov Date: Wed, 11 Jul 2012 17:23:14 +0400 Subject: [PATCH 16/25] Complete tests for CliController. --- app/CliController.php | 8 +++--- tests/app/CliControllerTest.php | 56 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 57 insertions(+), 7 deletions(-) diff --git a/app/CliController.php b/app/CliController.php index 89f5e5d..2e2c1b8 100644 --- a/app/CliController.php +++ b/app/CliController.php @@ -19,9 +19,12 @@ class CliController */ protected static $instance; + protected $error_stream; + protected function __construct() { ErrorHandler::init(); + $this->error_stream = fopen('php://stderr', 'rw'); } /** @@ -51,9 +54,8 @@ class CliController echo PHP_EOL . Profiler::getInstance()->getCli(); } } catch (Exception $e) { - $stderr = fopen('php://stderr', 'w'); - fputs($stderr, PHP_EOL . 'Error: ' . $e->getMessage() . PHP_EOL); - fputs($stderr, PHP_EOL . 'Stack trace: ' . PHP_EOL . $e->getTraceAsString() . PHP_EOL); + fwrite($this->error_stream, PHP_EOL . 'Error: ' . $e->getMessage() . PHP_EOL); + fwrite($this->error_stream, PHP_EOL . 'Stack trace: ' . PHP_EOL . $e->getTraceAsString() . PHP_EOL); } } } \ No newline at end of file diff --git a/tests/app/CliControllerTest.php b/tests/app/CliControllerTest.php index da4ff64..a8061ff 100644 --- a/tests/app/CliControllerTest.php +++ b/tests/app/CliControllerTest.php @@ -23,14 +23,17 @@ require_once __DIR__ . '/../../app/iCli.php'; */ class CliControllerTest extends PHPUnit_Framework_TestCase { - public function _testGetInstance() + protected $stream; + + public function testGetInstance() { $instance = CliController::getInstance(); $this->assertInstanceOf('CliController', $instance); } - public function _testExecute() + public function testExecute() { + Config::set('PROFILER', false); $cli_class = $this->getMockForAbstractClass('iCli', array(), '', '', '', '', array('run')); $cli_class->expects($this->once()) ->method('run') @@ -38,13 +41,58 @@ class CliControllerTest extends PHPUnit_Framework_TestCase 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 testExecuteImplementError() { $cli_class = new StdClass(); - $this->setExpectedException('ErrorException', 'Runner "' . get_class($cli_class) . '" need implement of "iCli" interface.'); - CliController::getInstance()->execute($cli_class); + $cli_controller = CliController::getInstance(); + $error_stream_prop = new ReflectionProperty($cli_controller, 'error_stream'); + $this->stream = fopen('php://memory', 'rw'); + $error_stream_prop->setAccessible(true); + $error_stream_prop->setValue($cli_controller, $this->stream); + $cli_controller->execute($cli_class); + rewind($this->stream); + $this->assertContains('Runner "' . get_class($cli_class) . '" need implement of "iCli" interface.', stream_get_contents($this->stream)); + } + + /** + * @runInSeparateProcess + */ + public function testExecuteWithRunThrowError() + { + 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'))); + $cli_controller = CliController::getInstance(); + $error_stream_prop = new ReflectionProperty($cli_controller, 'error_stream'); + $this->stream = fopen('php://memory', 'rw'); + $error_stream_prop->setAccessible(true); + $error_stream_prop->setValue($cli_controller, $this->stream); + $cli_controller->execute($cli_class); + rewind($this->stream); + $this->assertContains('Error frdom callback.', stream_get_contents($this->stream)); + } + + public function callbackWithThrow() + { + throw new ErrorException('Error from callback.'); } } \ No newline at end of file From 72ac3e21767b6d47d90946160acf0dc7e8dd7460 Mon Sep 17 00:00:00 2001 From: Alexander Demidov Date: Fri, 13 Jul 2012 17:44:10 +0400 Subject: [PATCH 17/25] Modified commented code in ProfilerTest, LayoutTest with DEBUG const. --- tests/layout/LayoutTest.php | 4 ++-- tests/util/profiler/ProfilerTest.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/layout/LayoutTest.php b/tests/layout/LayoutTest.php index dd785b6..bdff686 100644 --- a/tests/layout/LayoutTest.php +++ b/tests/layout/LayoutTest.php @@ -126,8 +126,8 @@ class LayoutTest extends PHPUnit_Framework_TestCase public function tearDown() { -// if (defined('DEBUG')) { -// $debug = DEBUG ? 'TRUE' : 'FALSE'; +// if (!is_null(Config::get('DEBUG'))) { +// $debug = Config::get('DEBUG') ? 'TRUE' : 'FALSE'; // echo PHP_EOL . __CLASS__ . ' DEBUG = ' . $debug . PHP_EOL; // } else { // echo PHP_EOL . __CLASS__ . ' ' . 'DEBUG NOT DEFINED' . PHP_EOL; diff --git a/tests/util/profiler/ProfilerTest.php b/tests/util/profiler/ProfilerTest.php index 6c01510..c3d4a90 100644 --- a/tests/util/profiler/ProfilerTest.php +++ b/tests/util/profiler/ProfilerTest.php @@ -131,8 +131,8 @@ class ProfilerTest extends PHPUnit_Framework_TestCase public function tearDown() { -// if (defined('DEBUG')) { -// $debug = DEBUG ? 'TRUE' : 'FALSE'; +// 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; From ed0c9f207fffae61124733f15c87c201e4e84744 Mon Sep 17 00:00:00 2001 From: Alexander Demidov Date: Thu, 19 Jul 2012 17:05:56 +0400 Subject: [PATCH 18/25] Corrected CliControllerTest. --- tests/app/CliControllerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/app/CliControllerTest.php b/tests/app/CliControllerTest.php index a8061ff..231c3f4 100644 --- a/tests/app/CliControllerTest.php +++ b/tests/app/CliControllerTest.php @@ -88,7 +88,7 @@ class CliControllerTest extends PHPUnit_Framework_TestCase $error_stream_prop->setValue($cli_controller, $this->stream); $cli_controller->execute($cli_class); rewind($this->stream); - $this->assertContains('Error frdom callback.', stream_get_contents($this->stream)); + $this->assertContains('Error from callback.', stream_get_contents($this->stream)); } public function callbackWithThrow() From a16a995ed3566487e6d34c66d42a5b63e5c2c0ea Mon Sep 17 00:00:00 2001 From: Alexander Demidov Date: Thu, 19 Jul 2012 17:07:54 +0400 Subject: [PATCH 19/25] Add Profiler support report message with turn on PROFILER_DETAILS (if use to show queries). Modified tests MongoStatementTest, MySQLiStatementTest. Adding new tests to ProfilerTest. --- tests/model/MongoStatementTest.php | 3 +- tests/model/MySQLiStatementTest.php | 2 + tests/util/profiler/ProfilerTest.php | 184 ++++++++++++++++++++++++++++++++++- util/profiler/Profiler.php | 22 ++++- 4 files changed, 204 insertions(+), 7 deletions(-) diff --git a/tests/model/MongoStatementTest.php b/tests/model/MongoStatementTest.php index bef39ef..a85f626 100644 --- a/tests/model/MongoStatementTest.php +++ b/tests/model/MongoStatementTest.php @@ -207,7 +207,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase public function testFetch() { Config::set('PROFILER', true); - Config::set('PROFILER_DETAILS', true); + Config::set('PROFILER_DETAILS', false); $this->assertFalse($this->stmt->fetch()); $this->setDriverGetConnectionMethod()->setRequestForFetch(); @@ -216,6 +216,7 @@ class MongoStatementTest extends PHPUnit_Framework_TestCase $result = $this->stmt->fetch(); $this->assertEquals('prev', $result->next); $this->assertFalse($this->stmt->fetch()); + $this->assertContains('Queries not counted.', Profiler::getInstance()->getCli()); } /** diff --git a/tests/model/MySQLiStatementTest.php b/tests/model/MySQLiStatementTest.php index 22c13be..2a66c89 100644 --- a/tests/model/MySQLiStatementTest.php +++ b/tests/model/MySQLiStatementTest.php @@ -220,6 +220,7 @@ class MySQLiStatementTest extends PHPUnit_Framework_TestCase */ public function testClose() { + Config::set('PROFILER', true); Config::set('PROFILER_DETAILS', false); $this->assertAttributeEquals(null, 'result', $this->stmt); $this->setDriverGetConnectionMethod(); @@ -227,6 +228,7 @@ class MySQLiStatementTest extends PHPUnit_Framework_TestCase $this->assertAttributeNotEquals(null, 'result', $this->stmt); $this->stmt->close(); $this->assertAttributeEquals(null, 'result', $this->stmt); + $this->assertContains('Queries not counted.', Profiler::getInstance()->getCli()); } /** diff --git a/tests/util/profiler/ProfilerTest.php b/tests/util/profiler/ProfilerTest.php index c3d4a90..2f2022c 100644 --- a/tests/util/profiler/ProfilerTest.php +++ b/tests/util/profiler/ProfilerTest.php @@ -72,6 +72,7 @@ class ProfilerTest extends PHPUnit_Framework_TestCase public function testStartEndNoCommandProfiler() { Config::set('PROFILER', true); + Config::set('PROFILER_DETAILS', true); $profiler = Profiler::getInstance(); $profiler->start(); $result = $profiler->end(''); @@ -90,6 +91,7 @@ class ProfilerTest extends PHPUnit_Framework_TestCase 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(); @@ -105,14 +107,146 @@ class ProfilerTest extends PHPUnit_Framework_TestCase /** * @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(''); + $this->assertNotEquals('', $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(); + $cmdProfiler = $profiler->profilerCommand('command', 'type'); + $profiler->start(); + $result = $profiler->end(''); + $this->assertNotEquals('', $result); + $this->assertStringEndsNotWith(']
', $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(''); + $this->assertNotEquals('', $result); + $this->assertStringEndsWith(']
', $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'); - $this->assertNull($profiler->getJson()); + $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()); } /** @@ -121,12 +255,60 @@ class ProfilerTest extends PHPUnit_Framework_TestCase 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() diff --git a/util/profiler/Profiler.php b/util/profiler/Profiler.php index e883b4c..a0c5067 100644 --- a/util/profiler/Profiler.php +++ b/util/profiler/Profiler.php @@ -80,8 +80,12 @@ class Profiler $queriesTime += $query->getElapsed(); } $html = '
' - . 'Elapsed time: ' . round(($this->end - $this->start) * 1000, 2) . 'ms.
' - . 'Queries: ' . count($this->queries) . ' [' . round($queriesTime * 1000, 2) . ' ms]
'; + . 'Elapsed time: ' . round(($this->end - $this->start) * 1000, 2) . 'ms.
'; + if (count($this->queries) == 0 && !Config::get('PROFILER_DETAILS')) { + $html .= 'Queries not counted. Turn PROFILER_DETAILS on true, if you want to profile queries.
'; + } else { + $html .= 'Queries: ' . count($this->queries) . ' [' . round($queriesTime * 1000, 2) . ' ms]
'; + } $html .= $temp; $html .= '
'; return $html; @@ -99,7 +103,11 @@ class Profiler $table[] = array($query->getType(), round($query->getElapsed() * 1000, 2), $query->getCommand()); $queriesTime += $query->getElapsed(); } - FB::table('Queries: ' . count($this->queries) . ' [' . round($queriesTime * 1000, 2) . ' ms]', $table); + if (count($this->queries) == 0 && !Config::get('PROFILER_DETAILS')) { + FB::table('Queries not counted. Turn PROFILER_DETAILS on true, if you want to profile queries.', $table); + } else { + FB::table('Queries: ' . count($this->queries) . ' [' . round($queriesTime * 1000, 2) . ' ms]', $table); + } } public function getCli() @@ -112,8 +120,12 @@ class Profiler $queriesTime += $query->getElapsed(); } $html = str_pad(PHP_EOL, 60, '-', STR_PAD_LEFT); - $html .= 'Elapsed time: ' . round(($this->end - $this->start) * 1000, 2) . 'ms.' . PHP_EOL - . 'Queries: ' . count($this->queries) . ' [' . round($queriesTime * 1000, 2) . ' ms] ' . PHP_EOL; + $html .= 'Elapsed time: ' . round(($this->end - $this->start) * 1000, 2) . 'ms.' . PHP_EOL; + if (count($this->queries) == 0 && !Config::get('PROFILER_DETAILS')) { + $html .= 'Queries not counted. Turn PROFILER_DETAILS on true, if you want to profile queries.' . PHP_EOL; + } else { + $html .= 'Queries: ' . count($this->queries) . ' [' . round($queriesTime * 1000, 2) . ' ms] ' . PHP_EOL; + } $html .= $temp; return $html; } From 9d0e22ebd214726f81f68433598f00d610b6da31 Mon Sep 17 00:00:00 2001 From: Alexander Demidov Date: Thu, 19 Jul 2012 17:18:54 +0400 Subject: [PATCH 20/25] Modified Profiler message. --- tests/util/profiler/ProfilerTest.php | 2 +- util/profiler/Profiler.php | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/util/profiler/ProfilerTest.php b/tests/util/profiler/ProfilerTest.php index 2f2022c..78e50ad 100644 --- a/tests/util/profiler/ProfilerTest.php +++ b/tests/util/profiler/ProfilerTest.php @@ -39,7 +39,7 @@ class ProfilerTest extends PHPUnit_Framework_TestCase public function testGetInstaceNoDebug() { Config::set('PROFILER', false); - $this->setExpectedException('GeneralException', 'Need to turn on PROFILER before use.'); + $this->setExpectedException('GeneralException', 'Need turn PROFILER before use.'); Profiler::getInstance(); } diff --git a/util/profiler/Profiler.php b/util/profiler/Profiler.php index a0c5067..9e6fe80 100644 --- a/util/profiler/Profiler.php +++ b/util/profiler/Profiler.php @@ -23,7 +23,7 @@ class Profiler private function __construct() { if (Config::get('PROFILER') == false) { - throw new GeneralException('Need to turn on PROFILER before use.'); + throw new GeneralException('Need turn PROFILER before use.'); } } @@ -82,7 +82,7 @@ class Profiler $html = '
' . 'Elapsed time: ' . round(($this->end - $this->start) * 1000, 2) . 'ms.
'; if (count($this->queries) == 0 && !Config::get('PROFILER_DETAILS')) { - $html .= 'Queries not counted. Turn PROFILER_DETAILS on true, if you want to profile queries.
'; + $html .= 'Queries not counted. Turn PROFILER_DETAILS if you want to profile queries.
'; } else { $html .= 'Queries: ' . count($this->queries) . ' [' . round($queriesTime * 1000, 2) . ' ms]
'; } @@ -104,7 +104,7 @@ class Profiler $queriesTime += $query->getElapsed(); } if (count($this->queries) == 0 && !Config::get('PROFILER_DETAILS')) { - FB::table('Queries not counted. Turn PROFILER_DETAILS on true, if you want to profile queries.', $table); + FB::table('Queries not counted. Turn PROFILER_DETAILS if you want to profile queries.', $table); } else { FB::table('Queries: ' . count($this->queries) . ' [' . round($queriesTime * 1000, 2) . ' ms]', $table); } @@ -122,7 +122,7 @@ class Profiler $html = str_pad(PHP_EOL, 60, '-', STR_PAD_LEFT); $html .= 'Elapsed time: ' . round(($this->end - $this->start) * 1000, 2) . 'ms.' . PHP_EOL; if (count($this->queries) == 0 && !Config::get('PROFILER_DETAILS')) { - $html .= 'Queries not counted. Turn PROFILER_DETAILS on true, if you want to profile queries.' . PHP_EOL; + $html .= 'Queries not counted. Turn PROFILER_DETAILS if you want to profile queries.' . PHP_EOL; } else { $html .= 'Queries: ' . count($this->queries) . ' [' . round($queriesTime * 1000, 2) . ' ms] ' . PHP_EOL; } From 8d60abe7d1b2d1d575e9e13c83aadd93ca3256f0 Mon Sep 17 00:00:00 2001 From: Anton Grebnev Date: Fri, 5 Oct 2012 19:38:24 +0400 Subject: [PATCH 21/25] modified ErrorHandler not to process error closed by @ --- exception/ErrorHandler.php | 5 ++++- tests/exception/ErrorHandlerTest.php | 13 +++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/exception/ErrorHandler.php b/exception/ErrorHandler.php index eec155e..954f8dc 100644 --- a/exception/ErrorHandler.php +++ b/exception/ErrorHandler.php @@ -23,7 +23,10 @@ class ErrorHandler if (!empty($ob_handlers)) { ob_end_clean(); } - throw new ErrorException($errstr, 0, $errno, $errfile, $errline); + if (error_reporting() !== 0) { + throw new ErrorException($errstr, 0, $errno, $errfile, $errline); + } + return true; } static protected function getSource($file, $hiline) diff --git a/tests/exception/ErrorHandlerTest.php b/tests/exception/ErrorHandlerTest.php index acbed11..4789eaf 100644 --- a/tests/exception/ErrorHandlerTest.php +++ b/tests/exception/ErrorHandlerTest.php @@ -39,6 +39,19 @@ class ErrorHandlerTest extends PHPUnit_Framework_TestCase trigger_error("test error", E_USER_ERROR); } + public function testHandleAt() + { + $my_eh = array('ErrorHandler', 'error_handler'); + ErrorHandler::init(); + set_error_handler($my_eh); + $var = ''; + $ok = @$var['some']; + $this->assertSame('', $var); + ob_start(); + $this->setExpectedException('ErrorException'); + $ex = $var['some']; + } + /** * @TODO: ErrorHandler->wrapTrace() not used */ From 5bc65513eb538a4a497da381b71ba9624e2134fd Mon Sep 17 00:00:00 2001 From: Anton Grebnev Date: Fri, 5 Oct 2012 19:39:23 +0400 Subject: [PATCH 22/25] modified CliController to use ErrorStream conf var and separate logging by LOGGING/PROFILER settings --- app/CliController.php | 17 ++++++++++---- tests/app/CliControllerTest.php | 51 ++++++++++++++++++++++------------------- 2 files changed, 40 insertions(+), 28 deletions(-) diff --git a/app/CliController.php b/app/CliController.php index 2e2c1b8..480c81b 100644 --- a/app/CliController.php +++ b/app/CliController.php @@ -24,7 +24,7 @@ class CliController protected function __construct() { ErrorHandler::init(); - $this->error_stream = fopen('php://stderr', 'rw'); + $this->error_stream = Config::get('ErrorStream'); } /** @@ -51,11 +51,20 @@ class CliController } $cli_class->run(); if (Config::get('PROFILER')) { - echo PHP_EOL . Profiler::getInstance()->getCli(); + $profile = Profiler::getInstance()->getCli(); + if (Config::get('LOGGING')) { + Logger::getInstance()->log($profile); + } else { + echo $profile; + } } } catch (Exception $e) { - fwrite($this->error_stream, PHP_EOL . 'Error: ' . $e->getMessage() . PHP_EOL); - fwrite($this->error_stream, PHP_EOL . 'Stack trace: ' . PHP_EOL . $e->getTraceAsString() . PHP_EOL); + $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); } } } \ No newline at end of file diff --git a/tests/app/CliControllerTest.php b/tests/app/CliControllerTest.php index 231c3f4..7e6f719 100644 --- a/tests/app/CliControllerTest.php +++ b/tests/app/CliControllerTest.php @@ -36,8 +36,8 @@ class CliControllerTest extends PHPUnit_Framework_TestCase Config::set('PROFILER', false); $cli_class = $this->getMockForAbstractClass('iCli', array(), '', '', '', '', array('run')); $cli_class->expects($this->once()) - ->method('run') - ->with(); + ->method('run') + ->with(); CliController::getInstance()->execute($cli_class); } @@ -47,8 +47,8 @@ class CliControllerTest extends PHPUnit_Framework_TestCase Config::set('PROFILER', true); $cli_class = $this->getMockForAbstractClass('iCli', array(), '', '', '', '', array('run')); $cli_class->expects($this->once()) - ->method('run') - ->with(); + ->method('run') + ->with(); CliController::getInstance()->execute($cli_class); $output = ob_get_clean(); $this->assertContains('Elapsed time:', $output); @@ -57,38 +57,41 @@ class CliControllerTest extends PHPUnit_Framework_TestCase /** * @runInSeparateProcess */ - public function testExecuteImplementError() + public function testExecuteImplementErrorToFile() { + Config::set('ErrorStream', __DIR__ . '/temp.txt'); + touch(Config::get('ErrorStream')); $cli_class = new StdClass(); - $cli_controller = CliController::getInstance(); - $error_stream_prop = new ReflectionProperty($cli_controller, 'error_stream'); - $this->stream = fopen('php://memory', 'rw'); - $error_stream_prop->setAccessible(true); - $error_stream_prop->setValue($cli_controller, $this->stream); - $cli_controller->execute($cli_class); - rewind($this->stream); - $this->assertContains('Runner "' . get_class($cli_class) . '" need implement of "iCli" interface.', stream_get_contents($this->stream)); + 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'))); - $cli_controller = CliController::getInstance(); - $error_stream_prop = new ReflectionProperty($cli_controller, 'error_stream'); - $this->stream = fopen('php://memory', 'rw'); - $error_stream_prop->setAccessible(true); - $error_stream_prop->setValue($cli_controller, $this->stream); - $cli_controller->execute($cli_class); - rewind($this->stream); - $this->assertContains('Error from callback.', stream_get_contents($this->stream)); + ->method('run') + ->with() + ->will($this->returnCallback(array($this, 'callbackWithThrow'))); + + $this->expectOutputRegex('/.*Error from callback\..*/'); + CliController::getInstance()->execute($cli_class); } public function callbackWithThrow() From 774c86acd287e3a283f3a789770f0dc39fdee645 Mon Sep 17 00:00:00 2001 From: Anton Grebnev Date: Fri, 5 Oct 2012 19:39:45 +0400 Subject: [PATCH 23/25] modified Profiler to clear queries array on start() --- tests/util/profiler/ProfilerTest.php | 5 +++-- util/profiler/Profiler.php | 1 + 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/util/profiler/ProfilerTest.php b/tests/util/profiler/ProfilerTest.php index 78e50ad..feda43c 100644 --- a/tests/util/profiler/ProfilerTest.php +++ b/tests/util/profiler/ProfilerTest.php @@ -95,9 +95,10 @@ class ProfilerTest extends PHPUnit_Framework_TestCase $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false); $profiler = Profiler::getInstance(); - $cmdProfiler = $profiler->profilerCommand('command', 'type'); $profiler->start(); + $cmdProfiler = $profiler->profilerCommand('command', 'type'); + $this->assertNotNull($cmdProfiler); $result = $profiler->end(''); $this->assertNotEquals('', $result); $this->assertStringEndsNotWith(']
', $result); @@ -130,8 +131,8 @@ class ProfilerTest extends PHPUnit_Framework_TestCase $this->getMock('CommandProfiler', array('getType', 'getCommand', 'getElapsed'), array(), 'CommandProfilerMock', false); $profiler = Profiler::getInstance(); - $cmdProfiler = $profiler->profilerCommand('command', 'type'); $profiler->start(); + $cmdProfiler = $profiler->profilerCommand('command', 'type'); $result = $profiler->end(''); $this->assertNotEquals('', $result); $this->assertStringEndsNotWith(']
', $result); diff --git a/util/profiler/Profiler.php b/util/profiler/Profiler.php index 9e6fe80..dbce47b 100644 --- a/util/profiler/Profiler.php +++ b/util/profiler/Profiler.php @@ -59,6 +59,7 @@ class Profiler public function start() { + $this->queries = array(); $this->start = microtime(true); } From 910a770ea22fa278553d4cc28f83ae10e1e8c972 Mon Sep 17 00:00:00 2001 From: Anton Grebnev Date: Mon, 8 Oct 2012 19:29:53 +0400 Subject: [PATCH 24/25] modified CliController to use stderr as default error stream --- app/CliController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/CliController.php b/app/CliController.php index 480c81b..9b25702 100644 --- a/app/CliController.php +++ b/app/CliController.php @@ -24,7 +24,7 @@ class CliController protected function __construct() { ErrorHandler::init(); - $this->error_stream = Config::get('ErrorStream'); + $this->error_stream = Config::get('ErrorStream', 'php://stderr'); } /** From 11a59681050180e62b5efcbc58ff1076ada33e72 Mon Sep 17 00:00:00 2001 From: Anton Grebnev Date: Fri, 19 Oct 2012 20:15:26 +0400 Subject: [PATCH 25/25] refactored MongoModel to hide fetch method, fixed PHPDoc errors --- model/Db.php | 1 + model/DbStatement.php | 3 + model/Model.php | 17 ++- model/MongoDbCommand.php | 32 +++++ model/MongoDriver.php | 20 ++-- model/MongoModel.php | 157 +++++++++++++++++++++--- model/MongoStatement.php | 5 +- model/MySQLiDriver.php | 52 ++++---- model/MySQLiStatement.php | 3 +- model/SqlDbDriver.php | 14 +-- model/SqlModel.php | 26 ++-- tests/model/MongoDbCommandTest.php | 11 +- tests/model/MongoDriverTest.php | 33 ++---- tests/model/MongoModelTest.php | 237 ++++++++++++++++++++++++++----------- 14 files changed, 430 insertions(+), 181 deletions(-) diff --git a/model/Db.php b/model/Db.php index 0eec589..933b011 100644 --- a/model/Db.php +++ b/model/Db.php @@ -31,6 +31,7 @@ class Db * @param array $config Configuration array. * * @return DbDriver + * @throws InitializationException */ static public function connect($name = 'default', $config = null) { diff --git a/model/DbStatement.php b/model/DbStatement.php index 26caba2..9ee5feb 100644 --- a/model/DbStatement.php +++ b/model/DbStatement.php @@ -66,6 +66,7 @@ abstract class DbStatement /** * @param string $field + * @return array Value of queried field for all matching rows */ public function fetchColumn($field) { @@ -78,6 +79,7 @@ abstract class DbStatement /** * @param string $field + * @return mixed Value of queried filed for first matching row */ public function fetchField($field) { @@ -108,6 +110,7 @@ abstract class DbStatement abstract public function numRows(); /** + * @param mixed $request * @return bool */ abstract protected function driverExecute($request); diff --git a/model/Model.php b/model/Model.php index fd4fd01..7af41aa 100644 --- a/model/Model.php +++ b/model/Model.php @@ -63,7 +63,6 @@ abstract class Model /** * @param array $data - * @param array $on_duplicate * @return int Id of inserted row */ public function insert($data) @@ -152,22 +151,22 @@ abstract class Model abstract public function delete($id); /** - * @param string $sql - * @param array $params - * @param string $field - * @param CacheKey $cache_key + * @param string $data Request + * @param array $params Request parameters + * @param string $field Requested field name + * @param CacheKey $cache_key Key for caching in */ abstract protected function fetchField($data, $params = array(), $field, $cache_key = null); /** - * @param string $sql - * @param array $params - * @param CacheKey $cache_key + * @param string $data Request + * @param array $params Request parameters + * @param CacheKey $cache_key Key for caching in */ abstract protected function fetch($data, $params = array(), $cache_key = null); /** - * @param string $sql + * @param string $data * @param array $params * @param CacheKey $cache_key */ diff --git a/model/MongoDbCommand.php b/model/MongoDbCommand.php index 319ac84..18bc7ee 100644 --- a/model/MongoDbCommand.php +++ b/model/MongoDbCommand.php @@ -22,6 +22,12 @@ class MongoCommandBuilder const COMMAND = 'Command'; + + /** + * @param string $type + * @param MongoCollection $collection + * @return MongoDbCommand + */ static public function factory($type, $collection = null) { $class = ucfirst($type) . 'MongoCommand'; @@ -33,8 +39,18 @@ class MongoCommandBuilder abstract class MongoDbCommand { + + /** + * @var MongoCollection + */ protected $collection; + /** + * Execute Mongo command/query + * + * @return mixed + * @throws GeneralException + */ public function execute() { if ($this->checkParams()) { @@ -44,6 +60,12 @@ abstract class MongoDbCommand } } + + /** + * @param string $name Parameter name + * @param mixed $value Parameter value + * @return MongoDbCommand + */ public function bindParam($name, $value) { if (property_exists($this, $name)) { @@ -52,12 +74,22 @@ abstract class MongoDbCommand return $this; } + /** + * @param MongoCollection $collection Mongo collection + * @return MongoDbCommand + */ public function setCollection($collection) { $this->collection = $collection; return $this; } + /** + * Convert query parameters array to string + * + * @param array $array + * @return string + */ protected function arrayToString($array) { $string = '['; diff --git a/model/MongoDriver.php b/model/MongoDriver.php index 455b0a4..5af2a51 100644 --- a/model/MongoDriver.php +++ b/model/MongoDriver.php @@ -38,6 +38,12 @@ class MongoDriver extends NoSqlDbDriver return $this->query($command, $params)->affectedRows(); } + /** + * @param string $collection Mongo collection name to search in + * @param array $condition Search conditions + * @param array $fields Fields to return in result set + * @return MongoStatement + */ public function find($collection, $condition = array(), $fields = array()) { $command = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->getCollection($collection)); @@ -49,17 +55,6 @@ class MongoDriver extends NoSqlDbDriver return $this->query($command, $params); } - public function get($collection, $condition, $fields = array()) - { - $command = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, $this->getCollection($collection)); - $params = array( - 'condition' => $condition, - 'fields' => $fields, - 'multiple' => false - ); - return $this->query($command, $params); - } - public function findAndModify($collection, $query, $update, $sort = array(), $field = array(), $upsert = false, $new = false, $remove = false) { $command = MongoCommandBuilder::factory(MongoCommandBuilder::COMMAND, $this->getCollection($collection)); @@ -123,10 +118,9 @@ class MongoDriver extends NoSqlDbDriver } - /** * @param mixed $request - * @return DbStatement + * @return MongoStatement */ public function prepare($request) { diff --git a/model/MongoModel.php b/model/MongoModel.php index 6bf7093..9a5c672 100644 --- a/model/MongoModel.php +++ b/model/MongoModel.php @@ -10,9 +10,20 @@ * @since 2011-11-15 */ +/** + * @property MongoDriver $db + */ abstract class MongoModel extends Model { + /** + * @var string Mongo id field to use as key + */ + protected $key = '_id'; + + /** + * @var bool Is Mongo key field uses original MongoId + */ protected $useMongoId = true; public function __construct($connection = 'default') @@ -20,16 +31,11 @@ abstract class MongoModel extends Model parent::__construct($connection); } - public function count($query = array(), $limit = 0, $skip = 0) + protected function count($query = array(), $limit = 0, $skip = 0) { return $this->db->count($this->table(), $query, $limit, $skip); } - public function find($condition = array(), $fields = array()) - { - return $this->db->find($this->table(), $condition, $fields); - } - public function get($id) { if ($this->useMongoId) { @@ -37,7 +43,7 @@ abstract class MongoModel extends Model $id = new MongoId($id); } } - return $this->db->get($this->table(), array('_id' => $id))->fetch(); + return $this->fetch(array($this->key => $id)); } public function batchInsert($data) @@ -45,25 +51,42 @@ abstract class MongoModel extends Model return $this->db->insert($this->table(), $data, true); } - public function delete($id) + public function update($data, $where) { if ($this->useMongoId) { - if (!$id instanceof MongoId) { - $id = new MongoId($id); + if (!$where instanceof MongoId) { + $where = new MongoId($where); } } - return $this->db->delete($this->table(), array('_id' => $id)); + return $this->db->update($this->table(), $data, array($this->key => $where)); } - public function deleteAll($query = array()) + public function delete($id) { - $this->db->delete($this->table(), $query); + if ($this->useMongoId) { + if (!$id instanceof MongoId) { + $id = new MongoId($id); + } + } + return $this->db->delete($this->table(), array($this->key => $id)); } + /** + * @param array $data Request + * @param array $params Request parameters + * @param string $field Requested field name + * @param CacheKey $cache_key Key for caching in + * @return mixed + */ protected function fetchField($data, $params = array(), $field, $cache_key = null) { if (!$cache_key || !$result = $cache_key->get()) { - $result = $this->db->find($this->table(), $data, array($field => 1))->fetchField($field); + $result = $this->db + ->find($this->table(), $data, array($field => 1)) + ->limit($this->getLimit($params)) + ->skip($this->getSkip($params)) + ->order($this->getOrder($params)) + ->fetchField($field); if ($cache_key) { $cache_key->set($result); } @@ -71,10 +94,21 @@ abstract class MongoModel extends Model return $result; } + /** + * @param array $data Request + * @param array $params Request parameters + * @param CacheKey $cache_key Key for caching in + * @return mixed + */ protected function fetch($data, $params = array(), $cache_key = null) { if (!$cache_key || !$result = $cache_key->get()) { - $result = $this->db->find($this->table(), $data)->fetch(); + $result = $this->db + ->find($this->table(), $data, $this->getFields($params)) + ->limit(1) + ->skip($this->getSkip($params)) + ->order($this->getOrder($params)) + ->fetch(); if ($cache_key) { $cache_key->set($result); } @@ -82,14 +116,105 @@ abstract class MongoModel extends Model return $result; } + /** + * @param array $data + * @param array $params + * @param CacheKey $cache_key + * @return array + */ protected function fetchAll($data, $params = array(), $cache_key = null) { if (!$cache_key || !$result = $cache_key->get()) { - $result = $this->db->find($this->table(), $data)->fetchAll(); + $result = $this->db + ->find($this->table(), $data, $this->getFields($params)) + ->limit($this->getLimit($params)) + ->skip($this->getSkip($params)) + ->order($this->getOrder($params)) + ->fetchAll(); if ($cache_key) { $cache_key->set($result); } } return $result; } + + /** + * @param array $params Parameters for find query + * @return int Query result limits rule + * */ + private function getLimit($params = array()) + { + return isset($params['limit']) ? (int) $params['limit'] : 0; + } + + /** + * @param array $params Parameters for find query + * @return int Query result skip rule + * */ + private function getSkip($params = array()) + { + return isset($params['skip']) ? (int) $params['skip'] : 0; + } + + /** + * @param array $params Parameters for find query + * @return array Query result sort rules + * @throws GeneralException + * */ + private function getOrder($params = array()) + { + $order = array(); + if (isset($params['order'])) { + if (is_string($params['order'])) { + $order = array($params['order'] => 1); + } elseif (is_array($params['order'])) { + $order = $params['order']; + } else { + throw new GeneralException('Wrong order parameter given to query.'); + } + } + return $order; + } + + /** + * @param array $params Parameters for find query + * @return array Query result sort rules + * @throws GeneralException + * */ + private function getFields($params) + { + $fields = array(); + if (isset($params['fields'])) { + if (is_string($params['fields'])) { + $fields = array($params['fields'] => 1); + } elseif (is_array($params['fields'])) { + if (array_keys($params['fields']) === range(0, count($params['fields']) - 1)) { + foreach ($params['fields'] as $filed) { + $fields[$filed] = 1; + } + } else { + $fields = $params['fields']; + } + } else { + throw new GeneralException('Wrong fields parameter given to query.'); + } + } + return $fields; + } + + /** + * Sets the additional condition if it is set + * + * @param array $query Initial query + * @param string $filed Field name to set + * @param mixed $value Field value to set + * @return MongoModel Current model object + */ + protected function addCondition(&$query, $filed, $value) + { + if(!is_null($value) && $value !== false) { + $query[$filed] = $value; + } + return $this; + } } \ No newline at end of file diff --git a/model/MongoStatement.php b/model/MongoStatement.php index 49f4c53..221e49c 100644 --- a/model/MongoStatement.php +++ b/model/MongoStatement.php @@ -114,7 +114,7 @@ class MongoStatement extends DbStatement } else { return false; } - } elseif (is_int($this->result)) { + } elseif (is_int($this->result) || $this->result instanceof MongoId) { return $this->result; } return false; @@ -156,6 +156,9 @@ class MongoStatement extends DbStatement if (is_array($result) && isset($result['values'])) { $this->result = $result['values']; } + if (is_array($result) && isset($result['upserted'])) { + $this->result = $result['n'] > 1 ? $result['n'] : $result['upserted']; + } } elseif (is_int($result)) { $this->result = $result; } diff --git a/model/MySQLiDriver.php b/model/MySQLiDriver.php index 76b060a..179815e 100644 --- a/model/MySQLiDriver.php +++ b/model/MySQLiDriver.php @@ -14,7 +14,7 @@ */ class MySQLiDriver extends SqlDbDriver { - + public function insert($table, $bind, $on_duplicate = array()) { $columns = array(); @@ -22,7 +22,7 @@ class MySQLiDriver extends SqlDbDriver $columns[] = $this->quoteIdentifier($col); } $values = array_values($bind); - + if ($on_duplicate) { $update = array(); foreach ($on_duplicate as $col => $val) { @@ -30,16 +30,16 @@ class MySQLiDriver extends SqlDbDriver } $on_duplicate = ' ON DUPLICATE KEY UPDATE ' . implode(', ', $update); } - + $sql = 'INSERT INTO ' . $this->quoteIdentifier($table) - . ' (' . implode(', ', $columns) . ') VALUES (' . $this->quote($values) . ')' - . (($on_duplicate) ? $on_duplicate : ''); + . ' (' . implode(', ', $columns) . ') VALUES (' . $this->quote($values) . ')' + . (($on_duplicate) ? $on_duplicate : ''); return $this->query($sql)->affectedRows(); } - + /** * @param mixed $sql - * @return DbStatement + * @return MySQLiStatement */ public function prepare($sql) { @@ -50,73 +50,73 @@ class MySQLiDriver extends SqlDbDriver { return $this->connection->insert_id; } - + public function isConnected() { return ($this->connection instanceof MySQLi); } - + public function disconnect() { if ($this->isConnected()) { $this->connection->close(); } - $this->connection = null; + $this->connection = null; } - + protected function connect() { if ($this->connection) { return; } - + $port = isset($this->config['port']) ? (int) $this->config['port'] : null; $this->connection = mysqli_init(); - $connected = @mysqli_real_connect($this->connection, - $this->config['hostname'], - $this->config['username'], - $this->config['password'], - $this->config['database'], - $port); + @mysqli_real_connect($this->connection, + $this->config['hostname'], + $this->config['username'], + $this->config['password'], + $this->config['database'], + $port); // Connection errors check if (mysqli_connect_error()) { throw new GeneralException(mysqli_connect_error(), mysqli_connect_errno()); } - + $charset = (!empty($this->config['charset'])) ? $this->config['charset'] : 'utf8'; $this->connection->set_charset($charset); } - + protected function driverQuote($value) { if (is_int($value) || is_float($value)) { return $value; } - + if (is_bool($value)) { return (int) $value; } - + if ($value === null) { return 'NULL'; } - + $this->connect(); return '\'' . $this->connection->real_escape_string($value) . '\''; } - + protected function driverBeginTransaction() { $this->connect(); $this->connection->autocommit(false); } - + protected function driverCommitTransaction() { $this->connection->commit(); $this->connection->autocommit(true); } - + protected function driverRollbackTransaction() { $this->connection->rollback(); diff --git a/model/MySQLiStatement.php b/model/MySQLiStatement.php index a89adb4..5700d80 100644 --- a/model/MySQLiStatement.php +++ b/model/MySQLiStatement.php @@ -83,6 +83,7 @@ class MySQLiStatement extends DbStatement * * @param mixed $style * @return mixed + * @throws GeneralException */ public function fetch($style = Db::FETCH_OBJ) { @@ -90,7 +91,6 @@ class MySQLiStatement extends DbStatement return false; } - $row = false; switch ($style) { case Db::FETCH_OBJ: $row = $this->result->fetch_object(); @@ -112,6 +112,7 @@ class MySQLiStatement extends DbStatement /** * @param string $class + * @return object */ public function fetchObject($class = 'stdClass') { diff --git a/model/SqlDbDriver.php b/model/SqlDbDriver.php index fa86668..ec62c49 100644 --- a/model/SqlDbDriver.php +++ b/model/SqlDbDriver.php @@ -37,7 +37,7 @@ abstract class SqlDbDriver extends DbDriver /** * @param string $table - * @param mixed $bind + * @param mixed $data * @param mixed $on_duplicate * @return int Affected rows count */ @@ -56,9 +56,9 @@ abstract class SqlDbDriver extends DbDriver /** * @param string $table - * @param array $bind - * @param mixed $where - * @return int + * @param array $data + * @param mixed $condition + * @return int Number of updated rows */ public function update($table, $data, $condition = '') { @@ -74,7 +74,7 @@ abstract class SqlDbDriver extends DbDriver /** * @param string $table - * @param mixed $where + * @param mixed $condition * @return int */ public function delete($table, $condition = '') @@ -152,10 +152,6 @@ abstract class SqlDbDriver extends DbDriver return implode(' AND ', $where); } - /** - * @return DbStatement - */ - abstract protected function driverQuote($value); abstract protected function driverBeginTransaction(); diff --git a/model/SqlModel.php b/model/SqlModel.php index 7724232..7eb98ae 100644 --- a/model/SqlModel.php +++ b/model/SqlModel.php @@ -10,6 +10,9 @@ * @since 2011-11-11 */ +/** + * @property SqlDbDriver $db + */ abstract class SqlModel extends Model { @@ -105,8 +108,10 @@ abstract class SqlModel extends Model /** * This method appends to params table and primary key. - * So they can be accessed thru `:table` and `:pk` placeholders. + * So they can be accessed through `:table` and `:pk` placeholders. * + * @param string $sql + * @param array $params * @return DbStatement */ protected function query($sql, $params = array()) @@ -122,10 +127,11 @@ abstract class SqlModel extends Model } /** - * @param string $sql - * @param array $params - * @param string $field - * @param CacheKey $cache_key + * @param string $data Request + * @param array $params Request parameters + * @param string $field Requested field name + * @param CacheKey $cache_key Key for caching in + * @return mixed */ protected function fetchField($data, $params = array(), $field, $cache_key = null) { @@ -139,9 +145,10 @@ abstract class SqlModel extends Model } /** - * @param string $sql - * @param array $params - * @param CacheKey $cache_key + * @param string $data Request + * @param array $params Request parameters + * @param CacheKey $cache_key Key for caching in + * @return mixed */ protected function fetch($data, $params = array(), $cache_key = null) { @@ -155,9 +162,10 @@ abstract class SqlModel extends Model } /** - * @param string $sql + * @param string $data * @param array $params * @param CacheKey $cache_key + * @return array */ protected function fetchAll($data, $params = array(), $cache_key = null) { diff --git a/tests/model/MongoDbCommandTest.php b/tests/model/MongoDbCommandTest.php index a6f2339..f8e5d0c 100644 --- a/tests/model/MongoDbCommandTest.php +++ b/tests/model/MongoDbCommandTest.php @@ -16,7 +16,7 @@ require_once dirname(__FILE__) . '/../../model/MongoDriver.php'; require_once dirname(__FILE__) . '/../../model/MongoDbCommand.php'; require_once dirname(__FILE__) . '/../../exception/GeneralException.php'; -class MongoDbCommandTest extends PHPUnit_Framework_TestCase +class MongoDbCommandTest extends PHPUnit_Framework_TestCase { private $conf = array(); @@ -323,11 +323,14 @@ class MongoDbCommandTest extends PHPUnit_Framework_TestCase */ public function testToString() { - $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::COUNT, new CollectionMock()); - $cmd->bindParam('condition', array()); + $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::COUNT); + $this->assertSame('Command properties not set', $cmd->__toString()); + $cmd->bindParam('collection', new CollectionMock()); $this->assertStringStartsWith("\n" . 'Collection: CollectionMock', $cmd->__toString()); - $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND, new CollectionMock()); + $cmd = MongoCommandBuilder::factory(MongoCommandBuilder::FIND); + $this->assertSame('Command properties not set', $cmd->__toString()); + $cmd->bindParam('collection', new CollectionMock()); $cmd->bindParam('condition', array()); $this->assertStringStartsWith("\n" . 'Collection: CollectionMock', $cmd->__toString()); $this->assertContains('Condition: ' . '[]' . PHP_EOL, $cmd->__toString()); diff --git a/tests/model/MongoDriverTest.php b/tests/model/MongoDriverTest.php index 63cfd1e..5a5352e 100644 --- a/tests/model/MongoDriverTest.php +++ b/tests/model/MongoDriverTest.php @@ -204,22 +204,6 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase * @runInSeparateProcess * @group Mongo */ - public function testGet() - { - Config::set('DEBUG', false); - - $mongo = new MongoDriver($this->conf); - $eggs = $mongo->get('items', array('name' => 'eggs'))->fetchObject(); - $this->assertEquals(20, $eggs->quantity); - $eggs = $mongo->get('items', array('name' => 'eggs'))->fetch(); - $this->assertEquals('eggs', $eggs->name); - $this->assertInstanceOf('MongoId', $eggs->_id); - } - - /** - * @runInSeparateProcess - * @group Mongo - */ public function testRemove() { Config::set('DEBUG', false); @@ -246,7 +230,7 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase $this->assertNotEmpty($mongo->getInsertId()); $this->assertEquals(2, $mongo->find('items', array('name' => 'bread'))->numRows()); $this->assertEquals(0, $mongo->insert('items', array('name' => 'meat', 'weight' => 230))); - $this->assertEquals(230, $mongo->get('items', array('name' => 'meat'))->fetch()->weight); + $this->assertEquals(230, $mongo->find('items', array('name' => 'meat'))->fetch()->weight); } /** @@ -304,7 +288,7 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase $this->assertEquals(1, $mongo->find('items', array('name' => 'bread'))->numRows()); $this->assertEquals(1, $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'fish'))); $this->assertEquals(2, $mongo->update('items', array('$set' => array('price' => 1)), array('name' => 'eggs'))); - $fish = $mongo->get('items', array('name' => 'fish'))->fetch(); + $fish = $mongo->find('items', array('name' => 'fish'))->fetch(); $this->assertEquals(200, $fish->price); $this->assertEquals('today', $fish->date); $this->assertEquals(0, $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'ball'))); @@ -321,8 +305,9 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase $mongo = new MongoDriver($this->conf); $mongo->insert('items', array('name' => 'bread')); - $this->assertEquals(1, $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'ball'), true, true)); - $this->assertEquals('today', $mongo->get('items', array('name' => 'ball'))->fetch()->date); + $this->assertInstanceOf('MongoId', $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'ball'), true, true)); + $this->assertEquals('today', $mongo->find('items', array('name' => 'ball'))->fetch()->date); + $this->assertEquals(2, $mongo->update('items', array('$set' => array('price' => 200, 'date' => 'today')), array('name' => 'eggs'), true, true)); } /** @@ -335,10 +320,10 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase $mongo = new MongoDriver($this->conf); - $this->assertEquals(10, $mongo->get('items', array('name' => 'bread'))->fetch()->quantity); + $this->assertEquals(10, $mongo->find('items', array('name' => 'bread'))->fetch()->quantity); $result = $mongo->findAndModify('items', array('name' => 'bread'), array('$set' => array('quantity' => 20))); $this->assertEquals(10, $result->fetch()->quantity); - $this->assertEquals(20, $mongo->get('items', array('name' => 'bread'))->fetch()->quantity); + $this->assertEquals(20, $mongo->find('items', array('name' => 'bread'))->fetch()->quantity); } /** @@ -351,10 +336,10 @@ class MongoDriverTest extends PHPUnit_Framework_TestCase $mongo = new MongoDriver($this->conf); - $this->assertEquals(10, $mongo->get('items', array('name' => 'bread'))->fetch()->quantity); + $this->assertEquals(10, $mongo->find('items', array('name' => 'bread'))->fetch()->quantity); $result = $mongo->findAndModify('items', array('name' => 'breading'), array('$set' => array('quantity' => 20)))->fetch(); $this->assertFalse($result); - $this->assertEquals(10, $mongo->get('items', array('name' => 'bread'))->fetch()->quantity); + $this->assertEquals(10, $mongo->find('items', array('name' => 'bread'))->fetch()->quantity); } /** diff --git a/tests/model/MongoModelTest.php b/tests/model/MongoModelTest.php index 848dc99..9f3abb0 100644 --- a/tests/model/MongoModelTest.php +++ b/tests/model/MongoModelTest.php @@ -13,6 +13,7 @@ require_once dirname(__FILE__) . '/../../Registry.php'; require_once dirname(__FILE__) . '/../../Config.php'; require_once dirname(__FILE__) . '/../../cache/Cacher.php'; +require_once dirname(__FILE__) . '/../../exception/GeneralException.php'; require_once dirname(__FILE__) . '/../../model/DbExpr.php'; require_once dirname(__FILE__) . '/../../model/Db.php'; require_once dirname(__FILE__) . '/../../model/MongoDbCommand.php'; @@ -27,8 +28,21 @@ require_once dirname(__FILE__) . '/../../model/MongoModel.php'; class MongoModelTest extends PHPUnit_Framework_TestCase { + /** + * @var MongoModel + */ private $model; + /** + * @var ReflectionMethod + */ + private $method_count; + + /** + * @var ReflectionMethod + */ + private $method_fetch; + public function run(PHPUnit_Framework_TestResult $result = NULL) { $this->setPreserveGlobalState(false); @@ -47,6 +61,13 @@ class MongoModelTest extends PHPUnit_Framework_TestCase } else { $this->model = new MongoMockModel(); } + + $model = new ReflectionClass('MongoModel'); + $this->method_count = $model->getMethod('count'); + $this->method_count->setAccessible(true); + $this->method_fetch = $model->getMethod('fetch'); + $this->method_fetch->setAccessible(true); + set_new_overload(array($this, 'newCallback')); } @@ -62,126 +83,204 @@ class MongoModelTest extends PHPUnit_Framework_TestCase * @runInSeparateProcess * @group Mongo */ - public function testFind() + public function testFetch() { Config::set('DEBUG', false); - $result = $this->model->find(); - $this->assertInstanceOf('MongoStatement', $result); - $this->assertEquals('milk', $result->limit(2)->order(array('name' => -1))->fetch()->name); - $this->assertEquals('fish', $result->fetch()->name); - $result = $this->model->find(array(), array('name'))->fetch(); + + $mock = $this->getMock('CacheKey', array('set', 'get')); + $mock->expects($this->once()) + ->method('set') + ->will($this->returnValue(true)); + $mock->expects($this->once()) + ->method('get') + ->will($this->returnValue(false)); + + $result = $this->method_fetch->invoke($this->model, array(), array('order' => array('name' => -1), 'limit' => 2), $mock); + $this->assertInstanceOf('ArrayObject', $result); + $this->assertEquals('milk', $result->name); + $result = $this->method_fetch->invoke($this->model, array(), array('fields' => array('name'))); $this->assertSame('bread', $result->name); + $result = $this->method_fetch->invoke($this->model, array()); $this->setExpectedException('PHPUnit_Framework_Error'); - $this->assertNull($result->price); + $this->assertNull($result->pounds); } /** * @runInSeparateProcess * @group Mongo */ - public function testGet() + public function testFetchField() { Config::set('DEBUG', false); - $result = $this->model->find()->limit(1)->order(array('name' => 1)); - $result = $result->fetch(); - $this->assertEquals('bread', $result->name); - $id = $result->_id; - $this->assertEquals(10, $this->model->get($id)->quantity); + + $mock = $this->getMock('CacheKey', array('set', 'get')); + $mock->expects($this->exactly(2)) + ->method('set') + ->will($this->returnValue(true)); + $mock->expects($this->exactly(2)) + ->method('get') + ->will($this->returnValue(false)); + + $model = new ReflectionClass('MongoModel'); + $method = $model->getMethod('fetchField'); + $method->setAccessible(true); + + $result = $method->invoke($this->model, array('name' => 'milk'), array(), 'quantity', $mock); + $this->assertEquals(1, $result); + $result = $method->invoke($this->model, array(), array('skip' => 2), 'quantity', $mock); + $this->assertEquals($result, $this->method_fetch->invoke($this->model, array(), array('skip' => 2))->quantity); } /** * @runInSeparateProcess * @group Mongo */ - public function testDelete() + public function testFetchAll() { Config::set('DEBUG', false); - $result = $this->model->find()->limit(1)->order(array('name' => 1)); - $id = $result->fetch()->_id; - $this->assertEquals(1, $this->model->delete($id)); - $this->assertFalse($this->model->find(array('name' => 'bread'))->fetch()); + + $mock = $this->getMock('CacheKey', array('set', 'get')); + $mock->expects($this->once()) + ->method('set') + ->will($this->returnValue(true)); + $mock->expects($this->once()) + ->method('get') + ->will($this->returnValue(false)); + + $model = new ReflectionClass('MongoModel'); + $method = $model->getMethod('fetchAll'); + $method->setAccessible(true); + + $result = $method->invoke($this->model, array('name' => 'eggs'), array(), $mock); + $this->assertEquals(2, count($result)); + $result = $method->invoke($this->model, array(), array('skip' => 2)); + $this->assertEquals(3, count($result)); + $this->assertEquals('fish', $result[0]->name); + $this->assertEquals('milk', $result[1]->name); + $this->assertEquals('eggs', $result[2]->name); } /** * @runInSeparateProcess * @group Mongo */ - public function testBatchInsert() + public function testFetchOrderParam() { Config::set('DEBUG', false); - $data = array( - array('name' => 'first object'), - array('name' => 'second object'), - array('name' => 'equal object'), - array('name' => 'equal object') - ); - $this->model->batchInsert($data); - $this->assertEquals(1, $this->model->count(array('name' => 'first object'))); - $this->assertEquals(2, $this->model->count(array('name' => 'equal object'))); - $this->model->batchInsert(array()); + $result = $this->method_fetch->invoke($this->model, array(), array('order' => 'name')); + $this->assertSame('bread', $result->name); + $result = $this->method_fetch->invoke($this->model, array(), array('order' => array('name' => 1, 'quantity' => -1), 'skip' => 1)); + $this->assertSame(2.1, $result->price); + $this->setExpectedException('GeneralException', 'Wrong order parameter given to query.'); + $this->method_fetch->invoke($this->model, array(), array('order' => 1)); } /** * @runInSeparateProcess * @group Mongo */ - public function testDeleteAll() + public function testFetchSkipLimitParam() { Config::set('DEBUG', false); + $result = $this->method_fetch->invoke($this->model, array(), array('order' => 'name')); + $this->assertSame('bread', $result->name); + $result = $this->method_fetch->invoke($this->model, array(), array('order' => array('name' => 1, 'quantity' => -1), 'skip' => 1, 'limit' => 1)); + $this->assertSame(2.1, $result->price); - $this->assertEquals(2, $this->model->count(array('name' => 'eggs'))); - $this->assertEquals(0, $this->model->deleteAll(array('name' => 'eggs'))); - $this->assertFalse($this->model->find(array('name' => 'eggs'))->fetch()); + $model = new ReflectionClass('MongoModel'); + $method = $model->getMethod('fetchAll'); + $method->setAccessible(true); + $this->assertCount(3, $method->invoke($this->model, array(), array('limit' => 3))); + $this->assertCount(2, $method->invoke($this->model, array(), array('skip' => 3))); + $this->assertCount(5, $method->invoke($this->model, array(), array())); } /** * @runInSeparateProcess * @group Mongo */ - public function testCount() + public function testFetchFieldsParam() { Config::set('DEBUG', false); - $this->assertEquals(5, $this->model->count()); - $this->assertEquals(2, $this->model->count(array('name' => 'eggs'))); + $result = $this->method_fetch->invoke($this->model, array(), array('fields' => 'name')); + $this->assertTrue(!isset($result->quantity)); + $result = $this->method_fetch->invoke($this->model, array(), array('fields' => array('name', 'price'))); + $this->assertTrue(!isset($result->quantity)); + $result = $this->method_fetch->invoke($this->model, array(), array('fields' => array('name' => 1, 'price' => 1))); + $this->assertTrue(!isset($result->quantity)); + $this->setExpectedException('GeneralException', 'Wrong fields parameter given to query.'); + $this->method_fetch->invoke($this->model, array(), array('fields' => 1)); } /** * @runInSeparateProcess * @group Mongo */ - public function testFetch() + public function testGet() { Config::set('DEBUG', false); + $result = $this->method_fetch->invoke($this->model, array(), array('order' => array('name' => 1))); + $this->assertEquals('bread', $result->name); + $id = $result->_id; + $this->assertEquals(10, $this->model->get($id)->quantity); + } - $mock = $this->getMock('CacheKey', array('set', 'get')); - $mock->expects($this->exactly(3)) - ->method('set') - ->will($this->returnValue(true)); - $mock->expects($this->exactly(3)) - ->method('get') - ->will($this->returnValue(false)); - - $model = new ReflectionClass('MongoModel'); - $method = $model->getMethod('fetchField'); - $method->setAccessible(true); - - $result = $method->invoke($this->model, array('name' => 'milk'), array(), 'quantity', $mock); - $this->assertEquals(1, $result); - - $model = new ReflectionClass('MongoModel'); - $method = $model->getMethod('fetch'); - $method->setAccessible(true); + /** + * @runInSeparateProcess + * @group Mongo + */ + public function testDelete() + { + Config::set('DEBUG', false); + $result = $this->method_fetch->invoke($this->model, array(), array('order' => array('name' => 1))); + $id = $result->_id; + $this->assertEquals(1, $this->model->delete($id)); + $this->assertFalse($this->method_fetch->invoke($this->model, array('name' => 'bread'))); + } - $result = $method->invoke($this->model, array('name' => 'bread'), array(), $mock); - $this->assertEquals('bread', $result->name); + /** + * @runInSeparateProcess + * @group Mongo + */ + public function testUpdate() + { + Config::set('DEBUG', false); + $this->model->insert(array('name' => 'testbread', 'price' => 3.2, 'quantity' => 10)); + $result = $this->method_fetch->invoke($this->model, array('name' => 'testbread')); + $this->assertEquals(10, $result->quantity); + $this->model->update(array('$set' => array('quantity' => 3)), $result->_id); + $this->assertEquals(3, $this->model->get($result->_id)->quantity); + $this->model->update(array('$set' => array('quantity' => 13)), (string)$result->_id); + $this->assertEquals(13, $this->model->get($result->_id)->quantity); + } - $model = new ReflectionClass('MongoModel'); - $method = $model->getMethod('fetchAll'); - $method->setAccessible(true); + /** + * @runInSeparateProcess + * @group Mongo + */ + public function testBatchInsert() + { + Config::set('DEBUG', false); + $data = array( + array('name' => 'first object'), + array('name' => 'second object'), + array('name' => 'equal object'), + array('name' => 'equal object') + ); + $this->model->batchInsert($data); + $this->assertEquals(1, $this->method_count->invoke($this->model, array('name' => 'first object'))); + $this->assertEquals(2, $this->method_count->invoke($this->model, array('name' => 'equal object'))); + } - $result = $method->invoke($this->model, array('name' => 'eggs'), array(), $mock); - $this->assertEquals(2, count($result)); - $this->assertEquals('eggs', $result[0]->name); + /** + * @runInSeparateProcess + * @group Mongo + */ + public function testCount() + { + Config::set('DEBUG', false); + $this->assertEquals(5, $this->method_count->invoke($this->model)); + $this->assertEquals(2, $this->method_count->invoke($this->model, array('name' => 'eggs'))); } /** @@ -208,11 +307,11 @@ class MongoModelTest extends PHPUnit_Framework_TestCase $this->model->insert(array('_id' => 1, 'name' => 'testbread', 'price' => 3.2, 'quantity' => 10)); $this->model->insert(array('_id' => 2, 'name' => 'testbread', 'price' => 12, 'quantity' => 2)); - $this->assertSame(2, $this->model->count(array('name' => 'testbread'))); + $this->assertSame(2, $this->method_count->invoke($this->model, array('name' => 'testbread'))); $prop->setValue($this->model, false); $this->model->delete(1); - $this->assertSame(1, $this->model->count(array('name' => 'testbread'))); + $this->assertSame(1, $this->method_count->invoke($this->model, array('name' => 'testbread'))); } /** @@ -225,13 +324,13 @@ class MongoModelTest extends PHPUnit_Framework_TestCase $this->model->insert(array('name' => 'testbread', 'price' => 3.2, 'quantity' => 10)); $this->model->insert(array('name' => 'testbread', 'price' => 12, 'quantity' => 2)); - $this->assertSame(2, $this->model->count(array('name' => 'testbread'))); - $id = $this->model->find(array('name' => 'testbread'))->limit(1)->fetch()->_id->__toString(); + $this->assertSame(2, $this->method_count->invoke($this->model, array('name' => 'testbread'))); + $id = $this->method_fetch->invoke($this->model, array('name' => 'testbread'))->_id->__toString(); $this->assertInternalType('string', $id); $item = $this->model->get($id); $this->assertSame('testbread', $item->name); $this->model->delete($id); - $this->assertSame(1, $this->model->count(array('name' => 'testbread'))); + $this->assertSame(1, $this->method_count->invoke($this->model, array('name' => 'testbread'))); } public function tearDown() @@ -242,7 +341,7 @@ class MongoModelTest extends PHPUnit_Framework_TestCase $db = $connection->selectDB($conf['database']); $db->authenticate($conf['username'], $conf['password']); $collection = 'mongomock'; - $db->dropCollection($collection); + $db->selectCollection($collection)->remove(array()); } protected function newCallback($className)