PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/Zend/Cache/FunctionFrontendTest.php

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