Completed
Push — master ( ef5eaa...6d70d2 )
by Lars
03:21 queued 30s
created

CacheTest53::testCustomCacheCallback()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 57
Code Lines 41

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 57
rs 9.031
cc 4
eloc 41
nc 2
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
use idiorm\orm\ORM;
4
5
class CacheTest53 extends PHPUnit_Framework_TestCase {
6
7
    const ALTERNATE = 'alternate'; // Used as name of alternate connection
8
9
    public function setUp() {
10
        // Set up the dummy database connections
11
        ORM::set_db(new MockPDO('sqlite::memory:'));
12
        ORM::set_db(new MockDifferentPDO('sqlite::memory:'), self::ALTERNATE);
13
14
        // Enable logging
15
        ORM::configure('logging', true);
16
        ORM::configure('logging', true, self::ALTERNATE);
17
        ORM::configure('caching', true);
18
        ORM::configure('caching', true, self::ALTERNATE);
19
    }
20
21
    public function tearDown() {
22
        ORM::reset_config();
23
        ORM::reset_db();
24
    }
25
26
     
27
    public function testCustomCacheCallback() {
28
        $phpunit = $this;
29
        $my_cache = array();
30
        ORM::configure('caching_auto_clear', true);
31
 
32
        ORM::configure('create_cache_key', function ($query, $parameters, $table_name, $connection) use ($phpunit, &$my_cache) {
33
            $phpunit->assertEquals(true, is_string($query));
34
            $phpunit->assertEquals(true, is_array($parameters));
35
            $phpunit->assertEquals(true, is_string($connection));
36
            $phpunit->assertEquals('widget', $table_name);
37
            $parameter_string = join(',', $parameters);
38
            $key = $query . ':' . $parameter_string;
39
            $my_key = 'some-prefix'.crc32($key);
40
            return $my_key;
41
        });
42
        ORM::configure('cache_query_result', function ($cache_key, $value, $table_name, $connection_name) use ($phpunit, &$my_cache) {
43
            $phpunit->assertEquals(true, is_string($cache_key));
44
            $phpunit->assertEquals('widget', $table_name);
45
            $my_cache[$cache_key] = $value;
46
        });
47
        ORM::configure('check_query_cache', function ($cache_key, $table_name, $connection_name) use ($phpunit, &$my_cache) {
48
            $phpunit->assertEquals(true, is_string($cache_key));
49
            $phpunit->assertEquals(true, is_string($connection_name));
50
            $phpunit->assertEquals('widget', $table_name);
51
52
            if(isset($my_cache) and isset($my_cache[$cache_key])){
53
               $phpunit->assertEquals(true, is_array($my_cache[$cache_key]));
54
               return $my_cache[$cache_key];
55
            } else {
56
               return false;
57
            }
58
        });
59
        ORM::configure('clear_cache', function ($table_name, $connection_name) use ($phpunit, &$my_cache) {
60
             $phpunit->assertEquals(true, is_string($table_name)); 
61
             $phpunit->assertEquals(true, is_string($connection_name));
62
             $my_cache = array();
63
        });
64
        ORM::for_table('widget')->where('name', 'Fred')->where('age', 21)->find_one();
65
        ORM::for_table('widget')->where('name', 'Fred')->where('age', 21)->find_one();
66
        ORM::for_table('widget')->where('name', 'Bob')->where('age', 42)->find_one();
67
 
68
        //our custom cache should be full now 
69
        $this->assertEquals(true, !empty($my_cache));
70
 
71
        //checking custom cache key 
72
        foreach($my_cache as $k=>$v){
73
        $this->assertEquals('some-prefix', substr($k,0,11));
74
        }
75
        
76
        $new = ORM::for_table('widget')->create();
77
        $new->name = "Joe";
78
        $new->age = 25;
79
        $saved = $new->save();
80
        
81
        //our custom cache should be empty now 
82
        $this->assertEquals(true, empty($my_cache));
83
    }
84
}