CacheTest.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * Cache tests
  4. */
  5. // required to access $_SESSION array
  6. session_start();
  7. require_once 'application/Cache.php';
  8. /**
  9. * Unitary tests for cached pages
  10. */
  11. class CacheTest extends PHPUnit_Framework_TestCase
  12. {
  13. // test cache directory
  14. protected static $testCacheDir = 'sandbox/dummycache';
  15. // dummy cached file names / content
  16. protected static $pages = array('a', 'toto', 'd7b59c');
  17. /**
  18. * Populate the cache with dummy files
  19. */
  20. public function setUp()
  21. {
  22. if (! is_dir(self::$testCacheDir)) {
  23. mkdir(self::$testCacheDir);
  24. } else {
  25. array_map('unlink', glob(self::$testCacheDir.'/*'));
  26. }
  27. foreach (self::$pages as $page) {
  28. file_put_contents(self::$testCacheDir.'/'.$page.'.cache', $page);
  29. }
  30. file_put_contents(self::$testCacheDir.'/intru.der', 'ShouldNotBeThere');
  31. }
  32. /**
  33. * Remove dummycache folder after each tests.
  34. */
  35. public function tearDown()
  36. {
  37. array_map('unlink', glob(self::$testCacheDir.'/*'));
  38. rmdir(self::$testCacheDir);
  39. }
  40. /**
  41. * Purge cached pages
  42. */
  43. public function testPurgeCachedPages()
  44. {
  45. purgeCachedPages(self::$testCacheDir);
  46. foreach (self::$pages as $page) {
  47. $this->assertFileNotExists(self::$testCacheDir.'/'.$page.'.cache');
  48. }
  49. $this->assertFileExists(self::$testCacheDir.'/intru.der');
  50. }
  51. /**
  52. * Purge cached pages - missing directory
  53. */
  54. public function testPurgeCachedPagesMissingDir()
  55. {
  56. $oldlog = ini_get('error_log');
  57. ini_set('error_log', '/dev/null');
  58. $this->assertEquals(
  59. 'Cannot purge sandbox/dummycache_missing: no directory',
  60. purgeCachedPages(self::$testCacheDir.'_missing')
  61. );
  62. ini_set('error_log', $oldlog);
  63. }
  64. /**
  65. * Purge cached pages and session cache
  66. */
  67. public function testInvalidateCaches()
  68. {
  69. $this->assertArrayNotHasKey('tags', $_SESSION);
  70. $_SESSION['tags'] = array('goodbye', 'cruel', 'world');
  71. invalidateCaches(self::$testCacheDir);
  72. foreach (self::$pages as $page) {
  73. $this->assertFileNotExists(self::$testCacheDir.'/'.$page.'.cache');
  74. }
  75. $this->assertArrayNotHasKey('tags', $_SESSION);
  76. }
  77. }