diff options
author | Peter Ivanov <[email protected]> | 2014-06-20 15:19:47 +0300 |
---|---|---|
committer | Simon Holywell <[email protected]> | 2014-06-22 01:03:11 +0100 |
commit | e7b77adc4378c9a7581d7793a7e6717cbadc10f8 (patch) | |
tree | 91561424fb6b0a10a45b70c323df01d4cc4c3beb /test | |
parent | 00c6f6cce5f31c239c217a3a21e4ba96e9b1fc91 (diff) |
Added custom caching functions
This is a combination of 20 commits:
added cache callback #212
added test
Added text for custom cache
formating and tests
added caching_auto_clear option
moved custom cache test for php 5.3+
fixed ConfigTest.php
tabs to spaces
formating
formating
added `create_cache_key` callback option
added $table_name to clear cache function
added $table_name to _create_cache_key
added missing params
added $table_name to cache_query_result
formating
tabs
added $table_name to check_query_cache
unify cache parameters order
`table_name` is more important than `connection_name`
formating
Diffstat (limited to 'test')
-rw-r--r-- | test/CacheTest.php | 1 | ||||
-rw-r--r-- | test/CacheTest53.php | 82 | ||||
-rw-r--r-- | test/ConfigTest.php | 1 |
3 files changed, 84 insertions, 0 deletions
diff --git a/test/CacheTest.php b/test/CacheTest.php index 0f7b491..a3daa91 100644 --- a/test/CacheTest.php +++ b/test/CacheTest.php @@ -38,4 +38,5 @@ class CacheTest extends PHPUnit_Framework_TestCase { ORM::for_table('widget', self::ALTERNATE)->where('name', 'Steve')->where('age', 80)->find_one(); // this shouldn't run a query! $this->assertEquals($expected, ORM::get_last_query(self::ALTERNATE)); } + }
\ No newline at end of file diff --git a/test/CacheTest53.php b/test/CacheTest53.php new file mode 100644 index 0000000..bc30b0a --- /dev/null +++ b/test/CacheTest53.php @@ -0,0 +1,82 @@ +<?php + +class CacheTest53 extends PHPUnit_Framework_TestCase { + + const ALTERNATE = 'alternate'; // Used as name of alternate connection + + public function setUp() { + // Set up the dummy database connections + ORM::set_db(new MockPDO('sqlite::memory:')); + ORM::set_db(new MockDifferentPDO('sqlite::memory:'), self::ALTERNATE); + + // Enable logging + ORM::configure('logging', true); + ORM::configure('logging', true, self::ALTERNATE); + ORM::configure('caching', true); + ORM::configure('caching', true, self::ALTERNATE); + } + + public function tearDown() { + ORM::reset_config(); + ORM::reset_db(); + } + + + public function testCustomCacheCallback() { + $phpunit = $this; + $my_cache = array(); + ORM::configure('caching_auto_clear', true); + + ORM::configure('create_cache_key', function ($query, $parameters, $table_name, $connection) use ($phpunit, &$my_cache) { + $phpunit->assertEquals(true, is_string($query)); + $phpunit->assertEquals(true, is_array($parameters)); + $phpunit->assertEquals(true, is_string($connection)); + $phpunit->assertEquals('widget', $table_name); + $parameter_string = join(',', $parameters); + $key = $query . ':' . $parameter_string; + $my_key = 'some-prefix'.crc32($key); + return $my_key; + }); + ORM::configure('cache_query_result', function ($cache_key, $value, $table_name, $connection_name) use ($phpunit, &$my_cache) { + $phpunit->assertEquals(true, is_string($cache_key)); + $phpunit->assertEquals('widget', $table_name); + $my_cache[$cache_key] = $value; + }); + ORM::configure('check_query_cache', function ($cache_key, $table_name, $connection_name) use ($phpunit, &$my_cache) { + $phpunit->assertEquals(true, is_string($cache_key)); + $phpunit->assertEquals(true, is_string($connection_name)); + $phpunit->assertEquals('widget', $table_name); + + if(isset($my_cache) and isset($my_cache[$cache_key])){ + $phpunit->assertEquals(true, is_array($my_cache[$cache_key])); + return $my_cache[$cache_key]; + } else { + return false; + } + }); + ORM::configure('clear_cache', function ($table_name, $connection_name) use ($phpunit, &$my_cache) { + $phpunit->assertEquals(true, is_string($table_name)); + $phpunit->assertEquals(true, is_string($connection_name)); + $my_cache = array(); + }); + ORM::for_table('widget')->where('name', 'Fred')->where('age', 21)->find_one(); + ORM::for_table('widget')->where('name', 'Fred')->where('age', 21)->find_one(); + ORM::for_table('widget')->where('name', 'Bob')->where('age', 42)->find_one(); + + //our custom cache should be full now + $this->assertEquals(true, !empty($my_cache)); + + //checking custom cache key + foreach($my_cache as $k=>$v){ + $this->assertEquals('some-prefix', substr($k,0,11)); + } + + $new = ORM::for_table('widget')->create(); + $new->name = "Joe"; + $new->age = 25; + $saved = $new->save(); + + //our custom cache should be empty now + $this->assertEquals(true, empty($my_cache)); + } +}
\ No newline at end of file diff --git a/test/ConfigTest.php b/test/ConfigTest.php index c65c539..c6f4631 100644 --- a/test/ConfigTest.php +++ b/test/ConfigTest.php @@ -115,6 +115,7 @@ class ConfigTest extends PHPUnit_Framework_TestCase { 'logging' => true, 'logger' => null, 'caching' => false, + 'caching_auto_clear' => false, 'return_result_sets' => false, 'limit_clause_style' => 'limit', ); |