Add tests for cacheMethodFilter

This commit is contained in:
Mischa ter Smitten 2016-11-14 11:44:35 +01:00
parent 71535d2d2c
commit 1952d2ee17
2 changed files with 59 additions and 0 deletions

View file

@ -797,6 +797,37 @@ class DboSource extends DataSource {
* Filters to apply to the results of `name` and `fields`. When the filter for a given method does not return `true` * 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. * 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 $method Name of the method being cached.
* @param string $key The key name for the cache operation. * @param string $key The key name for the cache operation.
* @param mixed $value The value to cache into memory. * @param mixed $value The value to cache into memory.

View file

@ -737,6 +737,34 @@ class DboSourceTest extends CakeTestCase {
$this->assertNull($result); $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 * Test that rare collisions do not happen with method caching
* *