You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.4 KiB

7 years ago
  1. <?php
  2. class CacheTest extends PHPUnit_Framework_TestCase {
  3. public function testCreateFromConfig() {
  4. p3k\Cache::redis('tcp://127.0.0.1:6379');
  5. p3k\Cache::set('foo', 'bar');
  6. $this->assertEquals('bar', p3k\Cache::get('foo'));
  7. p3k\Cache::reset();
  8. }
  9. public function testAutoCreate() {
  10. p3k\Cache::set('foo', 'bar');
  11. $this->assertEquals('bar', p3k\Cache::get('foo'));
  12. p3k\Cache::reset();
  13. }
  14. public function testSet() {
  15. p3k\Cache::set('foo', 'bar', 0);
  16. $this->assertEquals('bar', p3k\Cache::get('foo'));
  17. $redis = p3k\Cache::redis();
  18. $this->assertEquals(-1, $redis->ttl('foo'));
  19. }
  20. public function testSetEx() {
  21. p3k\Cache::set('foo', 'bar', 600);
  22. $this->assertEquals('bar', p3k\Cache::get('foo'));
  23. $redis = p3k\Cache::redis();
  24. $this->assertGreaterThan(500, $redis->ttl('foo'));
  25. }
  26. public function testGetExpired() {
  27. p3k\Cache::set('foo', 'bar', 1);
  28. sleep(1);
  29. $this->assertEquals('default', p3k\Cache::get('foo', 'default'));
  30. }
  31. public function testDelete() {
  32. p3k\Cache::set('foo', 'bar', 600);
  33. $this->assertEquals('bar', p3k\Cache::get('foo'));
  34. p3k\Cache::delete('foo');
  35. $this->assertEquals('default', p3k\Cache::get('foo', 'default'));
  36. }
  37. public function testExpire() {
  38. p3k\Cache::set('foo', 'bar', 600);
  39. $this->assertEquals('bar', p3k\Cache::get('foo'));
  40. p3k\Cache::expire('foo');
  41. $this->assertEquals('default', p3k\Cache::get('foo', 'default'));
  42. p3k\Cache::set('foo', 'bar', 600);
  43. $this->assertEquals('bar', p3k\Cache::get('foo'));
  44. p3k\Cache::expire('foo', 1);
  45. sleep(1);
  46. $this->assertEquals('default', p3k\Cache::get('foo', 'default'));
  47. }
  48. public function testIncr() {
  49. p3k\Cache::delete('test1');
  50. p3k\Cache::incr('test1');
  51. $this->assertEquals(1, p3k\Cache::get('test1'));
  52. p3k\Cache::set('test2', 10);
  53. p3k\Cache::incr('test2');
  54. $this->assertEquals(11, p3k\Cache::get('test2'));
  55. p3k\Cache::set('test3', 10);
  56. p3k\Cache::incr('test3', 4);
  57. $this->assertEquals(14, p3k\Cache::get('test3'));
  58. }
  59. public function testDecr() {
  60. p3k\Cache::delete('test4');
  61. p3k\Cache::decr('test4');
  62. $this->assertEquals(-1, p3k\Cache::get('test4'));
  63. p3k\Cache::set('test5', 10);
  64. p3k\Cache::decr('test5');
  65. $this->assertEquals(9, p3k\Cache::get('test5'));
  66. p3k\Cache::set('test6', 10);
  67. p3k\Cache::decr('test6', 4);
  68. $this->assertEquals(6, p3k\Cache::get('test6'));
  69. }
  70. }