CacheTest.php 1.7 KB

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