FactoryTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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($unix);
  46. $parameters = $connection->getParameters();
  47. $this->assertInstanceOf('Predis\Connection\StreamConnection', $connection);
  48. $this->assertEquals($unix->scheme, $parameters->scheme);
  49. $this->assertEquals($unix->path, $parameters->path);
  50. $this->assertEquals($unix->database, $parameters->database);
  51. }
  52. /**
  53. * @group disconnected
  54. */
  55. public function testCreateConnectionWithNullParameters()
  56. {
  57. $factory = new Factory();
  58. $connection = $factory->create(null);
  59. $parameters = $connection->getParameters();
  60. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
  61. $this->assertEquals('tcp', $parameters->scheme);
  62. $this->assertFalse(isset($parameters->custom));
  63. $this->assertNull($parameters->custom);
  64. }
  65. /**
  66. * @group disconnected
  67. */
  68. public function testCreateConnectionWithArrayParameters()
  69. {
  70. $factory = new Factory();
  71. $connection = $factory->create(array('scheme' => 'tcp', 'custom' => 'foobar'));
  72. $parameters = $connection->getParameters();
  73. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
  74. $this->assertEquals('tcp', $parameters->scheme);
  75. $this->assertTrue(isset($parameters->custom));
  76. $this->assertSame('foobar', $parameters->custom);
  77. }
  78. /**
  79. * @group disconnected
  80. */
  81. public function testCreateConnectionWithStringURI()
  82. {
  83. $factory = new Factory();
  84. $connection = $factory->create('tcp://127.0.0.1?custom=foobar');
  85. $parameters = $connection->getParameters();
  86. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
  87. $this->assertEquals('tcp', $parameters->scheme);
  88. $this->assertTrue(isset($parameters->custom));
  89. $this->assertSame('foobar', $parameters->custom);
  90. }
  91. /**
  92. * @group disconnected
  93. */
  94. public function testCreateConnectionWithoutInitializationCommands()
  95. {
  96. $profile = $this->getMock('Predis\Profile\ProfileInterface');
  97. $profile->expects($this->never())->method('create');
  98. $factory = new Factory($profile);
  99. $parameters = new Parameters();
  100. $connection = $factory->create($parameters);
  101. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
  102. }
  103. /**
  104. * @group disconnected
  105. * @todo This test smells but there's no other way around it right now.
  106. */
  107. public function testCreateConnectionWithInitializationCommands()
  108. {
  109. $parameters = new Parameters(array(
  110. 'database' => '0',
  111. 'password' => 'foobar'
  112. ));
  113. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  114. $connection->expects($this->once())
  115. ->method('getParameters')
  116. ->will($this->returnValue($parameters));
  117. $connection->expects($this->at(1))
  118. ->method('addConnectCommand')
  119. ->with($this->isRedisCommand('AUTH', array('foobar')));
  120. $connection->expects($this->at(2))
  121. ->method('addConnectCommand')
  122. ->with($this->isRedisCommand('SELECT', array(0)));
  123. $factory = new Factory();
  124. $reflection = new \ReflectionObject($factory);
  125. $prepareConnection = $reflection->getMethod('prepareConnection');
  126. $prepareConnection->setAccessible(true);
  127. $prepareConnection->invoke($factory, $connection);
  128. }
  129. /**
  130. * @group disconnected
  131. * @expectedException \InvalidArgumentException
  132. * @expecteExceptionMessage Unknown connection scheme: 'unknown'.
  133. */
  134. public function testCreateUndefinedConnection()
  135. {
  136. $factory = new Factory();
  137. $factory->create(new Parameters(array('scheme' => 'unknown')));
  138. }
  139. /**
  140. * @group disconnected
  141. */
  142. public function testDefineConnectionWithFQN()
  143. {
  144. list(, $connectionClass) = $this->getMockConnectionClass();
  145. $parameters = new Parameters(array('scheme' => 'foobar'));
  146. $factory = new Factory();
  147. $factory->define($parameters->scheme, $connectionClass);
  148. $connection = $factory->create($parameters);
  149. $this->assertInstanceOf($connectionClass, $connection);
  150. }
  151. /**
  152. * @group disconnected
  153. */
  154. public function testDefineConnectionWithCallable()
  155. {
  156. list(, $connectionClass) = $this->getMockConnectionClass();
  157. $parameters = new Parameters(array('scheme' => 'foobar'));
  158. $initializer = function ($parameters) use ($connectionClass) {
  159. return new $connectionClass($parameters);
  160. };
  161. $initializerMock = $this->getMock('stdClass', array('__invoke'));
  162. $initializerMock->expects($this->exactly(2))
  163. ->method('__invoke')
  164. ->with($parameters)
  165. ->will($this->returnCallback($initializer));
  166. $factory = new Factory();
  167. $factory->define($parameters->scheme, $initializerMock);
  168. $connection1 = $factory->create($parameters);
  169. $connection2 = $factory->create($parameters);
  170. $this->assertInstanceOf($connectionClass, $connection1);
  171. $this->assertInstanceOf($connectionClass, $connection2);
  172. $this->assertNotSame($connection1, $connection2);
  173. }
  174. /**
  175. * @group disconnected
  176. * @expectedException \InvalidArgumentException
  177. */
  178. public function testDefineConnectionWithInvalidArgument()
  179. {
  180. $factory = new Factory();
  181. $factory->define('foobar', new \stdClass());
  182. }
  183. /**
  184. * @group disconnected
  185. * @expectedException \InvalidArgumentException
  186. * @expecteExceptionMessage Unknown connection scheme: 'tcp'.
  187. */
  188. public function testUndefineDefinedConnection()
  189. {
  190. $factory = new Factory();
  191. $factory->undefine('tcp');
  192. $factory->create('tcp://127.0.0.1');
  193. }
  194. /**
  195. * @group disconnected
  196. */
  197. public function testUndefineUndefinedConnection()
  198. {
  199. $factory = new Factory();
  200. $factory->undefine('unknown');
  201. $connection = $factory->create('tcp://127.0.0.1');
  202. $this->assertInstanceOf('Predis\Connection\NodeConnectionInterface', $connection);
  203. }
  204. /**
  205. * @group disconnected
  206. * @expectedException \InvalidArgumentException
  207. * @expecteExceptionMessage Unknown connection scheme: 'redis'.
  208. */
  209. public function testDefineAndUndefineConnection()
  210. {
  211. list(, $connectionClass) = $this->getMockConnectionClass();
  212. $factory = new Factory();
  213. $factory->define('redis', $connectionClass);
  214. $this->assertInstanceOf($connectionClass, $factory->create('redis://127.0.0.1'));
  215. $factory->undefine('redis');
  216. $factory->create('redis://127.0.0.1');
  217. }
  218. /**
  219. * @group disconnected
  220. */
  221. public function testAggregateConnectionSkipCreationOnConnectionInstance()
  222. {
  223. list(, $connectionClass) = $this->getMockConnectionClass();
  224. $cluster = $this->getMock('Predis\Connection\Aggregate\ClusterInterface');
  225. $cluster->expects($this->exactly(2))
  226. ->method('add')
  227. ->with($this->isInstanceOf('Predis\Connection\NodeConnectionInterface'));
  228. $factory = $this->getMock('Predis\Connection\Factory', array('create'));
  229. $factory->expects($this->never())
  230. ->method('create');
  231. $factory->aggregate($cluster, array(new $connectionClass(), new $connectionClass()));
  232. }
  233. /**
  234. * @group disconnected
  235. */
  236. public function testAggregateConnectionWithMixedParameters()
  237. {
  238. list(, $connectionClass) = $this->getMockConnectionClass();
  239. $cluster = $this->getMock('Predis\Connection\Aggregate\ClusterInterface');
  240. $cluster->expects($this->exactly(4))
  241. ->method('add')
  242. ->with($this->isInstanceOf('Predis\Connection\NodeConnectionInterface'));
  243. $factory = $this->getMock('Predis\Connection\Factory', array('create'));
  244. $factory->expects($this->exactly(3))
  245. ->method('create')
  246. ->will($this->returnCallback(function ($_) use ($connectionClass) {
  247. return new $connectionClass;
  248. }));
  249. $factory->aggregate($cluster, array(null, 'tcp://127.0.0.1', array('scheme' => 'tcp'), new $connectionClass()));
  250. }
  251. /**
  252. * @group disconnected
  253. */
  254. public function testAggregateConnectionWithEmptyListOfParameters()
  255. {
  256. $cluster = $this->getMock('Predis\Connection\Aggregate\ClusterInterface');
  257. $cluster->expects($this->never())->method('add');
  258. $factory = $this->getMock('Predis\Connection\Factory', array('create'));
  259. $factory->expects($this->never())->method('create');
  260. $factory->aggregate($cluster, array());
  261. }
  262. // ******************************************************************** //
  263. // ---- HELPER METHODS ------------------------------------------------ //
  264. // ******************************************************************** //
  265. /**
  266. * Returns a mocked Predis\Connection\NodeConnectionInterface.
  267. *
  268. * @return array Mock instance and class name
  269. */
  270. protected function getMockConnectionClass()
  271. {
  272. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  273. return array($connection, get_class($connection));
  274. }
  275. }