FactoryTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) Daniele Alessandri <suppakilla@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Predis\Connection;
  11. use PredisTestCase;
  12. /**
  13. *
  14. */
  15. class FactoryTest extends PredisTestCase
  16. {
  17. /**
  18. * @group disconnected
  19. */
  20. public function testImplementsCorrectInterface()
  21. {
  22. $factory = new Factory();
  23. $this->assertInstanceOf('Predis\Connection\FactoryInterface', $factory);
  24. }
  25. /**
  26. * @group disconnected
  27. */
  28. public function testCreateConnection()
  29. {
  30. $factory = new Factory();
  31. $tcp = new Parameters(array(
  32. 'scheme' => 'tcp',
  33. 'host' => 'locahost',
  34. ));
  35. $connection = $factory->create($tcp);
  36. $parameters = $connection->getParameters();
  37. $this->assertInstanceOf('Predis\Connection\StreamConnection', $connection);
  38. $this->assertEquals($tcp->scheme, $parameters->scheme);
  39. $this->assertEquals($tcp->host, $parameters->host);
  40. $this->assertEquals($tcp->database, $parameters->database);
  41. $unix = new Parameters(array(
  42. 'scheme' => 'unix',
  43. 'path' => '/tmp/redis.sock',
  44. ));
  45. $connection = $factory->create($tcp);
  46. $parameters = $connection->getParameters();
  47. $this->assertInstanceOf('Predis\Connection\StreamConnection', $connection);
  48. $this->assertEquals($tcp->scheme, $parameters->scheme);
  49. $this->assertEquals($tcp->database, $parameters->database);
  50. }
  51. /**
  52. * @group disconnected
  53. */
  54. public function testCreateConnectionWithNullParameters()
  55. {
  56. $factory = new Factory();
  57. $connection = $factory->create(null);
  58. $parameters = $connection->getParameters();
  59. $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $connection);
  60. $this->assertEquals('tcp', $parameters->scheme);
  61. $this->assertFalse(isset($parameters->custom));
  62. $this->assertNull($parameters->custom);
  63. }
  64. /**
  65. * @group disconnected
  66. */
  67. public function testCreateConnectionWithArrayParameters()
  68. {
  69. $factory = new Factory();
  70. $connection = $factory->create(array('scheme' => 'tcp', 'custom' => 'foobar'));
  71. $parameters = $connection->getParameters();
  72. $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $connection);
  73. $this->assertEquals('tcp', $parameters->scheme);
  74. $this->assertTrue(isset($parameters->custom));
  75. $this->assertSame('foobar', $parameters->custom);
  76. }
  77. /**
  78. * @group disconnected
  79. */
  80. public function testCreateConnectionWithStringURI()
  81. {
  82. $factory = new Factory();
  83. $connection = $factory->create('tcp://127.0.0.1?custom=foobar');
  84. $parameters = $connection->getParameters();
  85. $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $connection);
  86. $this->assertEquals('tcp', $parameters->scheme);
  87. $this->assertTrue(isset($parameters->custom));
  88. $this->assertSame('foobar', $parameters->custom);
  89. }
  90. /**
  91. * @group disconnected
  92. */
  93. public function testCreateConnectionWithoutInitializationCommands()
  94. {
  95. $profile = $this->getMock('Predis\Profile\ProfileInterface');
  96. $profile->expects($this->never())->method('create');
  97. $factory = new Factory($profile);
  98. $parameters = new Parameters();
  99. $connection = $factory->create($parameters);
  100. $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $connection);
  101. }
  102. /**
  103. * @group disconnected
  104. * @todo This test smells but there's no other way around it right now.
  105. */
  106. public function testCreateConnectionWithInitializationCommands()
  107. {
  108. $parameters = new Parameters(array(
  109. 'database' => '0',
  110. 'password' => 'foobar'
  111. ));
  112. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  113. $connection->expects($this->once())
  114. ->method('getParameters')
  115. ->will($this->returnValue($parameters));
  116. $connection->expects($this->at(1))
  117. ->method('addConnectCommand')
  118. ->with($this->isRedisCommand('AUTH', array('foobar')));
  119. $connection->expects($this->at(2))
  120. ->method('addConnectCommand')
  121. ->with($this->isRedisCommand('SELECT', array(0)));
  122. $factory = new Factory();
  123. $reflection = new \ReflectionObject($factory);
  124. $prepareConnection = $reflection->getMethod('prepareConnection');
  125. $prepareConnection->setAccessible(true);
  126. $prepareConnection->invoke($factory, $connection);
  127. }
  128. /**
  129. * @group disconnected
  130. */
  131. public function testCreateUndefinedConnection()
  132. {
  133. $scheme = 'unknown';
  134. $this->setExpectedException('InvalidArgumentException', "Unknown connection scheme: $scheme");
  135. $factory = new Factory();
  136. $factory->create(new Parameters(array('scheme' => $scheme)));
  137. }
  138. /**
  139. * @group disconnected
  140. */
  141. public function testDefineConnectionWithFQN()
  142. {
  143. list(, $connectionClass) = $this->getMockConnectionClass();
  144. $parameters = new Parameters(array('scheme' => 'foobar'));
  145. $factory = new Factory();
  146. $factory->define($parameters->scheme, $connectionClass);
  147. $connection = $factory->create($parameters);
  148. $this->assertInstanceOf($connectionClass, $connection);
  149. }
  150. /**
  151. * @group disconnected
  152. */
  153. public function testDefineConnectionWithCallable()
  154. {
  155. list(, $connectionClass) = $this->getMockConnectionClass();
  156. $parameters = new Parameters(array('scheme' => 'foobar'));
  157. $initializer = function ($parameters) use ($connectionClass) {
  158. return new $connectionClass($parameters);
  159. };
  160. $initializerMock = $this->getMock('stdClass', array('__invoke'));
  161. $initializerMock->expects($this->exactly(2))
  162. ->method('__invoke')
  163. ->with($parameters)
  164. ->will($this->returnCallback($initializer));
  165. $factory = new Factory();
  166. $factory->define($parameters->scheme, $initializerMock);
  167. $connection1 = $factory->create($parameters);
  168. $connection2 = $factory->create($parameters);
  169. $this->assertInstanceOf($connectionClass, $connection1);
  170. $this->assertInstanceOf($connectionClass, $connection2);
  171. $this->assertNotSame($connection1, $connection2);
  172. }
  173. /**
  174. * @group disconnected
  175. */
  176. public function testDefineConnectionWithInvalidArgument()
  177. {
  178. $this->setExpectedException('InvalidArgumentException');
  179. $factory = new Factory();
  180. $factory->define('foobar', new \stdClass());
  181. }
  182. /**
  183. * @group disconnected
  184. */
  185. public function testUndefineDefinedConnection()
  186. {
  187. $this->setExpectedException('InvalidArgumentException', 'Unknown connection scheme: tcp');
  188. $factory = new Factory();
  189. $factory->undefine('tcp');
  190. $factory->create('tcp://127.0.0.1');
  191. }
  192. /**
  193. * @group disconnected
  194. */
  195. public function testUndefineUndefinedConnection()
  196. {
  197. $factory = new Factory();
  198. $factory->undefine('unknown');
  199. $connection = $factory->create('tcp://127.0.0.1');
  200. $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $connection);
  201. }
  202. /**
  203. * @group disconnected
  204. */
  205. public function testDefineAndUndefineConnection()
  206. {
  207. list(, $connectionClass) = $this->getMockConnectionClass();
  208. $factory = new Factory();
  209. $factory->define('redis', $connectionClass);
  210. $this->assertInstanceOf($connectionClass, $factory->create('redis://127.0.0.1'));
  211. $factory->undefine('redis');
  212. $this->setExpectedException('InvalidArgumentException', 'Unknown connection scheme: redis');
  213. $factory->create('redis://127.0.0.1');
  214. }
  215. /**
  216. * @group disconnected
  217. */
  218. public function testAggregateConnectionSkipCreationOnConnectionInstance()
  219. {
  220. list(, $connectionClass) = $this->getMockConnectionClass();
  221. $cluster = $this->getMock('Predis\Connection\ClusterConnectionInterface');
  222. $cluster->expects($this->exactly(2))
  223. ->method('add')
  224. ->with($this->isInstanceOf('Predis\Connection\SingleConnectionInterface'));
  225. $factory = $this->getMock('Predis\Connection\Factory', array('create'));
  226. $factory->expects($this->never())
  227. ->method('create');
  228. $factory->aggregate($cluster, array(new $connectionClass(), new $connectionClass()));
  229. }
  230. /**
  231. * @group disconnected
  232. */
  233. public function testAggregateConnectionWithMixedParameters()
  234. {
  235. list(, $connectionClass) = $this->getMockConnectionClass();
  236. $cluster = $this->getMock('Predis\Connection\ClusterConnectionInterface');
  237. $cluster->expects($this->exactly(4))
  238. ->method('add')
  239. ->with($this->isInstanceOf('Predis\Connection\SingleConnectionInterface'));
  240. $factory = $this->getMock('Predis\Connection\Factory', array('create'));
  241. $factory->expects($this->exactly(3))
  242. ->method('create')
  243. ->will($this->returnCallback(function ($_) use ($connectionClass) {
  244. return new $connectionClass;
  245. }));
  246. $factory->aggregate($cluster, array(null, 'tcp://127.0.0.1', array('scheme' => 'tcp'), new $connectionClass()));
  247. }
  248. /**
  249. * @group disconnected
  250. */
  251. public function testAggregateConnectionWithEmptyListOfParameters()
  252. {
  253. $cluster = $this->getMock('Predis\Connection\ClusterConnectionInterface');
  254. $cluster->expects($this->never())->method('add');
  255. $factory = $this->getMock('Predis\Connection\Factory', array('create'));
  256. $factory->expects($this->never())->method('create');
  257. $factory->aggregate($cluster, array());
  258. }
  259. // ******************************************************************** //
  260. // ---- HELPER METHODS ------------------------------------------------ //
  261. // ******************************************************************** //
  262. /**
  263. * Returns a mocked Predis\Connection\SingleConnectionInterface.
  264. *
  265. * @return array Mock instance and class name
  266. */
  267. protected function getMockConnectionClass()
  268. {
  269. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  270. return array($connection, get_class($connection));
  271. }
  272. }