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
This commit is contained in:
pies 2005-05-17 20:55:27 +00:00
parent 289f5ac21c
commit ae6c6d0a36
25 changed files with 1190 additions and 63 deletions

View file

@ -100,6 +100,7 @@ class Bake extends Object {
*/
function template ($type) {
switch ($type) {
case 'view': return "%s";
case 'model': return "<?PHP\n\nclass %s extends AppModel {\n}\n\n?>";
case 'action': return "\n\tfunction %s () {\n\t}\n";
case 'ctrl': return "<?PHP\n\nclass %s extends %s {\n%s\n}\n\n?>";
@ -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'), "<p>Edit <b>app/views/{$path}</b> to change this message.</p>"));
$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')) {

View file

@ -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');
}
}

View file

@ -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);
}

62
libs/db_factory.php Normal file
View file

@ -0,0 +1,62 @@
<?PHP
/**
* Purpose: DbFactory
*
* Description:
* Creates DBO-descendant objects from a given db connection configuration
*
* @filesource
* @author Michal Tatarynowicz <tatarynowicz@gmail.com>
* @author Larry E. Masters aka PhpNut <nut@phpnut.com>
* @author Kamil Dzielinski aka Brego <brego.dk@gmail.com>
* @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;
}
}
}
}
?>

View file

@ -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("<p style=\"text-align:left\"><b>Query:</b> {$sql} <small>[Aff:{$this->affected} Num:{$this->numRows} Took:{$this->took}ms]</small>");
if($error) {

View file

@ -21,7 +21,7 @@
/**
* Purpose: DBO_AdoDB
* Basic Cake functionalities.
* AdoDB layer for DBO
*
* @filesource
* @author Michal Tatarynowicz <tatarynowicz@gmail.com>

View file

@ -21,7 +21,7 @@
/**
* Purpose: DBO_MySQL
* Enter description here...
* MySQL layer for DBO
*
* @filesource
* @author Michal Tatarynowicz <tatarynowicz@gmail.com>
@ -148,7 +148,7 @@ class DBO_MySQL extends DBO {
* @return unknown
*/
function prepare ($data) {
return "'".str_replace("'", "\\'", $data)."'";
return "'".mysql_real_escape_string($data)."'";
}
/**

View file

@ -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));
}

View file

@ -38,6 +38,12 @@
*
*/
/**
* Enter description here...
*
*/
define ('ERROR_UNKNOWN_DATABASE_DRIVER', '[DbFactory] Specified database driver (%s) not found');
/**
* Enter description here...
*

View file

@ -132,7 +132,6 @@ class Folder extends Object {
}
else {
return false;
}
}

View file

@ -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<sizeof($data); $ii++) {
if ($data[$ii]['parent_id'] == $root) {
$tmp = $data[$ii];
$tmp['children'] = isset($data[$ii]['id'])? $this->_do_thread($data, $data[$ii]['id']): null;
$tmp['children'] = isset($data[$ii]['id'])? $this->_doThread($data, $data[$ii]['id']): null;
$out[] = $tmp;
}
}

View file

@ -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

View file

@ -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();

View file

@ -1,3 +1,4 @@
#!/usr/local/bin/php
<?PHP
//////////////////////////////////////////////////////////////////////////
// + $Id$
@ -38,7 +39,6 @@
*
*/
## START-UP
/**
* START-UP
*/

View file

@ -0,0 +1,66 @@
<?PHP
uses ('test', 'bake', 'inflector');
class BakeTest extends TestCase {
var $abc;
var $test_name = "TheOriginalAcmeFoobars";
var $model_test_name = "AcmeFoobar";
// called before the test functions will be executed
function setUp() {
$fn = Inflector::underscore($this->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);
}
*/
}
?>

View file

@ -0,0 +1,251 @@
<?PHP
uses ('test', 'controller');
class ControllerTest extends TestCase {
var $abc;
// constructor of the test suite
function ControllerTest($name) {
$this->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 = '<a href="/ease/test/ok" style="color:Red" onClick="return confirm(\'Sure?\')">Testing ó³¼</a>';
$this->assertEquals($result, $expected);
$result = $this->abc->linkTo ('Ok', 'ok');
$expected = '<a href="/ease/test/ok">Ok</a>';
$this->assertEquals($result, $expected);
}
function testLinkOut () {
$result = $this->abc->linkOut ('Sputnik.pl', 'http://www.sputnik.pl/', array('style'=>'color:Red'));
$expected = '<a href="http://www.sputnik.pl/" style="color:Red">Sputnik.pl</a>';
$this->assertEquals($result, $expected);
$result = $this->abc->linkOut ('http://sputnik.pl');
$expected = '<a href="http://sputnik.pl">http://sputnik.pl</a>';
$this->assertEquals($result, $expected);
}
function testFormTag () {
$result = $this->abc->formTag();
$expected = '<form action="/ease/test/test_action" method="post">';
$this->assertEquals($result, $expected);
$result = $this->abc->formTag('foo', 'get');
$expected = '<form action="/ease/test/foo" method="get">';
$this->assertEquals($result, $expected);
$result = $this->abc->formTag('/bar/baz', 'file');
$expected = '<form action="/ease/bar/baz" method="post" enctype="multipart/form-data">';
$this->assertEquals($result, $expected);
}
function testSubmitTag () {
$result = $this->abc->submitTag();
$expected = '<input type="submit" value="Submit" />';
$this->assertEquals($result, $expected);
$result = $this->abc->submitTag('Foo', array('class'=>'Bar'));
$expected = '<input type="submit" class="Bar" value="Foo" />';
$this->assertEquals($result, $expected);
}
function testInputTag () {
$result = $this->abc->inputTag('foo');
$expected = '<input name="data[foo]" size="20" value="foo_value" />';
$this->assertEquals($result, $expected);
$result = $this->abc->inputTag('bar', 20, array('class'=>'Foobar'));
$expected = '<input name="data[bar]" class="Foobar" size="20" value="" />';
$this->assertEquals($result, $expected);
}
function testPasswordTag () {
$result = $this->abc->passwordTag('foo');
$expected = '<input type="password" name="data[foo]" size="20" value="foo_value" />';
$this->assertEquals($result, $expected);
$result = $this->abc->passwordTag('foo', 33, array('class'=>'Bar'));
$expected = '<input type="password" name="data[foo]" class="Bar" size="33" value="foo_value" />';
$this->assertEquals($result, $expected);
}
function testHiddenTag () {
$result = $this->abc->hiddenTag('foo');
$expected = '<input type="hidden" name="data[foo]" value="foo_value" />';
$this->assertEquals($result, $expected);
$result = $this->abc->hiddenTag('bar', 'baz');
$expected = '<input type="hidden" name="data[bar]" value="baz" />';
$this->assertEquals($result, $expected);
$result = $this->abc->hiddenTag('foobar', null, array('class'=>'Bar'));
$expected = '<input type="hidden" name="data[foobar]" class="Bar" value="foobar_value" />';
$this->assertEquals($result, $expected);
}
function testFileTag () {
$result = $this->abc->fileTag('bar', array('class'=>'Foo', 'disabled'=>'1'));
$expected = '<input type="file" name="bar" class="Foo" disabled="1" />';
$this->assertEquals($result, $expected);
}
function testAreaTag () {
$result = $this->abc->areaTag('foo');
$expected = '<textarea name="data[foo]" cols="60" rows="10">foo_value</textarea>';
$this->assertEquals($result, $expected);
$result = $this->abc->areaTag('foo', 33, 33, array('class'=>'Bar'));
$expected = '<textarea name="data[foo]" class="Bar" cols="33" rows="33">foo_value</textarea>';
$this->assertEquals($result, $expected);
}
function testCheckboxTag () {
$result = $this->abc->checkboxTag('bar');
$expected = '<label for="tag_bar"><input type="checkbox" name="data[bar]" id="tag_bar" />Bar</label>';
$this->assertEquals($result, $expected);
$result = $this->abc->checkboxTag('tofu', 'ToFu title', array('class'=>'Baz'));
$expected = '<label for="tag_tofu"><input type="checkbox" name="data[tofu]" id="tag_tofu" class="Baz" checked="checked" />ToFu title</label>';
$this->assertEquals($result, $expected);
}
function testRadioTags () {
$result = $this->abc->radioTags('foo', array('foo'=>'Foo', 'bar'=>'Bar'), '---', array('class'=>'Foo'));
$expected = '<label for="tag_foo_foo"><input type="radio" name="data[foo]" id="tag_foo_foo" class="Foo" value="foo" />Foo</label>---<label for="tag_foo_bar"><input type="radio" name="data[foo]" id="tag_foo_bar" class="Foo" value="bar" />Bar</label>';
$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 = '<select name="data[tofu]" class="Outer">'."\n".'<option value="" class="Inner" id="FooID"></option>'."\n".'<option value="m" class="Inner" id="FooID">male</option>'."\n".'<option value="f" class="Inner" id="FooID">female</option>'."\n".'</select>';
$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 = '<tr><th>One</th> <th>Two</th> <th>Three</th></tr>';
$this->assertEquals($result, $expected);
$result = $this->abc->tableHeaders(array('Foo Bar', 'Baz'), array('class'=>'Eco'), array('align'=>'left'));
$expected = '<tr class="Eco"><th align="left">Foo Bar</th> <th align="left">Baz</th></tr>';
$this->assertEquals($result, $expected);
}
function testTableCells () {
$result = $this->abc->tableCells(array('Foo', 'Bar'));
$expected = '<tr><td>Foo</td> <td>Bar</td></tr>';
$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('<tr class="Mini"><td>Foo</td> <td>Bar</td></tr>', '<tr align="left"><td>Baz</td> <td>Echo</td></tr>', '<tr class="Mini"><td>Nul</td> <td>Pio</td></tr>'));
$this->assertEquals($result, $expected);
}
function testImageTag () {
$result = $this->abc->imageTag('foo.gif');
$expected = '<img src="/ease/images/foo.gif" alt="" />';
$this->assertEquals($result, $expected);
$result = $this->abc->imageTag('bar/baz.gif', 'Foobar', array('class'=>'Zet'));
$expected = '<img src="/ease/images/bar/baz.gif" alt="Foobar" class="Zet" />';
$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 = '<a href="/ease">START</a> &raquo; <a href="/ease/bar/foo">Foo</a>';
$this->assertEquals($result, $expected);
}
/*
function test () {
$result = $this->abc->();
$expected = '';
$this->assertEquals($result, $expected);
}
*/
}
?>

View file

@ -0,0 +1,58 @@
<?php
uses ('test', 'db_factory');
class DbFactoryTest extends TestCase {
var $abc;
// constructor of the test suite
function DbFactoryTest($name) {
$this->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);
// }
}
?>

View file

@ -0,0 +1,76 @@
<?php
uses ('test', 'dbo_adodb');
class DboAdodbTest extends TestCase {
var $abc;
// constructor of the test suite
function DboAdodbTest ($name) {
$this->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()));
}
}
?>

View file

@ -0,0 +1,74 @@
<?php
uses ('test', 'dbo_mysql');
class DboMysqlTest extends TestCase {
var $abc;
// constructor of the test suite
function DboMysqlTest($name) {
$this->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()));
}
}
?>

View file

@ -0,0 +1,74 @@
<?php
uses ('test', 'dbo_postgres');
class DBOPostgresTest extends TestCase {
var $abc;
// constructor of the test suite
function DBOPostgresTest($name) {
$this->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()));
}
}
?>

75
tests/libs/dbo_test.php Normal file
View file

@ -0,0 +1,75 @@
<?php
uses ('test', 'db_factory');
class DboTest extends TestCase {
var $abc;
// constructor of the test suite
function DboTest ($name) {
$this->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()));
}
}
?>

96
tests/libs/flay_test.php Normal file
View file

@ -0,0 +1,96 @@
<?php
uses ('test', 'flay');
class FlayTest extends TestCase {
var $abc;
// constructor of the test suite
function FlayTest($name) {
$this->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'=>"<p>This is a text.</p>\n"
),
array(
'text'=>"This is a line.\n\n\nThis is\n another one.\n\n",
'html'=>"<p>This is a line.</p>\n<p>This is<br />\n another one.</p>\n"
),
array(
'text'=>"This line has *bold*, _italic_, and a _combo *bold* and italic_ texts.",
'html'=>"<p>This line has <strong>bold</strong>, <em>italic</em>, and a <em>combo <strong>bold</strong> and italic</em> texts.</p>\n"
),
array(
'text'=>"This line has <b>tags</b> which are <br />not allowed.",
'html'=>"<p>This line has &lt;b&gt;tags&lt;/b&gt; which are &lt;br /&gt;not allowed.</p>\n",
),
array(
'text'=>"[http://sputnik.pl] is a link, but so is [http://sputnik.pl/bla/ this one], and [this is not.",
'html'=>"<p><a href=\"http://sputnik.pl\" target=\"_blank\">http://sputnik.pl</a> is a link, but so is <a href=\"http://sputnik.pl/bla/\" target=\"_blank\">this one</a>, and [this is not.</p>\n"
),
array(
'text'=>"Why don't we try to insert an image.\n\n[http://sputnik.pl/test.jpg]",
'html'=>"<p>Why don't we try to insert an image.</p>\n<p><img src=\"http://sputnik.pl/test.jpg\" alt=\"\" /></p>\n"
),
array(
'text'=>"Auto-link my.name+real@my-server.com and example@example.com, should work.",
'html'=>"<p>Auto-link <a href=\"mailto:my.name+real@my-server.com\">my.name+real@my-server.com</a> and <a href=\"mailto:example@example.com\">example@example.com</a>, should work.</p>\n"
),
array(
'text'=>"\"\"\"This is a blockquote\"\"\"",
'html'=>"<blockquote>\n<p>This is a blockquote</p>\n</blockquote>\n"
),
array(
'text'=>"How about a blockquote?\"\"\"This is a multiline blockquote.\n\nThis is the second line.\"\"\"\nAnd this is not.",
'html'=>"<p>How about a blockquote?</p>\n<blockquote>\n<p>This is a multiline blockquote.</p>\n<p>This is the second line.</p>\n</blockquote>\n<p>And this is not.</p>\n"
),
array(
'text'=>"Now auto-link an url such as http://sputnik.pl or www.robocik-malowany.com/dupa[4] - or any other.",
'html'=>"<p>Now auto-link an url such as <a href=\"http://sputnik.pl\">http://sputnik.pl</a> or <a href=\"www.robocik-malowany.com/dupa[4]\">www.robocik-malowany.com/dupa[4]</a> &ndash; or any other.</p>\n"
),
array(
'text'=>"===This be centered===",
'html'=>"<center>\n<p>This be centered</p>\n</center>\n"
),
array(
'text'=>"===This be centered.\n\nAnd this be centered too,\nalong with this.===\nThis, alas, be not.",
'html'=>"<center>\n<p>This be centered.</p>\n<p>And this be centered too,<br />\nalong with this.</p>\n</center>\n<p>This, alas, be not.</p>\n"
),
array(
'text'=>"This tests (C)2004 Someone Else, \"Layer Your Apps(R)\" and Cake(TM).",
'html'=>"<p>This tests &copy;2004 Someone Else, \"Layer Your Apps&reg;\" and Cake&trade;.</p>\n"
),
);
foreach ($tests_to_html as $test) {
$this->assertEquals($this->abc->toHtml($test['text']), $test['html']);
}
}
}
?>

127
tests/libs/folder_test.php Normal file
View file

@ -0,0 +1,127 @@
<?PHP
uses ('test', 'folder');
class FolderTest extends TestCase {
var $abc;
// constructor of the test suite
function ControllerTest($name) {
$this->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);
}
*/
}
?>

View file

@ -0,0 +1,70 @@
<?PHP
uses ('test', 'inflector');
class InflectorTest extends TestCase {
var $abc;
function ControllerTest($name) {
$this->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);
}
*/
}
?>

View file

@ -0,0 +1,72 @@
<?PHP
uses ('test', 'router');
class RouterTest extends TestCase {
var $abc;
// constructor of the test suite
function ControllerTest($name) {
$this->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);
}
*/
}
?>