PageRenderTime 55ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/Zend/Cache/ClassFrontendTest.php

https://github.com/bobdevelop/testgithubrepository
PHP | 268 lines | 188 code | 28 blank | 52 comment | 2 complexity | 71eeb149c57aaa05d822dec165e38118 MD5 | raw file
Possible License(s): LGPL-2.0, MIT, ISC, BSD-3-Clause
  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-2011 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 23775 2011-03-01 17:25:24Z ralph $
  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-2011 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-2011 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_clean();
  127. ob_implicit_flush(true);
  128. $this->assertEquals('bar', $return);
  129. $this->assertEquals('foo', $data);
  130. }
  131. public function testCallCorrectCall2()
  132. {
  133. ob_start();
  134. ob_implicit_flush(false);
  135. $return = $this->_instance1->foobar('param3', 'param4');
  136. $data = ob_get_clean();
  137. ob_implicit_flush(true);
  138. $this->assertEquals('foobar_return(param3, param4)', $return);
  139. $this->assertEquals('foobar_output(param3, param4)', $data);
  140. }
  141. public function testCallCorrectCall3()
  142. {
  143. ob_start();
  144. ob_implicit_flush(false);
  145. $return = $this->_instance2->foobar2('param1', 'param2');
  146. $data = ob_get_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_clean();
  157. ob_implicit_flush(true);
  158. $this->assertEquals('foobar2_return(param3, param4)', $return);
  159. $this->assertEquals('hello !foobar2_output(param3, param4)', $data);
  160. }
  161. public function testCallCorrectCall5()
  162. {
  163. // cacheByDefault = false
  164. $this->_instance1->setOption('cache_by_default', false);
  165. ob_start();
  166. ob_implicit_flush(false);
  167. $return = $this->_instance1->foobar('param1', 'param2');
  168. $data = ob_get_clean();
  169. ob_implicit_flush(true);
  170. $this->assertEquals('foobar_return(param1, param2)', $return);
  171. $this->assertEquals('foobar_output(param1, param2)', $data);
  172. }
  173. public function testCallCorrectCall6()
  174. {
  175. // cacheByDefault = false
  176. // cachedMethods = array('foobar')
  177. $this->_instance1->setOption('cache_by_default', false);
  178. $this->_instance1->setOption('cached_methods', array('foobar'));
  179. ob_start();
  180. ob_implicit_flush(false);
  181. $return = $this->_instance1->foobar('param1', 'param2');
  182. $data = ob_get_clean();
  183. ob_implicit_flush(true);
  184. $this->assertEquals('bar', $return);
  185. $this->assertEquals('foo', $data);
  186. }
  187. public function testCallCorrectCall7()
  188. {
  189. // cacheByDefault = true
  190. // nonCachedMethods = array('foobar')
  191. $this->_instance1->setOption('cache_by_default', true);
  192. $this->_instance1->setOption('non_cached_methods', array('foobar'));
  193. ob_start();
  194. ob_implicit_flush(false);
  195. $return = $this->_instance1->foobar('param1', 'param2');
  196. $data = ob_get_clean();
  197. ob_implicit_flush(true);
  198. $this->assertEquals('foobar_return(param1, param2)', $return);
  199. $this->assertEquals('foobar_output(param1, param2)', $data);
  200. }
  201. public function testConstructorWithABadCachedEntity()
  202. {
  203. try {
  204. $options = array(
  205. 'cached_entity' => array()
  206. );
  207. $instance = new Zend_Cache_Frontend_Class($options);
  208. } catch (Zend_Cache_Exception $e) {
  209. return;
  210. }
  211. $this->fail('Zend_Cache_Exception was expected but not thrown');
  212. }
  213. /**
  214. * @group ZF-5034
  215. */
  216. public function testCallingConstructorWithInvalidOptionShouldNotRaiseException()
  217. {
  218. $options = array(
  219. 'cached_entity' => new test(),
  220. 'this_key_does_not_exist' => true
  221. );
  222. $test = new Zend_Cache_Frontend_Class($options);
  223. }
  224. /**
  225. * @ZF-10521
  226. */
  227. public function testOutputBufferingOnException()
  228. {
  229. ob_start();
  230. ob_implicit_flush(false);
  231. echo 'start';
  232. try {
  233. $this->_instance2->throwException();
  234. $this->fail("An exception should be thrown");
  235. } catch (Exception $e) {}
  236. echo 'end';
  237. $output = ob_get_clean();
  238. $this->assertEquals('startend', $output);
  239. }
  240. }