FactoryTest.php 15 KB

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