PageRenderTime 58ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/Cache/Pattern/ObjectCacheTest.php

http://github.com/zendframework/zf2
PHP | 262 lines | 180 code | 44 blank | 38 comment | 2 complexity | 651a4dbe71873581f62c99ce9d5251dc MD5 | raw file
Possible License(s): BSD-3-Clause
  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-2010 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\Pattern;
  22. use Zend\Cache;
  23. /**
  24. * Test class
  25. */
  26. class TestObjectCache
  27. {
  28. /**
  29. * A counter how oftern the method "bar" was called
  30. */
  31. public static $fooCounter = 0;
  32. public $property = 'testProperty';
  33. public function bar()
  34. {
  35. ++self::$fooCounter;
  36. $args = func_get_args();
  37. echo 'foobar_output('.implode(', ', $args) . ') : ' . self::$fooCounter;
  38. return 'foobar_return('.implode(', ', $args) . ') : ' . self::$fooCounter;
  39. }
  40. public function __invoke()
  41. {
  42. return call_user_func_array(array($this, 'bar'), func_get_args());
  43. }
  44. public function emptyMethod() {}
  45. }
  46. /**
  47. * @category Zend
  48. * @package Zend_Cache
  49. * @subpackage UnitTests
  50. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  51. * @license http://framework.zend.com/license/new-bsd New BSD License
  52. * @group Zend_Cache
  53. */
  54. class ObjectCacheTest extends CommonPatternTest
  55. {
  56. /**
  57. * @var Zend\Cache\Storage\Adapter
  58. */
  59. protected $_storage;
  60. public function setUp()
  61. {
  62. $class = __NAMESPACE__ . '\TestObjectCache';
  63. $this->_storage = new Cache\Storage\Adapter\Memory();
  64. $this->_options = new Cache\Pattern\PatternOptions(array(
  65. 'object' => new $class(),
  66. 'storage' => $this->_storage,
  67. ));
  68. $this->_pattern = new Cache\Pattern\ObjectCache();
  69. $this->_pattern->setOptions($this->_options);
  70. parent::setUp();
  71. }
  72. public function tearDown()
  73. {
  74. parent::tearDown();
  75. }
  76. public function testCallEnabledCacheOutputByDefault()
  77. {
  78. $this->_testCall(
  79. 'bar',
  80. array('testCallEnabledCacheOutputByDefault', 'arg2')
  81. );
  82. }
  83. public function testCallDisabledCacheOutput()
  84. {
  85. $this->_options->setCacheOutput(false);
  86. $this->_testCall(
  87. 'bar',
  88. array('testCallDisabledCacheOutput', 'arg2')
  89. );
  90. }
  91. public function testCallInvoke()
  92. {
  93. $this->_options->setCacheOutput(false);
  94. $this->_testCall('__invoke', array('arg1', 'arg2'));
  95. }
  96. public function testGenerateKey()
  97. {
  98. $args = array('arg1', 2, 3.33, null);
  99. $generatedKey = $this->_pattern->generateKey('emptyMethod', $args);
  100. $usedKey = null;
  101. $this->_options->getStorage()->events()->attach('setItem.pre', function ($event) use (&$usedKey) {
  102. $params = $event->getParams();
  103. $usedKey = $params['key'];
  104. });
  105. $this->_pattern->call('emptyMethod', $args);
  106. $this->assertEquals($generatedKey, $usedKey);
  107. }
  108. public function testGenerateKeyWithPredefinedCallbackAndArgumentKey()
  109. {
  110. $args = array('arg1', 2, 3.33, null);
  111. $options = array(
  112. 'callback_key' => 'callback',
  113. 'argument_key' => 'arguments',
  114. );
  115. $expectedKey = md5($options['callback_key'].$options['argument_key']);
  116. $this->assertEquals(
  117. $expectedKey,
  118. $this->_pattern->generateKey('emptyMethod', $args, $options)
  119. );
  120. }
  121. public function testGenerateKeyWithPredefinedEntityKey()
  122. {
  123. $args = array('arg1', 2, 3.33, null);
  124. $options = array(
  125. 'entity_key' => 'object',
  126. 'argument_key' => 'arguments',
  127. );
  128. $callbackKey = $options['entity_key'].'::emptymethod';
  129. $expectedKey = md5($callbackKey.$options['argument_key']);
  130. $this->assertEquals(
  131. $expectedKey,
  132. $this->_pattern->generateKey('emptyMethod', $args, $options)
  133. );
  134. }
  135. public function testGenerateKeyWithClassObjectKey()
  136. {
  137. $args = array('arg1', 2, 3.33, null);
  138. $key = $this->_pattern->generateKey('emptyMethod', $args, array('callback_key' => 'test-object-cache::emptymethod'));
  139. $class = __NAMESPACE__ . '\TestObjectCache';
  140. $options = new Cache\Pattern\PatternOptions(array(
  141. 'object' => new $class(),
  142. 'storage' => $this->_storage,
  143. 'objectKey' => 'test-object-cache',
  144. ));
  145. $this->_pattern->setOptions($options);
  146. $keyWithClassObjectKey = $this->_pattern->generateKey('emptyMethod', $args);
  147. $this->assertEquals($key, $keyWithClassObjectKey);
  148. }
  149. public function testCallWithClassObjectKey()
  150. {
  151. $class = __NAMESPACE__ . '\TestObjectCache';
  152. $options = new Cache\Pattern\PatternOptions(array(
  153. 'object' => new $class(),
  154. 'storage' => $this->_storage,
  155. 'objectKey' => 'test-object-cache',
  156. ));
  157. $this->_pattern->setOptions($options);
  158. $args = array('arg1', 2, 3.33, null);
  159. $usedCallbackKey = null;
  160. $this->_options->getStorage()->events()->attach('setItem.pre', function ($event) use (&$usedCallbackKey) {
  161. $params = $event->getParams();
  162. $usedCallbackKey = $params['options']['callback_key'];
  163. });
  164. $this->_pattern->call('emptyMethod', $args);
  165. $this->assertEquals('test-object-cache::emptymethod', $usedCallbackKey);
  166. }
  167. public function testCallUnknownMethodException()
  168. {
  169. $this->setExpectedException('Zend\Cache\Exception\InvalidArgumentException');
  170. $this->_pattern->call('notExiststingMethod');
  171. }
  172. public function testSetProperty()
  173. {
  174. $this->_pattern->property = 'testSetProperty';
  175. $this->assertEquals('testSetProperty', $this->_options->getObject()->property);
  176. }
  177. public function testGetProperty()
  178. {
  179. $this->assertEquals($this->_options->getObject()->property, $this->_pattern->property);
  180. }
  181. public function testIssetProperty()
  182. {
  183. $this->assertTrue(isset($this->_pattern->property));
  184. $this->assertFalse(isset($this->_pattern->unknownProperty));
  185. }
  186. public function testUnsetProperty()
  187. {
  188. unset($this->_pattern->property);
  189. $this->assertFalse(isset($this->_pattern->property));
  190. }
  191. protected function _testCall($method, array $args)
  192. {
  193. $returnSpec = 'foobar_return(' . implode(', ', $args) . ') : ';
  194. $outputSpec = 'foobar_output(' . implode(', ', $args) . ') : ';
  195. $callback = array($this->_pattern, $method);
  196. // first call - not cached
  197. $firstCounter = TestObjectCache::$fooCounter + 1;
  198. ob_start();
  199. ob_implicit_flush(false);
  200. $return = call_user_func_array($callback, $args);
  201. $data = ob_get_contents();
  202. ob_end_clean();
  203. $this->assertEquals($returnSpec . $firstCounter, $return);
  204. $this->assertEquals($outputSpec . $firstCounter, $data);
  205. // second call - cached
  206. ob_start();
  207. ob_implicit_flush(false);
  208. $return = call_user_func_array($callback, $args);
  209. $data = ob_get_contents();
  210. ob_end_clean();
  211. $this->assertEquals($returnSpec . $firstCounter, $return);
  212. if ($this->_options->getCacheOutput()) {
  213. $this->assertEquals($outputSpec . $firstCounter, $data);
  214. } else {
  215. $this->assertEquals('', $data);
  216. }
  217. }
  218. }