From ae6c6d0a36ca1e6b9f75104573533ab5b0029574 Mon Sep 17 00:00:00 2001 From: pies Date: Tue, 17 May 2005 20:55:27 +0000 Subject: [PATCH] Bugfixes, cleanups and a new DbFactory class to generate DBO objects according to the db configuration. git-svn-id: https://svn.cakephp.org/repo/trunk/cake@129 3807eeeb-6ff5-0310-8944-8be069107fe0 --- libs/bake.php | 19 +- libs/basics.php | 16 +- libs/controller.php | 8 +- libs/db_factory.php | 62 ++++++ libs/dbo.php | 54 +++-- libs/dbo_adodb.php | 2 +- libs/dbo_mysql.php | 4 +- libs/dispatcher.php | 6 +- libs/error_messages.php | 6 + libs/folder.php | 1 - libs/model.php | 15 +- libs/template.php | 17 +- public/dispatch.php | 2 +- scripts/add.php | 2 +- tests/libs/bake_test.php.disabled | 66 ++++++ tests/libs/controller_test.php | 251 ++++++++++++++++++++++ tests/libs/db_factory_test.php | 58 +++++ tests/libs/dbo_adodb_test.php.disabled | 76 +++++++ tests/libs/dbo_mysql_test.php.disabled | 74 +++++++ tests/libs/dbo_postgres_test.php.disabled | 74 +++++++ tests/libs/dbo_test.php | 75 +++++++ tests/libs/flay_test.php | 96 +++++++++ tests/libs/folder_test.php | 127 +++++++++++ tests/libs/inflector_test.php | 70 ++++++ tests/libs/router_test.php | 72 +++++++ 25 files changed, 1190 insertions(+), 63 deletions(-) create mode 100644 libs/db_factory.php create mode 100644 tests/libs/bake_test.php.disabled create mode 100644 tests/libs/controller_test.php create mode 100644 tests/libs/db_factory_test.php create mode 100644 tests/libs/dbo_adodb_test.php.disabled create mode 100644 tests/libs/dbo_mysql_test.php.disabled create mode 100644 tests/libs/dbo_postgres_test.php.disabled create mode 100644 tests/libs/dbo_test.php create mode 100644 tests/libs/flay_test.php create mode 100644 tests/libs/folder_test.php create mode 100644 tests/libs/inflector_test.php create mode 100644 tests/libs/router_test.php diff --git a/libs/bake.php b/libs/bake.php index 2d3856f97..d68f082eb 100644 --- a/libs/bake.php +++ b/libs/bake.php @@ -100,6 +100,7 @@ class Bake extends Object { */ function template ($type) { switch ($type) { + case 'view': return "%s"; case 'model': return ""; case 'action': return "\n\tfunction %s () {\n\t}\n"; case 'ctrl': return ""; @@ -192,9 +193,10 @@ class %sTest extends TestCase { */ function newView ($controller, $name) { $dir = Inflector::underscore($controller); + $path = "{$dir}/".strtolower($name).".thtml"; $this->createDir(VIEWS.$dir); - $fn = VIEWS.$dir.'/'.strtolower($name).'.thtml'; - $this->createFile($fn, ''); + $fn = VIEWS.$path; + $this->createFile($fn, sprintf($this->template('view'), "

Edit app/views/{$path} to change this message.

")); $this->actions++; } @@ -380,16 +382,19 @@ class %sTest extends TestCase { fwrite($this->stdout, "File {$path} exists, overwrite? (yNaq) "); $key = fgets($this->stdin); - if (preg_match("/^q/", $key)) { + if (preg_match("/^q$/", $key)) { + fwrite($this->stdout, "Quitting.\n"); exit; } - if (preg_match("/^n/", $key)) { + elseif (preg_match("/^a$/", $key)) { + $this->dont_ask = true; + } + elseif (preg_match("/^y$/", $key)) { + } + else { fwrite($this->stdout, "Skip {$path}\n"); return false; } - if (preg_match("/^a/", $key)) { - $this->dont_ask = true; - } } if ($f = fopen($path, 'w')) { diff --git a/libs/basics.php b/libs/basics.php index d95f226b0..526ca2cdd 100644 --- a/libs/basics.php +++ b/libs/basics.php @@ -122,19 +122,7 @@ function uses_config () { function uses_database ($level='devel') { global $DB; - if ($config = loadDatabaseConfig($level)) { - - $db_driver_class = 'DBO_'.$config['driver']; - $db_driver_fn = LIBS.strtolower($db_driver_class.'.php'); - - if (file_exists($db_driver_fn)) { - uses (strtolower($db_driver_class)); - $DB = new $db_driver_class ($config); - } - else { - die('Specified ('.$config['driver'].') database driver not found.'); - } - } + $DB = DbFactory::make(loadDatabaseConfig($level)); } /** @@ -173,7 +161,7 @@ function uses_tag_generator () { function uses () { $args = func_get_args(); foreach ($args as $arg) { - require_once (LIBS.$arg.'.php'); + require_once (LIBS.strtolower($arg).'.php'); } } diff --git a/libs/controller.php b/libs/controller.php index 6421a34a7..f41528445 100644 --- a/libs/controller.php +++ b/libs/controller.php @@ -135,8 +135,10 @@ class Controller extends Template { foreach ($uses as $model_name) { $model_class = ucfirst(strtolower($model_name)); - if (class_exists($model_class)) - $this->$model_name = new $model_name (false); + + if (class_exists($model_class)) { + $this->$model_name = new $model_class (false); + } else die("Controller::__construct() : ".ucfirst($this->name)." requires missing model {$model_class}, exiting."); } @@ -151,7 +153,7 @@ class Controller extends Template { * @param unknown_type $url */ function redirect ($url) { - $this->auto_render = false; + $this->autoRender = false; header ('Location: '.$this->base.$url); } diff --git a/libs/db_factory.php b/libs/db_factory.php new file mode 100644 index 000000000..3fa247a72 --- /dev/null +++ b/libs/db_factory.php @@ -0,0 +1,62 @@ + + * @author Larry E. Masters aka PhpNut + * @author Kamil Dzielinski aka Brego + * @copyright Copyright (c) 2005, Cake Authors/Developers + * @link https://developers.nextco.com/cake/wiki/Authors Authors/Developers + * @version $Revision: 113 $ + * @modifiedby $LastChangedBy: pies $ + * @lastmodified $Date: 2005-05-17 00:53:41 +0200 (Tue, 17 May 2005) $ + * @license http://www.opensource.org/licenses/mit-license.php The MIT License + * + */ + +/** + * Enter description here... + * + */ +uses('object'); + +/** + * Enter description here... + * + * @package cake + * @subpackage cake.libs + * @since Cake v 1.0.0.0 + * + */ +class DbFactory extends Object { + + function make ($config) { + + // special case for AdoDB -- driver name in the form of 'adodb_drivername' + if (preg_match('#^adodb_(.*)$#i', $config['driver'], $res)) { + uses('DBO_AdoDB'); + $config['driver'] = $res[1]; + $conn = new DBO_AdoDB($config); + return $conn; + } + // regular, Cake-native db drivers + else { + $db_driver_class = 'DBO_'.$config['driver']; + $db_driver_fn = LIBS.strtolower($db_driver_class.'.php'); + if (file_exists($db_driver_fn)) { + uses(strtolower($db_driver_class)); + return new $db_driver_class ($config); + } + else { + trigger_error (ERROR_UNKNOWN_DATABASE_DRIVER, E_USER_ERROR); + return false; + } + } + } +} + +?> \ No newline at end of file diff --git a/libs/dbo.php b/libs/dbo.php index 86a77e867..9853fbc4f 100644 --- a/libs/dbo.php +++ b/libs/dbo.php @@ -104,6 +104,14 @@ class DBO extends Object { */ var $debug=FALSE; +/** + * Enter description here... + * + * @var unknown_type + * @access public + */ + var $fullDebug=FALSE; + /** * Enter description here... * @@ -197,7 +205,8 @@ class DBO extends Object { * @return unknown */ function __construct ($config=NULL) { - $this->debug = DEBUG > 1; + $this->debug = DEBUG > 0; + $this->fullDebug = DEBUG > 1; parent::__construct(); return $this->connect($config); } @@ -206,9 +215,9 @@ class DBO extends Object { * Enter description here... * */ - function __destructor() { - $this->close(); - } + function __destructor () { + $this->close(); + } /** * Enter description here... @@ -225,7 +234,7 @@ class DBO extends Object { * */ function close () { - if ($this->debug) $this->showLog(); + if ($this->fullDebug) $this->showLog(); $this->disconnect(); $this->_conn = NULL; $this->connected = false; @@ -249,16 +258,17 @@ class DBO extends Object { * @return unknown */ function query($sql) { - $t = getMicrotime(); - $this->_result = $this->execute($sql); - $this->affected = $this->lastAffected(); - $this->took = round((getMicrotime()-$t)*1000, 0); - $this->error = $this->lastError(); - $this->numRows = $this->lastNumRows($this->_result); - $this->logQuery($sql); - if ($this->debug) $this->showQuery($sql); + $t = getMicrotime(); + $this->_result = $this->execute($sql); + $this->affected = $this->lastAffected(); + $this->took = round((getMicrotime()-$t)*1000, 0); + $this->error = $this->lastError(); + $this->numRows = $this->lastNumRows($this->_result); + $this->logQuery($sql); + if (($this->debug && $this->error) || ($this->fullDebug)) + $this->showQuery($sql); - return $this->error? false: $this->_result; + return $this->error? false: $this->_result; } /** @@ -332,6 +342,19 @@ class DBO extends Object { return $this->connected; } +/** + * Enter description here... + * + * @return unknown + */ + function prepareArray($data) { + $out = null; + foreach ($data as $key=>$item) { + $out[$key] = $this->prepare($item); + } + return $out; + } + /** * Enter description here... * @@ -380,6 +403,9 @@ class DBO extends Object { function showQuery($sql) { $error = $this->error; + if (strlen($sql)>200 && !$this->fullDebug) + $sql = substr($sql, 0, 200) .'[...]'; + if ($this->debug || $error) { print("

Query: {$sql} [Aff:{$this->affected} Num:{$this->numRows} Took:{$this->took}ms]"); if($error) { diff --git a/libs/dbo_adodb.php b/libs/dbo_adodb.php index 986d7a640..f135538c4 100644 --- a/libs/dbo_adodb.php +++ b/libs/dbo_adodb.php @@ -21,7 +21,7 @@ /** * Purpose: DBO_AdoDB - * Basic Cake functionalities. + * AdoDB layer for DBO * * @filesource * @author Michal Tatarynowicz diff --git a/libs/dbo_mysql.php b/libs/dbo_mysql.php index a01896468..a2e41e070 100644 --- a/libs/dbo_mysql.php +++ b/libs/dbo_mysql.php @@ -21,7 +21,7 @@ /** * Purpose: DBO_MySQL - * Enter description here... + * MySQL layer for DBO * * @filesource * @author Michal Tatarynowicz @@ -148,7 +148,7 @@ class DBO_MySQL extends DBO { * @return unknown */ function prepare ($data) { - return "'".str_replace("'", "\\'", $data)."'"; + return "'".mysql_real_escape_string($data)."'"; } /** diff --git a/libs/dispatcher.php b/libs/dispatcher.php index c1c032641..490fe9b3a 100644 --- a/libs/dispatcher.php +++ b/libs/dispatcher.php @@ -128,7 +128,7 @@ class Dispatcher extends Object { // EXECUTE THE REQUESTED ACTION call_user_func_array(array(&$controller, $params['action']), empty($params['pass'])? null: $params['pass']); - if ($controller->auto_render) + if ($controller->autoRender) $controller->render(); if (CACHE_PAGES) $Cache->remember(null); @@ -191,7 +191,7 @@ class Dispatcher extends Object { * @param unknown_type $name * @param unknown_type $message */ - function error ($code, $name, $message) { + function error ($code, $name, $message) { $controller = new Controller ($this); $controller->base = $this->base; $controller->error($code, $name, $message); @@ -203,7 +203,7 @@ class Dispatcher extends Object { * @param unknown_type $url * @param unknown_type $message */ - function error404 ($url, $message) { + function error404 ($url, $message) { $this->error('404', 'Not found', sprintf(ERROR_404, $url, $message)); } diff --git a/libs/error_messages.php b/libs/error_messages.php index 7d038dd17..34a3350c2 100644 --- a/libs/error_messages.php +++ b/libs/error_messages.php @@ -38,6 +38,12 @@ * */ +/** + * Enter description here... + * + */ +define ('ERROR_UNKNOWN_DATABASE_DRIVER', '[DbFactory] Specified database driver (%s) not found'); + /** * Enter description here... * diff --git a/libs/folder.php b/libs/folder.php index 85f47a6c8..b161d9c9b 100644 --- a/libs/folder.php +++ b/libs/folder.php @@ -132,7 +132,6 @@ class Folder extends Object { } else { return false; - } } diff --git a/libs/model.php b/libs/model.php index acfa2aea9..b2ca993d9 100644 --- a/libs/model.php +++ b/libs/model.php @@ -160,7 +160,6 @@ class Model extends Object { $table_name = $this->use_table? $this->use_table: Inflector::tableize(get_class($this)); $this->use_table ($table_name); parent::__construct(); - $this->create_links(); } @@ -481,22 +480,22 @@ class Model extends Object { * Enter description here... * * @param unknown_type $conditions - * @param unknown_type $fields * @return unknown */ - function findAllThreaded ($conditions=null, $fields=null) { - return $this->_doThread($this->findAll($conditions, $fields), null); + function findCount ($conditions) { + list($data) = $this->findAll($conditions, 'COUNT(id) AS count'); + return $data['count']; } /** * Enter description here... * * @param unknown_type $conditions + * @param unknown_type $fields * @return unknown */ - function findCount ($conditions) { - list($data) = $this->findAll($conditions, 'COUNT(id) AS count'); - return $data['count']; + function findAllThreaded ($conditions=null, $fields=null, $sort=null) { + return $this->_doThread($this->findAll($conditions, $fields, $sort), null); } /** @@ -512,7 +511,7 @@ class Model extends Object { for ($ii=0; $ii_do_thread($data, $data[$ii]['id']): null; + $tmp['children'] = isset($data[$ii]['id'])? $this->_doThread($data, $data[$ii]['id']): null; $out[] = $tmp; } } diff --git a/libs/template.php b/libs/template.php index df7028a20..374aba58d 100644 --- a/libs/template.php +++ b/libs/template.php @@ -78,7 +78,7 @@ class Template extends Object { * @var unknown_type * @access public */ - var $auto_render = true; + var $autoRender = true; /** * Enter description here... @@ -86,7 +86,7 @@ class Template extends Object { * @var unknown_type * @access public */ - var $auto_layout = true; + var $autoLayout = true; /** * Enter description here... @@ -163,8 +163,8 @@ class Template extends Object { * @param unknown_type $time */ function flash ($message, $url, $time=1) { - $this->auto_render = false; - $this->auto_layout = false; + $this->autoRender = false; + $this->autoLayout = false; $this->set('url', $this->base.$url); $this->set('message', $message); @@ -181,7 +181,7 @@ class Template extends Object { * @param unknown_type $file */ function render ($action=null, $layout=null, $file=null) { - $this->auto_render = false; + $this->autoRender = false; if (!$action) $action = $this->action; if ($layout) $this->set_layout($layout); @@ -197,8 +197,10 @@ class Template extends Object { $out = $this->_do_render($view_fn, $this->_view_vars, 0); if ($out !== false) { - if ($this->layout && $this->auto_layout) $out = $this->render_layout($out); - if (CACHE_PAGES) $this->cache->append($out); + if ($this->layout && $this->autoLayout) + $out = $this->render_layout($out); + if (CACHE_PAGES) + $this->cache->append($out); print $out; } else { @@ -270,7 +272,6 @@ class Template extends Object { $BASE = $this->base; $params = &$this->params; $page_title = $this->_page_title; - $data = empty($this->data)? false: $this->data; ob_start(); # start caching output (eval outputs directly so we need to cache) # include the template diff --git a/public/dispatch.php b/public/dispatch.php index 6dd1483fd..00326dcda 100644 --- a/public/dispatch.php +++ b/public/dispatch.php @@ -49,7 +49,7 @@ require ('../config/paths.php'); */ require (LIBS.'basics.php'); -uses ('dispatcher'); +uses ('dispatcher', 'db_factory'); uses_config(); uses_database(); uses_tag_generator(); diff --git a/scripts/add.php b/scripts/add.php index ceb0d86e6..3bd06cca6 100644 --- a/scripts/add.php +++ b/scripts/add.php @@ -1,3 +1,4 @@ +#!/usr/local/bin/php test_name); + $mfn = Inflector::underscore($this->model_test_name); + + $this->expected_files = array( + VIEWS.$fn.'/foo.thtml', + VIEWS.$fn.'/bar.thtml', + VIEWS.$fn.'/baz.thtml', + CONTROLLERS.$fn.'_controller.php', + HELPERS.$fn.'_helper.php', + CONTROLLER_TESTS.$fn.'_controller_test.php', + HELPER_TESTS.$fn.'_helper_test.php', + MODEL_TESTS.$mfn.'_test.php' + ); + $this->test_fn = $fn; + } + + // called after the test functions are executed + function tearDown() { + foreach ($this->expected_files as $expected_file) + $this->assertTrue(unlink($expected_file)); + + rmdir(VIEWS.$this->test_fn); + unset($this->abc); + } + + + function testBake () { + // run the baking procedure + $this->abc = new Bake ('ctrl', array($this->test_name, 'foo', 'bar', 'baz')); + $this->abc = new Bake ('model', array($this->model_test_name)); + + // test if all expected files exist + foreach ($this->expected_files as $expected_file) + $this->assertFileExists($expected_file); + + // test controller content + // there has to be a better way of doing this + $fn = CONTROLLER_TESTS.$this->test_fn.'_controller_test.php'; + $this->assertFileContains($fn, "class {$this->test_name}ControllerTest extends TestCase"); + + // test if view directory exists + $this->assertDirExists(VIEWS.$this->test_fn); + } + + +/* + function test () { + $result = $this->abc->(); + $expected = ''; + $this->assertEquals($result, $expected); + } +*/ +} + +?> \ No newline at end of file diff --git a/tests/libs/controller_test.php b/tests/libs/controller_test.php new file mode 100644 index 000000000..012f3a17d --- /dev/null +++ b/tests/libs/controller_test.php @@ -0,0 +1,251 @@ +TestCase($name); + } + + // called before the test functions will be executed + // this function is defined in PHPUnit_TestCase and overwritten + // here + function setUp() { + $this->abc = new Controller (); + $this->abc->base = '/ease'; + + $data = array('foo'=>'foo_value', 'foobar'=>'foobar_value', 'tofu'=>'1'); + $params = array('controller'=>'Test', 'action'=>'test_action', 'data'=>$data); + $this->abc->params = $params; + $this->abc->data = $data; + + $this->abc->action = $this->abc->params['action']; + $this->abc->passed_args = null; + } + + // called after the test functions are executed + // this function is defined in PHPUnit_TestCase and overwritten + // here + function tearDown() { + unset($this->abc); + } + + + function testUrlFor () { + $result = $this->abc->urlFor ('/foo/bar'); + $expected = '/ease/foo/bar'; + $this->assertEquals($result, $expected); + + $result = $this->abc->urlFor ('baz'); + $expected = '/ease/test/baz'; + $this->assertEquals($result, $expected); + + $result = $this->abc->urlFor (); + $expected = '/ease/test/test_action'; + $this->assertEquals($result, $expected); + } + + function testParseHtmlOptions () { + $result = $this->abc->parseHtmlOptions(null); + $expected = null; + $this->assertEquals($result, $expected); + + $result = $this->abc->parseHtmlOptions(array()); + $expected = null; + $this->assertEquals($result, $expected); + + $result = $this->abc->parseHtmlOptions (array('class'=>'foo')); + $expected = ' class="foo"'; + $this->assertEquals($result, $expected); + + $result = $this->abc->parseHtmlOptions (array('class'=>'foo', 'id'=>'bar'), '', ' '); + $expected = 'class="foo" id="bar" '; + $this->assertEquals($result, $expected); + } + + function testLinkTo() { + $result = $this->abc->linkTo ('Testing ó³¼', '/test/ok', array('style'=>'color:Red'), 'Sure?'); + $expected = 'Testing ó³¼'; + $this->assertEquals($result, $expected); + + $result = $this->abc->linkTo ('Ok', 'ok'); + $expected = 'Ok'; + $this->assertEquals($result, $expected); + } + + function testLinkOut () { + $result = $this->abc->linkOut ('Sputnik.pl', 'http://www.sputnik.pl/', array('style'=>'color:Red')); + $expected = 'Sputnik.pl'; + $this->assertEquals($result, $expected); + + $result = $this->abc->linkOut ('http://sputnik.pl'); + $expected = 'http://sputnik.pl'; + $this->assertEquals($result, $expected); + } + + function testFormTag () { + $result = $this->abc->formTag(); + $expected = '

'; + $this->assertEquals($result, $expected); + + $result = $this->abc->formTag('foo', 'get'); + $expected = ''; + $this->assertEquals($result, $expected); + + $result = $this->abc->formTag('/bar/baz', 'file'); + $expected = ''; + $this->assertEquals($result, $expected); + } + + function testSubmitTag () { + $result = $this->abc->submitTag(); + $expected = ''; + $this->assertEquals($result, $expected); + + $result = $this->abc->submitTag('Foo', array('class'=>'Bar')); + $expected = ''; + $this->assertEquals($result, $expected); + } + + function testInputTag () { + $result = $this->abc->inputTag('foo'); + $expected = ''; + $this->assertEquals($result, $expected); + + $result = $this->abc->inputTag('bar', 20, array('class'=>'Foobar')); + $expected = ''; + $this->assertEquals($result, $expected); + } + + function testPasswordTag () { + $result = $this->abc->passwordTag('foo'); + $expected = ''; + $this->assertEquals($result, $expected); + + $result = $this->abc->passwordTag('foo', 33, array('class'=>'Bar')); + $expected = ''; + $this->assertEquals($result, $expected); + } + + function testHiddenTag () { + $result = $this->abc->hiddenTag('foo'); + $expected = ''; + $this->assertEquals($result, $expected); + + $result = $this->abc->hiddenTag('bar', 'baz'); + $expected = ''; + $this->assertEquals($result, $expected); + + $result = $this->abc->hiddenTag('foobar', null, array('class'=>'Bar')); + $expected = ''; + $this->assertEquals($result, $expected); + } + + function testFileTag () { + $result = $this->abc->fileTag('bar', array('class'=>'Foo', 'disabled'=>'1')); + $expected = ''; + $this->assertEquals($result, $expected); + } + + function testAreaTag () { + $result = $this->abc->areaTag('foo'); + $expected = ''; + $this->assertEquals($result, $expected); + + $result = $this->abc->areaTag('foo', 33, 33, array('class'=>'Bar')); + $expected = ''; + $this->assertEquals($result, $expected); + } + + function testCheckboxTag () { + $result = $this->abc->checkboxTag('bar'); + $expected = ''; + $this->assertEquals($result, $expected); + + $result = $this->abc->checkboxTag('tofu', 'ToFu title', array('class'=>'Baz')); + $expected = ''; + $this->assertEquals($result, $expected); + } + + function testRadioTags () { + $result = $this->abc->radioTags('foo', array('foo'=>'Foo', 'bar'=>'Bar'), '---', array('class'=>'Foo')); + $expected = '---'; + $this->assertEquals($result, $expected); + + $result = $this->abc->radioTags('bar', array()); + $expected = null; + $this->assertEquals($result, $expected); + } + + function testSelectTag () { + $result = $this->abc->selectTag('tofu', array('m'=>'male', 'f'=>'female'), array('class'=>'Outer'), array('class'=>'Inner', 'id'=>'FooID')); + $expected = ''; + $this->assertEquals($result, $expected); + + $result = $this->abc->selectTag('tofu', array()); + $expected = null; + $this->assertEquals($result, $expected); + } + + function testTableHeaders () { + $result = $this->abc->tableHeaders(array('One', 'Two', 'Three')); + $expected = 'One Two Three'; + $this->assertEquals($result, $expected); + + $result = $this->abc->tableHeaders(array('Foo Bar', 'Baz'), array('class'=>'Eco'), array('align'=>'left')); + $expected = 'Foo Bar Baz'; + $this->assertEquals($result, $expected); + } + + function testTableCells () { + $result = $this->abc->tableCells(array('Foo', 'Bar')); + $expected = 'Foo Bar'; + $this->assertEquals($result, $expected); + + $result = $this->abc->tableCells(array(array('Foo','Bar'),array('Baz','Echo'),array('Nul','Pio')), array('class'=>'Mini'), array('align'=>'left')); + $expected = join("\n", array('Foo Bar', 'Baz Echo', 'Nul Pio')); + $this->assertEquals($result, $expected); + } + + function testImageTag () { + $result = $this->abc->imageTag('foo.gif'); + $expected = ''; + $this->assertEquals($result, $expected); + + $result = $this->abc->imageTag('bar/baz.gif', 'Foobar', array('class'=>'Zet')); + $expected = 'Foobar'; + $this->assertEquals($result, $expected); + } + + function testTagValue () { + $result = $this->abc->tagValue('foo'); + $expected = 'foo_value'; + $this->assertEquals($result, $expected); + + $result = $this->abc->tagValue('bar'); + $expected = false; + $this->assertEquals($result, $expected); + } + + function testGetCrumbs () { + $this->abc->addCrumb('Foo', '/bar/foo'); + $result = $this->abc->getCrumbs(); + $expected = 'START » Foo'; + $this->assertEquals($result, $expected); + } + + + +/* + function test () { + $result = $this->abc->(); + $expected = ''; + $this->assertEquals($result, $expected); + } +*/ +} + +?> \ No newline at end of file diff --git a/tests/libs/db_factory_test.php b/tests/libs/db_factory_test.php new file mode 100644 index 000000000..9a11ac29e --- /dev/null +++ b/tests/libs/db_factory_test.php @@ -0,0 +1,58 @@ +TestCase($name); + } + + // called before the test functions will be executed + // this function is defined in PHPUnit_TestCase and overwritten + // here + function setUp() { + $this->abc = new DbFactory (); + } + + // called after the test functions are executed + // this function is defined in PHPUnit_TestCase and overwritten + // here + function tearDown() { + unset($this->abc); + } + + + function testMake () { + $config = loadDatabaseConfig ('test'); + + $output = $this->abc->make($config); + $this->assertTrue(is_object($output)); + + if (preg_match('#^(adodb)_.*$#i', $config['driver'], $res)) { + $desired_driver_name = $res[1]; + } + else + $desired_driver_name = $config['driver']; + + $desired_class_name = 'dbo_'.strtolower($desired_driver_name); + $output_class_name = is_object($output)? get_class($output): false; + + $this->assertEquals($output_class_name, $desired_class_name); + + $this->assertTrue($output->connected); + } + +// this test expect an E_USER_ERROR to occur during it's run +// disabled until I find a way to assert it happen +// +// function testBadConfig () { +// $output = $this->abc->make(null); +// $this->assertTrue($output === false); +// } +} + + +?> \ No newline at end of file diff --git a/tests/libs/dbo_adodb_test.php.disabled b/tests/libs/dbo_adodb_test.php.disabled new file mode 100644 index 000000000..59b15ba43 --- /dev/null +++ b/tests/libs/dbo_adodb_test.php.disabled @@ -0,0 +1,76 @@ +TestCase($name); + } + + // called before the test functions will be executed + // this function is defined in PHPUnit_TestCase and overwritten + // here + function setUp() { + $this->abc = new DBO_AdoDB (loadDatabaseConfig('test')); + $this->createTemporaryTable(); + } + + // called after the test functions are executed + // this function is defined in PHPUnit_TestCase and overwritten + // here + function tearDown() { + $this->dropTemporaryTable(); + } + + function createTemporaryTable () { + $create_sqls = array( + 'mysql' => 'CREATE TABLE __test (id INT UNSIGNED PRIMARY KEY, body VARCHAR(255))', + 'postgres' => 'CREATE TABLE __test (id serial NOT NULL, body CHARACTER VARYING(255))' + ); + $sql = $create_sqls[$this->abc->config['driver']]; + + return $this->abc->query($sql); + } + + function dropTemporaryTable () { + return $this->abc->query("DROP TABLE __test"); + } + + function testHasImplementation () { + $functions = array( + 'connect', + 'disconnect', + 'execute', + 'fetchRow', + 'tables', + 'fields', + 'prepare', + 'lastError', + 'lastAffected', + 'lastNumRows', + 'lastInsertId' + ); + + foreach ($functions as $function) { + $this->assertTrue(method_exists($this->abc, $function)); + } + } + + function testConnectivity () { + $this->assertTrue($this->abc->connected); + } + + function testFields () { + $fields = $this->abc->fields('__test'); + $this->assertEquals(count($fields), 2, 'equals'); + } + + function testTables () { + $this->assertTrue(in_array('__test', $this->abc->tables())); + } +} + +?> \ No newline at end of file diff --git a/tests/libs/dbo_mysql_test.php.disabled b/tests/libs/dbo_mysql_test.php.disabled new file mode 100644 index 000000000..64382ed73 --- /dev/null +++ b/tests/libs/dbo_mysql_test.php.disabled @@ -0,0 +1,74 @@ +TestCase($name); + } + + // called before the test functions will be executed + // this function is defined in PHPUnit_TestCase and overwritten + // here + function setUp() { + $this->abc = new DBO_MySQL (loadDatabaseConfig('test')); + $this->createTemporaryTable(); + } + + // called after the test functions are executed + // this function is defined in PHPUnit_TestCase and overwritten + // here + function tearDown() { + $this->dropTemporaryTable(); + } + + + function createTemporaryTable () { + // $sql = "CREATE TABLE __test (id serial NOT NULL, body CHARACTER VARYING(255))"; // postgresql + $sql = "CREATE TABLE __test (id INT UNSIGNED PRIMARY KEY, body VARCHAR(255))"; // mysql + + return $this->abc->query($sql); + } + + function dropTemporaryTable () { + return $this->abc->query("DROP TABLE __test"); + } + + function testHasImplementation () { + $functions = array( + 'connect', + 'disconnect', + 'execute', + 'fetchRow', + 'tables', + 'fields', + 'prepare', + 'lastError', + 'lastAffected', + 'lastNumRows', + 'lastInsertId' + ); + + foreach ($functions as $function) { + $this->assertTrue(method_exists($this->abc, $function)); + } + } + + function testConnectivity () { + $this->assertTrue($this->abc->connected); + } + + function testFields () { + $fields = $this->abc->fields('__test'); + $this->assertEquals(count($fields), 2, 'equals'); + } + + function testTables () { + $this->assertTrue(in_array('__test', $this->abc->tables())); + } +} + +?> \ No newline at end of file diff --git a/tests/libs/dbo_postgres_test.php.disabled b/tests/libs/dbo_postgres_test.php.disabled new file mode 100644 index 000000000..57d3ac733 --- /dev/null +++ b/tests/libs/dbo_postgres_test.php.disabled @@ -0,0 +1,74 @@ +TestCase($name); + } + + // called before the test functions will be executed + // this function is defined in PHPUnit_TestCase and overwritten + // here + function setUp() { + $this->abc = new DBO_Postgres (loadDatabaseConfig('test')); + $this->createTemporaryTable(); + } + + // called after the test functions are executed + // this function is defined in PHPUnit_TestCase and overwritten + // here + function tearDown() { + $this->dropTemporaryTable(); + } + + + function createTemporaryTable () { + $sql = "CREATE TABLE __test (id serial NOT NULL, body CHARACTER VARYING(255))"; // postgresql + // $sql = "CREATE TABLE __test (id INT UNSIGNED PRIMARY KEY, body VARCHAR(255))"; // mysql + + return $this->abc->query($sql); + } + + function dropTemporaryTable () { + return $this->abc->query("DROP TABLE __test"); + } + + function testHasImplementation () { + $functions = array( + 'connect', + 'disconnect', + 'execute', + 'fetchRow', + 'tables', + 'fields', + 'prepare', + 'lastError', + 'lastAffected', + 'lastNumRows', + 'lastInsertId' + ); + + foreach ($functions as $function) { + $this->assertTrue(method_exists($this->abc, $function)); + } + } + + function testConnectivity () { + $this->assertTrue($this->abc->connected); + } + + function testFields () { + $fields = $this->abc->fields('__test'); + $this->assertEquals(count($fields), 2, 'equals'); + } + + function testTables () { + $this->assertTrue(in_array('__test', $this->abc->tables())); + } +} + +?> \ No newline at end of file diff --git a/tests/libs/dbo_test.php b/tests/libs/dbo_test.php new file mode 100644 index 000000000..c140bf250 --- /dev/null +++ b/tests/libs/dbo_test.php @@ -0,0 +1,75 @@ +TestCase($name); + } + + // called before the test functions will be executed + // this function is defined in PHPUnit_TestCase and overwritten + // here + function setUp() { + $this->abc = DbFactory::make(loadDatabaseConfig('test')); + $this->createTemporaryTable(); + } + + // called after the test functions are executed + // this function is defined in PHPUnit_TestCase and overwritten + // here + function tearDown() { + $this->dropTemporaryTable(); + } + + function createTemporaryTable () { + if ($this->abc->config['driver'] == 'postgres') + $sql = 'CREATE TABLE __test (id serial NOT NULL, body CHARACTER VARYING(255))'; + else + $sql = 'CREATE TABLE __test (id INT UNSIGNED PRIMARY KEY, body VARCHAR(255))'; + + return $this->abc->query($sql); + } + + function dropTemporaryTable () { + return $this->abc->query("DROP TABLE __test"); + } + + function testHasImplementation () { + $functions = array( + 'connect', + 'disconnect', + 'execute', + 'fetchRow', + 'tables', + 'fields', + 'prepare', + 'lastError', + 'lastAffected', + 'lastNumRows', + 'lastInsertId' + ); + + foreach ($functions as $function) { + $this->assertTrue(method_exists($this->abc, $function)); + } + } + + function testConnectivity () { + $this->assertTrue($this->abc->connected); + } + + function testFields () { + $fields = $this->abc->fields('__test'); + $this->assertEquals(count($fields), 2, 'equals'); + } + + function testTables () { + $this->assertTrue(in_array('__test', $this->abc->tables())); + } +} + +?> \ No newline at end of file diff --git a/tests/libs/flay_test.php b/tests/libs/flay_test.php new file mode 100644 index 000000000..6c11ab7b8 --- /dev/null +++ b/tests/libs/flay_test.php @@ -0,0 +1,96 @@ +TestCase($name); + } + + // called before the test functions will be executed + // this function is defined in PHPUnit_TestCase and overwritten + // here + function setUp() { + $this->abc = new Flay (); + } + + // called after the test functions are executed + // this function is defined in PHPUnit_TestCase and overwritten + // here + function tearDown() { + unset($this->abc); + } + + + function testToHtml () { + $tests_to_html = array( + array( + 'text'=>"", + 'html'=>"" + ), + array( + 'text'=>"This is a text.", + 'html'=>"

This is a text.

\n" + ), + array( + 'text'=>"This is a line.\n\n\nThis is\n another one.\n\n", + 'html'=>"

This is a line.

\n

This is
\n another one.

\n" + ), + array( + 'text'=>"This line has *bold*, _italic_, and a _combo *bold* and italic_ texts.", + 'html'=>"

This line has bold, italic, and a combo bold and italic texts.

\n" + ), + array( + 'text'=>"This line has tags which are
not allowed.", + 'html'=>"

This line has <b>tags</b> which are <br />not allowed.

\n", + ), + array( + 'text'=>"[http://sputnik.pl] is a link, but so is [http://sputnik.pl/bla/ this one], and [this is not.", + 'html'=>"

http://sputnik.pl is a link, but so is this one, and [this is not.

\n" + ), + array( + 'text'=>"Why don't we try to insert an image.\n\n[http://sputnik.pl/test.jpg]", + 'html'=>"

Why don't we try to insert an image.

\n

\"\"

\n" + ), + array( + 'text'=>"Auto-link my.name+real@my-server.com and example@example.com, should work.", + 'html'=>"

Auto-link my.name+real@my-server.com and example@example.com, should work.

\n" + ), + array( + 'text'=>"\"\"\"This is a blockquote\"\"\"", + 'html'=>"
\n

This is a blockquote

\n
\n" + ), + array( + 'text'=>"How about a blockquote?\"\"\"This is a multiline blockquote.\n\nThis is the second line.\"\"\"\nAnd this is not.", + 'html'=>"

How about a blockquote?

\n
\n

This is a multiline blockquote.

\n

This is the second line.

\n
\n

And this is not.

\n" + ), + array( + 'text'=>"Now auto-link an url such as http://sputnik.pl or www.robocik-malowany.com/dupa[4] - or any other.", + 'html'=>"

Now auto-link an url such as http://sputnik.pl or www.robocik-malowany.com/dupa[4] – or any other.

\n" + ), + array( + 'text'=>"===This be centered===", + 'html'=>"
\n

This be centered

\n
\n" + ), + array( + 'text'=>"===This be centered.\n\nAnd this be centered too,\nalong with this.===\nThis, alas, be not.", + 'html'=>"
\n

This be centered.

\n

And this be centered too,
\nalong with this.

\n
\n

This, alas, be not.

\n" + ), + array( + 'text'=>"This tests (C)2004 Someone Else, \"Layer Your Apps(R)\" and Cake(TM).", + 'html'=>"

This tests ©2004 Someone Else, \"Layer Your Apps®\" and Cake™.

\n" + ), + ); + + foreach ($tests_to_html as $test) { + $this->assertEquals($this->abc->toHtml($test['text']), $test['html']); + } + + } +} + + +?> \ No newline at end of file diff --git a/tests/libs/folder_test.php b/tests/libs/folder_test.php new file mode 100644 index 000000000..bf62eaaa5 --- /dev/null +++ b/tests/libs/folder_test.php @@ -0,0 +1,127 @@ +TestCase($name); + } + + // called before the test functions will be executed + // this function is defined in PHPUnit_TestCase and overwritten + // here + function setUp() { + $this->abc = new Folder (); + } + + // called after the test functions are executed + // this function is defined in PHPUnit_TestCase and overwritten + // here + function tearDown() { + unset($this->abc); + } + + + function testLs () { + $result = $this->abc->ls(); + $expected = array(array('css', 'files', 'img'),array('.htaccess', '500.html', 'dispatch.php')); + $this->assertEquals($result, $expected); + } + + function testPwd () { + $result = $this->abc->pwd(); + $expected = getcwd(); + $this->assertEquals($result, $expected); + } + + function testCd () { + $this->abc->cd(getcwd()); + $result = $this->abc->pwd(); + $this->assertEquals($result, getcwd()); + + $this->abc->cd('css'); + $result = $this->abc->pwd(); + $this->assertEquals($result, Folder::addPathElement(getcwd(), 'css')); + } + + function testFindRecursive () { + $result = $this->abc->findRecursive('.*\.php'); + $expected = array(Folder::addPathElement($this->abc->pwd(), 'dispatch.php')); + $this->assertEquals($result, $expected); + } + + function testIsWindowsPath() { + $result = Folder::isWindowsPath('C:\foo'); + $expected = true; + $this->assertEquals($result, $expected); + + $result = Folder::isWindowsPath('/foo/bar'); + $expected = false; + $this->assertEquals($result, $expected); + } + + function testIsAbsolute () { + $result = Folder::isAbsolute('foo/bar'); + $expected = false; + $this->assertEquals($result, $expected); + + $result = Folder::isAbsolute('c:\foo\bar'); + $expected = true; + $this->assertEquals($result, $expected); + } + + function testAddPathElement () { + $result = Folder::addPathElement('c:\foo', 'bar'); + $expected = 'c:\foo\bar'; + $this->assertEquals($result, $expected); + + $result = Folder::addPathElement('C:\foo\bar\\', 'baz'); + $expected = 'C:\foo\bar\baz'; + $this->assertEquals($result, $expected); + + $result = Folder::addPathElement('/foo/bar/', 'baz'); + $expected = '/foo/bar/baz'; + $this->assertEquals($result, $expected); + } + + function testIsSlashTerm () { + $result = Folder::isSlashTerm('/foo/bar/'); + $this->assertEquals($result, true); + + $result = Folder::isSlashTerm('/foo/bar'); + $this->assertEquals($result, false); + } + + function testCorrectSlashFor () { + $result = Folder::correctSlashFor('/foo/bar/'); + $this->assertEquals($result, '/'); + + $result = Folder::correctSlashFor('C:\foo\bar'); + $this->assertEquals($result, '\\'); + } + + function testSlashTerm () { + $result = Folder::slashTerm('/foo/bar/'); + $this->assertEquals($result, '/foo/bar/'); + + $result = Folder::slashTerm('/foo/bar'); + $this->assertEquals($result, '/foo/bar/'); + + $result = Folder::slashTerm('C:\foo\bar'); + $this->assertEquals($result, 'C:\foo\bar\\'); + } + + +/* + function test () { + $result = $this->abc->(); + $expected = ''; + $this->assertEquals($result, $expected); + } +*/ +} + +?> \ No newline at end of file diff --git a/tests/libs/inflector_test.php b/tests/libs/inflector_test.php new file mode 100644 index 000000000..afbd8065a --- /dev/null +++ b/tests/libs/inflector_test.php @@ -0,0 +1,70 @@ +TestCase($name); + } + + function setUp() { + $this->abc = new Inflector (); + } + + function tearDown() { + unset($this->abc); + } + + function testPluralizeSingularize () { + $singulars = array( + 'search', 'switch', 'fix', 'box', 'process', 'address', 'query', 'ability', + 'agency', 'half', 'safe', 'wife', 'basis', 'diagnosis', 'datum', 'medium', + 'person', 'salesperson', 'man', 'woman', 'spokesman', 'child', 'page', 'robot'); + $plurals = array( + 'searches', 'switches', 'fixes', 'boxes', 'processes', 'addresses', 'queries', 'abilities', + 'agencies', 'halves', 'saves', 'wives', 'bases', 'diagnoses', 'data', 'media', + 'people', 'salespeople', 'men', 'women', 'spokesmen', 'children', 'pages', 'robots'); + + foreach (array_combine($singulars, $plurals) as $singular => $plural) { + $this->assertEquals($this->abc->pluralize($singular), $plural); + $this->assertEquals($this->abc->singularize($plural), $singular); + } + } + + function testCamelize() { + $this->asEq($this->abc->camelize('foo_bar_baz'), 'FooBarBaz'); + } + + function testUnderscore () { + $this->asEq($this->abc->underscore('FooBarBaz'), 'foo_bar_baz'); + } + + function testHumanize () { + $this->asEq($this->abc->humanize('foo_bar_baz'), 'Foo Bar Baz'); + } + + function testTableize () { + $this->asEq($this->abc->tableize('Bar'), 'bars'); + } + + function testClassify () { + $this->asEq($this->abc->classify('bars'), 'Bar'); + } + + function testForeignKey () { + $this->asEq($this->abc->foreignKey('Bar'), 'bar_id'); + } + + +/* + function test () { + $result = $this->abc->(); + $expected = ''; + $this->assertEquals($result, $expected); + } +*/ +} + +?> \ No newline at end of file diff --git a/tests/libs/router_test.php b/tests/libs/router_test.php new file mode 100644 index 000000000..8d946143d --- /dev/null +++ b/tests/libs/router_test.php @@ -0,0 +1,72 @@ +TestCase($name); + } + + // called before the test functions will be executed + // this function is defined in PHPUnit_TestCase and overwritten + // here + function setUp() { + $this->abc = new Router (); + } + + // called after the test functions are executed + // this function is defined in PHPUnit_TestCase and overwritten + // here + function tearDown() { + unset($this->abc); + } + + + function _testConnect () { + $tests = array( + '/' => array('controller'=>'Foo', 'action'=>'bar'), + '/foo/baz' => array('controller'=>'Foo', 'action'=>'baz'), + '/foo/*' => array('controller'=>'Foo', 'action'=>'dodo'), + '/foobar' => array('controller'=>'Foobar', 'action'=>'bar'), + ); + + foreach ($tests as $route=>$data) + $this->abc->connect ($route, $data); + } + + + function testParse () { + + $this->_testConnect(); + + $tests = array( + '' => array('controller'=>'Foo', 'action'=>'bar'), + '/' => array('controller'=>'Foo', 'action'=>'bar'), + '/foo/baz/' => array('controller'=>'Foo', 'action'=>'baz'), + '/foo/foo+bar' => array('pass'=>array('foo+bar'), 'controller'=>'Foo', 'action'=>'dodo'), + '/foobar/' => array('controller'=>'Foobar', 'action'=>'bar'), + '/foo/bar/baz' => array('pass'=>array('bar', 'baz'), 'controller'=>'Foo', 'action'=>'dodo'), + '/one/two/three/' => array('controller'=>'one', 'action'=>'two', 'pass'=>array('three')), + '/foo' => array('controller'=>'foo','action'=>null), + '???' => array() + ); + + foreach ($tests as $test=>$expected) { + $this->asEq($this->abc->parse($test), $expected); + } + } + + +/* + function test () { + $result = $this->abc->(); + $expected = ''; + $this->assertEquals($result, $expected); + } +*/ +} + +?> \ No newline at end of file