PageFrontendTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Cache
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * Zend_Cache
  24. */
  25. require_once 'Zend/Cache.php';
  26. require_once 'Zend/Cache/Frontend/Page.php';
  27. require_once 'Zend/Cache/Backend/Test.php';
  28. /**
  29. * PHPUnit test case
  30. */
  31. require_once 'PHPUnit/Framework/TestCase.php';
  32. /**
  33. * @category Zend
  34. * @package Zend_Cache
  35. * @subpackage UnitTests
  36. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. * @group Zend_Cache
  39. */
  40. class Zend_Cache_PageFrontendTest extends PHPUnit_Framework_TestCase {
  41. private $_instance;
  42. public function setUp()
  43. {
  44. if (!$this->_instance) {
  45. $this->_instance = new Zend_Cache_Frontend_Page(array());
  46. $this->_backend = new Zend_Cache_Backend_Test();
  47. $this->_instance->setBackend($this->_backend);
  48. }
  49. }
  50. public function tearDown()
  51. {
  52. unset($this->_instance);
  53. }
  54. public function testConstructorCorrectCall()
  55. {
  56. $test = new Zend_Cache_Frontend_Page(array('lifetime' => 3600, 'caching' => true));
  57. }
  58. public function testConstructorUnimplementedOption()
  59. {
  60. try {
  61. $test = new Zend_Cache_Frontend_Page(array('http_conditional' => true));
  62. } catch (Exception $e) {
  63. return;
  64. }
  65. $this->fail('Zend_Cache_Exception was expected but not thrown');
  66. }
  67. public function testConstructorWithBadDefaultOptions()
  68. {
  69. try {
  70. $test = new Zend_Cache_Frontend_Page(array('default_options' => 'foo'));
  71. } catch (Exception $e) {
  72. return;
  73. }
  74. $this->fail('Zend_Cache_Exception was expected but not thrown');
  75. }
  76. /**
  77. * The only bad default options are non-string keys
  78. * @group ZF-5034
  79. */
  80. public function testConstructorWithBadDefaultOptions2()
  81. {
  82. try {
  83. $test = new Zend_Cache_Frontend_Page(array('default_options' => array('cache' => true, 1 => 'bar')));
  84. } catch (Exception $e) {
  85. return;
  86. }
  87. $this->fail('Zend_Cache_Exception was expected but not thrown');
  88. }
  89. public function testConstructorWithBadRegexps()
  90. {
  91. try {
  92. $test = new Zend_Cache_Frontend_Page(array('regexps' => 'foo'));
  93. } catch (Exception $e) {
  94. return;
  95. }
  96. $this->fail('Zend_Cache_Exception was expected but not thrown');
  97. }
  98. public function testConstructorWithBadRegexps2()
  99. {
  100. try {
  101. $test = new Zend_Cache_Frontend_Page(array('regexps' => array('foo', 'bar')));
  102. } catch (Exception $e) {
  103. return;
  104. }
  105. $this->fail('Zend_Cache_Exception was expected but not thrown');
  106. }
  107. /**
  108. * Only non-string keys should raise exceptions
  109. * @group ZF-5034
  110. */
  111. public function testConstructorWithBadRegexps3()
  112. {
  113. $array = array(
  114. '^/$' => array('cache' => true),
  115. '^/index/' => array('cache' => true),
  116. '^/article/' => array('cache' => false),
  117. '^/article/view/' => array(
  118. 1 => true,
  119. 'cache_with_post_variables' => true,
  120. 'make_id_with_post_variables' => true,
  121. )
  122. );
  123. try {
  124. $test = new Zend_Cache_Frontend_Page(array('regexps' => $array));
  125. } catch (Exception $e) {
  126. return;
  127. }
  128. $this->fail('Zend_Cache_Exception was expected but not thrown');
  129. }
  130. public function testConstructorWithGoodRegexps()
  131. {
  132. $array = array(
  133. '^/$' => array('cache' => true),
  134. '^/index/' => array('cache' => true),
  135. '^/article/' => array('cache' => false),
  136. '^/article/view/' => array(
  137. 'cache' => true,
  138. 'cache_with_post_variables' => true,
  139. 'make_id_with_post_variables' => true,
  140. )
  141. );
  142. $test = new Zend_Cache_Frontend_Page(array('regexps' => $array));
  143. }
  144. public function testConstructorWithGoodDefaultOptions()
  145. {
  146. $test = new Zend_Cache_Frontend_Page(array('default_options' => array('cache' => true)));
  147. }
  148. public function testStartEndCorrectCall1()
  149. {
  150. ob_start();
  151. ob_implicit_flush(false);
  152. if (!($this->_instance->start('serialized2', true))) {
  153. echo('foobar');
  154. ob_end_flush();
  155. }
  156. $data = ob_get_contents();
  157. ob_end_clean();
  158. ob_implicit_flush(true);
  159. $this->assertEquals('foo', $data);
  160. }
  161. public function testStartEndCorrectCall2()
  162. {
  163. ob_start();
  164. ob_implicit_flush(false);
  165. if (!($this->_instance->start('false', true))) {
  166. echo('foobar');
  167. ob_end_flush();
  168. }
  169. $data = ob_get_contents();
  170. ob_end_clean();
  171. ob_implicit_flush(true);
  172. $this->assertEquals('foobar', $data);
  173. }
  174. public function testStartEndCorrectCallWithDebug()
  175. {
  176. $this->_instance->setOption('debug_header', true);
  177. ob_start();
  178. ob_implicit_flush(false);
  179. if (!($this->_instance->start('serialized2', true))) {
  180. echo('foobar');
  181. ob_end_flush();
  182. }
  183. $data = ob_get_contents();
  184. ob_end_clean();
  185. ob_implicit_flush(true);
  186. $this->assertEquals('DEBUG HEADER : This is a cached page !foo', $data);
  187. }
  188. }