ClassFrontendTest.php 7.5 KB

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