Skip to content

Commit

Permalink
Adding Inflector::reset(). This makes non-isolated test runs possible…
Browse files Browse the repository at this point in the history
…. Adding inflector to the libs suite.
  • Loading branch information
markstory committed Sep 26, 2010
1 parent 63d23df commit 3395f42
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
28 changes: 28 additions & 0 deletions cake/libs/inflector.php
Expand Up @@ -234,6 +234,13 @@ class Inflector {
*/
protected static $_cache = array();

/**
* The initial state of Inflector so reset() works.
*
* @var array
*/
protected static $_initialState = array();

/**
* Cache inflected values, and return if already available
*
Expand All @@ -255,6 +262,24 @@ protected static function _cache($type, $key, $value = false) {
return self::$_cache[$type][$key];
}

/**
* Clears Inflectors inflected value caches. And resets the inflection
* rules to the initial values.
*
* @return void
*/
public static function reset() {
if (empty(self::$_initialState)) {
self::$_initialState = get_class_vars('Inflector');
return;
}
foreach (self::$_initialState as $key => $val) {
if ($key != '_initialState') {
self::${$key} = $val;
}
}
}

/**
* Adds custom inflection $rules, of either 'plural', 'singular' or 'transliteration' $type.
*
Expand Down Expand Up @@ -540,3 +565,6 @@ public static function slug($string, $replacement = '_', $map = array()) {
return preg_replace(array_keys($map), array_values($map), $string);
}
}

// Store the initial state
Inflector::reset();
1 change: 1 addition & 0 deletions cake/tests/cases/libs/all_libs.test.php
Expand Up @@ -42,6 +42,7 @@ public static function suite() {
$suite->addTestFile(CORE_TEST_CASES . DS . 'libs' . DS . 'error_handler.test.php');
$suite->addTestFile(CORE_TEST_CASES . DS . 'libs' . DS . 'file.test.php');
$suite->addTestFile(CORE_TEST_CASES . DS . 'libs' . DS . 'folder.test.php');
$suite->addTestFile(CORE_TEST_CASES . DS . 'libs' . DS . 'inflector.test.php');
$suite->addTestFile(CORE_TEST_CASES . DS . 'libs' . DS . 'log' . DS . 'file_log.test.php');
$suite->addTestFile(CORE_TEST_CASES . DS . 'libs' . DS . 'cake_log.test.php');
$suite->addTestFile(CORE_TEST_CASES . DS . 'libs' . DS . 'class_registry.test.php');
Expand Down
10 changes: 6 additions & 4 deletions cake/tests/cases/libs/inflector.test.php
Expand Up @@ -35,12 +35,14 @@
class InflectorTest extends CakeTestCase {

/**
* Inflector property
* teardown
*
* @var mixed null
* @access public
* @return void
*/
public $Inflector = null;
function tearDown() {
parent::tearDown();
Inflector::reset();
}

/**
* testInflectingSingulars method
Expand Down

0 comments on commit 3395f42

Please sign in to comment.