ClassFrontendTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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-2010 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/Class.php';
  27. require_once 'Zend/Cache/Backend/Test.php';
  28. /**
  29. * @todo: Should this class be named Zend_Cache_Something?
  30. *
  31. * @category Zend
  32. * @package Zend_Cache
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_Cache
  37. */
  38. class test {
  39. private $_string = 'hello !';
  40. public static function foobar($param1, $param2)
  41. {
  42. echo "foobar_output($param1, $param2)";
  43. return "foobar_return($param1, $param2)";
  44. }
  45. public function foobar2($param1, $param2)
  46. {
  47. echo($this->_string);
  48. echo "foobar2_output($param1, $param2)";
  49. return "foobar2_return($param1, $param2)";
  50. }
  51. public function throwException()
  52. {
  53. echo 'throw exception';
  54. throw new Exception('test exception');
  55. }
  56. }
  57. /**
  58. * @category Zend
  59. * @package Zend_Cache
  60. * @subpackage UnitTests
  61. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  62. * @license http://framework.zend.com/license/new-bsd New BSD License
  63. * @group Zend_Cache
  64. */
  65. class Zend_Cache_ClassFrontendTest extends PHPUnit_Framework_TestCase {
  66. private $_instance1;
  67. private $_instance2;
  68. public function setUp()
  69. {
  70. if (!$this->_instance1) {
  71. $options1 = array(
  72. 'cached_entity' => 'test'
  73. );
  74. $this->_instance1 = new Zend_Cache_Frontend_Class($options1);
  75. $this->_backend1 = new Zend_Cache_Backend_Test();
  76. $this->_instance1->setBackend($this->_backend1);
  77. }
  78. if (!$this->_instance2) {
  79. $options2 = array(
  80. 'cached_entity' => new test()
  81. );
  82. $this->_instance2 = new Zend_Cache_Frontend_Class($options2);
  83. $this->_backend2 = new Zend_Cache_Backend_Test();
  84. $this->_instance2->setBackend($this->_backend2);
  85. }
  86. }
  87. public function tearDown()
  88. {
  89. unset($this->_instance1);
  90. unset($this->_instance2);
  91. }
  92. public function testConstructorCorrectCall1()
  93. {
  94. $options = array(
  95. 'cache_by_default' => false,
  96. 'cached_entity' => 'test'
  97. );
  98. $test = new Zend_Cache_Frontend_Class($options);
  99. }
  100. public function testConstructorCorrectCall2()
  101. {
  102. $options = array(
  103. 'cache_by_default' => false,
  104. 'cached_entity' => new test()
  105. );
  106. $test = new Zend_Cache_Frontend_Class($options);
  107. }
  108. public function testConstructorBadCall()
  109. {
  110. $options = array(
  111. 'cached_entity' => new test(),
  112. 0 => true,
  113. );
  114. try {
  115. $test = new Zend_Cache_Frontend_Class($options);
  116. } catch (Zend_Cache_Exception $e) {
  117. return;
  118. }
  119. $this->fail('Zend_Cache_Exception was expected but not thrown');
  120. }
  121. public function testCallCorrectCall1()
  122. {
  123. ob_start();
  124. ob_implicit_flush(false);
  125. $return = $this->_instance1->foobar('param1', 'param2');
  126. $data = ob_get_contents();
  127. ob_end_clean();
  128. ob_implicit_flush(true);
  129. $this->assertEquals('bar', $return);
  130. $this->assertEquals('foo', $data);
  131. }
  132. public function testCallCorrectCall2()
  133. {
  134. ob_start();
  135. ob_implicit_flush(false);
  136. $return = $this->_instance1->foobar('param3', 'param4');
  137. $data = ob_get_contents();
  138. ob_end_clean();
  139. ob_implicit_flush(true);
  140. $this->assertEquals('foobar_return(param3, param4)', $return);
  141. $this->assertEquals('foobar_output(param3, param4)', $data);
  142. }
  143. public function testCallCorrectCall3()
  144. {
  145. ob_start();
  146. ob_implicit_flush(false);
  147. $return = $this->_instance2->foobar2('param1', 'param2');
  148. $data = ob_get_contents();
  149. ob_end_clean();
  150. ob_implicit_flush(true);
  151. $this->assertEquals('bar', $return);
  152. $this->assertEquals('foo', $data);
  153. }
  154. public function testCallCorrectCall4()
  155. {
  156. ob_start();
  157. ob_implicit_flush(false);
  158. $return = $this->_instance2->foobar2('param3', 'param4');
  159. $data = ob_get_contents();
  160. ob_end_clean();
  161. ob_implicit_flush(true);
  162. $this->assertEquals('foobar2_return(param3, param4)', $return);
  163. $this->assertEquals('hello !foobar2_output(param3, param4)', $data);
  164. }
  165. public function testCallCorrectCall5()
  166. {
  167. // cacheByDefault = false
  168. $this->_instance1->setOption('cache_by_default', false);
  169. ob_start();
  170. ob_implicit_flush(false);
  171. $return = $this->_instance1->foobar('param1', 'param2');
  172. $data = ob_get_contents();
  173. ob_end_clean();
  174. ob_implicit_flush(true);
  175. $this->assertEquals('foobar_return(param1, param2)', $return);
  176. $this->assertEquals('foobar_output(param1, param2)', $data);
  177. }
  178. public function testCallCorrectCall6()
  179. {
  180. // cacheByDefault = false
  181. // cachedMethods = array('foobar')
  182. $this->_instance1->setOption('cache_by_default', false);
  183. $this->_instance1->setOption('cached_methods', array('foobar'));
  184. ob_start();
  185. ob_implicit_flush(false);
  186. $return = $this->_instance1->foobar('param1', 'param2');
  187. $data = ob_get_contents();
  188. ob_end_clean();
  189. ob_implicit_flush(true);
  190. $this->assertEquals('bar', $return);
  191. $this->assertEquals('foo', $data);
  192. }
  193. public function testCallCorrectCall7()
  194. {
  195. // cacheByDefault = true
  196. // nonCachedMethods = array('foobar')
  197. $this->_instance1->setOption('cache_by_default', true);
  198. $this->_instance1->setOption('non_cached_methods', array('foobar'));
  199. ob_start();
  200. ob_implicit_flush(false);
  201. $return = $this->_instance1->foobar('param1', 'param2');
  202. $data = ob_get_contents();
  203. ob_end_clean();
  204. ob_implicit_flush(true);
  205. $this->assertEquals('foobar_return(param1, param2)', $return);
  206. $this->assertEquals('foobar_output(param1, param2)', $data);
  207. }
  208. public function testConstructorWithABadCachedEntity()
  209. {
  210. try {
  211. $options = array(
  212. 'cached_entity' => array()
  213. );
  214. $instance = new Zend_Cache_Frontend_Class($options);
  215. } catch (Zend_Cache_Exception $e) {
  216. return;
  217. }
  218. $this->fail('Zend_Cache_Exception was expected but not thrown');
  219. }
  220. /**
  221. * @group ZF-5034
  222. */
  223. public function testCallingConstructorWithInvalidOptionShouldNotRaiseException()
  224. {
  225. $options = array(
  226. 'cached_entity' => new test(),
  227. 'this_key_does_not_exist' => true
  228. );
  229. $test = new Zend_Cache_Frontend_Class($options);
  230. }
  231. /**
  232. * @ZF-10521
  233. */
  234. public function testOutputBufferingOnException()
  235. {
  236. ob_start();
  237. ob_implicit_flush(false);
  238. echo 'start';
  239. try {
  240. $this->_instance2->throwException();
  241. $this->fail("An exception should be thrown");
  242. } catch (Exception $e) {}
  243. echo 'end';
  244. $output = ob_get_clean();
  245. $this->assertEquals('startend', $output);
  246. }
  247. }