ResponseTest.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace GraphAware\NeoClient\Formatter\Tests\Unit;
  3. use GuzzleHttp\Psr7\Response as HttpResponse;
  4. use GraphAware\NeoClient\Formatter\Response;
  5. /**
  6. * Class ResponseTest
  7. * @package GraphAware\NeoClient\Formatter\Tests\Unit
  8. *
  9. * @group response
  10. */
  11. class ResponseTest extends \PHPUnit_Framework_TestCase
  12. {
  13. /**
  14. * @var \GraphAware\NeoClient\Formatter\Response
  15. */
  16. protected $response;
  17. public function setUp()
  18. {
  19. $this->response = new Response($this->loadResponse(__DIR__.'/../_resources/response-profile.json'));
  20. }
  21. public function testConstruction()
  22. {
  23. $this->assertInstanceOf('\Psr\Http\Message\ResponseInterface', $this->response->getHttpResponse());
  24. $this->assertEmpty($this->response->getResults());
  25. }
  26. public function testResponseIsAnalysed()
  27. {
  28. $this->assertInternalType('array', $this->response->getBody());
  29. $this->assertFalse($this->response->hasError());
  30. }
  31. public function testResponseWithError()
  32. {
  33. $response = new Response($this->loadResponse(__DIR__.'/../_resources/response-with-fail.json'));
  34. $this->assertTrue($response->hasError());
  35. $this->assertInstanceOf('GraphAware\NeoClient\Formatter\Neo4jError', $response->getError());
  36. }
  37. public function testGetResultWillReturnNullWhenNoResultsArePresent()
  38. {
  39. $this->assertNull($this->response->getSingleResultOrNull());
  40. }
  41. /**
  42. * @expectedException \RuntimeException
  43. */
  44. public function testExceptionIsThrownWhenNoResultsButExpected()
  45. {
  46. $response = new Response($this->loadResponse(__DIR__.'/../_resources/response-with-fail.json'));
  47. $response->getSingleResult();
  48. }
  49. /**
  50. * @expectedException \RuntimeException
  51. */
  52. public function testExceptionIsThrownWhenCallingSingleResultButMoreResultsArePresent()
  53. {
  54. $response = new Response($this->loadResponse(__DIR__.'/../_resources/response-profile.json'));
  55. $response->getSingleResult();
  56. }
  57. /**
  58. * @param $file
  59. * @return \GuzzleHttp\Psr7\Response
  60. */
  61. private function loadResponse($file)
  62. {
  63. $body = file_get_contents($file);
  64. $httpResponse = new HttpResponse(200, array(), $body);
  65. return $httpResponse;
  66. }
  67. }