/libraries/lithium/tests/cases/storage/cache/adapter/FileTest.php

https://bitbucket.org/thesyncim/admin-biarq · PHP · 193 lines · 134 code · 41 blank · 18 comment · 3 complexity · 7dc00bb6e8eecf7eb36b8b88fdac9d47 MD5 · raw file

  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2011, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\tests\cases\storage\cache\adapter;
  9. use SplFileInfo;
  10. use lithium\core\Libraries;
  11. use lithium\storage\cache\adapter\File;
  12. class FileTest extends \lithium\test\Unit {
  13. /**
  14. * Checks whether the 'empty' file exists in `app/resources/tmp/cache` and, if so, ensures
  15. * that it is restored at the end of the testing cycle.
  16. *
  17. * @var string
  18. */
  19. protected $_hasEmpty = true;
  20. /**
  21. * Skip the test if the default File adapter read/write path
  22. * is not read/write-able.
  23. *
  24. * @return void
  25. */
  26. public function skip() {
  27. $directory = new SplFileInfo(Libraries::get(true, 'resources') . "/tmp/cache/");
  28. $accessible = ($directory->isDir() && $directory->isReadable() && $directory->isWritable());
  29. $message = 'The File cache adapter path does not have the proper permissions.';
  30. $this->skipIf(!$accessible, $message);
  31. }
  32. public function setUp() {
  33. $this->_hasEmpty = file_exists(Libraries::get(true, 'resources') . "/tmp/cache/empty");
  34. $this->File = new File();
  35. }
  36. public function tearDown() {
  37. if ($this->_hasEmpty) {
  38. touch(Libraries::get(true, 'resources') . "/tmp/cache/empty");
  39. touch(Libraries::get(true, 'resources') . "/tmp/cache/templates/empty");
  40. }
  41. unset($this->File);
  42. }
  43. public function testEnabled() {
  44. $file = $this->File;
  45. $this->assertTrue($file::enabled());
  46. }
  47. public function testWrite() {
  48. $key = 'key';
  49. $data = 'data';
  50. $expiry = '+1 minute';
  51. $time = time() + 60;
  52. $closure = $this->File->write($key, $data, $expiry);
  53. $this->assertTrue(is_callable($closure));
  54. $params = compact('key', 'data', 'expiry');
  55. $result = $closure($this->File, $params, null);
  56. $expected = 25;
  57. $this->assertEqual($expected, $result);
  58. $this->assertTrue(file_exists(Libraries::get(true, 'resources') . "/tmp/cache/{$key}"));
  59. $this->assertEqual(
  60. file_get_contents(Libraries::get(true, 'resources') . "/tmp/cache/{$key}"),
  61. "{:expiry:$time}\ndata"
  62. );
  63. $this->assertTrue(unlink(Libraries::get(true, 'resources') . "/tmp/cache/{$key}"));
  64. $this->assertFalse(file_exists(Libraries::get(true, 'resources') . "/tmp/cache/{$key}"));
  65. }
  66. public function testWriteDefaultCacheExpiry() {
  67. $File = new File(array('expiry' => '+1 minute'));
  68. $key = 'default_keykey';
  69. $data = 'data';
  70. $time = time() + 60;
  71. $closure = $File->write($key, $data);
  72. $this->assertTrue(is_callable($closure));
  73. $params = compact('key', 'data');
  74. $result = $closure($File, $params, null);
  75. $expected = 25;
  76. $this->assertEqual($expected, $result);
  77. $this->assertTrue(file_exists(Libraries::get(true, 'resources') . "/tmp/cache/{$key}"));
  78. $this->assertEqual(
  79. file_get_contents(Libraries::get(true, 'resources') . "/tmp/cache/{$key}"),
  80. "{:expiry:{$time}}\ndata"
  81. );
  82. $this->assertTrue(unlink(Libraries::get(true, 'resources') . "/tmp/cache/{$key}"));
  83. $this->assertFalse(file_exists(Libraries::get(true, 'resources') . "/tmp/cache/{$key}"));
  84. }
  85. public function testRead() {
  86. $key = 'key';
  87. $time = time() + 60;
  88. $closure = $this->File->read($key);
  89. $this->assertTrue(is_callable($closure));
  90. $path = Libraries::get(true, 'resources') . "/tmp/cache/{$key}";
  91. file_put_contents($path, "{:expiry:$time}\ndata");
  92. $this->assertTrue(file_exists($path));
  93. $params = compact('key');
  94. $result = $closure($this->File, $params, null);
  95. $this->assertEqual('data', $result);
  96. unlink($path);
  97. $key = 'non_existent';
  98. $params = compact('key');
  99. $closure = $this->File->read($key);
  100. $this->assertTrue(is_callable($closure));
  101. $result = $closure($this->File, $params, null);
  102. $this->assertFalse($result);
  103. }
  104. public function testExpiredRead() {
  105. $key = 'expired_key';
  106. $time = time() + 1;
  107. $closure = $this->File->read($key);
  108. $this->assertTrue(is_callable($closure));
  109. $path = Libraries::get(true, 'resources') . "/tmp/cache/{$key}";
  110. file_put_contents($path, "{:expiry:$time}\ndata");
  111. $this->assertTrue(file_exists($path));
  112. sleep(2);
  113. $params = compact('key');
  114. $this->assertFalse($closure($this->File, $params, null));
  115. }
  116. public function testDelete() {
  117. $key = 'key_to_delete';
  118. $time = time() + 1;
  119. $path = Libraries::get(true, 'resources') . "/tmp/cache/{$key}";
  120. file_put_contents($path, "{:expiry:$time}\ndata");
  121. $this->assertTrue(file_exists($path));
  122. $closure = $this->File->delete($key);
  123. $this->assertTrue(is_callable($closure));
  124. $params = compact('key');
  125. $this->assertTrue($closure($this->File, $params, null));
  126. $key = 'non_existent';
  127. $params = compact('key');
  128. $this->assertFalse($closure($this->File, $params, null));
  129. }
  130. public function testClear() {
  131. $key = 'key_to_clear';
  132. $time = time() + 1;
  133. $path = Libraries::get(true, 'resources') . "/tmp/cache/{$key}";
  134. file_put_contents($path, "{:expiry:$time}\ndata");
  135. $result = $this->File->clear();
  136. $this->assertTrue($result);
  137. $this->assertFalse(file_exists($path));
  138. $result = touch(Libraries::get(true, 'resources') . "/tmp/cache/empty");
  139. $this->assertTrue($result);
  140. }
  141. public function testIncrement() {
  142. $key = 'key_to_increment';
  143. $result = $this->File->increment($key);
  144. $this->assertEqual(false, $result);
  145. }
  146. public function testDecrement() {
  147. $key = 'key_to_decrement';
  148. $result = $this->File->decrement($key);
  149. $this->assertEqual(false, $result);
  150. }
  151. }
  152. ?>