FunctionFrontendTest.php 7.1 KB

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