| Conditions | 4 |
| Paths | 2 |
| Total Lines | 57 |
| Code Lines | 41 |
| Lines | 0 |
| Ratio | 0 % |
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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 | } |