ClassFrontendTest.php 8.7 KB

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