/tests/ZendTest/Cache/Storage/Adapter/MemcachedResourceManagerTest.php

https://github.com/cgmartin/zf2 · PHP · 253 lines · 181 code · 23 blank · 49 comment · 6 complexity · 701451084268aa617f9b259e609cadb0 MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace ZendTest\Cache\Storage\Adapter;
  10. use Zend\Cache\Storage\Adapter\MemcachedResourceManager;
  11. /**
  12. * PHPUnit test case
  13. */
  14. /**
  15. * @group Zend_Cache
  16. */
  17. class MemcachedResourceManagerTest extends \PHPUnit_Framework_TestCase
  18. {
  19. /**
  20. * The resource manager
  21. *
  22. * @var MemcachedResourceManager
  23. */
  24. protected $resourceManager;
  25. public function setUp()
  26. {
  27. $this->resourceManager = new MemcachedResourceManager();
  28. }
  29. /**
  30. * Data provider to test valid resources
  31. *
  32. * Returns an array of the following structure:
  33. * array(array(
  34. * <string resource id>,
  35. * <mixed input resource>,
  36. * <string normalized persistent id>,
  37. * <array normalized lib options>,
  38. * <array normalized server list>
  39. * )[, ...])
  40. *
  41. * @return array
  42. */
  43. public function validResourceProvider()
  44. {
  45. $data = array(
  46. // empty resource
  47. array(
  48. 'testEmptyResource',
  49. array(),
  50. '',
  51. array(),
  52. array(),
  53. ),
  54. // stringify persistent id
  55. array(
  56. 'testStringifyPersistentId',
  57. array('persistent_id' => 1234),
  58. '1234',
  59. array(),
  60. array(),
  61. ),
  62. // servers given as string
  63. array(
  64. 'testServersGivenAsString',
  65. array(
  66. 'servers' => '127.0.0.1:1234,127.0.0.1,192.1.0.1?weight=3,localhost,127.0.0.1:11211?weight=0',
  67. ),
  68. '',
  69. array(
  70. array('host' => '127.0.0.1', 'port' => 1234, 'weight' => 0),
  71. array('host' => '127.0.0.1', 'port' => 11211, 'weight' => 0),
  72. array('host' => '192.1.0.1', 'port' => 11211, 'weight' => 3),
  73. array('host' => 'localhost', 'port' => 11211, 'weight' => 0),
  74. ),
  75. array(),
  76. ),
  77. // servers given as list of strings
  78. array(
  79. 'testServersGivenAsListOfStrings',
  80. array(
  81. 'servers' => array(
  82. '127.0.0.1:1234',
  83. '127.0.0.1',
  84. '192.1.0.1?weight=3',
  85. 'localhost',
  86. '127.0.0.1:11211?weight=0'
  87. ),
  88. ),
  89. '',
  90. array(
  91. array('host' => '127.0.0.1', 'port' => 1234, 'weight' => 0),
  92. array('host' => '127.0.0.1', 'port' => 11211, 'weight' => 0),
  93. array('host' => '192.1.0.1', 'port' => 11211, 'weight' => 3),
  94. array('host' => 'localhost', 'port' => 11211, 'weight' => 0),
  95. ),
  96. array(),
  97. ),
  98. // servers given as list of arrays
  99. array(
  100. 'testServersGivenAsListOfArrays',
  101. array(
  102. 'servers' => array(
  103. array('127.0.0.1', 1234),
  104. array('127.0.0.1'),
  105. array('192.1.0.1', 11211, 3),
  106. array('localhost'),
  107. array('127.0.0.1', 11211, 0),
  108. ),
  109. ),
  110. '',
  111. array(
  112. array('host' => '127.0.0.1', 'port' => 1234, 'weight' => 0),
  113. array('host' => '127.0.0.1', 'port' => 11211, 'weight' => 0),
  114. array('host' => '192.1.0.1', 'port' => 11211, 'weight' => 3),
  115. array('host' => 'localhost', 'port' => 11211, 'weight' => 0),
  116. ),
  117. array(),
  118. ),
  119. // servers given as list of assoc arrays
  120. array(
  121. 'testServersGivenAsListOfAssocArrays',
  122. array(
  123. 'servers' => array(
  124. array(
  125. 'host' => '127.0.0.1',
  126. 'port' => 1234,
  127. ),
  128. array(
  129. 'host' => '127.0.0.1',
  130. ),
  131. array(
  132. 'host' => '192.1.0.1',
  133. 'weight' => 3,
  134. ),
  135. array(
  136. 'host' => 'localhost',
  137. ),
  138. array(
  139. 'host' => '127.0.0.1',
  140. 'port' => 11211,
  141. 'weight' => 0,
  142. ),
  143. ),
  144. ),
  145. '',
  146. array(
  147. array('host' => '127.0.0.1', 'port' => 1234, 'weight' => 0),
  148. array('host' => '127.0.0.1', 'port' => 11211, 'weight' => 0),
  149. array('host' => '192.1.0.1', 'port' => 11211, 'weight' => 3),
  150. array('host' => 'localhost', 'port' => 11211, 'weight' => 0),
  151. ),
  152. array(),
  153. ),
  154. // lib options given as name
  155. array(
  156. 'testLibOptionsGivenAsName',
  157. array(
  158. 'lib_options' => array(
  159. 'COMPRESSION' => false,
  160. 'PREFIX_KEY' => 'test_',
  161. ),
  162. ),
  163. '',
  164. array(),
  165. class_exists('Memcached', false) ? array(
  166. \Memcached::OPT_COMPRESSION => false,
  167. \Memcached::OPT_PREFIX_KEY => 'test_',
  168. ) : array(),
  169. ),
  170. // lib options given as constant value
  171. array(
  172. 'testLibOptionsGivenAsName',
  173. array(
  174. 'lib_options' => class_exists('Memcached', false) ? array(
  175. \Memcached::OPT_COMPRESSION => false,
  176. \Memcached::OPT_PREFIX_KEY => 'test_',
  177. ) : array(),
  178. ),
  179. '',
  180. array(),
  181. class_exists('Memcached', false) ? array(
  182. \Memcached::OPT_COMPRESSION => false,
  183. \Memcached::OPT_PREFIX_KEY => 'test_',
  184. ) : array(),
  185. ),
  186. );
  187. return $data;
  188. }
  189. /**
  190. * @dataProvider validResourceProvider
  191. * @param string $resourceId
  192. * @param mixed $resource
  193. * @param string $expectedPersistentId
  194. * @param array $expectedServers
  195. * @param array $expectedLibOptions
  196. */
  197. public function testValidResources($resourceId, $resource, $expectedPersistentId, $expectedServers, $expectedLibOptions)
  198. {
  199. // php-memcached is required to set libmemcached options
  200. if (is_array($resource) && isset($resource['lib_options']) && count($resource['lib_options']) > 0) {
  201. if (!class_exists('Memcached', false)) {
  202. $this->setExpectedException('Zend\Cache\Exception\InvalidArgumentException', 'Unknown libmemcached option');
  203. }
  204. }
  205. $this->assertSame($this->resourceManager, $this->resourceManager->setResource($resourceId, $resource));
  206. $this->assertTrue($this->resourceManager->hasResource($resourceId));
  207. $this->assertSame($expectedPersistentId, $this->resourceManager->getPersistentId($resourceId));
  208. $this->assertEquals($expectedServers, $this->resourceManager->getServers($resourceId));
  209. $this->assertEquals($expectedLibOptions, $this->resourceManager->getLibOptions($resourceId));
  210. $this->assertSame($this->resourceManager, $this->resourceManager->removeResource($resourceId));
  211. $this->assertFalse($this->resourceManager->hasResource($resourceId));
  212. }
  213. public function testSetLibOptionsOnExistingResource()
  214. {
  215. $memcachedInstalled = class_exists('Memcached', false);
  216. $libOptions = array('compression' => false);
  217. $resourceId = 'testResourceId';
  218. $resourceMock = $this->getMock('Memcached', array('setOptions'));
  219. if (!$memcachedInstalled) {
  220. $this->setExpectedException('Zend\Cache\Exception\InvalidArgumentException');
  221. } else {
  222. $resourceMock
  223. ->expects($this->once())
  224. ->method('setOptions')
  225. ->with($this->isType('array'));
  226. }
  227. $this->resourceManager->setResource($resourceId, $resourceMock);
  228. $this->resourceManager->setLibOptions($resourceId, $libOptions);
  229. }
  230. }