ConnectionFactoryTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 \PHPUnit_Framework_TestCase as StandardTestCase;
  12. /**
  13. *
  14. */
  15. class ConnectionFactoryTest extends StandardTestCase
  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();
  98. $parameters = new ConnectionParameters();
  99. $connection = $factory->create($parameters, $profile);
  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();
  122. $parameters = new ConnectionParameters(array('database' => $database, 'password' => $password));
  123. $connection = $factory->create($parameters, $profile);
  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. */
  132. public function testCreateUndefinedConnection()
  133. {
  134. $scheme = 'unknown';
  135. $this->setExpectedException('InvalidArgumentException', "Unknown connection scheme: $scheme");
  136. $factory = new ConnectionFactory();
  137. $factory->create(new ConnectionParameters(array('scheme' => $scheme)));
  138. }
  139. /**
  140. * @group disconnected
  141. */
  142. public function testDefineConnectionWithFQN()
  143. {
  144. list(, $connectionClass) = $this->getMockConnectionClass();
  145. $parameters = new ConnectionParameters(array('scheme' => 'foobar'));
  146. $factory = new ConnectionFactory();
  147. $factory->define($parameters->scheme, $connectionClass);
  148. $connection = $factory->create($parameters);
  149. $this->assertInstanceOf($connectionClass, $connection);
  150. }
  151. /**
  152. * @group disconnected
  153. */
  154. public function testDefineConnectionWithCallable()
  155. {
  156. list(, $connectionClass) = $this->getMockConnectionClass();
  157. $parameters = new ConnectionParameters(array('scheme' => 'foobar'));
  158. $initializer = function ($parameters) use ($connectionClass) {
  159. return new $connectionClass($parameters);
  160. };
  161. $initializerMock = $this->getMock('stdClass', array('__invoke'));
  162. $initializerMock->expects($this->exactly(2))
  163. ->method('__invoke')
  164. ->with($parameters)
  165. ->will($this->returnCallback($initializer));
  166. $factory = new ConnectionFactory();
  167. $factory->define($parameters->scheme, $initializerMock);
  168. $connection1 = $factory->create($parameters);
  169. $connection2 = $factory->create($parameters);
  170. $this->assertInstanceOf($connectionClass, $connection1);
  171. $this->assertInstanceOf($connectionClass, $connection2);
  172. $this->assertNotSame($connection1, $connection2);
  173. }
  174. /**
  175. * @group disconnected
  176. */
  177. public function testDefineConnectionWithInvalidArgument()
  178. {
  179. $this->setExpectedException('InvalidArgumentException');
  180. $factory = new ConnectionFactory();
  181. $factory->define('foobar', new \stdClass());
  182. }
  183. /**
  184. * @group disconnected
  185. */
  186. public function testUndefineDefinedConnection()
  187. {
  188. $this->setExpectedException('InvalidArgumentException', 'Unknown connection scheme: tcp');
  189. $factory = new ConnectionFactory();
  190. $factory->undefine('tcp');
  191. $factory->create('tcp://127.0.0.1');
  192. }
  193. /**
  194. * @group disconnected
  195. */
  196. public function testUndefineUndefinedConnection()
  197. {
  198. $factory = new ConnectionFactory();
  199. $factory->undefine('unknown');
  200. $connection = $factory->create('tcp://127.0.0.1');
  201. $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $connection);
  202. }
  203. /**
  204. * @group disconnected
  205. */
  206. public function testDefineAndUndefineConnection()
  207. {
  208. list(, $connectionClass) = $this->getMockConnectionClass();
  209. $factory = new ConnectionFactory();
  210. $factory->define('redis', $connectionClass);
  211. $this->assertInstanceOf($connectionClass, $factory->create('redis://127.0.0.1'));
  212. $factory->undefine('redis');
  213. $this->setExpectedException('InvalidArgumentException', 'Unknown connection scheme: redis');
  214. $factory->create('redis://127.0.0.1');
  215. }
  216. /**
  217. * @group disconnected
  218. */
  219. public function testAggregatedConnectionSkipCreationOnConnectionInstance()
  220. {
  221. list(, $connectionClass) = $this->getMockConnectionClass();
  222. $cluster = $this->getMock('Predis\Connection\ClusterConnectionInterface');
  223. $cluster->expects($this->exactly(2))
  224. ->method('add')
  225. ->with($this->isInstanceOf('Predis\Connection\SingleConnectionInterface'));
  226. $factory = $this->getMock('Predis\Connection\ConnectionFactory', array('create'));
  227. $factory->expects($this->never())
  228. ->method('create');
  229. $factory->createAggregated($cluster, array(new $connectionClass(), new $connectionClass()));
  230. }
  231. /**
  232. * @group disconnected
  233. */
  234. public function testAggregatedConnectionWithMixedParameters()
  235. {
  236. list(, $connectionClass) = $this->getMockConnectionClass();
  237. $cluster = $this->getMock('Predis\Connection\ClusterConnectionInterface');
  238. $cluster->expects($this->exactly(4))
  239. ->method('add')
  240. ->with($this->isInstanceOf('Predis\Connection\SingleConnectionInterface'));
  241. $factory = $this->getMock('Predis\Connection\ConnectionFactory', array('create'));
  242. $factory->expects($this->exactly(3))
  243. ->method('create')
  244. ->will($this->returnCallback(function ($_, $_) use ($connectionClass) {
  245. return new $connectionClass;
  246. }));
  247. $factory->createAggregated($cluster, array(null, 'tcp://127.0.0.1', array('scheme' => 'tcp'), new $connectionClass()));
  248. }
  249. /**
  250. * @group disconnected
  251. */
  252. public function testAggregatedConnectionWithEmptyListOfParameters()
  253. {
  254. $cluster = $this->getMock('Predis\Connection\ClusterConnectionInterface');
  255. $cluster->expects($this->never())->method('add');
  256. $factory = $this->getMock('Predis\Connection\ConnectionFactory', array('create'));
  257. $factory->expects($this->never())->method('create');
  258. $factory->createAggregated($cluster, array());
  259. }
  260. /**
  261. * @group disconnected
  262. * @todo We might want to add a test for SingleConnectionInterface::pushInitCommand().
  263. */
  264. public function testAggregatedConnectionWithServerProfileArgument()
  265. {
  266. list(, $connectionClass) = $this->getMockConnectionClass();
  267. $cluster = $this->getMock('Predis\Connection\ClusterConnectionInterface');
  268. $profile = $this->getMock('Predis\Profile\ServerProfileInterface');
  269. $factory = $this->getMock('Predis\Connection\ConnectionFactory', array('create'));
  270. $factory->expects($this->exactly(2))
  271. ->method('create')
  272. ->with($this->anything(), $profile)
  273. ->will($this->returnCallback(function ($_, $_) use ($connectionClass) {
  274. return new $connectionClass();
  275. }));
  276. $nodes = array('tcp://127.0.0.1:7001?password=foo', 'tcp://127.0.0.1:7002?password=bar');
  277. $factory->createAggregated($cluster, $nodes, $profile);
  278. }
  279. // ******************************************************************** //
  280. // ---- HELPER METHODS ------------------------------------------------ //
  281. // ******************************************************************** //
  282. /**
  283. * Returns a mocked Predis\Connection\SingleConnectionInterface.
  284. *
  285. * @return Array Mock instance and class name
  286. */
  287. protected function getMockConnectionClass()
  288. {
  289. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  290. return array($connection, get_class($connection));
  291. }
  292. }