Adding tests to __* functions and doing a little refactoring to reduce overload.

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7753 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
renan.saddam 2008-10-16 01:11:09 +00:00
parent f281f8814a
commit 6a33ca5b5b
2 changed files with 323 additions and 46 deletions

View file

@ -61,7 +61,7 @@ if (!function_exists('clone')) {
function config() {
$args = func_get_args();
foreach ($args as $arg) {
if (('database' == $arg) && file_exists(CONFIGS . $arg . '.php')) {
if ($arg === 'database' && file_exists(CONFIGS . 'database.php')) {
include_once(CONFIGS . $arg . '.php');
} elseif (file_exists(CONFIGS . $arg . '.php')) {
include_once(CONFIGS . $arg . '.php');
@ -106,15 +106,16 @@ if (!function_exists('clone')) {
if (Configure::read() > 0) {
if ($showFrom) {
$calledFrom = debug_backtrace();
print "<strong>".substr(r(ROOT, "", $calledFrom[0]['file']), 1)."</strong> (line <strong>".$calledFrom[0]['line']."</strong>)";
echo '<strong>' . substr(str_replace(ROOT, '', $calledFrom[0]['file']), 1) . '</strong>';
echo ' (line <strong>' . $calledFrom[0]['line'] . '</strong>)';
}
print "\n<pre class=\"cake-debug\">\n";
$var = print_r($var, true);
echo "\n<pre class=\"cake-debug\">\n";
$var = print_r($var, true);
if ($showHtml) {
$var = str_replace('<', '&lt;', str_replace('>', '&gt;', $var));
}
print "{$var}\n</pre>\n";
echo $var . "\n</pre>\n";
}
}
if (!function_exists('getMicrotime')) {
@ -124,7 +125,7 @@ if (!function_exists('getMicrotime')) {
* @return float Microtime
*/
function getMicrotime() {
list($usec, $sec) = explode(" ", microtime());
list($usec, $sec) = explode(' ', microtime());
return ((float)$usec + (float)$sec);
}
}
@ -243,13 +244,14 @@ if (!function_exists('array_combine')) {
*/
function aa() {
$args = func_get_args();
for ($l = 0, $c = count($args); $l < $c; $l++) {
if ($l + 1 < count($args)) {
$a[$args[$l]] = $args[$l + 1];
$argc = count($args);
for ($i = 0; $i < $argc; $i++) {
if ($i + 1 < $argc) {
$a[$args[$i]] = $args[$i + 1];
} else {
$a[$args[$l]] = null;
$a[$args[$i]] = null;
}
$l++;
$i++;
}
return $a;
}
@ -300,9 +302,9 @@ if (!function_exists('array_combine')) {
*/
function pr($var) {
if (Configure::read() > 0) {
echo "<pre>";
echo '<pre>';
print_r($var);
echo "</pre>";
echo '</pre>';
}
}
/**
@ -331,7 +333,8 @@ if (!function_exists('array_combine')) {
*/
function am() {
$r = array();
foreach (func_get_args()as $a) {
$args = func_get_args();
foreach ($args as $a) {
if (!is_array($a)) {
$a = array($a);
}
@ -351,7 +354,7 @@ if (!function_exists('array_combine')) {
function env($key) {
if ($key == 'HTTPS') {
if (isset($_SERVER) && !empty($_SERVER)) {
return (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on');
return (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on');
}
return (strpos(env('SCRIPT_URI'), 'https://') === 0);
}
@ -371,9 +374,9 @@ if (!function_exists('array_combine')) {
$val = getenv($key);
}
if ($key == 'REMOTE_ADDR' && $val == env('SERVER_ADDR')) {
if ($key === 'REMOTE_ADDR' && $val === env('SERVER_ADDR')) {
$addr = env('HTTP_PC_REMOTE_ADDR');
if ($addr != null) {
if ($addr !== null) {
$val = $addr;
}
}
@ -385,26 +388,28 @@ if (!function_exists('array_combine')) {
switch ($key) {
case 'SCRIPT_FILENAME':
if (defined('SERVER_IIS') && SERVER_IIS === true){
return str_replace('\\\\', '\\', env('PATH_TRANSLATED') );
return str_replace('\\\\', '\\', env('PATH_TRANSLATED'));
}
break;
case 'DOCUMENT_ROOT':
$name = env('SCRIPT_NAME');
$filename = env('SCRIPT_FILENAME');
$offset = 0;
if (!strpos(env('SCRIPT_NAME'), '.php')) {
if (!strpos($name, '.php')) {
$offset = 4;
}
return substr(env('SCRIPT_FILENAME'), 0, strlen(env('SCRIPT_FILENAME')) - (strlen(env('SCRIPT_NAME')) + $offset));
return substr($filename, 0, strlen($filename) - (strlen($name) + $offset));
break;
case 'PHP_SELF':
return r(env('DOCUMENT_ROOT'), '', env('SCRIPT_FILENAME'));
return str_replace(env('DOCUMENT_ROOT'), '', env('SCRIPT_FILENAME'));
break;
case 'CGI_MODE':
return (PHP_SAPI == 'cgi');
return (PHP_SAPI === 'cgi');
break;
case 'HTTP_BASE':
$host = env('HTTP_HOST');
if (substr_count($host, '.') != 1) {
return preg_replace ('/^([^.])*/i', null, env('HTTP_HOST'));
if (substr_count($host, '.') !== 1) {
return preg_replace('/^([^.])*/i', null, env('HTTP_HOST'));
}
return '.' . $host;
break;
@ -510,7 +515,7 @@ if (!function_exists('file_put_contents')) {
@unlink($cache . $ext);
return true;
} elseif (is_dir($cache)) {
$files = glob("$cache*");
$files = glob($cache . '*');
if ($files === false) {
return false;
@ -545,7 +550,7 @@ if (!function_exists('file_put_contents')) {
return true;
}
} elseif (is_array($params)) {
foreach ($params as $key => $file) {
foreach ($params as $file) {
clearCache($file, $type, $ext);
}
return true;
@ -566,7 +571,7 @@ if (!function_exists('file_put_contents')) {
} else {
$values = stripslashes($values);
}
return $values ;
return $values;
}
/**
* Returns a translated string if one is found, or the submitted message if not found.
@ -780,19 +785,20 @@ if (!function_exists('file_put_contents')) {
function array_diff_key() {
$valuesDiff = array();
if (func_num_args() < 2) {
$argc = func_num_args();
if ($argc < 2) {
return false;
}
foreach (func_get_args() as $param) {
$args = func_get_args();
foreach ($args as $param) {
if (!is_array($param)) {
return false;
}
}
$args = func_get_args();
foreach ($args[0] as $valueKey => $valueData) {
for ($i = 1; $i < func_num_args(); $i++) {
for ($i = 1; $i < $argc; $i++) {
if (isset($args[$i][$valueKey])) {
continue 2;
}
@ -812,8 +818,8 @@ if (!function_exists('file_put_contents')) {
if (!function_exists('array_intersect_key')) {
function array_intersect_key($arr1, $arr2) {
$res = array();
foreach ($arr1 as $key=>$value) {
if (array_key_exists($key, $arr2)) {
foreach ($arr1 as $key => $value) {
if (isset($arr2[$key])) {
$res[$key] = $arr1[$key];
}
}
@ -842,7 +848,7 @@ if (!function_exists('file_put_contents')) {
function fileExistsInPath($file) {
$paths = explode(PATH_SEPARATOR, ini_get('include_path'));
foreach ($paths as $path) {
$fullPath = $path . DIRECTORY_SEPARATOR . $file;
$fullPath = $path . DS . $file;
if (file_exists($fullPath)) {
return $fullPath;
@ -859,7 +865,7 @@ if (!function_exists('file_put_contents')) {
* @return string with underscore remove from start and end of string
*/
function convertSlash($string) {
$string = trim($string,"/");
$string = trim($string, '/');
$string = preg_replace('/\/\//', '/', $string);
$string = str_replace('/', '_', $string);
return $string;

View file

@ -34,6 +34,15 @@ require_once CAKE.'basics.php';
* @subpackage cake.tests.cases
*/
class BasicsTest extends CakeTestCase {
/**
* setUp method
*
* @access public
* @return void
*/
function setUp() {
Configure::write('Locale.path', TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'locale');
}
/**
* testHttpBase method
*
@ -66,7 +75,7 @@ class BasicsTest extends CakeTestCase {
* @access public
* @return void
*/
function testUses() {
function testUses() {
$this->assertFalse(class_exists('Security'));
$this->assertFalse(class_exists('Sanitize'));
@ -81,7 +90,7 @@ class BasicsTest extends CakeTestCase {
* @access public
* @return void
*/
function testH() {
function testH() {
$string = '<foo>';
$result = h($string);
$this->assertEqual('&lt;foo&gt;', $result);
@ -97,7 +106,7 @@ class BasicsTest extends CakeTestCase {
* @access public
* @return void
*/
function testA() {
function testA() {
$result = a('this', 'that', 'bar');
$this->assertEqual(array('this', 'that', 'bar'), $result);
}
@ -107,7 +116,7 @@ class BasicsTest extends CakeTestCase {
* @access public
* @return void
*/
function testAa() {
function testAa() {
$result = aa('a', 'b', 'c', 'd');
$expected = array('a' => 'b', 'c' => 'd');
$this->assertEqual($expected, $result);
@ -122,7 +131,7 @@ class BasicsTest extends CakeTestCase {
* @access public
* @return void
*/
function testAm() {
function testAm() {
$result = am(array('one', 'two'), 2, 3, 4);
$expected = array('one', 'two', 2, 3, 4);
$this->assertEqual($result, $expected);
@ -137,7 +146,7 @@ class BasicsTest extends CakeTestCase {
* @access public
* @return void
*/
function testCache() {
function testCache() {
Configure::write('Cache.disable', true);
$result = cache('basics_test', 'simple cache write');
$this->assertNull($result);
@ -159,6 +168,268 @@ class BasicsTest extends CakeTestCase {
$result = cache('basics_test', null, '+1 second');
$this->assertNull($result);
}
/**
* test clearCache()
*
* @access public
* @return void
*/
function testClearCache() {
cache('views' . DS . 'basics_test.cache', 'simple cache write');
$this->assertTrue(file_exists(CACHE . 'views' . DS . 'basics_test.cache'));
cache('views' . DS . 'basics_test_2.cache', 'simple cache write 2');
$this->assertTrue(file_exists(CACHE . 'views' . DS . 'basics_test_2.cache'));
cache('views' . DS . 'basics_test_3.cache', 'simple cache write 3');
$this->assertTrue(file_exists(CACHE . 'views' . DS . 'basics_test_3.cache'));
$result = clearCache(array('basics_test', 'basics_test_2'), 'views', '.cache');
$this->assertTrue($result);
$this->assertFalse(file_exists(CACHE . 'views' . DS . 'basics_test.cache'));
$this->assertFalse(file_exists(CACHE . 'views' . DS . 'basics_test.cache'));
$this->assertTrue(file_exists(CACHE . 'views' . DS . 'basics_test_3.cache'));
$result = clearCache(null, 'views', '.cache');
$this->assertTrue($result);
$this->assertFalse(file_exists(CACHE . 'views' . DS . 'basics_test_3.cache'));
// Different path from views and with prefix
cache('models' . DS . 'basics_test.cache', 'simple cache write');
$this->assertTrue(file_exists(CACHE . 'models' . DS . 'basics_test.cache'));
cache('models' . DS . 'basics_test_2.cache', 'simple cache write 2');
$this->assertTrue(file_exists(CACHE . 'models' . DS . 'basics_test_2.cache'));
cache('models' . DS . 'basics_test_3.cache', 'simple cache write 3');
$this->assertTrue(file_exists(CACHE . 'models' . DS . 'basics_test_3.cache'));
$result = clearCache('basics', 'models', '.cache');
$this->assertTrue($result);
$this->assertFalse(file_exists(CACHE . 'models' . DS . 'basics_test.cache'));
$this->assertFalse(file_exists(CACHE . 'models' . DS . 'basics_test_2.cache'));
$this->assertFalse(file_exists(CACHE . 'models' . DS . 'basics_test_3.cache'));
}
/**
* test __()
*
* @access public
* @return void
*/
function test__() {
Configure::write('Config.language', 'rule_1_po');
$result = __('Plural Rule 1', true);
$expected = 'Plural Rule 1 (translated)';
$this->assertEqual($result, $expected);
$result = __('Plural Rule 1 (from core)', true);
$expected = 'Plural Rule 1 (from core translated)';
$this->assertEqual($result, $expected);
ob_start();
__('Plural Rule 1 (from core)');
$result = ob_get_clean();
$expected = 'Plural Rule 1 (from core translated)';
$this->assertEqual($result, $expected);
}
/**
* test __n()
*
* @access public
* @return void
*/
function test__n() {
Configure::write('Config.language', 'rule_1_po');
$result = __n('%d = 1', '%d = 0 or > 1', 0, true);
$expected = '%d = 0 or > 1 (translated)';
$this->assertEqual($result, $expected);
$result = __n('%d = 1', '%d = 0 or > 1', 1, true);
$expected = '%d = 1 (translated)';
$this->assertEqual($result, $expected);
$result = __n('%d = 1 (from core)', '%d = 0 or > 1 (from core)', 2, true);
$expected = '%d = 0 or > 1 (from core translated)';
$this->assertEqual($result, $expected);
ob_start();
__n('%d = 1 (from core)', '%d = 0 or > 1 (from core)', 2);
$result = ob_get_clean();
$expected = '%d = 0 or > 1 (from core translated)';
$this->assertEqual($result, $expected);
}
/**
* test __d()
*
* @access public
* @return void
*/
function test__d() {
Configure::write('Config.language', 'rule_1_po');
$result = __d('default', 'Plural Rule 1', true);
$expected = 'Plural Rule 1 (translated)';
$this->assertEqual($result, $expected);
$result = __d('core', 'Plural Rule 1', true);
$expected = 'Plural Rule 1';
$this->assertEqual($result, $expected);
$result = __d('core', 'Plural Rule 1 (from core)', true);
$expected = 'Plural Rule 1 (from core translated)';
$this->assertEqual($result, $expected);
ob_start();
__d('core', 'Plural Rule 1 (from core)');
$result = ob_get_clean();
$expected = 'Plural Rule 1 (from core translated)';
$this->assertEqual($result, $expected);
}
/**
* test __dn()
*
* @access public
* @return void
*/
function test__dn() {
Configure::write('Config.language', 'rule_1_po');
$result = __dn('default', '%d = 1', '%d = 0 or > 1', 0, true);
$expected = '%d = 0 or > 1 (translated)';
$this->assertEqual($result, $expected);
$result = __dn('core', '%d = 1', '%d = 0 or > 1', 0, true);
$expected = '%d = 0 or > 1';
$this->assertEqual($result, $expected);
$result = __dn('core', '%d = 1 (from core)', '%d = 0 or > 1 (from core)', 0, true);
$expected = '%d = 0 or > 1 (from core translated)';
$this->assertEqual($result, $expected);
$result = __dn('default', '%d = 1', '%d = 0 or > 1', 1, true);
$expected = '%d = 1 (translated)';
$this->assertEqual($result, $expected);
ob_start();
__dn('core', '%d = 1 (from core)', '%d = 0 or > 1 (from core)', 2);
$result = ob_get_clean();
$expected = '%d = 0 or > 1 (from core translated)';
$this->assertEqual($result, $expected);
}
/**
* test __c()
*
* @access public
* @return void
*/
function test__c() {
Configure::write('Config.language', 'rule_1_po');
$result = __c('Plural Rule 1', 5, true);
$expected = 'Plural Rule 1 (translated)';
$this->assertEqual($result, $expected);
$result = __c('Plural Rule 1 (from core)', 5, true);
$expected = 'Plural Rule 1 (from core translated)';
$this->assertEqual($result, $expected);
ob_start();
__c('Plural Rule 1 (from core)', 5);
$result = ob_get_clean();
$expected = 'Plural Rule 1 (from core translated)';
$this->assertEqual($result, $expected);
}
/**
* test __dc()
*
* @access public
* @return void
*/
function test__dc() {
Configure::write('Config.language', 'rule_1_po');
$result = __dc('default', 'Plural Rule 1', 5, true);
$expected = 'Plural Rule 1 (translated)';
$this->assertEqual($result, $expected);
$result = __dc('default', 'Plural Rule 1 (from core)', 5, true);
$expected = 'Plural Rule 1 (from core translated)';
$this->assertEqual($result, $expected);
$result = __dc('core', 'Plural Rule 1', 5, true);
$expected = 'Plural Rule 1';
$this->assertEqual($result, $expected);
$result = __dc('core', 'Plural Rule 1 (from core)', 5, true);
$expected = 'Plural Rule 1 (from core translated)';
$this->assertEqual($result, $expected);
ob_start();
__dc('default', 'Plural Rule 1 (from core)', 5);
$result = ob_get_clean();
$expected = 'Plural Rule 1 (from core translated)';
$this->assertEqual($result, $expected);
}
/**
* test __dcn()
*
* @access public
* @return void
*/
function test__dcn() {
Configure::write('Config.language', 'rule_1_po');
$result = __dcn('default', '%d = 1', '%d = 0 or > 1', 0, 5, true);
$expected = '%d = 0 or > 1 (translated)';
$this->assertEqual($result, $expected);
$result = __dcn('default', '%d = 1 (from core)', '%d = 0 or > 1 (from core)', 1, 5, true);
$expected = '%d = 1 (from core translated)';
$this->assertEqual($result, $expected);
$result = __dcn('core', '%d = 1', '%d = 0 or > 1', 0, 5, true);
$expected = '%d = 0 or > 1';
$this->assertEqual($result, $expected);
ob_start();
__dcn('default', '%d = 1 (from core)', '%d = 0 or > 1 (from core)', 1, 5);
$result = ob_get_clean();
$expected = '%d = 1 (from core translated)';
$this->assertEqual($result, $expected);
}
/**
* test LogError()
*
* @access public
* @return void
*/
function testLogError() {
@unlink(LOGS . 'error.log');
LogError('Testing LogError() basic function');
LogError("Testing with\nmulti-line\nstring");
$result = file_get_contents(LOGS . 'error.log');
$this->assertPattern('/Error: Testing LogError\(\) basic function/', $result);
$this->assertNoPattern("/Error: Testing with\nmulti-line\nstring/", $result);
$this->assertPattern('/Error: Testing with multi-line string/', $result);
}
/**
* test convertSlash()
*
* @access public
* @return void
*/
function testConvertSlash() {
$result = convertSlash('\path\to\location\\');
$expected = '\path\to\location\\';
$this->assertEqual($result, $expected);
$result = convertSlash('/path/to/location/');
$expected = 'path_to_location';
$this->assertEqual($result, $expected);
}
}
?>