PageFrontendTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * @package Zend_Cache
  4. * @subpackage UnitTests
  5. */
  6. /**
  7. * Zend_Cache
  8. */
  9. require_once 'Zend/Cache.php';
  10. require_once 'Zend/Cache/Frontend/Page.php';
  11. require_once 'Zend/Cache/Backend/Test.php';
  12. /**
  13. * PHPUnit test case
  14. */
  15. require_once 'PHPUnit/Framework/TestCase.php';
  16. /**
  17. * @package Zend_Cache
  18. * @subpackage UnitTests
  19. */
  20. class Zend_Cache_PageFrontendTest extends PHPUnit_Framework_TestCase {
  21. private $_instance;
  22. public function setUp()
  23. {
  24. if (!$this->_instance) {
  25. $this->_instance = new Zend_Cache_Frontend_Page(array());
  26. $this->_backend = new Zend_Cache_Backend_Test();
  27. $this->_instance->setBackend($this->_backend);
  28. }
  29. }
  30. public function tearDown()
  31. {
  32. unset($this->_instance);
  33. }
  34. public function testConstructorCorrectCall()
  35. {
  36. $test = new Zend_Cache_Frontend_Page(array('lifetime' => 3600, 'caching' => true));
  37. }
  38. public function testConstructorUnimplementedOption()
  39. {
  40. try {
  41. $test = new Zend_Cache_Frontend_Page(array('http_conditional' => true));
  42. } catch (Exception $e) {
  43. return;
  44. }
  45. $this->fail('Zend_Cache_Exception was expected but not thrown');
  46. }
  47. public function testConstructorWithBadDefaultOptions()
  48. {
  49. try {
  50. $test = new Zend_Cache_Frontend_Page(array('default_options' => 'foo'));
  51. } catch (Exception $e) {
  52. return;
  53. }
  54. $this->fail('Zend_Cache_Exception was expected but not thrown');
  55. }
  56. /**
  57. * The only bad default options are non-string keys
  58. * @group ZF-5034
  59. */
  60. public function testConstructorWithBadDefaultOptions2()
  61. {
  62. try {
  63. $test = new Zend_Cache_Frontend_Page(array('default_options' => array('cache' => true, 1 => 'bar')));
  64. } catch (Exception $e) {
  65. return;
  66. }
  67. $this->fail('Zend_Cache_Exception was expected but not thrown');
  68. }
  69. public function testConstructorWithBadRegexps()
  70. {
  71. try {
  72. $test = new Zend_Cache_Frontend_Page(array('regexps' => 'foo'));
  73. } catch (Exception $e) {
  74. return;
  75. }
  76. $this->fail('Zend_Cache_Exception was expected but not thrown');
  77. }
  78. public function testConstructorWithBadRegexps2()
  79. {
  80. try {
  81. $test = new Zend_Cache_Frontend_Page(array('regexps' => array('foo', 'bar')));
  82. } catch (Exception $e) {
  83. return;
  84. }
  85. $this->fail('Zend_Cache_Exception was expected but not thrown');
  86. }
  87. /**
  88. * Only non-string keys should raise exceptions
  89. * @group ZF-5034
  90. */
  91. public function testConstructorWithBadRegexps3()
  92. {
  93. $array = array(
  94. '^/$' => array('cache' => true),
  95. '^/index/' => array('cache' => true),
  96. '^/article/' => array('cache' => false),
  97. '^/article/view/' => array(
  98. 1 => true,
  99. 'cache_with_post_variables' => true,
  100. 'make_id_with_post_variables' => true,
  101. )
  102. );
  103. try {
  104. $test = new Zend_Cache_Frontend_Page(array('regexps' => $array));
  105. } catch (Exception $e) {
  106. return;
  107. }
  108. $this->fail('Zend_Cache_Exception was expected but not thrown');
  109. }
  110. public function testConstructorWithGoodRegexps()
  111. {
  112. $array = array(
  113. '^/$' => array('cache' => true),
  114. '^/index/' => array('cache' => true),
  115. '^/article/' => array('cache' => false),
  116. '^/article/view/' => array(
  117. 'cache' => true,
  118. 'cache_with_post_variables' => true,
  119. 'make_id_with_post_variables' => true,
  120. )
  121. );
  122. $test = new Zend_Cache_Frontend_Page(array('regexps' => $array));
  123. }
  124. public function testConstructorWithGoodDefaultOptions()
  125. {
  126. $test = new Zend_Cache_Frontend_Page(array('default_options' => array('cache' => true)));
  127. }
  128. public function testStartEndCorrectCall1()
  129. {
  130. ob_start();
  131. ob_implicit_flush(false);
  132. if (!($this->_instance->start('serialized2', true))) {
  133. echo('foobar');
  134. ob_end_flush();
  135. }
  136. $data = ob_get_contents();
  137. ob_end_clean();
  138. ob_implicit_flush(true);
  139. $this->assertEquals('foo', $data);
  140. }
  141. public function testStartEndCorrectCall2()
  142. {
  143. ob_start();
  144. ob_implicit_flush(false);
  145. if (!($this->_instance->start('false', true))) {
  146. echo('foobar');
  147. ob_end_flush();
  148. }
  149. $data = ob_get_contents();
  150. ob_end_clean();
  151. ob_implicit_flush(true);
  152. $this->assertEquals('foobar', $data);
  153. }
  154. public function testStartEndCorrectCallWithDebug()
  155. {
  156. $this->_instance->setOption('debug_header', true);
  157. ob_start();
  158. ob_implicit_flush(false);
  159. if (!($this->_instance->start('serialized2', true))) {
  160. echo('foobar');
  161. ob_end_flush();
  162. }
  163. $data = ob_get_contents();
  164. ob_end_clean();
  165. ob_implicit_flush(true);
  166. $this->assertEquals('DEBUG HEADER : This is a cached page !foo', $data);
  167. }
  168. }