ConnectionFactoryTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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;
  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\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. return $test->getMock('Predis\Command\CommandInterface');
  114. };
  115. $profile = $this->getMock('Predis\Profile\ServerProfileInterface');
  116. $profile->expects($this->exactly(2))
  117. ->method('createCommand')
  118. ->with($this->isType('string'), $this->isType('array'))
  119. ->will($this->returnCallback($createCommand));
  120. $factory = new ConnectionFactory();
  121. $parameters = new ConnectionParameters(array('database' => $database, 'password' => $password));
  122. $connection = $factory->create($parameters, $profile);
  123. $this->assertInstanceOf('Predis\Connection\SingleConnectionInterface', $connection);
  124. $this->assertEquals(2, count($commands)); // TODO: assertCount()?
  125. $this->assertEquals(array($database), $commands['select']);
  126. $this->assertEquals(array($password), $commands['auth']);
  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 ConnectionFactory();
  136. $factory->create(new ConnectionParameters(array('scheme' => $scheme)));
  137. }
  138. /**
  139. * @group disconnected
  140. */
  141. public function testDefineConnectionWithFQN()
  142. {
  143. list(, $connectionClass) = $this->getMockConnectionClass();
  144. $parameters = new ConnectionParameters(array('scheme' => 'foobar'));
  145. $factory = new ConnectionFactory();
  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 ConnectionParameters(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 ConnectionFactory();
  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 ConnectionFactory();
  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 ConnectionFactory();
  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 ConnectionFactory();
  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 ConnectionFactory();
  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 testAggregatedConnectionSkipCreationOnConnectionInstance()
  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\ConnectionFactory', array('create'));
  226. $factory->expects($this->never())
  227. ->method('create');
  228. $factory->createAggregated($cluster, array(new $connectionClass(), new $connectionClass()));
  229. }
  230. /**
  231. * @group disconnected
  232. */
  233. public function testAggregatedConnectionWithMixedConnectionParameters()
  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\ConnectionFactory', 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->createAggregated($cluster, array(null, 'tcp://127.0.0.1', array('scheme' => 'tcp'), new $connectionClass()));
  247. }
  248. /**
  249. * @group disconnected
  250. */
  251. public function testAggregatedConnectionWithEmptyListOfParameters()
  252. {
  253. $cluster = $this->getMock('Predis\Connection\ClusterConnectionInterface');
  254. $cluster->expects($this->never())->method('add');
  255. $factory = $this->getMock('Predis\ConnectionFactory', array('create'));
  256. $factory->expects($this->never())->method('create');
  257. $factory->createAggregated($cluster, array());
  258. }
  259. /**
  260. * @group disconnected
  261. * @todo We might want to add a test for SingleConnectionInterface::pushInitCommand().
  262. */
  263. public function testAggregatedConnectionWithServerProfileArgument()
  264. {
  265. list(, $connectionClass) = $this->getMockConnectionClass();
  266. $cluster = $this->getMock('Predis\Connection\ClusterConnectionInterface');
  267. $profile = $this->getMock('Predis\Profile\ServerProfileInterface');
  268. $factory = $this->getMock('Predis\ConnectionFactory', array('create'));
  269. $factory->expects($this->exactly(2))
  270. ->method('create')
  271. ->with($this->anything(), $profile)
  272. ->will($this->returnCallback(function($_, $_) use($connectionClass) {
  273. return new $connectionClass();
  274. }));
  275. $nodes = array('tcp://127.0.0.1:7001?password=foo', 'tcp://127.0.0.1:7002?password=bar');
  276. $factory->createAggregated($cluster, $nodes, $profile);
  277. }
  278. // ******************************************************************** //
  279. // ---- HELPER METHODS ------------------------------------------------ //
  280. // ******************************************************************** //
  281. /**
  282. * Returns a mocked Predis\Connection\SingleConnectionInterface.
  283. *
  284. * @return Array Mock instance and class name
  285. */
  286. protected function getMockConnectionClass()
  287. {
  288. $connection = $this->getMock('Predis\Connection\SingleConnectionInterface');
  289. return array($connection, get_class($connection));
  290. }
  291. }