PageRenderTime 35ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/DevApp/library/ServerLibraries/ZendFramework/1.7/tests/Zend/Cache/ClassFrontendTest.php

http://firephp.googlecode.com/
PHP | 222 lines | 176 code | 24 blank | 22 comment | 2 complexity | 1dc449e6423e8772c8daee2914a74356 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, MIT, Apache-2.0
  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/Class.php';
  11. require_once 'Zend/Cache/Backend/Test.php';
  12. /**
  13. * PHPUnit test case
  14. */
  15. require_once 'PHPUnit/Framework/TestCase.php';
  16. class test {
  17. private $_string = 'hello !';
  18. public static function foobar($param1, $param2) {
  19. echo "foobar_output($param1, $param2)";
  20. return "foobar_return($param1, $param2)";
  21. }
  22. public function foobar2($param1, $param2) {
  23. echo($this->_string);
  24. echo "foobar2_output($param1, $param2)";
  25. return "foobar2_return($param1, $param2)";
  26. }
  27. }
  28. /**
  29. * @package Zend_Cache
  30. * @subpackage UnitTests
  31. */
  32. class Zend_Cache_ClassFrontendTest extends PHPUnit_Framework_TestCase {
  33. private $_instance1;
  34. private $_instance2;
  35. public function setUp()
  36. {
  37. if (!$this->_instance1) {
  38. $options1 = array(
  39. 'cached_entity' => 'test'
  40. );
  41. $this->_instance1 = new Zend_Cache_Frontend_Class($options1);
  42. $this->_backend1 = new Zend_Cache_Backend_Test();
  43. $this->_instance1->setBackend($this->_backend1);
  44. }
  45. if (!$this->_instance2) {
  46. $options2 = array(
  47. 'cached_entity' => new test()
  48. );
  49. $this->_instance2 = new Zend_Cache_Frontend_Class($options2);
  50. $this->_backend2 = new Zend_Cache_Backend_Test();
  51. $this->_instance2->setBackend($this->_backend2);
  52. }
  53. }
  54. public function tearDown()
  55. {
  56. unset($this->_instance1);
  57. unset($this->_instance2);
  58. }
  59. public function testConstructorCorrectCall1()
  60. {
  61. $options = array(
  62. 'cache_by_default' => false,
  63. 'cached_entity' => 'test'
  64. );
  65. $test = new Zend_Cache_Frontend_Class($options);
  66. }
  67. public function testConstructorCorrectCall2()
  68. {
  69. $options = array(
  70. 'cache_by_default' => false,
  71. 'cached_entity' => new test()
  72. );
  73. $test = new Zend_Cache_Frontend_Class($options);
  74. }
  75. public function testConstructorBadCall()
  76. {
  77. $options = array(
  78. 'cached_entity' => new test(),
  79. 0 => true,
  80. );
  81. try {
  82. $test = new Zend_Cache_Frontend_Class($options);
  83. } catch (Zend_Cache_Exception $e) {
  84. return;
  85. }
  86. $this->fail('Zend_Cache_Exception was expected but not thrown');
  87. }
  88. public function testCallCorrectCall1()
  89. {
  90. ob_start();
  91. ob_implicit_flush(false);
  92. $return = $this->_instance1->foobar('param1', 'param2');
  93. $data = ob_get_contents();
  94. ob_end_clean();
  95. ob_implicit_flush(true);
  96. $this->assertEquals('bar', $return);
  97. $this->assertEquals('foo', $data);
  98. }
  99. public function testCallCorrectCall2()
  100. {
  101. ob_start();
  102. ob_implicit_flush(false);
  103. $return = $this->_instance1->foobar('param3', 'param4');
  104. $data = ob_get_contents();
  105. ob_end_clean();
  106. ob_implicit_flush(true);
  107. $this->assertEquals('foobar_return(param3, param4)', $return);
  108. $this->assertEquals('foobar_output(param3, param4)', $data);
  109. }
  110. public function testCallCorrectCall3()
  111. {
  112. ob_start();
  113. ob_implicit_flush(false);
  114. $return = $this->_instance2->foobar2('param1', 'param2');
  115. $data = ob_get_contents();
  116. ob_end_clean();
  117. ob_implicit_flush(true);
  118. $this->assertEquals('bar', $return);
  119. $this->assertEquals('foo', $data);
  120. }
  121. public function testCallCorrectCall4()
  122. {
  123. ob_start();
  124. ob_implicit_flush(false);
  125. $return = $this->_instance2->foobar2('param3', 'param4');
  126. $data = ob_get_contents();
  127. ob_end_clean();
  128. ob_implicit_flush(true);
  129. $this->assertEquals('foobar2_return(param3, param4)', $return);
  130. $this->assertEquals('hello !foobar2_output(param3, param4)', $data);
  131. }
  132. public function testCallCorrectCall5()
  133. {
  134. // cacheByDefault = false
  135. $this->_instance1->setOption('cache_by_default', false);
  136. ob_start();
  137. ob_implicit_flush(false);
  138. $return = $this->_instance1->foobar('param1', 'param2');
  139. $data = ob_get_contents();
  140. ob_end_clean();
  141. ob_implicit_flush(true);
  142. $this->assertEquals('foobar_return(param1, param2)', $return);
  143. $this->assertEquals('foobar_output(param1, param2)', $data);
  144. }
  145. public function testCallCorrectCall6()
  146. {
  147. // cacheByDefault = false
  148. // cachedMethods = array('foobar')
  149. $this->_instance1->setOption('cache_by_default', false);
  150. $this->_instance1->setOption('cached_methods', array('foobar'));
  151. ob_start();
  152. ob_implicit_flush(false);
  153. $return = $this->_instance1->foobar('param1', 'param2');
  154. $data = ob_get_contents();
  155. ob_end_clean();
  156. ob_implicit_flush(true);
  157. $this->assertEquals('bar', $return);
  158. $this->assertEquals('foo', $data);
  159. }
  160. public function testCallCorrectCall7()
  161. {
  162. // cacheByDefault = true
  163. // nonCachedMethods = array('foobar')
  164. $this->_instance1->setOption('cache_by_default', true);
  165. $this->_instance1->setOption('non_cached_methods', array('foobar'));
  166. ob_start();
  167. ob_implicit_flush(false);
  168. $return = $this->_instance1->foobar('param1', 'param2');
  169. $data = ob_get_contents();
  170. ob_end_clean();
  171. ob_implicit_flush(true);
  172. $this->assertEquals('foobar_return(param1, param2)', $return);
  173. $this->assertEquals('foobar_output(param1, param2)', $data);
  174. }
  175. public function testConstructorWithABadCachedEntity()
  176. {
  177. try {
  178. $options = array(
  179. 'cached_entity' => array()
  180. );
  181. $instance = new Zend_Cache_Frontend_Class($options);
  182. } catch (Zend_Cache_Exception $e) {
  183. return;
  184. }
  185. $this->fail('Zend_Cache_Exception was expected but not thrown');
  186. }
  187. /**
  188. * @group ZF-5034
  189. */
  190. public function testCallingConstructorWithInvalidOptionShouldNotRaiseException()
  191. {
  192. $options = array(
  193. 'cached_entity' => new test(),
  194. 'this_key_does_not_exist' => true
  195. );
  196. $test = new Zend_Cache_Frontend_Class($options);
  197. }
  198. }