a tiny mvc framework for php using php-activerecord
1<?php
2include 'helpers/config.php';
3
4use ActiveRecord\Cache;
5
6class CacheTest extends SnakeCase_PHPUnit_Framework_TestCase
7{
8 public function set_up()
9 {
10 if (!extension_loaded('memcache'))
11 {
12 $this->markTestSkipped('The memcache extension is not available');
13 return;
14 }
15
16 Cache::initialize('memcache://localhost');
17 }
18
19 public function tear_down()
20 {
21 Cache::flush();
22 }
23
24 private function cache_get()
25 {
26 return Cache::get("1337", function() { return "abcd"; });
27 }
28
29 public function test_initialize()
30 {
31 $this->assert_not_null(Cache::$adapter);
32 }
33
34 public function test_initialize_with_null()
35 {
36 Cache::initialize(null);
37 $this->assert_null(Cache::$adapter);
38 }
39
40 public function test_get_returns_the_value()
41 {
42 $this->assert_equals("abcd", $this->cache_get());
43 }
44
45 public function test_get_writes_to_the_cache()
46 {
47 $this->cache_get();
48 $this->assert_equals("abcd", Cache::$adapter->read("1337"));
49 }
50
51 public function test_get_does_not_execute_closure_on_cache_hit()
52 {
53 $this->cache_get();
54 Cache::get("1337", function() { throw new Exception("I better not execute!"); });
55 }
56
57 public function test_cache_adapter_returns_false_on_cache_miss()
58 {
59 $this->assert_same(false, Cache::$adapter->read("some-key"));
60 }
61
62 public function test_get_works_without_caching_enabled()
63 {
64 Cache::$adapter = null;
65 $this->assert_equals("abcd", $this->cache_get());
66 }
67
68 public function test_cache_expire()
69 {
70 Cache::$options['expire'] = 1;
71 $this->cache_get();
72 sleep(2);
73
74 $this->assert_same(false, Cache::$adapter->read("1337"));
75 }
76
77 public function test_namespace_is_set_properly()
78 {
79 Cache::$options['namespace'] = 'myapp';
80 $this->cache_get();
81 $this->assert_same("abcd", Cache::$adapter->read("myapp::1337"));
82 }
83}
84?>