PageRenderTime 58ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/storage/arangodb-php/tests/ConnectionTest.php

https://bitbucket.org/hfab/webservice
PHP | 451 lines | 266 code | 78 blank | 107 comment | 6 complexity | 8f0a87cc86fd48824bf9d046a1d6bbe3 MD5 | raw file
Possible License(s): BSD-2-Clause, Apache-2.0, MIT, GPL-3.0, LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /**
  3. * ArangoDB PHP client testsuite
  4. * File: ConnectionTest.php
  5. *
  6. * @package ArangoDBClient
  7. * @author Frank Mayer
  8. */
  9. namespace ArangoDBClient;
  10. /**
  11. * Class ConnectionTest
  12. *
  13. * @property Connection $connection
  14. * @property Collection $collection
  15. * @property Collection $edgeCollection
  16. * @property CollectionHandler $collectionHandler
  17. * @property DocumentHandler $documentHandler
  18. *
  19. * @package ArangoDBClient
  20. */
  21. class ConnectionTest extends
  22. \PHPUnit_Framework_TestCase
  23. {
  24. public function setUp()
  25. {
  26. $this->connection = getConnection();
  27. $this->collectionHandler = new CollectionHandler($this->connection);
  28. try {
  29. $this->collectionHandler->drop('ArangoDB_PHP_TestSuite_TestTracer');
  30. } catch (\Exception $e) {
  31. //Silence the exception
  32. }
  33. }
  34. /**
  35. * Test if Connection instance can be initialized
  36. */
  37. public function testInitializeConnection()
  38. {
  39. $connection = getConnection();
  40. static::assertInstanceOf('ArangoDBClient\Connection', $connection);
  41. }
  42. /**
  43. * This is just a test to really test connectivity with the server before moving on to further tests.
  44. */
  45. public function testGetStatus()
  46. {
  47. $connection = getConnection();
  48. $response = $connection->get('/_admin/statistics');
  49. static::assertEquals($response->getHttpCode(), 200, 'Did not return http code 200');
  50. }
  51. /**
  52. * Test get options
  53. */
  54. public function testGetOptions()
  55. {
  56. $connection = getConnection();
  57. $value = $connection->getOption(ConnectionOptions::OPTION_TIMEOUT);
  58. static::assertEquals(12, $value);
  59. $value = $connection->getOption(ConnectionOptions::OPTION_CONNECTION);
  60. static::assertEquals('Close', $value);
  61. $value = $connection->getOption(ConnectionOptions::OPTION_RECONNECT);
  62. static::assertFalse($value);
  63. $value = $connection->getOption(ConnectionOptions::OPTION_DATABASE);
  64. static::assertEquals('_system', $value);
  65. $value = $connection->getOption(ConnectionOptions::OPTION_VERIFY_CERT);
  66. static::assertFalse($value);
  67. $value = $connection->getOption(ConnectionOptions::OPTION_ALLOW_SELF_SIGNED);
  68. static::assertTrue($value);
  69. }
  70. /**
  71. * Test set options
  72. */
  73. public function testSetOptions()
  74. {
  75. $connection = getConnection();
  76. // timeout
  77. $connection->setOption(ConnectionOptions::OPTION_TIMEOUT, 10);
  78. $value = $connection->getOption(ConnectionOptions::OPTION_TIMEOUT);
  79. static::assertEquals(10, $value);
  80. // connection
  81. $connection->setOption(ConnectionOptions::OPTION_CONNECTION, 'Keep-Alive');
  82. $value = $connection->getOption(ConnectionOptions::OPTION_CONNECTION);
  83. static::assertEquals('Keep-Alive', $value);
  84. // reconnect
  85. $connection->setOption(ConnectionOptions::OPTION_RECONNECT, true);
  86. $value = $connection->getOption(ConnectionOptions::OPTION_RECONNECT);
  87. static::assertTrue($value);
  88. $connection->setOption(ConnectionOptions::OPTION_RECONNECT, false);
  89. $value = $connection->getOption(ConnectionOptions::OPTION_RECONNECT);
  90. static::assertFalse($value);
  91. }
  92. /**
  93. * Test set invalid options
  94. *
  95. * @expectedException \ArangoDBClient\ClientException
  96. */
  97. public function testSetEndpointOption()
  98. {
  99. $connection = getConnection();
  100. // will fail!
  101. $connection->setOption(ConnectionOptions::OPTION_ENDPOINT, 'tcp://127.0.0.1:8529');
  102. }
  103. /**
  104. * Test set invalid options
  105. *
  106. * @expectedException \ArangoDBClient\ClientException
  107. */
  108. public function testSetAllowSelfSignedOption()
  109. {
  110. $connection = getConnection();
  111. // will fail!
  112. $connection->setOption(ConnectionOptions::OPTION_ALLOW_SELF_SIGNED, true);
  113. }
  114. /**
  115. * Test set invalid options
  116. *
  117. * @expectedException \ArangoDBClient\ClientException
  118. */
  119. public function testSetVerifyCert()
  120. {
  121. $connection = getConnection();
  122. // will fail!
  123. $connection->setOption(ConnectionOptions::OPTION_VERIFY_CERT, true);
  124. }
  125. /**
  126. * Test set invalid options
  127. *
  128. * @expectedException \ArangoDBClient\ClientException
  129. */
  130. public function testSetCiphers()
  131. {
  132. $connection = getConnection();
  133. // will fail!
  134. $connection->setOption(ConnectionOptions::OPTION_CIPHERS, 'ALL');
  135. }
  136. /**
  137. * Test set invalid options
  138. *
  139. * @expectedException \ArangoDBClient\ClientException
  140. */
  141. public function testSetHostOption()
  142. {
  143. $connection = getConnection();
  144. // will fail!
  145. $connection->setOption(ConnectionOptions::OPTION_HOST, '127.0.0.1');
  146. }
  147. /**
  148. * Test set invalid options
  149. *
  150. * @expectedException \ArangoDBClient\ClientException
  151. */
  152. public function testSetPortOption()
  153. {
  154. $connection = getConnection();
  155. // will fail!
  156. $connection->setOption(ConnectionOptions::OPTION_PORT, '127.0.0.1');
  157. }
  158. /**
  159. * Test get/set database
  160. */
  161. public function testGetSetDatabase()
  162. {
  163. $connection = getConnection();
  164. $value = $connection->getOption(ConnectionOptions::OPTION_DATABASE);
  165. static::assertEquals('_system', $value);
  166. $value = $connection->getDatabase();
  167. static::assertEquals('_system', $value);
  168. // set the database to something else and re-check
  169. $connection->setDatabase('foobar');
  170. $value = $connection->getOption(ConnectionOptions::OPTION_DATABASE);
  171. static::assertEquals('foobar', $value);
  172. $value = $connection->getDatabase();
  173. static::assertEquals('foobar', $value);
  174. // set the database back and re-check
  175. $connection->setOption(ConnectionOptions::OPTION_DATABASE, '_system');
  176. $value = $connection->getOption(ConnectionOptions::OPTION_DATABASE);
  177. static::assertEquals('_system', $value);
  178. $value = $connection->getDatabase();
  179. static::assertEquals('_system', $value);
  180. }
  181. /**
  182. * Test timeout exception
  183. *
  184. * @expectedException \ArangoDBClient\ClientException
  185. */
  186. public function testSetTimeoutException()
  187. {
  188. $connection = getConnection();
  189. $connection->setOption(ConnectionOptions::OPTION_TIMEOUT, 3);
  190. $query = 'RETURN SLEEP(6)';
  191. $statement = new Statement($connection, ['query' => $query]);
  192. try {
  193. // this is expected to fail
  194. $statement->execute();
  195. } catch (ClientException $exception) {
  196. static::assertEquals($exception->getCode(), 408);
  197. throw $exception;
  198. }
  199. }
  200. /**
  201. * Test timeout, no exception
  202. */
  203. public function testSetTimeout()
  204. {
  205. $connection = getConnection();
  206. $connection->setOption(ConnectionOptions::OPTION_TIMEOUT, 5);
  207. $query = 'RETURN SLEEP(1)';
  208. $statement = new Statement($connection, ['query' => $query]);
  209. // should work
  210. $cursor = $statement->execute();
  211. static::assertCount(1, $cursor->getAll());
  212. }
  213. /**
  214. * Test "connection: close"
  215. */
  216. public function testConnectionClose()
  217. {
  218. $done = false;
  219. $tracer = function ($type, $data) use (&$done) {
  220. if ($type === 'send') {
  221. static::assertNotFalse(stripos($data, 'Connection: Close'));
  222. $done = true;
  223. }
  224. };
  225. $options = getConnectionOptions();
  226. $options[ConnectionOptions::OPTION_CONNECTION] = 'Close';
  227. $options[ConnectionOptions::OPTION_TRACE] = $tracer;
  228. $connection = new Connection($options);
  229. $adminHandler = new AdminHandler($connection);
  230. $adminHandler->getServerVersion();
  231. static::assertTrue($done);
  232. }
  233. /**
  234. * Test "connection: close"
  235. */
  236. public function testConnectionKeepAlive()
  237. {
  238. $done = false;
  239. $tracer = function ($type, $data) use (&$done) {
  240. if ($type === 'send') {
  241. static::assertNotFalse(stripos($data, 'Connection: Keep-Alive'));
  242. $done = true;
  243. }
  244. };
  245. $options = getConnectionOptions();
  246. $options[ConnectionOptions::OPTION_CONNECTION] = 'Keep-Alive';
  247. $options[ConnectionOptions::OPTION_TRACE] = $tracer;
  248. $connection = new Connection($options);
  249. $adminHandler = new AdminHandler($connection);
  250. $adminHandler->getServerVersion();
  251. static::assertTrue($done);
  252. }
  253. /**
  254. * Test the authentication
  255. */
  256. public function testAuthentication()
  257. {
  258. $done = false;
  259. $tracer = function ($type, $data) use (&$done) {
  260. if ($type === 'send') {
  261. static::assertNotFalse(strpos($data, 'Authorization: Basic ' . base64_encode('theQuickBrownFox:jumped-over-it')));
  262. $done = true;
  263. }
  264. };
  265. $options = getConnectionOptions();
  266. $options[ConnectionOptions::OPTION_AUTH_USER] = 'theQuickBrownFox';
  267. $options[ConnectionOptions::OPTION_AUTH_PASSWD] = 'jumped-over-it';
  268. $options[ConnectionOptions::OPTION_TRACE] = $tracer;
  269. $connection = new Connection($options);
  270. $adminHandler = new AdminHandler($connection);
  271. $excepted = false;
  272. try {
  273. $adminHandler->getServerVersion();
  274. } catch (ServerException $exception) {
  275. $excepted = true;
  276. static::assertEquals($exception->getCode(), 401);
  277. }
  278. static::assertTrue($excepted);
  279. }
  280. /**
  281. * Test the basic tracer
  282. */
  283. public function testBasicTracer()
  284. {
  285. //Setup
  286. $basicTracer = function ($type, $data) {
  287. static::assertContains(
  288. $type,
  289. ['send', 'receive'],
  290. 'Basic tracer\'s type should only be \'send\' or \'receive\''
  291. );
  292. static::assertInternalType('string', $data, 'Basic tracer data is not a string!.');
  293. };
  294. $options = getConnectionOptions();
  295. $options[ConnectionOptions::OPTION_TRACE] = $basicTracer;
  296. $connection = new Connection($options);
  297. $collectionHandler = new CollectionHandler($connection);
  298. //Try creating a collection
  299. $collectionHandler->create('ArangoDB_PHP_TestSuite_TestTracer');
  300. //Delete the collection
  301. try {
  302. $collectionHandler->drop('ArangoDB_PHP_TestSuite_TestTracer');
  303. } catch (Exception $e) {
  304. }
  305. }
  306. /**
  307. * Test the enhanced tracer
  308. */
  309. public function testEnhancedTracer()
  310. {
  311. //Setup
  312. $enhancedTracer = function ($data) {
  313. static::assertTrue(
  314. $data instanceof TraceRequest || $data instanceof TraceResponse,
  315. '$data must be instance of TraceRequest or TraceResponse.'
  316. );
  317. static::assertInternalType('array', $data->getHeaders(), 'Headers should be an array!');
  318. static::assertNotEmpty($data->getHeaders(), 'Headers should not be an empty array!');
  319. static::assertInternalType('string', $data->getBody(), 'Body must be a string!');
  320. if ($data instanceof TraceRequest) {
  321. static::assertContains(
  322. $data->getMethod(),
  323. [
  324. HttpHelper::METHOD_DELETE,
  325. HttpHelper::METHOD_GET,
  326. HttpHelper::METHOD_HEAD,
  327. HttpHelper::METHOD_PATCH,
  328. HttpHelper::METHOD_POST,
  329. HttpHelper::METHOD_PUT
  330. ],
  331. 'Invalid http method!'
  332. );
  333. static::assertInternalType('string', $data->getRequestUrl(), 'Request url must be a string!');
  334. static::assertEquals('request', $data->getType());
  335. foreach ($data->getHeaders() as $header => $value) {
  336. static::assertInternalType('string', $value, 'The header value should be a string');
  337. static::assertInternalType('string', $header, 'The header should be a string');
  338. }
  339. } else {
  340. static::assertInternalType('integer', $data->getHttpCode(), 'Http code must be an integer!');
  341. static::assertInternalType(
  342. 'string',
  343. $data->getHttpCodeDefinition(),
  344. 'Http code definition must be a string!'
  345. );
  346. static::assertEquals('response', $data->getType());
  347. static::assertInternalType('float', $data->getTimeTaken());
  348. }
  349. };
  350. $options = getConnectionOptions();
  351. $options[ConnectionOptions::OPTION_TRACE] = $enhancedTracer;
  352. $options[ConnectionOptions::OPTION_ENHANCED_TRACE] = true;
  353. $connection = new Connection($options);
  354. $collectionHandler = new CollectionHandler($connection);
  355. //Try creating a collection
  356. $collectionHandler->create('ArangoDB_PHP_TestSuite_TestTracer');
  357. //Delete the collection
  358. try {
  359. $collectionHandler->drop('ArangoDB_PHP_TestSuite_TestTracer');
  360. } catch (Exception $e) {
  361. }
  362. }
  363. public function tearDown()
  364. {
  365. unset($this->connection);
  366. try {
  367. $this->collectionHandler->drop('ArangoDB_PHP_TestSuite_TestTracer');
  368. } catch (\Exception $e) {
  369. //Silence the exception
  370. }
  371. unset($this->collectionHandler);
  372. }
  373. }