Skip to content

Commit

Permalink
Making Cache::write() trigger warnings when a cache engine returns fa…
Browse files Browse the repository at this point in the history
…lse from a write. Tests added. Fixes #877
  • Loading branch information
markstory committed Jul 24, 2010
1 parent 88ea68a commit 43127ca
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
6 changes: 6 additions & 0 deletions cake/libs/cache.php
Expand Up @@ -270,6 +270,12 @@ public static function write($key, $value, $config = null) {

$success = self::$_engines[$config]->write($settings['prefix'] . $key, $value, $settings['duration']);
self::set();
if ($success === false) {
trigger_error(
sprintf(__("%s cache was unable to write '%s' to cache", true), $config, $key),
E_USER_WARNING
);
}
return $success;
}

Expand Down
22 changes: 22 additions & 0 deletions cake/tests/cases/libs/cache.test.php
Expand Up @@ -309,6 +309,28 @@ function testWriteEmptyValues() {
$this->assertIdentical(Cache::read('App.zeroTest2'), '0');
}

/**
* Test that failed writes cause errors to be triggered.
*
* @return void
*/
function testWriteTriggerError() {
App::build(array(
'libs' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'libs' . DS),
'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
), true);

Cache::config('test_trigger', array('engine' => 'TestAppCache'));
try {
Cache::write('fail', 'value', 'test_trigger');
$this->fail('No exception thrown');
} catch (PHPUnit_Framework_Error $e) {
$this->assertTrue(true);
}
Cache::drop('test_trigger');
App::build();
}

/**
* testCacheDisable method
*
Expand Down
6 changes: 5 additions & 1 deletion cake/tests/test_app/libs/cache/test_app_cache.php
Expand Up @@ -19,7 +19,11 @@
*/
class TestAppCacheEngine extends CacheEngine {

public function write($key, $value, $duration) { }
public function write($key, $value, $duration) {
if ($key = 'fail') {

This comment has been minimized.

Copy link
@hiromi2424

hiromi2424 Mar 14, 2011

missing '=' ?

This comment has been minimized.

Copy link
@markstory

markstory Mar 15, 2011

Author Member

You are totally right, fixed in [6fd5ff3]

return false;
}
}

public function read($key) { }

Expand Down

0 comments on commit 43127ca

Please sign in to comment.