FactoryTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 testCreateConnection()
  29. {
  30. $factory = new Factory();
  31. $tcp = new Parameters(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 Parameters(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 Factory();
  57. $connection = $factory->create(null);
  58. $parameters = $connection->getParameters();
  59. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $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 Factory();
  70. $connection = $factory->create(array('scheme' => 'tcp', 'custom' => 'foobar'));
  71. $parameters = $connection->getParameters();
  72. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $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 Factory();
  83. $connection = $factory->create('tcp://127.0.0.1?custom=foobar');
  84. $parameters = $connection->getParameters();
  85. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $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\ProfileInterface');
  96. $profile->expects($this->never())->method('create');
  97. $factory = new Factory($profile);
  98. $parameters = new Parameters();
  99. $connection = $factory->create($parameters);
  100. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
  101. }
  102. /**
  103. * @group disconnected
  104. * @todo This test smells but there's no other way around it right now.
  105. */
  106. public function testCreateConnectionWithInitializationCommands()
  107. {
  108. $parameters = new Parameters(array(
  109. 'database' => '0',
  110. 'password' => 'foobar'
  111. ));
  112. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  113. $connection->expects($this->once())
  114. ->method('getParameters')
  115. ->will($this->returnValue($parameters));
  116. $connection->expects($this->at(1))
  117. ->method('addConnectCommand')
  118. ->with($this->isRedisCommand('AUTH', array('foobar')));
  119. $connection->expects($this->at(2))
  120. ->method('addConnectCommand')
  121. ->with($this->isRedisCommand('SELECT', array(0)));
  122. $factory = new Factory();
  123. $reflection = new \ReflectionObject($factory);
  124. $prepareConnection = $reflection->getMethod('prepareConnection');
  125. $prepareConnection->setAccessible(true);
  126. $prepareConnection->invoke($factory, $connection);
  127. }
  128. /**
  129. * @group disconnected
  130. * @expectedException \InvalidArgumentException
  131. * @expecteExceptionMessage Unknown connection scheme: 'unknown'.
  132. */
  133. public function testCreateUndefinedConnection()
  134. {
  135. $factory = new Factory();
  136. $factory->create(new Parameters(array('scheme' => 'unknown')));
  137. }
  138. /**
  139. * @group disconnected
  140. */
  141. public function testDefineConnectionWithFQN()
  142. {
  143. list(, $connectionClass) = $this->getMockConnectionClass();
  144. $parameters = new Parameters(array('scheme' => 'foobar'));
  145. $factory = new Factory();
  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 Parameters(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 Factory();
  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. * @expectedException \InvalidArgumentException
  176. */
  177. public function testDefineConnectionWithInvalidArgument()
  178. {
  179. $factory = new Factory();
  180. $factory->define('foobar', new \stdClass());
  181. }
  182. /**
  183. * @group disconnected
  184. * @expectedException \InvalidArgumentException
  185. * @expecteExceptionMessage Unknown connection scheme: 'tcp'.
  186. */
  187. public function testUndefineDefinedConnection()
  188. {
  189. $factory = new Factory();
  190. $factory->undefine('tcp');
  191. $factory->create('tcp://127.0.0.1');
  192. }
  193. /**
  194. * @group disconnected
  195. */
  196. public function testUndefineUndefinedConnection()
  197. {
  198. $factory = new Factory();
  199. $factory->undefine('unknown');
  200. $connection = $factory->create('tcp://127.0.0.1');
  201. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
  202. }
  203. /**
  204. * @group disconnected
  205. * @expectedException \InvalidArgumentException
  206. * @expecteExceptionMessage Unknown connection scheme: 'redis'.
  207. */
  208. public function testDefineAndUndefineConnection()
  209. {
  210. list(, $connectionClass) = $this->getMockConnectionClass();
  211. $factory = new Factory();
  212. $factory->define('redis', $connectionClass);
  213. $this->assertInstanceOf($connectionClass, $factory->create('redis://127.0.0.1'));
  214. $factory->undefine('redis');
  215. $factory->create('redis://127.0.0.1');
  216. }
  217. /**
  218. * @group disconnected
  219. */
  220. public function testAggregateConnectionSkipCreationOnConnectionInstance()
  221. {
  222. list(, $connectionClass) = $this->getMockConnectionClass();
  223. $cluster = $this->getMock('Predis\Connection\Aggregate\ClusterInterface');
  224. $cluster->expects($this->exactly(2))
  225. ->method('add')
  226. ->with($this->isInstanceOf('Predis\Connection\NodeConnectionInterface'));
  227. $factory = $this->getMock('Predis\Connection\Factory', array('create'));
  228. $factory->expects($this->never())
  229. ->method('create');
  230. $factory->aggregate($cluster, array(new $connectionClass(), new $connectionClass()));
  231. }
  232. /**
  233. * @group disconnected
  234. */
  235. public function testAggregateConnectionWithMixedParameters()
  236. {
  237. list(, $connectionClass) = $this->getMockConnectionClass();
  238. $cluster = $this->getMock('Predis\Connection\Aggregate\ClusterInterface');
  239. $cluster->expects($this->exactly(4))
  240. ->method('add')
  241. ->with($this->isInstanceOf('Predis\Connection\NodeConnectionInterface'));
  242. $factory = $this->getMock('Predis\Connection\Factory', array('create'));
  243. $factory->expects($this->exactly(3))
  244. ->method('create')
  245. ->will($this->returnCallback(function ($_) use ($connectionClass) {
  246. return new $connectionClass;
  247. }));
  248. $factory->aggregate($cluster, array(null, 'tcp://127.0.0.1', array('scheme' => 'tcp'), new $connectionClass()));
  249. }
  250. /**
  251. * @group disconnected
  252. */
  253. public function testAggregateConnectionWithEmptyListOfParameters()
  254. {
  255. $cluster = $this->getMock('Predis\Connection\Aggregate\ClusterInterface');
  256. $cluster->expects($this->never())->method('add');
  257. $factory = $this->getMock('Predis\Connection\Factory', array('create'));
  258. $factory->expects($this->never())->method('create');
  259. $factory->aggregate($cluster, array());
  260. }
  261. // ******************************************************************** //
  262. // ---- HELPER METHODS ------------------------------------------------ //
  263. // ******************************************************************** //
  264. /**
  265. * Returns a mocked Predis\Connection\NodeConnectionInterface.
  266. *
  267. * @return array Mock instance and class name
  268. */
  269. protected function getMockConnectionClass()
  270. {
  271. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  272. return array($connection, get_class($connection));
  273. }
  274. }