FactoryTest.php 11 KB

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