ClassFrontendTest.php 8.7 KB

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