FileCachingTest.php 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. class Twig_Tests_FileCachingTest extends PHPUnit_Framework_TestCase
  3. {
  4. protected $fileName;
  5. protected $env;
  6. protected $tmpDir;
  7. public function setUp()
  8. {
  9. $this->tmpDir = sys_get_temp_dir().'/TwigTests';
  10. if (!file_exists($this->tmpDir)) {
  11. @mkdir($this->tmpDir, 0777, true);;
  12. }
  13. if (!is_writable($this->tmpDir)) {
  14. $this->markTestSkipped(sprintf('Unable to run the tests as "%s" is not writable.', $this->tmpDir));
  15. }
  16. $this->env = new Twig_Environment(new Twig_Loader_String(), array('cache' => $this->tmpDir));
  17. }
  18. public function tearDown()
  19. {
  20. if ($this->fileName) {
  21. unlink($this->fileName);
  22. }
  23. $this->removeDir($this->tmpDir);
  24. }
  25. public function testWritingCacheFiles()
  26. {
  27. $name = 'This is just text.';
  28. $template = $this->env->loadTemplate($name);
  29. $cacheFileName = $this->env->getCacheFilename($name);
  30. $this->assertTrue(file_exists($cacheFileName), 'Cache file does not exist.');
  31. $this->fileName = $cacheFileName;
  32. }
  33. public function testClearingCacheFiles()
  34. {
  35. $name = 'I will be deleted.';
  36. $template = $this->env->loadTemplate($name);
  37. $cacheFileName = $this->env->getCacheFilename($name);
  38. $this->assertTrue(file_exists($cacheFileName), 'Cache file does not exist.');
  39. $this->env->clearCacheFiles();
  40. $this->assertFalse(file_exists($cacheFileName), 'Cache file was not cleared.');
  41. }
  42. private function removeDir($target)
  43. {
  44. $fp = opendir($target);
  45. while (false !== $file = readdir($fp)) {
  46. if (in_array($file, array('.', '..'))) {
  47. continue;
  48. }
  49. if (is_dir($target.'/'.$file)) {
  50. self::removeDir($target.'/'.$file);
  51. } else {
  52. unlink($target.'/'.$file);
  53. }
  54. }
  55. closedir($fp);
  56. rmdir($target);
  57. }
  58. }