PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/Cache/FunctionFrontendTest.php

https://bitbucket.org/ksekar/campus
PHP | 218 lines | 155 code | 23 blank | 40 comment | 2 complexity | 9826479ec8166d11a5e3959a3e81f5f6 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, MIT
  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-2012 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 24594 2012-01-05 21:27:01Z matthew $
  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-2012 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_clean();
  93. ob_implicit_flush(true);
  94. $this->assertEquals('bar', $return);
  95. $this->assertEquals('foo', $data);
  96. }
  97. public function testCallCorrectCall2()
  98. {
  99. ob_start();
  100. ob_implicit_flush(false);
  101. $return = $this->_instance->call('foobar', array('param3', 'param4'));
  102. $data = ob_get_clean();
  103. ob_implicit_flush(true);
  104. $this->assertEquals('foobar_return(param3, param4)', $return);
  105. $this->assertEquals('foobar_output(param3, param4)', $data);
  106. }
  107. public function testCallCorrectCall3()
  108. {
  109. // cacheByDefault = false
  110. $this->_instance->setOption('cache_by_default', false);
  111. ob_start();
  112. ob_implicit_flush(false);
  113. $return = $this->_instance->call('foobar', array('param1', 'param2'));
  114. $data = ob_get_clean();
  115. ob_implicit_flush(true);
  116. $this->assertEquals('foobar_return(param1, param2)', $return);
  117. $this->assertEquals('foobar_output(param1, param2)', $data);
  118. }
  119. public function testCallCorrectCall4()
  120. {
  121. // cacheByDefault = false
  122. // cachedFunctions = array('foobar')
  123. $this->_instance->setOption('cache_by_default', false);
  124. $this->_instance->setOption('cached_functions', array('foobar'));
  125. ob_start();
  126. ob_implicit_flush(false);
  127. $return = $this->_instance->call('foobar', array('param1', 'param2'));
  128. $data = ob_get_clean();
  129. ob_implicit_flush(true);
  130. $this->assertEquals('bar', $return);
  131. $this->assertEquals('foo', $data);
  132. }
  133. public function testCallCorrectCall5()
  134. {
  135. // cacheByDefault = true
  136. // nonCachedFunctions = array('foobar')
  137. $this->_instance->setOption('cache_by_default', true);
  138. $this->_instance->setOption('non_cached_functions', array('foobar'));
  139. ob_start();
  140. ob_implicit_flush(false);
  141. $return = $this->_instance->call('foobar', array('param1', 'param2'));
  142. $data = ob_get_clean();
  143. ob_implicit_flush(true);
  144. $this->assertEquals('foobar_return(param1, param2)', $return);
  145. $this->assertEquals('foobar_output(param1, param2)', $data);
  146. }
  147. public function testCallObjectMethodCorrectCall1()
  148. {
  149. // cacheByDefault = true
  150. // nonCachedFunctions = array('foobar')
  151. $this->_instance->setOption('cache_by_default', true);
  152. $this->_instance->setOption('non_cached_functions', array('foobar'));
  153. ob_start();
  154. ob_implicit_flush(false);
  155. $object = new fooclass();
  156. $return = $this->_instance->call(array($object, 'foobar'), array('param1', 'param2'));
  157. $data = ob_get_clean();
  158. ob_implicit_flush(true);
  159. $this->assertEquals('foobar_return(param1, param2):1', $return);
  160. $this->assertEquals('foobar_output(param1, param2)', $data);
  161. }
  162. public function testCallObjectMethodCorrectCall2()
  163. {
  164. // cacheByDefault = true
  165. // nonCachedFunctions = array('foobar')
  166. $this->_instance->setOption('cache_by_default', true);
  167. $this->_instance->setOption('non_cached_functions', array('foobar'));
  168. ob_start();
  169. ob_implicit_flush(false);
  170. $object = new fooclass();
  171. $return = $this->_instance->call(array($object, 'foobar'), array('param1', 'param2'));
  172. $data = ob_get_clean();
  173. ob_implicit_flush(true);
  174. $this->assertEquals('foobar_return(param1, param2):2', $return);
  175. $this->assertEquals('foobar_output(param1, param2)', $data);
  176. }
  177. public function testCallClosureThrowsException()
  178. {
  179. if (version_compare(PHP_VERSION, '5.3', '<')) {
  180. $this->markTestSkipped();
  181. }
  182. $this->setExpectedException('Zend_Cache_Exception');
  183. eval('$closure = function () {};'); // no parse error on php < 5.3
  184. $this->_instance->call($closure);
  185. }
  186. public function testCallWithABadSyntax1()
  187. {
  188. try {
  189. $this->_instance->call(1, array());
  190. } catch (Zend_Cache_Exception $e) {
  191. return;
  192. }
  193. $this->fail('Zend_Cache_Exception was expected but not thrown');
  194. }
  195. }