ConnectionFactoryTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 ConnectionFactoryTest extends PredisTestCase
  16. {
  17. /**
  18. * @group disconnected
  19. */
  20. public function testImplementsCorrectInterface()
  21. {
  22. $factory = new ConnectionFactory();
  23. $this->assertInstanceOf('Predis\Connection\ConnectionFactoryInterface', $factory);
  24. }
  25. /**
  26. * @group disconnected
  27. */
  28. public function testCreateConnection()
  29. {
  30. $factory = new ConnectionFactory();
  31. $tcp = new ConnectionParameters(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 ConnectionParameters(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 ConnectionFactory();
  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 ConnectionFactory();
  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 ConnectionFactory();
  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\ServerProfileInterface');
  96. $profile->expects($this->never())->method('create');
  97. $factory = new ConnectionFactory($profile);
  98. $parameters = new ConnectionParameters();
  99. $connection = $factory->create($parameters);
  100. $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $connection);
  101. }
  102. /**
  103. * @group disconnected
  104. */
  105. public function testCreateConnectionWithInitializationCommands()
  106. {
  107. $test = $this;
  108. $database = 15;
  109. $password = 'foobar';
  110. $commands = array();
  111. $createCommand = function ($id, $arguments) use ($test, &$commands) {
  112. $commands[$id] = $arguments;
  113. $command = $test->getMock('Predis\Command\CommandInterface');
  114. return $command;
  115. };
  116. $profile = $this->getMock('Predis\Profile\ServerProfileInterface');
  117. $profile->expects($this->exactly(2))
  118. ->method('createCommand')
  119. ->with($this->isType('string'), $this->isType('array'))
  120. ->will($this->returnCallback($createCommand));
  121. $factory = new ConnectionFactory($profile);
  122. $parameters = new ConnectionParameters(array('database' => $database, 'password' => $password));
  123. $connection = $factory->create($parameters);
  124. $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $connection);
  125. $this->assertEquals(2, count($commands)); // TODO: assertCount()?
  126. $this->assertEquals(array($database), $commands['select']);
  127. $this->assertEquals(array($password), $commands['auth']);
  128. }
  129. /**
  130. * @group disconnected
  131. * @todo This test smells but there's no other way around it right now.
  132. */
  133. public function testCreateConnectionWithDatabaseAndPasswordButNoProfile()
  134. {
  135. $parameters = new ConnectionParameters(array('database' => 0, 'password' => 'foobar'));
  136. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  137. $connection->expects($this->never())
  138. ->method('getParameters')
  139. ->will($this->returnValue($parameters));
  140. $connection->expects($this->never())
  141. ->method('pushInitCommand');
  142. $factory = new ConnectionFactory();
  143. $reflection = new \ReflectionObject($factory);
  144. $prepareConnection = $reflection->getMethod('prepareConnection');
  145. $prepareConnection->setAccessible(true);
  146. $prepareConnection->invoke($factory, $connection);
  147. }
  148. /**
  149. * @group disconnected
  150. */
  151. public function testCreateUndefinedConnection()
  152. {
  153. $scheme = 'unknown';
  154. $this->setExpectedException('InvalidArgumentException', "Unknown connection scheme: $scheme");
  155. $factory = new ConnectionFactory();
  156. $factory->create(new ConnectionParameters(array('scheme' => $scheme)));
  157. }
  158. /**
  159. * @group disconnected
  160. */
  161. public function testDefineConnectionWithFQN()
  162. {
  163. list(, $connectionClass) = $this->getMockConnectionClass();
  164. $parameters = new ConnectionParameters(array('scheme' => 'foobar'));
  165. $factory = new ConnectionFactory();
  166. $factory->define($parameters->scheme, $connectionClass);
  167. $connection = $factory->create($parameters);
  168. $this->assertInstanceOf($connectionClass, $connection);
  169. }
  170. /**
  171. * @group disconnected
  172. */
  173. public function testDefineConnectionWithCallable()
  174. {
  175. list(, $connectionClass) = $this->getMockConnectionClass();
  176. $parameters = new ConnectionParameters(array('scheme' => 'foobar'));
  177. $initializer = function ($parameters) use ($connectionClass) {
  178. return new $connectionClass($parameters);
  179. };
  180. $initializerMock = $this->getMock('stdClass', array('__invoke'));
  181. $initializerMock->expects($this->exactly(2))
  182. ->method('__invoke')
  183. ->with($parameters)
  184. ->will($this->returnCallback($initializer));
  185. $factory = new ConnectionFactory();
  186. $factory->define($parameters->scheme, $initializerMock);
  187. $connection1 = $factory->create($parameters);
  188. $connection2 = $factory->create($parameters);
  189. $this->assertInstanceOf($connectionClass, $connection1);
  190. $this->assertInstanceOf($connectionClass, $connection2);
  191. $this->assertNotSame($connection1, $connection2);
  192. }
  193. /**
  194. * @group disconnected
  195. */
  196. public function testDefineConnectionWithInvalidArgument()
  197. {
  198. $this->setExpectedException('InvalidArgumentException');
  199. $factory = new ConnectionFactory();
  200. $factory->define('foobar', new \stdClass());
  201. }
  202. /**
  203. * @group disconnected
  204. */
  205. public function testUndefineDefinedConnection()
  206. {
  207. $this->setExpectedException('InvalidArgumentException', 'Unknown connection scheme: tcp');
  208. $factory = new ConnectionFactory();
  209. $factory->undefine('tcp');
  210. $factory->create('tcp://127.0.0.1');
  211. }
  212. /**
  213. * @group disconnected
  214. */
  215. public function testUndefineUndefinedConnection()
  216. {
  217. $factory = new ConnectionFactory();
  218. $factory->undefine('unknown');
  219. $connection = $factory->create('tcp://127.0.0.1');
  220. $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $connection);
  221. }
  222. /**
  223. * @group disconnected
  224. */
  225. public function testDefineAndUndefineConnection()
  226. {
  227. list(, $connectionClass) = $this->getMockConnectionClass();
  228. $factory = new ConnectionFactory();
  229. $factory->define('redis', $connectionClass);
  230. $this->assertInstanceOf($connectionClass, $factory->create('redis://127.0.0.1'));
  231. $factory->undefine('redis');
  232. $this->setExpectedException('InvalidArgumentException', 'Unknown connection scheme: redis');
  233. $factory->create('redis://127.0.0.1');
  234. }
  235. /**
  236. * @group disconnected
  237. */
  238. public function testAggregatedConnectionSkipCreationOnConnectionInstance()
  239. {
  240. list(, $connectionClass) = $this->getMockConnectionClass();
  241. $cluster = $this->getMock('Predis\Connection\ClusterConnectionInterface');
  242. $cluster->expects($this->exactly(2))
  243. ->method('add')
  244. ->with($this->isInstanceOf('Predis\Connection\SingleConnectionInterface'));
  245. $factory = $this->getMock('Predis\Connection\ConnectionFactory', array('create'));
  246. $factory->expects($this->never())
  247. ->method('create');
  248. $factory->createAggregated($cluster, array(new $connectionClass(), new $connectionClass()));
  249. }
  250. /**
  251. * @group disconnected
  252. */
  253. public function testAggregatedConnectionWithMixedParameters()
  254. {
  255. list(, $connectionClass) = $this->getMockConnectionClass();
  256. $cluster = $this->getMock('Predis\Connection\ClusterConnectionInterface');
  257. $cluster->expects($this->exactly(4))
  258. ->method('add')
  259. ->with($this->isInstanceOf('Predis\Connection\SingleConnectionInterface'));
  260. $factory = $this->getMock('Predis\Connection\ConnectionFactory', array('create'));
  261. $factory->expects($this->exactly(3))
  262. ->method('create')
  263. ->will($this->returnCallback(function ($_) use ($connectionClass) {
  264. return new $connectionClass;
  265. }));
  266. $factory->createAggregated($cluster, array(null, 'tcp://127.0.0.1', array('scheme' => 'tcp'), new $connectionClass()));
  267. }
  268. /**
  269. * @group disconnected
  270. */
  271. public function testAggregatedConnectionWithEmptyListOfParameters()
  272. {
  273. $cluster = $this->getMock('Predis\Connection\ClusterConnectionInterface');
  274. $cluster->expects($this->never())->method('add');
  275. $factory = $this->getMock('Predis\Connection\ConnectionFactory', array('create'));
  276. $factory->expects($this->never())->method('create');
  277. $factory->createAggregated($cluster, array());
  278. }
  279. /**
  280. * @group disconnected
  281. * @todo We might want to add a test for SingleConnectionInterface::pushInitCommand().
  282. */
  283. public function testAggregatedConnectionWithServerProfileArgument()
  284. {
  285. list(, $connectionClass) = $this->getMockConnectionClass();
  286. $cluster = $this->getMock('Predis\Connection\ClusterConnectionInterface');
  287. $profile = $this->getMock('Predis\Profile\ServerProfileInterface');
  288. $factory = $this->getMock('Predis\Connection\ConnectionFactory', array('create'), array($profile));
  289. $factory->expects($this->exactly(2))
  290. ->method('create')
  291. ->with($this->anything())
  292. ->will($this->returnCallback(function ($_) use ($connectionClass) {
  293. return new $connectionClass();
  294. }));
  295. $nodes = array('tcp://127.0.0.1:7001?password=foo', 'tcp://127.0.0.1:7002?password=bar');
  296. $factory->createAggregated($cluster, $nodes);
  297. }
  298. // ******************************************************************** //
  299. // ---- HELPER METHODS ------------------------------------------------ //
  300. // ******************************************************************** //
  301. /**
  302. * Returns a mocked Predis\Connection\SingleConnectionInterface.
  303. *
  304. * @return Array Mock instance and class name
  305. */
  306. protected function getMockConnectionClass()
  307. {
  308. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  309. return array($connection, get_class($connection));
  310. }
  311. }