/tests/Zend/Cache/ClassFrontendTest.php

https://github.com/WebTricks/WebTricks-CMS · PHP · 280 lines · 196 code · 29 blank · 55 comment · 2 complexity · d0627e7aa1fcdd3e0942f180310245e8 MD5 · raw file

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