Skip to content

Commit

Permalink
Add tests for cacheMethodFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
tersmitten committed Nov 14, 2016
1 parent 71535d2 commit 1952d2e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
31 changes: 31 additions & 0 deletions lib/Cake/Model/Datasource/DboSource.php
Expand Up @@ -797,6 +797,37 @@ public function cacheMethod($method, $key, $value = null) {
* Filters to apply to the results of `name` and `fields`. When the filter for a given method does not return `true`
* then the result is not added to the memory cache.
*
* Some examples:
*
* ```
* // For method fields, do not cache values that contain floats
* if ($method === 'fields') {
* $hasFloat = preg_grep('/(\d+)?\.\d+/', $value);
*
* return count($hasFloat) === 0;
* }
*
* return true;
* ```
*
* ```
* // For method name, do not cache values that have the name created
* if ($method === 'name') {
* return preg_match('/^`created`$/', $value) !== 1;
* }
*
* return true;
* ```
*
* ```
* // For method name, do not cache values that have the key 472551d38e1f8bbc78d7dfd28106166f
* if ($key === '472551d38e1f8bbc78d7dfd28106166f') {
* return false;
* }
*
* return true;
* ```
*
* @param string $method Name of the method being cached.
* @param string $key The key name for the cache operation.
* @param mixed $value The value to cache into memory.
Expand Down
28 changes: 28 additions & 0 deletions lib/Cake/Test/Case/Model/Datasource/DboSourceTest.php
Expand Up @@ -737,6 +737,34 @@ public function testCacheMethod() {
$this->assertNull($result);
}

/**
* Test that cacheMethodFilter does not filter by default.
*
* @return void
*/
public function testCacheMethodFilter() {
$method = 'name';
$key = '49d9207adfce6df1dd3ee8c30c434414';
$value = '`menus`';
$actual = $this->testDb->cacheMethodFilter($method, $key, $value);

$this->assertTrue($actual);

$method = 'fields';
$key = '2b57253ab1fffb3e95fa4f95299220b1';
$value = ["`Menu`.`id`", "`Menu`.`name`"];
$actual = $this->testDb->cacheMethodFilter($method, $key, $value);

$this->assertTrue($actual);

$method = 'non-existing';
$key = '';
$value = '``';
$actual = $this->testDb->cacheMethodFilter($method, $key, $value);

$this->assertTrue($actual);
}

/**
* Test that rare collisions do not happen with method caching
*
Expand Down

0 comments on commit 1952d2e

Please sign in to comment.