FunctionFrontendTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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-2009 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. /**
  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. /**
  37. * @category Zend
  38. * @package Zend_Cache
  39. * @subpackage UnitTests
  40. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  41. * @license http://framework.zend.com/license/new-bsd New BSD License
  42. * @group Zend_Cache
  43. */
  44. class Zend_Cache_FunctionFrontendTest extends PHPUnit_Framework_TestCase {
  45. private $_instance;
  46. public function setUp()
  47. {
  48. if (!$this->_instance) {
  49. $this->_instance = new Zend_Cache_Frontend_Function(array());
  50. $this->_backend = new Zend_Cache_Backend_Test();
  51. $this->_instance->setBackend($this->_backend);
  52. }
  53. }
  54. public function tearDown()
  55. {
  56. unset($this->_instance);
  57. }
  58. public function testConstructorCorrectCall()
  59. {
  60. $options = array(
  61. 'cache_by_default' => false,
  62. 'cached_functions' => array('foo', 'bar')
  63. );
  64. $test = new Zend_Cache_Frontend_Function($options);
  65. }
  66. public function testConstructorBadCall()
  67. {
  68. $options = array(
  69. 'cache_by_default' => false,
  70. 0 => array('foo', 'bar')
  71. );
  72. try {
  73. $test = new Zend_Cache_Frontend_Function($options);
  74. } catch (Zend_Cache_Exception $e) {
  75. return;
  76. }
  77. $this->fail('Zend_Cache_Exception was expected but not thrown');
  78. }
  79. public function testCallCorrectCall1()
  80. {
  81. ob_start();
  82. ob_implicit_flush(false);
  83. $return = $this->_instance->call('foobar', array('param1', 'param2'));
  84. $data = ob_get_contents();
  85. ob_end_clean();
  86. ob_implicit_flush(true);
  87. $this->assertEquals('bar', $return);
  88. $this->assertEquals('foo', $data);
  89. }
  90. public function testCallCorrectCall2()
  91. {
  92. ob_start();
  93. ob_implicit_flush(false);
  94. $return = $this->_instance->call('foobar', array('param3', 'param4'));
  95. $data = ob_get_contents();
  96. ob_end_clean();
  97. ob_implicit_flush(true);
  98. $this->assertEquals('foobar_return(param3, param4)', $return);
  99. $this->assertEquals('foobar_output(param3, param4)', $data);
  100. }
  101. public function testCallCorrectCall3()
  102. {
  103. // cacheByDefault = false
  104. $this->_instance->setOption('cache_by_default', false);
  105. ob_start();
  106. ob_implicit_flush(false);
  107. $return = $this->_instance->call('foobar', array('param1', 'param2'));
  108. $data = ob_get_contents();
  109. ob_end_clean();
  110. ob_implicit_flush(true);
  111. $this->assertEquals('foobar_return(param1, param2)', $return);
  112. $this->assertEquals('foobar_output(param1, param2)', $data);
  113. }
  114. public function testCallCorrectCall4()
  115. {
  116. // cacheByDefault = false
  117. // cachedFunctions = array('foobar')
  118. $this->_instance->setOption('cache_by_default', false);
  119. $this->_instance->setOption('cached_functions', array('foobar'));
  120. ob_start();
  121. ob_implicit_flush(false);
  122. $return = $this->_instance->call('foobar', array('param1', 'param2'));
  123. $data = ob_get_contents();
  124. ob_end_clean();
  125. ob_implicit_flush(true);
  126. $this->assertEquals('bar', $return);
  127. $this->assertEquals('foo', $data);
  128. }
  129. public function testCallCorrectCall5()
  130. {
  131. // cacheByDefault = true
  132. // nonCachedFunctions = array('foobar')
  133. $this->_instance->setOption('cache_by_default', true);
  134. $this->_instance->setOption('non_cached_functions', array('foobar'));
  135. ob_start();
  136. ob_implicit_flush(false);
  137. $return = $this->_instance->call('foobar', array('param1', 'param2'));
  138. $data = ob_get_contents();
  139. ob_end_clean();
  140. ob_implicit_flush(true);
  141. $this->assertEquals('foobar_return(param1, param2)', $return);
  142. $this->assertEquals('foobar_output(param1, param2)', $data);
  143. }
  144. public function testCallWithABadSyntax1()
  145. {
  146. try {
  147. $this->_instance->call(1, array());
  148. } catch (Zend_Cache_Exception $e) {
  149. return;
  150. }
  151. $this->fail('Zend_Cache_Exception was expected but not thrown');
  152. }
  153. public function testCallWithABadSyntax2()
  154. {
  155. try {
  156. $this->_instance->call('foo', 1);
  157. } catch (Zend_Cache_Exception $e) {
  158. return;
  159. }
  160. $this->fail('Zend_Cache_Exception was expected but not thrown');
  161. }
  162. }