/tests/Zend/Cache/FunctionFrontendTest.php

https://github.com/WebTricks/WebTricks-CMS · PHP · 230 lines · 163 code · 24 blank · 43 comment · 2 complexity · 611864a67ea27000d0e06a97cdc1b750 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: FunctionFrontendTest.php 22503 2010-06-29 17:18:05Z mabe $
  21. */
  22. /**
  23. * Zend_Cache
  24. */
  25. require_once 'Zend/Cache.php';
  26. require_once 'Zend/Cache/Frontend/Function.php';
  27. require_once 'Zend/Cache/Backend/Test.php';
  28. /**
  29. * PHPUnit test case
  30. */
  31. require_once 'PHPUnit/Framework/TestCase.php';
  32. function foobar($param1, $param2) {
  33. echo "foobar_output($param1, $param2)";
  34. return "foobar_return($param1, $param2)";
  35. }
  36. class fooclass {
  37. private static $_instanceCounter = 0;
  38. public function __construct()
  39. {
  40. self::$_instanceCounter++;
  41. }
  42. public function foobar($param1, $param2)
  43. {
  44. return foobar($param1, $param2)
  45. . ':' . self::$_instanceCounter;
  46. }
  47. }
  48. /**
  49. * @category Zend
  50. * @package Zend_Cache
  51. * @subpackage UnitTests
  52. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  53. * @license http://framework.zend.com/license/new-bsd New BSD License
  54. * @group Zend_Cache
  55. */
  56. class Zend_Cache_FunctionFrontendTest extends PHPUnit_Framework_TestCase {
  57. private $_instance;
  58. public function setUp()
  59. {
  60. if (!$this->_instance) {
  61. $this->_instance = new Zend_Cache_Frontend_Function(array());
  62. $this->_backend = new Zend_Cache_Backend_Test();
  63. $this->_instance->setBackend($this->_backend);
  64. }
  65. }
  66. public function tearDown()
  67. {
  68. unset($this->_instance);
  69. }
  70. public function testConstructorCorrectCall()
  71. {
  72. $options = array(
  73. 'cache_by_default' => false,
  74. 'cached_functions' => array('foo', 'bar')
  75. );
  76. $test = new Zend_Cache_Frontend_Function($options);
  77. }
  78. public function testConstructorBadCall()
  79. {
  80. $options = array(
  81. 'cache_by_default' => false,
  82. 0 => array('foo', 'bar')
  83. );
  84. try {
  85. $test = new Zend_Cache_Frontend_Function($options);
  86. } catch (Zend_Cache_Exception $e) {
  87. return;
  88. }
  89. $this->fail('Zend_Cache_Exception was expected but not thrown');
  90. }
  91. public function testCallCorrectCall1()
  92. {
  93. ob_start();
  94. ob_implicit_flush(false);
  95. $return = $this->_instance->call('foobar', array('param1', 'param2'));
  96. $data = ob_get_contents();
  97. ob_end_clean();
  98. ob_implicit_flush(true);
  99. $this->assertEquals('bar', $return);
  100. $this->assertEquals('foo', $data);
  101. }
  102. public function testCallCorrectCall2()
  103. {
  104. ob_start();
  105. ob_implicit_flush(false);
  106. $return = $this->_instance->call('foobar', array('param3', 'param4'));
  107. $data = ob_get_contents();
  108. ob_end_clean();
  109. ob_implicit_flush(true);
  110. $this->assertEquals('foobar_return(param3, param4)', $return);
  111. $this->assertEquals('foobar_output(param3, param4)', $data);
  112. }
  113. public function testCallCorrectCall3()
  114. {
  115. // cacheByDefault = false
  116. $this->_instance->setOption('cache_by_default', false);
  117. ob_start();
  118. ob_implicit_flush(false);
  119. $return = $this->_instance->call('foobar', array('param1', 'param2'));
  120. $data = ob_get_contents();
  121. ob_end_clean();
  122. ob_implicit_flush(true);
  123. $this->assertEquals('foobar_return(param1, param2)', $return);
  124. $this->assertEquals('foobar_output(param1, param2)', $data);
  125. }
  126. public function testCallCorrectCall4()
  127. {
  128. // cacheByDefault = false
  129. // cachedFunctions = array('foobar')
  130. $this->_instance->setOption('cache_by_default', false);
  131. $this->_instance->setOption('cached_functions', array('foobar'));
  132. ob_start();
  133. ob_implicit_flush(false);
  134. $return = $this->_instance->call('foobar', array('param1', 'param2'));
  135. $data = ob_get_contents();
  136. ob_end_clean();
  137. ob_implicit_flush(true);
  138. $this->assertEquals('bar', $return);
  139. $this->assertEquals('foo', $data);
  140. }
  141. public function testCallCorrectCall5()
  142. {
  143. // cacheByDefault = true
  144. // nonCachedFunctions = array('foobar')
  145. $this->_instance->setOption('cache_by_default', true);
  146. $this->_instance->setOption('non_cached_functions', array('foobar'));
  147. ob_start();
  148. ob_implicit_flush(false);
  149. $return = $this->_instance->call('foobar', array('param1', 'param2'));
  150. $data = ob_get_contents();
  151. ob_end_clean();
  152. ob_implicit_flush(true);
  153. $this->assertEquals('foobar_return(param1, param2)', $return);
  154. $this->assertEquals('foobar_output(param1, param2)', $data);
  155. }
  156. public function testCallObjectMethodCorrectCall1()
  157. {
  158. // cacheByDefault = true
  159. // nonCachedFunctions = array('foobar')
  160. $this->_instance->setOption('cache_by_default', true);
  161. $this->_instance->setOption('non_cached_functions', array('foobar'));
  162. ob_start();
  163. ob_implicit_flush(false);
  164. $object = new fooclass();
  165. $return = $this->_instance->call(array($object, 'foobar'), array('param1', 'param2'));
  166. $data = ob_get_contents();
  167. ob_end_clean();
  168. ob_implicit_flush(true);
  169. $this->assertEquals('foobar_return(param1, param2):1', $return);
  170. $this->assertEquals('foobar_output(param1, param2)', $data);
  171. }
  172. public function testCallObjectMethodCorrectCall2()
  173. {
  174. // cacheByDefault = true
  175. // nonCachedFunctions = array('foobar')
  176. $this->_instance->setOption('cache_by_default', true);
  177. $this->_instance->setOption('non_cached_functions', array('foobar'));
  178. ob_start();
  179. ob_implicit_flush(false);
  180. $object = new fooclass();
  181. $return = $this->_instance->call(array($object, 'foobar'), array('param1', 'param2'));
  182. $data = ob_get_contents();
  183. ob_end_clean();
  184. ob_implicit_flush(true);
  185. $this->assertEquals('foobar_return(param1, param2):2', $return);
  186. $this->assertEquals('foobar_output(param1, param2)', $data);
  187. }
  188. public function testCallClosureThrowsException()
  189. {
  190. if (version_compare(PHP_VERSION, '5.3', '<')) {
  191. $this->markTestSkipped();
  192. }
  193. $this->setExpectedException('Zend_Cache_Exception');
  194. eval('$closure = function () {};'); // no parse error on php < 5.3
  195. $this->_instance->call($closure);
  196. }
  197. public function testCallWithABadSyntax1()
  198. {
  199. try {
  200. $this->_instance->call(1, array());
  201. } catch (Zend_Cache_Exception $e) {
  202. return;
  203. }
  204. $this->fail('Zend_Cache_Exception was expected but not thrown');
  205. }
  206. }