FileCacheTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. /*
  3. * This file is part of the php-phantomjs.
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. */
  8. namespace JonnyW\PhantomJs\Tests\Unit\Cache;
  9. use JonnyW\PhantomJs\Cache\FileCache;
  10. /**
  11. * PHP PhantomJs
  12. *
  13. * @author Jon Wenmoth <contact@jonnyw.me>
  14. */
  15. class FileCacheTest extends \PHPUnit_Framework_TestCase
  16. {
  17. /**
  18. * Test filename
  19. *
  20. * @var string
  21. * @access protected
  22. */
  23. protected $filename;
  24. /**
  25. * Test directory
  26. *
  27. * @var string
  28. * @access protected
  29. */
  30. protected $directory;
  31. /** +++++++++++++++++++++++++++++++++++ **/
  32. /** ++++++++++++++ TESTS ++++++++++++++ **/
  33. /** +++++++++++++++++++++++++++++++++++ **/
  34. /**
  35. * Test false is returned if file
  36. * does not exist.
  37. *
  38. * @access public
  39. * @return void
  40. */
  41. public function testFalseIsReturnedIfFileDoesNotExist()
  42. {
  43. $fileCache = $this->getFileCache($this->directory, 'txt');
  44. $this->assertFalse($fileCache->exists($this->filename, 'Test'));
  45. }
  46. /**
  47. * Test true is returned if file does exist.
  48. *
  49. * @access public
  50. * @return void
  51. */
  52. public function testTrueIsReturnedIfFileDoesExist()
  53. {
  54. touch($this->getFilename());
  55. $fileCache = $this->getFileCache($this->directory, 'txt');
  56. $this->assertTrue($fileCache->exists($this->filename));
  57. }
  58. /**
  59. * Test not writable exception is thrown if file
  60. * cannot be saved due to write permissions.
  61. *
  62. * @return void
  63. */
  64. public function testNotWritableExceptionIsThrownIfFileCannotBeSavedDueToWritePermissions()
  65. {
  66. $this->setExpectedException('\JonnyW\PhantomJs\Exception\NotWritableException');
  67. $fileCache = $this->getFileCache('/This/Directory/Is/Not/Writable/', 'txt');
  68. $fileCache->save($this->filename, 'Test');
  69. }
  70. /**
  71. * Test test file location is returned
  72. * if file is successfully saved.
  73. *
  74. * @access public
  75. * @return void
  76. */
  77. public function testFileLocationIsReturnedIfFileIsSuccessfullySaved()
  78. {
  79. $fileCache = $this->getFileCache($this->directory, 'txt');
  80. $file = $fileCache->save($this->filename, 'Test');
  81. $this->assertInternalType('string', $file);
  82. $this->assertFileExists($file);
  83. }
  84. /**
  85. * Test file can be saved
  86. * with directory path
  87. *
  88. * @access public
  89. * @return void
  90. */
  91. public function testFileCanBeSavedWithDirectoryPath()
  92. {
  93. $fileCache = $this->getFileCache('', 'txt');
  94. $file = $fileCache->save($this->directory, 'Test');
  95. $this->assertSame(dirname($file), $this->directory);
  96. unlink($file);
  97. }
  98. /**
  99. * Test not writable exception is thrown
  100. * if directory path is not writable.
  101. *
  102. * @access public
  103. * @return void
  104. */
  105. public function testNotWritableExceptionIsThrownIfDirectoryPathIsNotWritable()
  106. {
  107. $this->setExpectedException('\JonnyW\PhantomJs\Exception\NotWritableException');
  108. $fileCache = $this->getFileCache($this->directory, 'txt');
  109. $file = $fileCache->save('/This/Directory/Is/Not/Writable/', 'Test');
  110. }
  111. /**
  112. * Test file can be saved with absolute
  113. * path.
  114. *
  115. * @access public
  116. * @return void
  117. */
  118. public function testFileCanBeSavedWithAbsolutePath()
  119. {
  120. $test = sprintf('%1$s/%2$s', $this->directory, 'new-file.txt');
  121. $fileCache = $this->getFileCache('', 'txt');
  122. $file = $fileCache->save($test, 'Test');
  123. $this->assertSame($test, $file);
  124. unlink($file);
  125. }
  126. /**
  127. * Test not exists exception is thrown when
  128. * fetching data that doesn't exsit
  129. *
  130. * @access public
  131. * @return void
  132. */
  133. public function testNotExistsExceptionIsThrownIfWhenFetchingDataThatDoesntExist()
  134. {
  135. $this->setExpectedException('\JonnyW\PhantomJs\Exception\NotExistsException');
  136. $fileCache = $this->getFileCache('', 'txt');
  137. $this->assertFalse($fileCache->fetch($this->filename));
  138. }
  139. /**
  140. * Test data can be fetched from cache.
  141. *
  142. * @access public
  143. * @return void
  144. */
  145. public function testDataCanBeFetchedFromCache()
  146. {
  147. $test = 'Test';
  148. $fileCache = $this->getFileCache($this->directory, 'txt');
  149. $fileCache->save($this->filename, $test);
  150. $content = $fileCache->fetch($this->filename);
  151. $this->assertSame($test, $content);
  152. }
  153. /**
  154. * Test data can be deleted from cache.
  155. *
  156. * @access public
  157. * @return void
  158. */
  159. public function testDataCanBeDeletedFromCache()
  160. {
  161. $fileCache = $this->getFileCache($this->directory, 'txt');
  162. $file = $fileCache->save($this->filename, 'Test');
  163. $fileCache->delete($this->filename);
  164. $this->assertFileNotExists($file);
  165. }
  166. /** +++++++++++++++++++++++++++++++++++ **/
  167. /** ++++++++++ TEST ENTITIES ++++++++++ **/
  168. /** +++++++++++++++++++++++++++++++++++ **/
  169. /**
  170. * Get file write instance
  171. *
  172. * @param string $directory
  173. * @param string $extension
  174. * @return \JonnyW\PhantomJs\Cache\FileCache
  175. */
  176. protected function getFileCache($directory, $extension)
  177. {
  178. $fileCache = new FileCache($directory, $extension);
  179. return $fileCache;
  180. }
  181. /** +++++++++++++++++++++++++++++++++++ **/
  182. /** ++++++++++++ UTILITIES ++++++++++++ **/
  183. /** +++++++++++++++++++++++++++++++++++ **/
  184. /**
  185. * Set up test environment.
  186. *
  187. * @access public
  188. * @return void
  189. */
  190. public function setUp()
  191. {
  192. $this->filename = 'test.txt';
  193. $this->directory = sys_get_temp_dir();
  194. if (!is_writable($this->directory)) {
  195. throw new \RuntimeException(sprintf('Test directory must be writable: %s', $this->directory));
  196. }
  197. }
  198. /**
  199. * Tear down test environment.
  200. *
  201. * @access public
  202. * @return void
  203. */
  204. public function tearDown()
  205. {
  206. $filename = $this->getFilename();
  207. if (file_exists($filename)) {
  208. unlink($filename);
  209. }
  210. }
  211. /**
  212. * Get test filename.
  213. *
  214. * @access public
  215. * @return string
  216. */
  217. public function getFilename()
  218. {
  219. return sprintf('%1$s/%2$s', $this->directory, $this->filename);
  220. }
  221. }