FunctionFrontendTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * @package Zend_Cache
  4. * @subpackage UnitTests
  5. */
  6. /**
  7. * Zend_Cache
  8. */
  9. require_once 'Zend/Cache.php';
  10. require_once 'Zend/Cache/Frontend/Function.php';
  11. require_once 'Zend/Cache/Backend/Test.php';
  12. /**
  13. * PHPUnit test case
  14. */
  15. require_once 'PHPUnit/Framework/TestCase.php';
  16. function foobar($param1, $param2) {
  17. echo "foobar_output($param1, $param2)";
  18. return "foobar_return($param1, $param2)";
  19. }
  20. /**
  21. * @package Zend_Cache
  22. * @subpackage UnitTests
  23. */
  24. class Zend_Cache_FunctionFrontendTest extends PHPUnit_Framework_TestCase {
  25. private $_instance;
  26. public function setUp()
  27. {
  28. if (!$this->_instance) {
  29. $this->_instance = new Zend_Cache_Frontend_Function(array());
  30. $this->_backend = new Zend_Cache_Backend_Test();
  31. $this->_instance->setBackend($this->_backend);
  32. }
  33. }
  34. public function tearDown()
  35. {
  36. unset($this->_instance);
  37. }
  38. public function testConstructorCorrectCall()
  39. {
  40. $options = array(
  41. 'cache_by_default' => false,
  42. 'cached_functions' => array('foo', 'bar')
  43. );
  44. $test = new Zend_Cache_Frontend_Function($options);
  45. }
  46. public function testConstructorBadCall()
  47. {
  48. $options = array(
  49. 'cache_by_default' => false,
  50. 0 => array('foo', 'bar')
  51. );
  52. try {
  53. $test = new Zend_Cache_Frontend_Function($options);
  54. } catch (Zend_Cache_Exception $e) {
  55. return;
  56. }
  57. $this->fail('Zend_Cache_Exception was expected but not thrown');
  58. }
  59. public function testCallCorrectCall1()
  60. {
  61. ob_start();
  62. ob_implicit_flush(false);
  63. $return = $this->_instance->call('foobar', array('param1', 'param2'));
  64. $data = ob_get_contents();
  65. ob_end_clean();
  66. ob_implicit_flush(true);
  67. $this->assertEquals('bar', $return);
  68. $this->assertEquals('foo', $data);
  69. }
  70. public function testCallCorrectCall2()
  71. {
  72. ob_start();
  73. ob_implicit_flush(false);
  74. $return = $this->_instance->call('foobar', array('param3', 'param4'));
  75. $data = ob_get_contents();
  76. ob_end_clean();
  77. ob_implicit_flush(true);
  78. $this->assertEquals('foobar_return(param3, param4)', $return);
  79. $this->assertEquals('foobar_output(param3, param4)', $data);
  80. }
  81. public function testCallCorrectCall3()
  82. {
  83. // cacheByDefault = false
  84. $this->_instance->setOption('cache_by_default', false);
  85. ob_start();
  86. ob_implicit_flush(false);
  87. $return = $this->_instance->call('foobar', array('param1', 'param2'));
  88. $data = ob_get_contents();
  89. ob_end_clean();
  90. ob_implicit_flush(true);
  91. $this->assertEquals('foobar_return(param1, param2)', $return);
  92. $this->assertEquals('foobar_output(param1, param2)', $data);
  93. }
  94. public function testCallCorrectCall4()
  95. {
  96. // cacheByDefault = false
  97. // cachedFunctions = array('foobar')
  98. $this->_instance->setOption('cache_by_default', false);
  99. $this->_instance->setOption('cached_functions', array('foobar'));
  100. ob_start();
  101. ob_implicit_flush(false);
  102. $return = $this->_instance->call('foobar', array('param1', 'param2'));
  103. $data = ob_get_contents();
  104. ob_end_clean();
  105. ob_implicit_flush(true);
  106. $this->assertEquals('bar', $return);
  107. $this->assertEquals('foo', $data);
  108. }
  109. public function testCallCorrectCall5()
  110. {
  111. // cacheByDefault = true
  112. // nonCachedFunctions = array('foobar')
  113. $this->_instance->setOption('cache_by_default', true);
  114. $this->_instance->setOption('non_cached_functions', array('foobar'));
  115. ob_start();
  116. ob_implicit_flush(false);
  117. $return = $this->_instance->call('foobar', array('param1', 'param2'));
  118. $data = ob_get_contents();
  119. ob_end_clean();
  120. ob_implicit_flush(true);
  121. $this->assertEquals('foobar_return(param1, param2)', $return);
  122. $this->assertEquals('foobar_output(param1, param2)', $data);
  123. }
  124. public function testCallWithABadSyntax1()
  125. {
  126. try {
  127. $this->_instance->call(1, array());
  128. } catch (Zend_Cache_Exception $e) {
  129. return;
  130. }
  131. $this->fail('Zend_Cache_Exception was expected but not thrown');
  132. }
  133. public function testCallWithABadSyntax2()
  134. {
  135. try {
  136. $this->_instance->call('foo', 1);
  137. } catch (Zend_Cache_Exception $e) {
  138. return;
  139. }
  140. $this->fail('Zend_Cache_Exception was expected but not thrown');
  141. }
  142. }