ConnectionTestCase.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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\Network;
  11. use \PHPUnit_Framework_TestCase as StandardTestCase;
  12. use Predis\ConnectionParameters;
  13. use Predis\Profiles\ServerProfile;
  14. /**
  15. * @group realm-connection
  16. */
  17. abstract class ConnectionTestCase extends StandardTestCase
  18. {
  19. /**
  20. * @group disconnected
  21. * @group slow
  22. * @expectedException Predis\Network\ConnectionException
  23. */
  24. public function testThrowExceptionWhenUnableToConnect()
  25. {
  26. $parameters = array('host' => '169.254.10.10', 'connection_timeout' => 0.5);
  27. $connection = $this->getConnection($profile, false, $parameters);
  28. $connection->executeCommand($this->getProfile()->createCommand('ping'));
  29. }
  30. // ******************************************************************** //
  31. // ---- INTEGRATION TESTS --------------------------------------------- //
  32. // ******************************************************************** //
  33. /**
  34. * @group connected
  35. */
  36. public function testConnectForcesConnection()
  37. {
  38. $connection = $this->getConnection();
  39. $this->assertFalse($connection->isConnected());
  40. $connection->connect();
  41. $this->assertTrue($connection->isConnected());
  42. }
  43. /**
  44. * @group connected
  45. * @expectedException Predis\ClientException
  46. * @expectedExceptionMessage Connection already estabilished
  47. */
  48. public function testThrowsExceptionOnConnectWhenAlreadyConnected()
  49. {
  50. $connection = $this->getConnection();
  51. $connection->connect();
  52. $connection->connect();
  53. }
  54. /**
  55. * @group connected
  56. */
  57. public function testDisconnectForcesDisconnection()
  58. {
  59. $connection = $this->getConnection();
  60. $connection->connect();
  61. $this->assertTrue($connection->isConnected());
  62. $connection->disconnect();
  63. $this->assertFalse($connection->isConnected());
  64. }
  65. /**
  66. * @group disconnected
  67. */
  68. public function testDoesNotThrowExceptionOnDisconnectWhenAlreadyDisconnected()
  69. {
  70. $connection = $this->getConnection();
  71. $this->assertFalse($connection->isConnected());
  72. $connection->disconnect();
  73. $this->assertFalse($connection->isConnected());
  74. }
  75. /**
  76. * @group connected
  77. */
  78. public function testGetResourceForcesConnection()
  79. {
  80. $connection = $this->getConnection();
  81. $this->assertFalse($connection->isConnected());
  82. $this->assertInternalType('resource', $connection->getResource());
  83. $this->assertTrue($connection->isConnected());
  84. }
  85. /**
  86. * @group connected
  87. */
  88. public function testSendingCommandForcesConnection()
  89. {
  90. $connection = $this->getConnection($profile);
  91. $cmdPing = $profile->createCommand('ping');
  92. $this->assertTrue($connection->executeCommand($cmdPing));
  93. $this->assertTrue($connection->isConnected());
  94. }
  95. /**
  96. * @group connected
  97. */
  98. public function testExecutesCommandOnServer()
  99. {
  100. $connection = $this->getConnection($profile);
  101. $cmdPing = $this->getMock($profile->getCommandClass('ping'), array('parseResponse'));
  102. $cmdPing->expects($this->once())
  103. ->method('parseResponse')
  104. ->with('PONG')
  105. ->will($this->returnValue(true));
  106. $this->assertTrue($connection->executeCommand($cmdPing));
  107. }
  108. /**
  109. * @group connected
  110. */
  111. public function testWritesCommandToServer()
  112. {
  113. $connection = $this->getConnection($profile);
  114. $cmdPing = $this->getMock($profile->getCommandClass('ping'), array('parseResponse'));
  115. $cmdPing->expects($this->never())
  116. ->method('parseResponse');
  117. $connection->writeCommand($cmdPing);
  118. $connection->disconnect();
  119. }
  120. /**
  121. * @group connected
  122. */
  123. public function testReadsCommandFromServer()
  124. {
  125. $connection = $this->getConnection($profile);
  126. $cmdPing = $this->getMock($profile->getCommandClass('ping'), array('parseResponse'));
  127. $cmdPing->expects($this->once())
  128. ->method('parseResponse')
  129. ->with('PONG')
  130. ->will($this->returnValue(true));
  131. $connection->writeCommand($cmdPing);
  132. $this->assertTrue($connection->readResponse($cmdPing));
  133. }
  134. /**
  135. * @group connected
  136. */
  137. public function testIsAbleToWriteMultipleCommandsAndReadThemBackForPipelining()
  138. {
  139. $connection = $this->getConnection($profile);
  140. $cmdPing = $this->getMock($profile->getCommandClass('ping'), array('parseResponse'));
  141. $cmdPing->expects($this->once())
  142. ->method('parseResponse')
  143. ->with('PONG')
  144. ->will($this->returnValue(true));
  145. $cmdEcho = $this->getMock($profile->getCommandClass('echo'), array('parseResponse'));
  146. $cmdEcho->setArguments(array('ECHOED'));
  147. $cmdEcho->expects($this->once())
  148. ->method('parseResponse')
  149. ->with('ECHOED')
  150. ->will($this->returnValue('ECHOED'));
  151. $connection = $this->getConnection();
  152. $connection->writeCommand($cmdPing);
  153. $connection->writeCommand($cmdEcho);
  154. $this->assertTrue($connection->readResponse($cmdPing));
  155. $this->assertSame('ECHOED', $connection->readResponse($cmdEcho));
  156. }
  157. /**
  158. * @group connected
  159. */
  160. public function testSendsInitializationCommandsOnConnection()
  161. {
  162. $connection = $this->getConnection($profile, true);
  163. $cmdPing = $this->getMock($profile->getCommandClass('ping'), array('parseResponse'));
  164. $cmdPing->expects($this->once())
  165. ->method('parseResponse')
  166. ->with('PONG')
  167. ->will($this->returnValue(true));
  168. $cmdEcho = $this->getMock($profile->getCommandClass('echo'), array('parseResponse'));
  169. $cmdEcho->setArguments(array('ECHOED'));
  170. $cmdEcho->expects($this->once())
  171. ->method('parseResponse')
  172. ->with('ECHOED')
  173. ->will($this->returnValue('ECHOED'));
  174. $connection->pushInitCommand($cmdPing);
  175. $connection->pushInitCommand($cmdEcho);
  176. $connection->connect();
  177. }
  178. /**
  179. * @group connected
  180. */
  181. public function testReadsStatusReplies()
  182. {
  183. $connection = $this->getConnection($profile, true);
  184. $connection->writeCommand($profile->createCommand('set', array('foo', 'bar')));
  185. $this->assertTrue($connection->read());
  186. $connection->writeCommand($profile->createCommand('ping'));
  187. $this->assertSame('PONG', $connection->read());
  188. $connection->writeCommand($profile->createCommand('multi'));
  189. $connection->writeCommand($profile->createCommand('ping'));
  190. $this->assertTrue($connection->read());
  191. $this->assertInstanceOf('Predis\ResponseQueued', $connection->read());
  192. }
  193. /**
  194. * @group connected
  195. */
  196. public function testReadsBulkReplies()
  197. {
  198. $connection = $this->getConnection($profile, true);
  199. $connection->executeCommand($profile->createCommand('set', array('foo', 'bar')));
  200. $connection->writeCommand($profile->createCommand('get', array('foo')));
  201. $this->assertSame('bar', $connection->read());
  202. $connection->writeCommand($profile->createCommand('get', array('hoge')));
  203. $this->assertNull($connection->read());
  204. }
  205. /**
  206. * @group connected
  207. */
  208. public function testReadsIntegerReplies()
  209. {
  210. $connection = $this->getConnection($profile, true);
  211. $connection->executeCommand($profile->createCommand('rpush', array('metavars', 'foo', 'hoge', 'lol')));
  212. $connection->writeCommand($profile->createCommand('llen', array('metavars')));
  213. $this->assertSame(3, $connection->read());
  214. }
  215. /**
  216. * @group connected
  217. * @expectedException Predis\ServerException
  218. * @expectedExceptionMessage ERR Operation against a key holding the wrong kind of value
  219. */
  220. public function testReadsErrorRepliesAsExceptions()
  221. {
  222. $connection = $this->getConnection($profile, true, array('throw_errors' => true));
  223. $connection->executeCommand($profile->createCommand('set', array('foo', 'bar')));
  224. $connection->writeCommand($profile->createCommand('rpush', array('foo', 'baz')));
  225. $connection->read();
  226. }
  227. /**
  228. * @group connected
  229. */
  230. public function testReadsErrorRepliesAsResponseErrorObjects()
  231. {
  232. $connection = $this->getConnection($profile, true, array('throw_errors' => false));
  233. $connection->executeCommand($profile->createCommand('set', array('foo', 'bar')));
  234. $connection->writeCommand($profile->createCommand('rpush', array('foo', 'baz')));
  235. $this->assertInstanceOf('Predis\ResponseError', $error = $connection->read());
  236. $this->assertSame('ERR Operation against a key holding the wrong kind of value', $error->getMessage());
  237. }
  238. /**
  239. * @group connected
  240. */
  241. public function testReadsMultibulkRepliesAsArrays()
  242. {
  243. $connection = $this->getConnection($profile, true);
  244. $connection->executeCommand($profile->createCommand('rpush', array('metavars', 'foo', 'hoge', 'lol')));
  245. $connection->writeCommand($profile->createCommand('lrange', array('metavars', 0, -1)));
  246. $this->assertSame(array('foo', 'hoge', 'lol'), $connection->read());
  247. }
  248. /**
  249. * @group connected
  250. * @group slow
  251. * @expectedException Predis\Network\ConnectionException
  252. * @expectedExceptionMessage Connection timed out
  253. */
  254. public function testThrowsExceptionOnConnectionTimeout()
  255. {
  256. $connection = $this->getConnection($_, false, array('host' => '169.254.10.10', 'connection_timeout' => 0.5));
  257. $connection->connect();
  258. }
  259. /**
  260. * @group connected
  261. * @group slow
  262. * @expectedException Predis\Network\ConnectionException
  263. */
  264. public function testThrowsExceptionOnReadWriteTimeout()
  265. {
  266. $connection = $this->getConnection($profile, true, array('read_write_timeout' => 0.5));
  267. $connection->executeCommand($profile->createCommand('brpop', array('foo', 3)));
  268. }
  269. // ******************************************************************** //
  270. // ---- HELPER METHODS ------------------------------------------------ //
  271. // ******************************************************************** //
  272. /**
  273. * Returns a named array with the default connection parameters and their values.
  274. *
  275. * @return Array Default connection parameters.
  276. */
  277. protected function getDefaultParametersArray()
  278. {
  279. return array(
  280. 'scheme' => 'tcp',
  281. 'host' => REDIS_SERVER_HOST,
  282. 'port' => REDIS_SERVER_PORT,
  283. 'database' => REDIS_SERVER_DBNUM,
  284. 'read_write_timeout' => 2,
  285. );
  286. }
  287. /**
  288. * Returns a new instance of connection parameters.
  289. *
  290. * @param array $additional Additional connection parameters.
  291. * @return ConnectionParameters Default connection parameters.
  292. */
  293. protected function getParameters($additional = array())
  294. {
  295. $parameters = array_merge($this->getDefaultParametersArray(), $additional);
  296. $parameters = new ConnectionParameters($parameters);
  297. return $parameters;
  298. }
  299. /**
  300. * Returns a new instance of server profile.
  301. *
  302. * @param array $additional Additional connection parameters.
  303. * @return ServerProfile
  304. */
  305. protected function getProfile($version = null)
  306. {
  307. return ServerProfile::get($version ?: REDIS_SERVER_VERSION);
  308. }
  309. /**
  310. * Returns a new instance of a connection instance.
  311. *
  312. * @param ServerProfile $profile Reference to the server profile instance.
  313. * @param Boolean $initialize Push default initialization commands (SELECT and FLUSHDB).
  314. * @param array $parameters Additional connection parameters.
  315. * @return StreamConnection
  316. */
  317. protected abstract function getConnection(&$profile = null, $initialize = false, Array $parameters = array());
  318. }