CachedPageTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * PageCache tests
  4. */
  5. require_once 'application/CachedPage.php';
  6. /**
  7. * Unitary tests for cached pages
  8. */
  9. class CachedPageTest extends PHPUnit_Framework_TestCase
  10. {
  11. // test cache directory
  12. protected static $testCacheDir = 'sandbox/pagecache';
  13. protected static $url = 'http://shaar.li/?do=atom';
  14. protected static $filename;
  15. /**
  16. * Create the cache directory if needed
  17. */
  18. public static function setUpBeforeClass()
  19. {
  20. if (! is_dir(self::$testCacheDir)) {
  21. mkdir(self::$testCacheDir);
  22. }
  23. self::$filename = self::$testCacheDir.'/'.sha1(self::$url).'.cache';
  24. }
  25. /**
  26. * Reset the page cache
  27. */
  28. public function setUp()
  29. {
  30. if (file_exists(self::$filename)) {
  31. unlink(self::$filename);
  32. }
  33. }
  34. /**
  35. * Create a new cached page
  36. */
  37. public function testConstruct()
  38. {
  39. new CachedPage(self::$testCacheDir, '', true);
  40. new CachedPage(self::$testCacheDir, '', false);
  41. new CachedPage(self::$testCacheDir, 'http://shaar.li/?do=rss', true);
  42. new CachedPage(self::$testCacheDir, 'http://shaar.li/?do=atom', false);
  43. }
  44. /**
  45. * Cache a page's content
  46. */
  47. public function testCache()
  48. {
  49. $page = new CachedPage(self::$testCacheDir, self::$url, true);
  50. $this->assertFileNotExists(self::$filename);
  51. $page->cache('<p>Some content</p>');
  52. $this->assertFileExists(self::$filename);
  53. $this->assertEquals(
  54. '<p>Some content</p>',
  55. file_get_contents(self::$filename)
  56. );
  57. }
  58. /**
  59. * "Cache" a page's content - the page is not to be cached
  60. */
  61. public function testShouldNotCache()
  62. {
  63. $page = new CachedPage(self::$testCacheDir, self::$url, false);
  64. $this->assertFileNotExists(self::$filename);
  65. $page->cache('<p>Some content</p>');
  66. $this->assertFileNotExists(self::$filename);
  67. }
  68. /**
  69. * Return a page's cached content
  70. */
  71. public function testCachedVersion()
  72. {
  73. $page = new CachedPage(self::$testCacheDir, self::$url, true);
  74. $this->assertFileNotExists(self::$filename);
  75. $page->cache('<p>Some content</p>');
  76. $this->assertFileExists(self::$filename);
  77. $this->assertEquals(
  78. '<p>Some content</p>',
  79. $page->cachedVersion()
  80. );
  81. }
  82. /**
  83. * Return a page's cached content - the file does not exist
  84. */
  85. public function testCachedVersionNoFile()
  86. {
  87. $page = new CachedPage(self::$testCacheDir, self::$url, true);
  88. $this->assertFileNotExists(self::$filename);
  89. $this->assertEquals(
  90. null,
  91. $page->cachedVersion()
  92. );
  93. }
  94. /**
  95. * Return a page's cached content - the page is not to be cached
  96. */
  97. public function testNoCachedVersion()
  98. {
  99. $page = new CachedPage(self::$testCacheDir, self::$url, false);
  100. $this->assertFileNotExists(self::$filename);
  101. $this->assertEquals(
  102. null,
  103. $page->cachedVersion()
  104. );
  105. }
  106. }