ConnectionFactoryTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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\IConnectionFactory', $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\Network\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\Network\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\Network\IConnectionSingle', $connection);
  60. $this->assertEquals('tcp', $parameters->scheme);
  61. $this->assertFalse($parameters->isSetByUser('custom'));
  62. }
  63. /**
  64. * @group disconnected
  65. */
  66. public function testCreateConnectionWithArrayParameters()
  67. {
  68. $factory = new ConnectionFactory();
  69. $connection = $factory->create(array('scheme' => 'tcp', 'custom' => 'foobar'));
  70. $parameters = $connection->getParameters();
  71. $this->assertInstanceOf('Predis\Network\IConnectionSingle', $connection);
  72. $this->assertEquals('tcp', $parameters->scheme);
  73. $this->assertTrue($parameters->isSetByUser('custom'));
  74. }
  75. /**
  76. * @group disconnected
  77. */
  78. public function testCreateConnectionWithStringURI()
  79. {
  80. $factory = new ConnectionFactory();
  81. $connection = $factory->create('tcp://127.0.0.1?custom=foobar');
  82. $parameters = $connection->getParameters();
  83. $this->assertInstanceOf('Predis\Network\IConnectionSingle', $connection);
  84. $this->assertEquals('tcp', $parameters->scheme);
  85. $this->assertTrue($parameters->isSetByUser('custom'));
  86. }
  87. /**
  88. * @group disconnected
  89. */
  90. public function testCreateConnectionWithoutInitializationCommands()
  91. {
  92. $profile = $this->getMock('Predis\Profiles\IServerProfile');
  93. $profile->expects($this->never())->method('create');
  94. $factory = new ConnectionFactory();
  95. $parameters = new ConnectionParameters();
  96. $connection = $factory->create($parameters, $profile);
  97. $this->assertInstanceOf('Predis\Network\IConnectionSingle', $connection);
  98. }
  99. /**
  100. * @group disconnected
  101. */
  102. public function testCreateConnectionWithInitializationCommands()
  103. {
  104. $test = $this;
  105. $database = 15;
  106. $password = 'foobar';
  107. $commands = array();
  108. $createCommand = function($id, $arguments) use($test, &$commands) {
  109. $commands[$id] = $arguments;
  110. return $test->getMock('Predis\Commands\ICommand');
  111. };
  112. $profile = $this->getMock('Predis\Profiles\IServerProfile');
  113. $profile->expects($this->exactly(2))
  114. ->method('createCommand')
  115. ->with($this->isType('string'), $this->isType('array'))
  116. ->will($this->returnCallback($createCommand));
  117. $factory = new ConnectionFactory();
  118. $parameters = new ConnectionParameters(array('database' => $database, 'password' => $password));
  119. $connection = $factory->create($parameters, $profile);
  120. $this->assertInstanceOf('Predis\Network\IConnectionSingle', $connection);
  121. $this->assertEquals(2, count($commands)); // TODO: assertCount()?
  122. $this->assertEquals(array($database), $commands['select']);
  123. $this->assertEquals(array($password), $commands['auth']);
  124. }
  125. /**
  126. * @group disconnected
  127. */
  128. public function testCreateUndefinedConnection()
  129. {
  130. $scheme = 'unknown';
  131. $this->setExpectedException('InvalidArgumentException', "Unknown connection scheme: $scheme");
  132. $factory = new ConnectionFactory();
  133. $factory->create(new ConnectionParameters(array('scheme' => $scheme)));
  134. }
  135. /**
  136. * @group disconnected
  137. */
  138. public function testDefineConnectionWithFQN()
  139. {
  140. list(, $connectionClass) = $this->getMockConnectionClass();
  141. $parameters = new ConnectionParameters(array('scheme' => 'foobar'));
  142. $factory = new ConnectionFactory();
  143. $factory->define($parameters->scheme, $connectionClass);
  144. $connection = $factory->create($parameters);
  145. $this->assertInstanceOf($connectionClass, $connection);
  146. }
  147. /**
  148. * @group disconnected
  149. */
  150. public function testDefineConnectionWithCallable()
  151. {
  152. list(, $connectionClass) = $this->getMockConnectionClass();
  153. $parameters = new ConnectionParameters(array('scheme' => 'foobar'));
  154. $initializer = function($parameters) use($connectionClass) {
  155. return new $connectionClass($parameters);
  156. };
  157. $initializerMock = $this->getMock('stdClass', array('__invoke'));
  158. $initializerMock->expects($this->exactly(2))
  159. ->method('__invoke')
  160. ->with($parameters)
  161. ->will($this->returnCallback($initializer));
  162. $factory = new ConnectionFactory();
  163. $factory->define($parameters->scheme, $initializerMock);
  164. $connection1 = $factory->create($parameters);
  165. $connection2 = $factory->create($parameters);
  166. $this->assertInstanceOf($connectionClass, $connection1);
  167. $this->assertInstanceOf($connectionClass, $connection2);
  168. $this->assertNotSame($connection1, $connection2);
  169. }
  170. /**
  171. * @group disconnected
  172. */
  173. public function testDefineConnectionWithInvalidArgument()
  174. {
  175. $this->setExpectedException('InvalidArgumentException');
  176. $factory = new ConnectionFactory();
  177. $factory->define('foobar', new \stdClass());
  178. }
  179. /**
  180. * @group disconnected
  181. */
  182. public function testUndefineDefinedConnection()
  183. {
  184. $this->setExpectedException('InvalidArgumentException', 'Unknown connection scheme: tcp');
  185. $factory = new ConnectionFactory();
  186. $factory->undefine('tcp');
  187. $factory->create('tcp://127.0.0.1');
  188. }
  189. /**
  190. * @group disconnected
  191. */
  192. public function testUndefineUndefinedConnection()
  193. {
  194. $factory = new ConnectionFactory();
  195. $factory->undefine('unknown');
  196. $connection = $factory->create('tcp://127.0.0.1');
  197. $this->assertInstanceOf('Predis\Network\IConnectionSingle', $connection);
  198. }
  199. /**
  200. * @group disconnected
  201. */
  202. public function testDefineAndUndefineConnection()
  203. {
  204. list(, $connectionClass) = $this->getMockConnectionClass();
  205. $factory = new ConnectionFactory();
  206. $factory->define('redis', $connectionClass);
  207. $this->assertInstanceOf($connectionClass, $factory->create('redis://127.0.0.1'));
  208. $factory->undefine('redis');
  209. $this->setExpectedException('InvalidArgumentException', 'Unknown connection scheme: redis');
  210. $factory->create('redis://127.0.0.1');
  211. }
  212. /**
  213. * @group disconnected
  214. */
  215. public function testClusterSkipCreationOnConnectionInstance()
  216. {
  217. list(, $connectionClass) = $this->getMockConnectionClass();
  218. $cluster = $this->getMock('Predis\Network\IConnectionCluster');
  219. $cluster->expects($this->exactly(2))
  220. ->method('add')
  221. ->with($this->isInstanceOf('Predis\Network\IConnectionSingle'));
  222. $factory = $this->getMock('Predis\ConnectionFactory', array('create'));
  223. $factory->expects($this->never())
  224. ->method('create');
  225. $factory->createCluster($cluster, array(new $connectionClass(), new $connectionClass()));
  226. }
  227. /**
  228. * @group disconnected
  229. */
  230. public function testClusterWithMixedConnectionParameters()
  231. {
  232. list(, $connectionClass) = $this->getMockConnectionClass();
  233. $cluster = $this->getMock('Predis\Network\IConnectionCluster');
  234. $cluster->expects($this->exactly(4))
  235. ->method('add')
  236. ->with($this->isInstanceOf('Predis\Network\IConnectionSingle'));
  237. $factory = $this->getMock('Predis\ConnectionFactory', array('create'));
  238. $factory->expects($this->exactly(3))
  239. ->method('create')
  240. ->will($this->returnCallback(function($_, $_) use($connectionClass) {
  241. return new $connectionClass;
  242. }));
  243. $factory->createCluster($cluster, array(null, 'tcp://127.0.0.1', array('scheme' => 'tcp'), new $connectionClass()));
  244. }
  245. /**
  246. * @group disconnected
  247. */
  248. public function testClusterWithEmptyListOfParameters()
  249. {
  250. $cluster = $this->getMock('Predis\Network\IConnectionCluster');
  251. $cluster->expects($this->never())->method('add');
  252. $factory = $this->getMock('Predis\ConnectionFactory', array('create'));
  253. $factory->expects($this->never())->method('create');
  254. $factory->createCluster($cluster, array());
  255. }
  256. /**
  257. * @group disconnected
  258. * @todo We might want to add a test for IConnectionSingle::pushInitCommand().
  259. */
  260. public function testClusterWithServerProfileArgument()
  261. {
  262. list(, $connectionClass) = $this->getMockConnectionClass();
  263. $cluster = $this->getMock('Predis\Network\IConnectionCluster');
  264. $profile = $this->getMock('Predis\Profiles\IServerProfile');
  265. $factory = $this->getMock('Predis\ConnectionFactory', array('create'));
  266. $factory->expects($this->exactly(2))
  267. ->method('create')
  268. ->with($this->anything(), $profile)
  269. ->will($this->returnCallback(function($_, $_) use($connectionClass) {
  270. return new $connectionClass();
  271. }));
  272. $nodes = array('tcp://127.0.0.1:7001?password=foo', 'tcp://127.0.0.1:7002?password=bar');
  273. $factory->createCluster($cluster, $nodes, $profile);
  274. }
  275. /**
  276. * @group disconnected
  277. */
  278. public function testReplicationWithMixedConnectionParameters()
  279. {
  280. list(, $connectionClass) = $this->getMockConnectionClass();
  281. $replication = $this->getMock('Predis\Network\IConnectionReplication');
  282. $replication->expects($this->exactly(4))
  283. ->method('add')
  284. ->with($this->isInstanceOf('Predis\Network\IConnectionSingle'));
  285. $factory = $this->getMock('Predis\ConnectionFactory', array('create'));
  286. $factory->expects($this->exactly(3))
  287. ->method('create')
  288. ->will($this->returnCallback(function($_, $_) use($connectionClass) {
  289. return new $connectionClass;
  290. }));
  291. $factory->createReplication($replication, array(null, 'tcp://127.0.0.1', array('scheme' => 'tcp'), new $connectionClass()));
  292. }
  293. // ******************************************************************** //
  294. // ---- HELPER METHODS ------------------------------------------------ //
  295. // ******************************************************************** //
  296. /**
  297. * Returns a mocked Predis\Network\IConnectionSingle.
  298. *
  299. * @return Array Mock instance and class name
  300. */
  301. protected function getMockConnectionClass()
  302. {
  303. $connection = $this->getMock('Predis\Network\IConnectionSingle');
  304. return array($connection, get_class($connection));
  305. }
  306. }