FactoryTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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
  218. ->expects($this->never())
  219. ->method('addConnectCommand');
  220. $parameters = new Parameters(array('scheme' => 'test'));
  221. $factory = new Factory();
  222. $factory->define('test', function ($scheme, $parameters) use ($connection) {
  223. return $connection;
  224. });
  225. $this->assertSame($connection, $factory->create($parameters));
  226. }
  227. /**
  228. * @group disconnected
  229. *
  230. * @todo This test smells but there's no other way around it right now.
  231. */
  232. public function testCreateConnectionWithInitializationCommands()
  233. {
  234. $parameters = new Parameters(array(
  235. 'database' => '0',
  236. 'password' => 'foobar',
  237. ));
  238. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  239. $connection
  240. ->expects($this->once())
  241. ->method('getParameters')
  242. ->will($this->returnValue($parameters));
  243. $connection
  244. ->expects($this->at(1))
  245. ->method('addConnectCommand')
  246. ->with($this->isRedisCommand('AUTH', array('foobar')));
  247. $connection
  248. ->expects($this->at(2))
  249. ->method('addConnectCommand')
  250. ->with($this->isRedisCommand('SELECT', array(0)));
  251. $factory = new Factory();
  252. $reflection = new \ReflectionObject($factory);
  253. $prepareConnection = $reflection->getMethod('prepareConnection');
  254. $prepareConnection->setAccessible(true);
  255. $prepareConnection->invoke($factory, $connection);
  256. }
  257. /**
  258. * @group disconnected
  259. * @expectedException \InvalidArgumentException
  260. * @expecteExceptionMessage Unknown connection scheme: 'unknown'.
  261. */
  262. public function testCreateUndefinedConnection()
  263. {
  264. $factory = new Factory();
  265. $factory->create(new Parameters(array('scheme' => 'unknown')));
  266. }
  267. /**
  268. * @group disconnected
  269. */
  270. public function testDefineConnectionWithFQN()
  271. {
  272. list(, $connectionClass) = $this->getMockConnectionClass();
  273. $parameters = new Parameters(array('scheme' => 'foobar'));
  274. $factory = new Factory();
  275. $factory->define($parameters->scheme, $connectionClass);
  276. $connection = $factory->create($parameters);
  277. $this->assertInstanceOf($connectionClass, $connection);
  278. }
  279. /**
  280. * @group disconnected
  281. */
  282. public function testDefineConnectionWithCallable()
  283. {
  284. list(, $connectionClass) = $this->getMockConnectionClass();
  285. $parameters = new Parameters(array('scheme' => 'foobar'));
  286. $initializer = function ($parameters) use ($connectionClass) {
  287. return new $connectionClass($parameters);
  288. };
  289. $initializerMock = $this->getMock('stdClass', array('__invoke'));
  290. $initializerMock
  291. ->expects($this->exactly(2))
  292. ->method('__invoke')
  293. ->with($parameters)
  294. ->will($this->returnCallback($initializer));
  295. $factory = new Factory();
  296. $factory->define($parameters->scheme, $initializerMock);
  297. $connection1 = $factory->create($parameters);
  298. $connection2 = $factory->create($parameters);
  299. $this->assertInstanceOf($connectionClass, $connection1);
  300. $this->assertInstanceOf($connectionClass, $connection2);
  301. $this->assertNotSame($connection1, $connection2);
  302. }
  303. /**
  304. * @group disconnected
  305. * @expectedException \InvalidArgumentException
  306. */
  307. public function testDefineConnectionWithInvalidArgument()
  308. {
  309. $factory = new Factory();
  310. $factory->define('foobar', new \stdClass());
  311. }
  312. /**
  313. * @group disconnected
  314. * @expectedException \InvalidArgumentException
  315. * @expecteExceptionMessage Unknown connection scheme: 'tcp'.
  316. */
  317. public function testUndefineDefinedConnection()
  318. {
  319. $factory = new Factory();
  320. $factory->undefine('tcp');
  321. $factory->create('tcp://127.0.0.1');
  322. }
  323. /**
  324. * @group disconnected
  325. */
  326. public function testUndefineUndefinedConnection()
  327. {
  328. $factory = new Factory();
  329. $factory->undefine('unknown');
  330. $connection = $factory->create('tcp://127.0.0.1');
  331. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
  332. }
  333. /**
  334. * @group disconnected
  335. * @expectedException \InvalidArgumentException
  336. * @expecteExceptionMessage Unknown connection scheme: 'test'.
  337. */
  338. public function testDefineAndUndefineConnection()
  339. {
  340. list(, $connectionClass) = $this->getMockConnectionClass();
  341. $factory = new Factory();
  342. $factory->define('test', $connectionClass);
  343. $this->assertInstanceOf($connectionClass, $factory->create('test://127.0.0.1'));
  344. $factory->undefine('test');
  345. $factory->create('test://127.0.0.1');
  346. }
  347. /**
  348. * @group disconnected
  349. */
  350. public function testAggregateConnectionSkipCreationOnConnectionInstance()
  351. {
  352. list(, $connectionClass) = $this->getMockConnectionClass();
  353. $cluster = $this->getMock('Predis\Connection\Cluster\ClusterInterface');
  354. $cluster
  355. ->expects($this->exactly(2))
  356. ->method('add')
  357. ->with($this->isInstanceOf('Predis\Connection\NodeConnectionInterface'));
  358. $factory = $this->getMock('Predis\Connection\Factory', array('create'));
  359. $factory
  360. ->expects($this->never())
  361. ->method('create');
  362. $factory->aggregate($cluster, array(new $connectionClass(), new $connectionClass()));
  363. }
  364. /**
  365. * @group disconnected
  366. */
  367. public function testAggregateConnectionWithMixedParameters()
  368. {
  369. list(, $connectionClass) = $this->getMockConnectionClass();
  370. $cluster = $this->getMock('Predis\Connection\Cluster\ClusterInterface');
  371. $cluster
  372. ->expects($this->exactly(4))
  373. ->method('add')
  374. ->with($this->isInstanceOf('Predis\Connection\NodeConnectionInterface'));
  375. $factory = $this->getMock('Predis\Connection\Factory', array('create'));
  376. $factory
  377. ->expects($this->exactly(3))
  378. ->method('create')
  379. ->will($this->returnCallback(function ($_) use ($connectionClass) {
  380. return new $connectionClass();
  381. }));
  382. $factory->aggregate($cluster, array(null, 'tcp://127.0.0.1', array('scheme' => 'tcp'), new $connectionClass()));
  383. }
  384. /**
  385. * @group disconnected
  386. */
  387. public function testAggregateConnectionWithEmptyListOfParameters()
  388. {
  389. $cluster = $this->getMock('Predis\Connection\Cluster\ClusterInterface');
  390. $cluster
  391. ->expects($this->never())
  392. ->method('add');
  393. $factory = $this->getMock('Predis\Connection\Factory', array('create'));
  394. $factory
  395. ->expects($this->never())
  396. ->method('create');
  397. $factory->aggregate($cluster, array());
  398. }
  399. // ******************************************************************** //
  400. // ---- HELPER METHODS ------------------------------------------------ //
  401. // ******************************************************************** //
  402. /**
  403. * Returns a mocked Predis\Connection\NodeConnectionInterface.
  404. *
  405. * @return array Mock instance and class name
  406. */
  407. protected function getMockConnectionClass()
  408. {
  409. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  410. return array($connection, get_class($connection));
  411. }
  412. }