FactoryTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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 testSettingDefaultParameters()
  29. {
  30. $factory = new Factory();
  31. $factory->setDefaultParameters($defaults = array(
  32. 'password' => 'secret',
  33. 'database' => 10,
  34. 'custom' => 'foobar',
  35. ));
  36. $this->assertSame($defaults, $factory->getDefaultParameters());
  37. $parameters = array('database' => 10, 'persistent' => true);
  38. }
  39. /**
  40. * @group disconnected
  41. */
  42. public function testCreateTcpConnection()
  43. {
  44. $factory = new Factory();
  45. $parameters = new Parameters(array('scheme' => 'tcp'));
  46. $connection = $factory->create($parameters);
  47. $this->assertInstanceOf('Predis\Connection\StreamConnection', $connection);
  48. $this->assertSame($parameters, $connection->getParameters());
  49. $parameters = new Parameters(array('scheme' => 'redis'));
  50. $connection = $factory->create($parameters);
  51. $this->assertInstanceOf('Predis\Connection\StreamConnection', $connection);
  52. $this->assertSame($parameters, $connection->getParameters());
  53. }
  54. /**
  55. * @group disconnected
  56. */
  57. public function testCreateSslConnection()
  58. {
  59. $factory = new Factory();
  60. $parameters = new Parameters(array('scheme' => 'tls'));
  61. $connection = $factory->create($parameters);
  62. $this->assertInstanceOf('Predis\Connection\StreamConnection', $connection);
  63. $this->assertSame($parameters, $connection->getParameters());
  64. $parameters = new Parameters(array('scheme' => 'rediss'));
  65. $connection = $factory->create($parameters);
  66. $this->assertInstanceOf('Predis\Connection\StreamConnection', $connection);
  67. $this->assertSame($parameters, $connection->getParameters());
  68. }
  69. /**
  70. * @group disconnected
  71. */
  72. public function testCreateUnixConnection()
  73. {
  74. $factory = new Factory();
  75. $parameters = new Parameters(array('scheme' => 'unix', 'path' => '/tmp/redis.sock'));
  76. $connection = $factory->create($parameters);
  77. $this->assertInstanceOf('Predis\Connection\StreamConnection', $connection);
  78. $this->assertSame($parameters, $connection->getParameters());
  79. }
  80. /**
  81. * @group disconnected
  82. */
  83. public function testCreateConnectionWithParametersInstanceAndDefaultsDoesNotAlterOriginalParameters()
  84. {
  85. $factory = new Factory();
  86. $factory->setDefaultParameters($defaultParams = array(
  87. 'port' => 7000,
  88. 'password' => 'secret',
  89. 'database' => 10,
  90. 'custom' => 'foobar',
  91. ));
  92. $inputParams = new Parameters(array(
  93. 'host' => 'localhost',
  94. 'database' => 5,
  95. ));
  96. $connection = $factory->create($inputParams);
  97. $parameters = $connection->getParameters();
  98. $this->assertEquals('localhost', $parameters->host);
  99. $this->assertEquals(6379, $parameters->port);
  100. $this->assertEquals(5, $parameters->database);
  101. $this->assertFalse(isset($parameters->password));
  102. $this->assertNull($parameters->password);
  103. $this->assertFalse(isset($parameters->custom));
  104. $this->assertNull($parameters->custom);
  105. }
  106. /**
  107. * @group disconnected
  108. */
  109. public function testCreateConnectionWithNullParameters()
  110. {
  111. $factory = new Factory();
  112. $connection = $factory->create(null);
  113. $parameters = $connection->getParameters();
  114. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
  115. $this->assertEquals('tcp', $parameters->scheme);
  116. $this->assertFalse(isset($parameters->custom));
  117. $this->assertNull($parameters->custom);
  118. }
  119. /**
  120. * @group disconnected
  121. */
  122. public function testCreateConnectionWithNullParametersAndDefaults()
  123. {
  124. $factory = new Factory();
  125. $factory->setDefaultParameters($defaultParams = array(
  126. 'port' => 7000,
  127. 'password' => 'secret',
  128. 'custom' => 'foobar',
  129. ));
  130. $connection = $factory->create(null);
  131. $parameters = $connection->getParameters();
  132. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
  133. $this->assertEquals('127.0.0.1', $parameters->host);
  134. $this->assertEquals($defaultParams['port'], $parameters->port);
  135. $this->assertEquals($defaultParams['password'], $parameters->password);
  136. $this->assertEquals($defaultParams['custom'], $parameters->custom);
  137. $this->assertNull($parameters->path);
  138. }
  139. /**
  140. * @group disconnected
  141. */
  142. public function testCreateConnectionWithArrayParameters()
  143. {
  144. $factory = new Factory();
  145. $connection = $factory->create(array('scheme' => 'tcp', 'custom' => 'foobar'));
  146. $parameters = $connection->getParameters();
  147. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
  148. $this->assertEquals('tcp', $parameters->scheme);
  149. $this->assertTrue(isset($parameters->custom));
  150. $this->assertSame('foobar', $parameters->custom);
  151. }
  152. /**
  153. * @group disconnected
  154. */
  155. public function testCreateConnectionWithArrayParametersAndDefaults()
  156. {
  157. $factory = new Factory();
  158. $factory->setDefaultParameters($defaultParams = array(
  159. 'port' => 7000,
  160. 'password' => 'secret',
  161. 'custom' => 'foobar',
  162. ));
  163. $connection = $factory->create($inputParams = array(
  164. 'host' => 'localhost',
  165. 'port' => 8000,
  166. 'persistent' => true,
  167. ));
  168. $parameters = $connection->getParameters();
  169. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
  170. $this->assertEquals($inputParams['host'], $parameters->host);
  171. $this->assertEquals($inputParams['port'], $parameters->port);
  172. $this->assertEquals($defaultParams['password'], $parameters->password);
  173. $this->assertEquals($defaultParams['custom'], $parameters->custom);
  174. $this->assertEquals($inputParams['persistent'], $parameters->persistent);
  175. $this->assertNull($parameters->path);
  176. }
  177. /**
  178. * @group disconnected
  179. */
  180. public function testCreateConnectionWithStringURI()
  181. {
  182. $factory = new Factory();
  183. $connection = $factory->create('tcp://127.0.0.1?custom=foobar');
  184. $parameters = $connection->getParameters();
  185. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
  186. $this->assertEquals('tcp', $parameters->scheme);
  187. $this->assertTrue(isset($parameters->custom));
  188. $this->assertSame('foobar', $parameters->custom);
  189. }
  190. /**
  191. * @group disconnected
  192. */
  193. public function testCreateConnectionWithStrinURIAndDefaults()
  194. {
  195. $factory = new Factory();
  196. $factory->setDefaultParameters($defaultParams = array(
  197. 'port' => 7000,
  198. 'password' => 'secret',
  199. 'custom' => 'foobar',
  200. ));
  201. $connection = $factory->create('tcp://localhost:8000?persistent=1');
  202. $parameters = $connection->getParameters();
  203. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
  204. $this->assertEquals('localhost', $parameters->host);
  205. $this->assertEquals('8000', $parameters->port);
  206. $this->assertEquals($defaultParams['password'], $parameters->password);
  207. $this->assertEquals($defaultParams['custom'], $parameters->custom);
  208. $this->assertEquals(true, $parameters->persistent);
  209. $this->assertNull($parameters->path);
  210. }
  211. /**
  212. * @group disconnected
  213. */
  214. public function testCreateConnectionWithoutInitializationCommands()
  215. {
  216. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  217. $connection->expects($this->never())->method('addConnectCommand');
  218. $parameters = new Parameters(array('scheme' => 'test'));
  219. $factory = new Factory();
  220. $factory->define('test', function ($scheme, $parameters) use ($connection) {
  221. return $connection;
  222. });
  223. $this->assertSame($connection, $factory->create($parameters));
  224. }
  225. /**
  226. * @group disconnected
  227. *
  228. * @todo This test smells but there's no other way around it right now.
  229. */
  230. public function testCreateConnectionWithInitializationCommands()
  231. {
  232. $parameters = new Parameters(array(
  233. 'database' => '0',
  234. 'password' => 'foobar',
  235. ));
  236. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  237. $connection->expects($this->once())
  238. ->method('getParameters')
  239. ->will($this->returnValue($parameters));
  240. $connection->expects($this->at(1))
  241. ->method('addConnectCommand')
  242. ->with($this->isRedisCommand('AUTH', array('foobar')));
  243. $connection->expects($this->at(2))
  244. ->method('addConnectCommand')
  245. ->with($this->isRedisCommand('SELECT', array(0)));
  246. $factory = new Factory();
  247. $reflection = new \ReflectionObject($factory);
  248. $prepareConnection = $reflection->getMethod('prepareConnection');
  249. $prepareConnection->setAccessible(true);
  250. $prepareConnection->invoke($factory, $connection);
  251. }
  252. /**
  253. * @group disconnected
  254. * @expectedException \InvalidArgumentException
  255. * @expecteExceptionMessage Unknown connection scheme: 'unknown'.
  256. */
  257. public function testCreateUndefinedConnection()
  258. {
  259. $factory = new Factory();
  260. $factory->create(new Parameters(array('scheme' => 'unknown')));
  261. }
  262. /**
  263. * @group disconnected
  264. */
  265. public function testDefineConnectionWithFQN()
  266. {
  267. list(, $connectionClass) = $this->getMockConnectionClass();
  268. $parameters = new Parameters(array('scheme' => 'foobar'));
  269. $factory = new Factory();
  270. $factory->define($parameters->scheme, $connectionClass);
  271. $connection = $factory->create($parameters);
  272. $this->assertInstanceOf($connectionClass, $connection);
  273. }
  274. /**
  275. * @group disconnected
  276. */
  277. public function testDefineConnectionWithCallable()
  278. {
  279. list(, $connectionClass) = $this->getMockConnectionClass();
  280. $parameters = new Parameters(array('scheme' => 'foobar'));
  281. $initializer = function ($parameters) use ($connectionClass) {
  282. return new $connectionClass($parameters);
  283. };
  284. $initializerMock = $this->getMock('stdClass', array('__invoke'));
  285. $initializerMock->expects($this->exactly(2))
  286. ->method('__invoke')
  287. ->with($parameters)
  288. ->will($this->returnCallback($initializer));
  289. $factory = new Factory();
  290. $factory->define($parameters->scheme, $initializerMock);
  291. $connection1 = $factory->create($parameters);
  292. $connection2 = $factory->create($parameters);
  293. $this->assertInstanceOf($connectionClass, $connection1);
  294. $this->assertInstanceOf($connectionClass, $connection2);
  295. $this->assertNotSame($connection1, $connection2);
  296. }
  297. /**
  298. * @group disconnected
  299. * @expectedException \InvalidArgumentException
  300. */
  301. public function testDefineConnectionWithInvalidArgument()
  302. {
  303. $factory = new Factory();
  304. $factory->define('foobar', new \stdClass());
  305. }
  306. /**
  307. * @group disconnected
  308. * @expectedException \InvalidArgumentException
  309. * @expecteExceptionMessage Unknown connection scheme: 'tcp'.
  310. */
  311. public function testUndefineDefinedConnection()
  312. {
  313. $factory = new Factory();
  314. $factory->undefine('tcp');
  315. $factory->create('tcp://127.0.0.1');
  316. }
  317. /**
  318. * @group disconnected
  319. */
  320. public function testUndefineUndefinedConnection()
  321. {
  322. $factory = new Factory();
  323. $factory->undefine('unknown');
  324. $connection = $factory->create('tcp://127.0.0.1');
  325. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
  326. }
  327. /**
  328. * @group disconnected
  329. * @expectedException \InvalidArgumentException
  330. * @expecteExceptionMessage Unknown connection scheme: 'test'.
  331. */
  332. public function testDefineAndUndefineConnection()
  333. {
  334. list(, $connectionClass) = $this->getMockConnectionClass();
  335. $factory = new Factory();
  336. $factory->define('test', $connectionClass);
  337. $this->assertInstanceOf($connectionClass, $factory->create('test://127.0.0.1'));
  338. $factory->undefine('test');
  339. $factory->create('test://127.0.0.1');
  340. }
  341. /**
  342. * @group disconnected
  343. */
  344. public function testAggregateConnectionSkipCreationOnConnectionInstance()
  345. {
  346. list(, $connectionClass) = $this->getMockConnectionClass();
  347. $cluster = $this->getMock('Predis\Connection\Cluster\ClusterInterface');
  348. $cluster->expects($this->exactly(2))
  349. ->method('add')
  350. ->with($this->isInstanceOf('Predis\Connection\NodeConnectionInterface'));
  351. $factory = $this->getMock('Predis\Connection\Factory', array('create'));
  352. $factory->expects($this->never())
  353. ->method('create');
  354. $factory->aggregate($cluster, array(new $connectionClass(), new $connectionClass()));
  355. }
  356. /**
  357. * @group disconnected
  358. */
  359. public function testAggregateConnectionWithMixedParameters()
  360. {
  361. list(, $connectionClass) = $this->getMockConnectionClass();
  362. $cluster = $this->getMock('Predis\Connection\Cluster\ClusterInterface');
  363. $cluster->expects($this->exactly(4))
  364. ->method('add')
  365. ->with($this->isInstanceOf('Predis\Connection\NodeConnectionInterface'));
  366. $factory = $this->getMock('Predis\Connection\Factory', array('create'));
  367. $factory->expects($this->exactly(3))
  368. ->method('create')
  369. ->will($this->returnCallback(function ($_) use ($connectionClass) {
  370. return new $connectionClass();
  371. }));
  372. $factory->aggregate($cluster, array(null, 'tcp://127.0.0.1', array('scheme' => 'tcp'), new $connectionClass()));
  373. }
  374. /**
  375. * @group disconnected
  376. */
  377. public function testAggregateConnectionWithEmptyListOfParameters()
  378. {
  379. $cluster = $this->getMock('Predis\Connection\Cluster\ClusterInterface');
  380. $cluster->expects($this->never())->method('add');
  381. $factory = $this->getMock('Predis\Connection\Factory', array('create'));
  382. $factory->expects($this->never())->method('create');
  383. $factory->aggregate($cluster, array());
  384. }
  385. // ******************************************************************** //
  386. // ---- HELPER METHODS ------------------------------------------------ //
  387. // ******************************************************************** //
  388. /**
  389. * Returns a mocked Predis\Connection\NodeConnectionInterface.
  390. *
  391. * @return array Mock instance and class name
  392. */
  393. protected function getMockConnectionClass()
  394. {
  395. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  396. return array($connection, get_class($connection));
  397. }
  398. }