From b717167c4f2cbc307c6b2306f1e0efc9e88cab99 Mon Sep 17 00:00:00 2001 From: pies Date: Mon, 20 Jun 2005 23:08:59 +0000 Subject: [PATCH] - changed NeatArray to Narray, it's shorter, easier to use, - Time::daysAsSql returns SQL limits for a set of days, perhaps should be in DBO, - cleanups git-svn-id: https://svn.cakephp.org/repo/trunk/cake@259 3807eeeb-6ff5-0310-8944-8be069107fe0 --- config/paths.php | 4 +- libs/basics.php | 85 ++- libs/controller.php | 1 - libs/dispatcher.php | 3 +- libs/flay.php | 5 + libs/model.php | 9 +- libs/{neat_array.php => narray.php} | 41 +- libs/object.php | 7 + libs/router.php | 4 +- libs/template.php | 4 +- libs/test.php | 1054 --------------------------- libs/time.php | 13 + public/index.php | 2 +- tests/libs/narray.php | 60 ++ 14 files changed, 173 insertions(+), 1119 deletions(-) rename libs/{neat_array.php => narray.php} (88%) delete mode 100644 libs/test.php create mode 100644 tests/libs/narray.php diff --git a/config/paths.php b/config/paths.php index 82b3736ba..0863f9d22 100644 --- a/config/paths.php +++ b/config/paths.php @@ -86,12 +86,12 @@ define ('MODULES', ROOT.'modules'.DS); /** * Path to the public directory. */ -define ('WWW_ROOT', ROOT.'public'.DS); +define ('WWW_ROOT', ROOT.'public'.DS); /** * Path to the public directory. */ -define ('CSS', WWW_ROOT.'css'.DS); +define ('CSS', WWW_ROOT.'css'.DS); /** * Path to the scripts direcotry. diff --git a/libs/basics.php b/libs/basics.php index 1a3fcc8a0..303a80612 100644 --- a/libs/basics.php +++ b/libs/basics.php @@ -48,9 +48,11 @@ define('YEAR', 365 * DAY); * @uses APP * @uses MODELS */ -function loadModels () { +function loadModels () +{ require (APP.'app_model.php'); - foreach (listClasses(MODELS) as $model_fn) { + foreach (listClasses(MODELS) as $model_fn) + { require (MODELS.$model_fn); } } @@ -63,14 +65,17 @@ function loadModels () { * @uses HELPERS * @uses CONTROLLERS */ -function loadControllers () { +function loadControllers () +{ require (APP.'app_controller.php'); - foreach (listClasses(HELPERS) as $helper) { + foreach (listClasses(HELPERS) as $helper) + { require (HELPERS.$helper.'.php'); } - foreach (listClasses(CONTROLLERS) as $controller) { + foreach (listClasses(CONTROLLERS) as $controller) + { require (CONTROLLERS.$controller.'.php'); } } @@ -81,7 +86,8 @@ function loadControllers () { * @param string $name * @return boolean */ -function loadController ($name) { +function loadController ($name) +{ $controller_fn = CONTROLLERS.Inflector::underscore($name).'_controller.php'; $helper_fn = HELPERS.Inflector::underscore($name).'_helper.php'; @@ -108,18 +114,22 @@ function listClasses($path) /** * Loads configuration files */ -function config () { +function config () +{ $args = func_get_args(); - foreach ($args as $arg) { + foreach ($args as $arg) + { if (('database' == $arg) && file_exists(CONFIGS.$arg.'.php')) { include_once(CONFIGS.$arg.'.php'); } - elseif (file_exists(CONFIGS.$arg.'.php')) { + elseif (file_exists(CONFIGS.$arg.'.php')) + { include (CONFIGS.$arg.'.php'); if (count($args) == 1) return true; } - else { + else + { if (count($args) == 1) return false; } } @@ -180,13 +190,15 @@ if (!function_exists('getMicrotime')) * * @return integer */ - function getMicrotime() { + function getMicrotime() + { list($usec, $sec) = explode(" ", microtime()); return ((float)$usec + (float)$sec); } } -if (!function_exists('sortByKey')) { +if (!function_exists('sortByKey')) +{ /** * Sorts given $array by key $sortby. * @@ -196,30 +208,31 @@ if (!function_exists('sortByKey')) { * @param integer $type * @return mixed */ - function sortByKey(&$array, $sortby, $order='asc', $type=SORT_NUMERIC) { - - if (is_array($array)) { - - foreach ($array as $key => $val) - $sa[$key] = $val[$sortby]; - - if ($order == 'asc') - asort($sa, $type); - else - arsort($sa, $type); - - foreach ($sa as $key=>$val) - $out[] = $array[$key]; - - return $out; - - } - else + function sortByKey(&$array, $sortby, $order='asc', $type=SORT_NUMERIC) + { + if (!is_array($array)) return null; + + foreach ($array as $key => $val) + { + $sa[$key] = $val[$sortby]; + } + + $order == 'asc' + ? asort($sa, $type) + : arsort($sa, $type); + + foreach ($sa as $key=>$val) + { + $out[] = $array[$key]; + } + + return $out; } } -if (!function_exists('array_combine')) { +if (!function_exists('array_combine')) +{ /** * Combines given identical arrays by using the first array's values as keys, * and the second one's values as values. (Implemented for back-compatibility with PHP4.) @@ -228,18 +241,20 @@ if (!function_exists('array_combine')) { * @param array $a2 * @return mixed Outputs either combined array or false. */ - function array_combine($a1, $a2) { + function array_combine($a1, $a2) + { $a1 = array_values($a1); $a2 = array_values($a2); $c1 = count($a1); $c2 = count($a2); if ($c1 != $c2) return false; // different lenghts - if ($c1 <= 0) return false; // arrays are the same and both are empty + if ($c1 <= 0) return false; // arrays are the same and both are empty $output = array(); - for ($i = 0; $i < $c1; $i++) { + for ($i = 0; $i < $c1; $i++) + { $output[$a1[$i]] = $a2[$i]; } diff --git a/libs/controller.php b/libs/controller.php index 3d127ac07..5331c651d 100644 --- a/libs/controller.php +++ b/libs/controller.php @@ -16,7 +16,6 @@ /** * Purpose: Controller * Application controller (controllers are where you put all the actual code) - * based on RoR (www.rubyonrails.com). * Provides basic functionality, such as rendering views (aka displaying templates). * Automatically selects model name from on singularized object class name * and creates the model object if proper class exists. diff --git a/libs/dispatcher.php b/libs/dispatcher.php index 3bfa39926..633cb833a 100644 --- a/libs/dispatcher.php +++ b/libs/dispatcher.php @@ -75,11 +75,10 @@ class Dispatcher extends Object parent::__construct(); } - /** * Enter description here... * - * @param unknown_type $url + * @param string $url * @return unknown */ function dispatch($url) diff --git a/libs/flay.php b/libs/flay.php index 6fe73fd86..049db2bd2 100644 --- a/libs/flay.php +++ b/libs/flay.php @@ -288,6 +288,11 @@ class Flay extends Object { return strip_tags(html_entity_decode($text, ENT_QUOTES)); } + + function toParsedAndClean ($text) + { + return Flay::toClean(Flay::toHtml($text)); + } /** * Enter description here... diff --git a/libs/model.php b/libs/model.php index 587393ec0..6c30d5e83 100644 --- a/libs/model.php +++ b/libs/model.php @@ -297,7 +297,7 @@ class Model extends Object function loadInfo () { if (empty($this->_table_info)) - $this->_table_info = new NeatArray($this->db->fields($this->table)); + $this->_table_info = new Narray($this->db->fields($this->table)); return $this->_table_info; } @@ -337,6 +337,7 @@ class Model extends Object { if ($conditions) { + $conditions = $this->parseConditions($conditions); $data = $this->find($conditions); return $data[$name]; } @@ -552,7 +553,6 @@ class Model extends Object */ function findAll ($conditions = null, $fields = null, $order = null, $limit=50, $page=1) { - $conditions = $this->parseConditions($conditions); if (is_array($fields)) @@ -576,9 +576,10 @@ class Model extends Object $conditions .= ($conditions && $whers? ' AND ': null).$whers; $offset = $page > 1? $page*$limit: 0; - $limit = $limit? $limit: '-1'; - $limit_str = $this->db->selectLimit($limit, $offset); + $limit_str = $limit + ? $this->db->selectLimit($limit, $offset) + : ''; $sql = "SELECT " diff --git a/libs/neat_array.php b/libs/narray.php similarity index 88% rename from libs/neat_array.php rename to libs/narray.php index fcde6610c..088063327 100644 --- a/libs/neat_array.php +++ b/libs/narray.php @@ -7,9 +7,9 @@ * @subpackage cake.libs * @since Cake v 0.2.9 */ -class NeatArray { +class Narray { /** - * Value of NeatArray. + * Value of Narray. * * @var array * @access public @@ -21,20 +21,20 @@ class NeatArray { * * @param array $value * @access public - * @uses NeatArray::value + * @uses Narray::value */ - function NeatArray ($value=array()) { + function Narray ($value=array()) { $this->value = $value; } /** - * Checks whether $fieldName with $value exists in this NeatArray object. + * Finds and returns records with $fieldName equal $value from this Narray. * * @param string $fieldName * @param string $value * @return mixed * @access public - * @uses NeatArray::value + * @uses Narray::value */ function findIn ($fieldName, $value) { @@ -44,8 +44,10 @@ class NeatArray { } $out = false; - foreach ($this->value as $k=>$v) { - if (isset($v[$fieldName]) && ($v[$fieldName] == $value)) { + foreach ($this->value as $k=>$v) + { + if (isset($v[$fieldName]) && ($v[$fieldName] == $value)) + { $out[$k] = $v; } } @@ -57,7 +59,7 @@ class NeatArray { * Checks if $this->value is array, and removes all empty elements. * * @access public - * @uses NeatArray::value + * @uses Narray::value */ function cleanup () { $out = is_array($this->value)? array(): null; @@ -75,7 +77,7 @@ class NeatArray { * @param string $value * @return bool * @access public - * @uses NeatArray::value + * @uses Narray::value */ function add ($value) { return ($this->value = $this->plus($value))? true: false; @@ -84,10 +86,10 @@ class NeatArray { /** * Returns itself merged with given array. * - * @param array $value Array to add to NeatArray. + * @param array $value Array to add to Narray. * @return array * @access public - * @uses NeatArray::value + * @uses Narray::value */ function plus ($value) { return array_merge($this->value, (is_array($value)? $value: array($value))); @@ -99,7 +101,7 @@ class NeatArray { * @param int $sortedBy A value of 1 sorts by values, a value of 2 sorts by keys. Defaults to null (no sorting). * @return array * @access public - * @uses NeatArray::value + * @uses Narray::value */ function totals ($sortedBy=1,$reverse=true) { $out = array(); @@ -132,7 +134,7 @@ class NeatArray { * * @return array * @access public - * @uses NeatArray::value + * @uses Narray::value */ function walk ($with) { array_walk($this->value, $with); @@ -154,7 +156,7 @@ class NeatArray { * * @return array * @access public - * @uses NeatArray::value + * @uses Narray::value */ function extract ($name) { $out = array(); @@ -191,7 +193,7 @@ class NeatArray { * $alice = array('id'=>'1', 'name'=>'Alice'); * $bob = array('id'=>'2', 'name'=>'Bob'); * - * $users = new NeatArray(array($alice, $bob)); + * $users = new Narray(array($alice, $bob)); * * $born = array * ( @@ -219,7 +221,7 @@ class NeatArray { function joinWith ($his, $onMine, $onHis=null) { if (empty($onHis)) $onHis = $onMine; - $his = new NeatArray($his); + $his = new Narray($his); $out = array(); foreach ($this->value as $key=>$val) { @@ -261,4 +263,9 @@ class NeatArray { } } +// for backwards-compatibility, remove in 2008 +class NeatArray extends Narray +{ +} + ?> \ No newline at end of file diff --git a/libs/object.php b/libs/object.php index 4d99e9f04..d2e1ae285 100644 --- a/libs/object.php +++ b/libs/object.php @@ -42,6 +42,13 @@ uses('log'); class Object { + /** + * Database connection, if available. + * + * @var DBO + */ + var $db = null; + /** * A hack to support __construct() on PHP 4 * Hint: descendant classes have no PHP4 class_name() constructors, diff --git a/libs/router.php b/libs/router.php index a78bb8491..9fa0e0e5b 100644 --- a/libs/router.php +++ b/libs/router.php @@ -30,7 +30,7 @@ * @license http://www.opensource.org/licenses/mit-license.php The MIT License */ -uses('object'); +uses('object', 'narray'); /** * Parses the request URL into controller, action, and parameters. @@ -156,7 +156,7 @@ class Router extends Object { // unnamed elements go in as 'pass' else { - $pass = new NeatArray(explode('/', $found)); + $pass = new Narray(explode('/', $found)); $pass->cleanup(); $out['pass'] = $pass->value; } diff --git a/libs/template.php b/libs/template.php index 4bc9b0fe3..2c3ad313a 100644 --- a/libs/template.php +++ b/libs/template.php @@ -1022,7 +1022,9 @@ class Template extends Object function remoteFunction ($options=null) { $javascript_options = $this->__optionsForAjax($options); - $func = isset($options['update']) ? "new Ajax.Updater('{$options['update']}', " : "new Ajax.Request("; + $func = isset($options['update']) + ? "new Ajax.Updater('{$options['update']}', " + : "new Ajax.Request("; $func .= "'" . $this->urlFor($options['url']) . "'"; $func .= ", $javascript_options)"; diff --git a/libs/test.php b/libs/test.php deleted file mode 100644 index 9860a2ef6..000000000 --- a/libs/test.php +++ /dev/null @@ -1,1054 +0,0 @@ - + // -// + OntoSys, Inc + // -// + + // -// + Changes by Michal Tatarynowicz + // -// +---------------------------------------------------------------------+ // -// + Permission is hereby granted, free of charge, to any person + // -// + obtaining a copy of this software and associated documentation + // -// + files (the "Software"), to deal in the Software without + // -// + restriction, including without limitation the rights to use, copy, + // -// + modify, merge, publish, distribute, sublicense, and/or sell copies + // -// + of the Software, and to permit persons to whom the Software is + // -// + furnished to do so, subject to the following conditions: + // -// + + // -// + The above copyright notice and this permission notice shall be + // -// + included in all copies or substantial portions of the Software. + // -// + + // -// + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + // -// + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + // -// + MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + // -// + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS + // -// + BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN + // -// + ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN + // -// + CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + // -// + SOFTWARE. + // -///////////////////////////////////////////////////////////////////////////// - -/** - * Purpose: PHP framework for testing, based on the design of "JUnit". - * - * @filesource - * @author Fred Yankowski - * @copyright Copyright (c) 2000 Fred Yankowski - * @package cake - * @subpackage cake.libs - * @since Cake v 1.0.0.0 - * @version $Revision$ - * @modifiedby $LastChangedBy$ - * @lastmodified $Date$ - */ - -/** - * Outputs given string. - * - * @param string $msg - */ -function trace($msg) { - return; - print($msg); - flush(); -} -/** - * Enter description here... - * - */ -if (phpversion() >= '4') { -/** - * Enter description here... - * - * @param unknown_type $errno Error number - * @param string $errstr Error string - * @param unknown_type $errfile Error filename - * @param unknown_type $errline Error on line - */ - function PHPUnit_error_handler($errno, $errstr, $errfile, $errline) { - global $PHPUnit_testRunning; - $PHPUnit_testRunning[0]->fail("PHP ERROR: ".$errstr." in ".$errfile." at line ".$errline); - } -} -/** - * Exception class for testing - * - * @package cake - * @subpackage cake.libs - * @since Cake v 1.0.0.0 - * - */ -class TestException { - /* Emulate a Java exception, sort of... */ -/** - * Message of the exception - * - * @var string - */ - var $message; -/** - * Type of exception - * - * @var string - */ - var $type; -/** - * Constructor. - * - * @param string $message - * @param string $type - * @return TestException - */ - function TestException($message, $type = 'FAILURE') { - $this->message = $message; - $this->type = $type; - } -/** - * Returns the exception's message. - * - * @return string Message of exception - */ - function getMessage() { - return $this->message; - } -/** - * Returns the exception's type. - * - * @return string Type of exception - */ - function getType() { - return $this->type; - } -} - -/** - * Asserts for Unit Testing - * - * @package cake - * @subpackage cake.libs - * @since Cake v 1.0.0.0 - * - */ -class Assert { -/** - * Enter description here... - * - * @param boolean $boolean - * @param string $message - */ - function assert($boolean, $message=0) { - if (! $boolean) - $this->fail($message); - } - -/** - * Enter description here... - * - * @param unknown_type $expected - * @param unknown_type $actual - * @param unknown_type $message - * @return unknown - */ - function asEq ($expected, $actual, $message=0) { - return $this->assertEquals($expected, $actual, $message); - } -/** - * Enter description here... - * - * @param unknown_type $expected - * @param unknown_type $actual - * @param unknown_type $message - */ - function assertEquals($expected, $actual, $message=0) { - if (gettype($expected) != gettype($actual)) { - $this->failNotEquals($expected, $actual, "expected", $message); - return; - } - - if (phpversion() < '4') { - if (is_object($expected) or is_object($actual) or is_array($expected) or is_array($actual)) { - $this->error("INVALID TEST: cannot compare arrays or objects in PHP3"); - return; - } - } - - if (phpversion() >= '4' && is_object($expected)) { - if (get_class($expected) != get_class($actual)) { - $this->failNotEquals($expected, $actual, "expected", $message); - return; - } - - if (method_exists($expected, "equals")) { - if (! $expected->equals($actual)) { - $this->failNotEquals($expected, $actual, "expected", $message); - } - return; // no further tests after equals() - } - } - - if (phpversion() >= '4.0.4') { - if (is_null($expected) != is_null($actual)) { - $this->failNotEquals($expected, $actual, "expected", $message); - return; - } - } - - if ($expected != $actual) { - $this->failNotEquals($actual, $expected, "expected", $message); - } - } - -/** - * Assert regular expression - * - * @param string $regexp - * @param string $actual - * @param unknown_type $message - */ - function assertRegexp($regexp, $actual, $message=false) { - if (! preg_match($regexp, $actual)) { - $this->failNotEquals($regexp, $actual, "pattern", $message); - } - } - -/** - * Enter description here... - * - * @param unknown_type $string0 - * @param unknown_type $string1 - * @param unknown_type $message - */ - function assertEqualsMultilineStrings($string0, $string1, - $message="") { - $lines0 = split("\n",$string0); - $lines1 = split("\n",$string1); - if (sizeof($lines0) != sizeof($lines1)) { - $this->failNotEquals(sizeof($lines0)." line(s)", - sizeof($lines1)." line(s)", "expected", $message); - } - for($i=0; $i< sizeof($lines0); $i++) { - $this->assertEquals(trim($lines0[$i]), - trim($lines1[$i]), - "line ".($i+1)." of multiline strings differ. ".$message); - } - } - -/** - * Enter description here... - * - * @param unknown_type $fn - */ - function assertFileExists ($fn) { - if (!file_exists($fn)) - $this->failNotEquals($fn, "file not found", "file expected"); - } - -/** - * Enter description here... - * - * @param unknown_type $fn - * @param unknown_type $str - */ - function assertFileContains ($fn, $str) { - if (file_exists($fn)) { - $lines = file($fn); - $text = implode("\n", $lines); - - if (!preg_match("/{$str}/", $text)) - $this->failNotEquals($fn, 'expected '.$str, 'expected'); - } - else - $this->failNotEquals($fn, 'file doesn\'t exist', 'expected'); - } - -/** - * Enter description here... - * - * @param unknown_type $path - */ - function assertDirExists ($path) { - if (!is_dir($path)) - $this->failNotEquals($path, "directory not found", "expected"); - } - -/** - * Enter description here... - * - * @param unknown_type $value - */ - function assertTrue ($value) { - if (!$value) - $this->failNotEquals($value, true, "expected"); - } - - -/** - * Enter description here... - * - * @param mixed $value - * @return string - */ - function _formatValue ($value) { - - if (is_object($value) && method_exists($value, "toString")) { - $valueStr = $value->toString(); - } - elseif (is_bool($value)) { - $valueStr = $value? 'false': 'true'; - } - elseif (is_object($value) || is_array($value)) { - $valueStr = serialize($value); - } - else { - $valueStr = str_replace('<', '<', str_replace('>', '>', $value)); - } - - return array($valueStr, gettype($value)); - } - - -/** - * Enter description here... - * - * @param unknown_type $expected - * @param unknown_type $actual - * @param unknown_type $expected_label - * @param unknown_type $message - */ - function failNotEquals ($expected, $actual, $expected_label, $message=null) { - $out = array( - 'message'=>$message, - 'label'=>$expected_label, - 'expected'=>$this->_formatValue($expected, "expected"), - 'actual'=>$this->_formatValue($actual, "actual") - ); - - $this->fail($out); - } - -/** - * Enter description here... - * - * @param unknown_type $expected - * @param unknown_type $actual - * @param unknown_type $expected_label - * @param unknown_type $message - */ - function old_failNotEquals($expected, $actual, $expected_label, $message=0) { - // Private function for reporting failure to match. - $str = $message ? ($message . ' ') : ''; - //$str .= "($expected_label/actual)
"; - $str .= "
"; - $str .= sprintf("%s
%s", - $this->_formatValue($expected, "expected"), - $this->_formatValue($actual, "actual")); - $this->fail($str); - } -} - -/** - * Enter description here... - * - * - * @package cake - * @subpackage cake.libs - * @since Cake v 1.0.0.0 - * - */ -class TestCase extends Assert /* implements Test */ { - /* Defines context for running tests. Specific context -- such as - instance variables, global variables, global state -- is defined - by creating a subclass that specializes the setUp() and - tearDown() methods. A specific test is defined by a subclass - that specializes the runTest() method. */ -/** - * Enter description here... - * - * @var unknown_type - */ - var $fName; -/** - * Enter description here... - * - * @var unknown_type - */ - var $fClassName; -/** - * Enter description here... - * - * @var unknown_type - */ - var $fResult; -/** - * Enter description here... - * - * @var unknown_type - */ - var $fExceptions = array(); - -/** - * Enter description here... - * - * @param unknown_type $name - * @return TestCase - */ - function TestCase($name) { - $this->fName = $name; - } - -/** - * Enter description here... - * - * @param unknown_type $testResult - * @return unknown - */ - function run($testResult=0) { - /* Run this single test, by calling the run() method of the - TestResult object which will in turn call the runBare() method - of this object. That complication allows the TestResult object - to do various kinds of progress reporting as it invokes each - test. Create/obtain a TestResult object if none was passed in. - Note that if a TestResult object was passed in, it must be by - reference. */ - if (! $testResult) - $testResult = $this->_createResult(); - $this->fResult = $testResult; - $testResult->run(&$this); - $this->fResult = 0; - return $testResult; - } - -/** - * Enter description here... - * - * @return unknown - */ - function classname() { - if (isset($this->fClassName)) { - return $this->fClassName; - } else { - return get_class($this); - } - } - -/** - * Enter description here... - * - * @return unknown - */ - function countTestCases() { - return 1; - } - -/** - * Enter description here... - * - */ - function runTest() { - - if (version_compare(phpversion(), '4') >= 0) { - global $PHPUnit_testRunning; - eval('$PHPUnit_testRunning[0] = & $this;'); - - // Saved ref to current TestCase, so that the error handler - // can access it. This code won't even parse in PHP3, so we - // hide it in an eval. - - $old_handler = set_error_handler("PHPUnit_error_handler"); - // errors will now be handled by our error handler - } - - $name = $this->name(); - if ((version_compare(phpversion(), '4') >= 0) && ! method_exists($this, $name)) { - $this->error("Method '$name' does not exist"); - } - else - $this->$name(); - - if (version_compare(phpversion(), '4') >= 0) { - restore_error_handler(); // revert to prior error handler - $PHPUnit_testRunning = null; - } - } - -/** - * Enter description here... - * - */ - function setUp() /* expect override */ { - //print("TestCase::setUp()
\n"); - } - -/** - * Enter description here... - * - */ - function tearDown() /* possible override */ { - //print("TestCase::tearDown()
\n"); - } - - //////////////////////////////////////////////////////////////// - - -/** - * Enter description here... - * - * @return unknown - */ - function _createResult() /* protected */ { - /* override this to use specialized subclass of TestResult */ - return new TestResult; - } - -/** - * Enter description here... - * - * @param unknown_type $message - */ - function fail($message=0) { - //printf("TestCase::fail(%s)
\n", ($message) ? $message : ''); - /* JUnit throws AssertionFailedError here. We just record the - failure and carry on */ - $this->fExceptions[] = new TestException(&$message, 'FAILURE'); - } - -/** - * Enter description here... - * - * @param unknown_type $message - */ - function error($message) { - /* report error that requires correction in the test script - itself, or (heaven forbid) in this testing infrastructure */ - $this->fExceptions[] = new TestException(&$message, 'ERROR'); - $this->fResult->stop(); // [does not work] - } - -/** - * Enter description here... - * - * @return unknown - */ - function failed() { - reset($this->fExceptions); - while (list($key, $exception) = each($this->fExceptions)) { - if ($exception->type == 'FAILURE') - return true; - } - return false; - } -/** - * Enter description here... - * - * @return unknown - */ - function errored() { - reset($this->fExceptions); - while (list($key, $exception) = each($this->fExceptions)) { - if ($exception->type == 'ERROR') - return true; - } - return false; - } - -/** - * Enter description here... - * - * @return unknown - */ - function getExceptions() { - return $this->fExceptions; - } - -/** - * Enter description here... - * - * @return unknown - */ - function name() { - return $this->fName; - } - -/** - * Enter description here... - * - */ - function runBare() { - $this->setup(); - $this->runTest(); - $this->tearDown(); - } -} - - -/** - * Test Suite for Unit Testing. - * - * @package cake - * @subpackage cake.libs - * @since Cake v 1.0.0.0 - * - */ -class TestSuite /* implements Test */ { - /* Compose a set of Tests (instances of TestCase or TestSuite), and - run them all. */ -/** - * Enter description here... - * - * @var unknown_type - */ - var $fTests = array(); -/** - * Enter description here... - * - * @var unknown_type - */ - var $fClassname; - -/** - * Enter description here... - * - * @param unknown_type $classname - * @return TestSuite - */ - function TestSuite($classname=false) { - // Find all methods of the given class whose name starts with - // "test" and add them to the test suite. - - // PHP3: We are just _barely_ able to do this with PHP's limited - // introspection... Note that PHP seems to store method names in - // lower case, and we have to avoid the constructor function for - // the TestCase class superclass. Names of subclasses of TestCase - // must not start with "Test" since such a class will have a - // constructor method name also starting with "test" and we can't - // distinquish such a construtor from the real test method names. - // So don't name any TestCase subclasses as "Test..."! - - // PHP4: Never mind all that. We can now ignore constructor - // methods, so a test class may be named "Test...". - - if (empty($classname)) - return; - $this->fClassname = $classname; - - if (!class_exists($classname)) { - user_error('Tested class '.$classname.' doesn\'t appear to exist.', E_USER_WARNING); - return; - } - - if (floor(phpversion()) >= 4) { - // PHP4 introspection, submitted by Dylan Kuhn - - $names = get_class_methods($classname); - - while (list($key, $method) = each($names)) { - if (preg_match('/^test/', $method)) { - $test = new $classname($method); - if (strcasecmp($method, $classname) == 0 || is_subclass_of($test, $method)) { - // Ignore the given method name since it is a constructor: - // it's the name of our test class or it is the name of a - // superclass of our test class. (This code smells funny. - // Anyone got a better way?) - - //print "skipping $method
"; - } - else { - $this->addTest($test); - } - } - } - } - else { // PHP3 - $dummy = new $classname("dummy"); - $names = (array) $dummy; - while (list($key, $value) = each($names)) { - $type = gettype($value); - if ($type == "user function" && preg_match('/^test/', $key) - && $key != "testcase") { - $this->addTest(new $classname($key)); - } - } - } - } - -/** - * Enter description here... - * - * @param unknown_type $test - */ - function addTest($test) { - /* Add TestCase or TestSuite to this TestSuite */ - $this->fTests[] = $test; - } - -/** - * Enter description here... - * - * @param unknown_type $testResult - */ - function run(&$testResult) { - /* Run all TestCases and TestSuites comprising this TestSuite, - accumulating results in the given TestResult object. */ - reset($this->fTests); - while (list($na, $test) = each($this->fTests)) { - if ($testResult->shouldStop()) - break; - $test->run(&$testResult); - } - } - -/** - * Enter description here... - * - * @return unknown - */ - function countTestCases() { - /* Number of TestCases comprising this TestSuite (including those - in any constituent TestSuites) */ - $count = 0; - reset($fTests); - while (list($na, $test_case) = each($this->fTests)) { - $count += $test_case->countTestCases(); - } - return $count; - } -} - - -/** - * Test Failure for Unit Testing - * - * @package cake - * @subpackage cake.libs - * @since Cake v 1.0.0.0 - * - */ -class TestFailure { - /* Record failure of a single TestCase, associating it with the - exception that occurred */ -/** - * Enter description here... - * - * @var unknown_type - */ - var $fFailedTestName; -/** - * Enter description here... - * - * @var unknown_type - */ - var $fException; - -/** - * Enter description here... - * - * @param unknown_type $test - * @param unknown_type $exception - * @return TestFailure - */ - function TestFailure(&$test, &$exception) { - $this->fFailedTestName = $test->name(); - $this->fException = $exception; - } - -/** - * Enter description here... - * - * @return unknown - */ - function getExceptions() { - // deprecated - return array($this->fException); - } -/** - * Enter description here... - * - * @return unknown - */ - function getException() { - return $this->fException; - } - -/** - * Enter description here... - * - * @return unknown - */ - function getTestName() { - return $this->fFailedTestName; - } -} - - -/** - * Test Result for Unit Testing - * - * @package cake - * @subpackage cake.libs - * @since Cake v 1.0.0.0 - * - */ -class TestResult { - /* Collect the results of running a set of TestCases. */ -/** - * Enter description here... - * - * @var unknown_type - */ - var $fFailures = array(); -/** - * Enter description here... - * - * @var unknown_type - */ - var $fErrors = array(); -/** - * Enter description here... - * - * @var unknown_type - */ - var $fRunTests = 0; -/** - * Enter description here... - * - * @var unknown_type - */ - var $fStop = false; - -/** - * Enter description here... - * - * @return TestResult - */ - function TestResult() { } - -/** - * Enter description here... - * - * @param unknown_type $test - */ - function _endTest($test) /* protected */ { - /* specialize this for end-of-test action, such as progress - reports */ - } - -/** - * Enter description here... - * - * @param unknown_type $test - * @param unknown_type $exception - */ - function addError($test, $exception) { - $this->fErrors[] = new TestFailure(&$test, &$exception); - } - -/** - * Enter description here... - * - * @param unknown_type $test - * @param unknown_type $exception - */ - function addFailure($test, $exception) { - $this->fFailures[] = new TestFailure(&$test, &$exception); - } - -/** - * Enter description here... - * - * @return unknown - */ - function getFailures() { - return $this->fFailures; - } - -/** - * Enter description here... - * - * @param unknown_type $test - */ - function run($test) { - /* Run a single TestCase in the context of this TestResult */ - $this->_startTest($test); - $this->fRunTests++; - - $test->runBare(); - - /* this is where JUnit would catch AssertionFailedError */ - $exceptions = $test->getExceptions(); - reset($exceptions); - while (list($key, $exception) = each($exceptions)) { - if ($exception->type == 'ERROR') - $this->addError($test, $exception); - else if ($exception->type == 'FAILURE') - $this->addFailure($test, $exception); - } - // if ($exceptions) - // $this->fFailures[] = new TestFailure(&$test, &$exceptions); - $this->_endTest($test); - } - -/** - * Enter description here... - * - * @return unknown - */ - function countTests() { - return $this->fRunTests; - } - -/** - * Enter description here... - * - * @return unknown - */ - function shouldStop() { - return $this->fStop; - } - -/** - * Enter description here... - * - * @param unknown_type $test - */ - function _startTest($test) /* protected */ { - /* specialize this for start-of-test actions */ - } - -/** - * Enter description here... - * - */ - function stop() { - /* set indication that the test sequence should halt */ - $fStop = true; - } - -/** - * Enter description here... - * - * @return unknown - */ - function errorCount() { - return count($this->fErrors); - } -/** - * Enter description here... - * - * @return unknown - */ - function failureCount() { - return count($this->fFailures); - } -/** - * Enter description here... - * - * @return unknown - */ - function countFailures() { - // deprecated - return $this->failureCount(); - } -} - -/** - * Mines data from test results. Used for Unit Testing. - * - * @package cake - * @subpackage cake.libs - * @since Cake v 1.0.0.0 - * - */ -class ResultDataMiner extends TestResult { -/** - * Enter description here... - * - * @var unknown_type - */ - var $tests = null; -/** - * Total number of tests - * - * @var int - */ - var $total_tests = 0; -/** - * Total number of errors - * - * @var int - */ - var $total_errors = 0; - -/** - * Enter description here... - * - * @param unknown_type $test - */ - function _startTest($test) { - } - -/** - * Enter description here... - * - * @param unknown_type $test - */ - function _endTest($test) { - $class_name = $test->classname(); - if (preg_match('/^(.*)test$/i', $class_name, $r)) - $class_name = $r[1]; - $method_name = $test->name(); - if (preg_match('/^test(.*)$/i', $method_name, $r)) - $method_name = $r[1]; - - $errors = null; - foreach ($test->getExceptions() as $exception) { - $errors[] = $exception->message; - } - - $this->tests[] = array( - 'class' => $class_name, - 'method' => $method_name, - 'failed' => $test->failed(), - 'errors' => $errors - ); - - $this->total_tests++; - if ($test->failed()) $this->total_errors++; - } - -/** - * Returns an array with total number of performed tests, total number of errors, and details of the tests. - * - * @return array Array with information on how the tests went. - */ - function report () { - return array('tests'=>$this->total_tests, 'errors'=>$this->total_errors, 'details'=>$this->tests); - } - -} - -/** - * Test Runner for Unit Testing. - * - * @package cake - * @subpackage cake.libs - * @since Cake v 1.0.0.0 - * - */ -class TestRunner { -/** - * Run a suite of tests and report results. - * - * @param unknown_type $suite - * @return unknown - */ - function run($suite) { - $result = new ResultDataMiner; - $suite->run($result); - return $result->report(); - } -} - -?> \ No newline at end of file diff --git a/libs/time.php b/libs/time.php index d229c0f86..648f3c4da 100644 --- a/libs/time.php +++ b/libs/time.php @@ -85,6 +85,19 @@ class Time extends Object { function isToday ($date) { return date('Y-m-d', $date) == date('Y-m-d', time()); } + + function daysAsSql ($begin, $end, $field_name) + { + $begin = date('Y-m-d', $begin).' 00:00:00'; + $end = date('Y-m-d', $end). ' 23:59:59'; + + return "($field_name >= '$begin') AND ($field_name <= '$end')"; + } + + function dayAsSql ($date, $field_name) + { + return Time::daysAsSql($date, $date, $field_name); + } /** * Returns true if given datetime string is within current year. diff --git a/public/index.php b/public/index.php index 474c756ba..604c23020 100644 --- a/public/index.php +++ b/public/index.php @@ -57,7 +57,7 @@ require_once (ROOT.'config/paths.php'); require_once (ROOT.'libs/basics.php'); require_once (ROOT.'libs/log.php'); require_once (ROOT.'libs/object.php'); -require_once (ROOT.'libs/neat_array.php'); +require_once (ROOT.'libs/narray.php'); require_once (ROOT.'libs/inflector.php'); DEBUG? error_reporting(E_ALL): error_reporting(0); diff --git a/tests/libs/narray.php b/tests/libs/narray.php new file mode 100644 index 000000000..997ecb354 --- /dev/null +++ b/tests/libs/narray.php @@ -0,0 +1,60 @@ +UnitTestCase('Narray test'); + } + + // called before the test functions will be executed + // this function is defined in PHPUnit_TestCase and overwritten + // here + function setUp() + { + $this->narray = new Narray(); + } + + // called after the test functions are executed + // this function is defined in PHPUnit_TestCase and overwritten + // here + function tearDown() + { + unset($this->narray); + } + + + function testInArray() + { + $a = array('foo'=>' bar ', 'i-am'=>'a'); + $b = array('foo'=>'bar ', 'i-am'=>'b'); + $c = array('foo'=>' bar', 'i-am'=>'c'); + $d = array('foo'=>'bar', 'i-am'=>'d'); + + $n = new Narray(array($a, $b, $c, $d)); + + $result = $n->findIn('foo', ' bar '); + $expected = array(0=>$a); + $this->assertEqual($result, $expected); + + $result = $n->findIn('foo', 'bar '); + $expected = array(1=>$b); + $this->assertEqual($result, $expected); + + $result = $n->findIn('foo', ' bar'); + $expected = array(2=>$c); + $this->assertEqual($result, $expected); + + $result = $n->findIn('foo', 'bar'); + $expected = array(3=>$d); + $this->assertEqual($result, $expected); + } + +} + +?> \ No newline at end of file