ynloultratech /
graphql-bundle
| 1 | <?php |
||
| 2 | /******************************************************************************* |
||
| 3 | * This file is part of the GraphQL Bundle package. |
||
| 4 | * |
||
| 5 | * (c) YnloUltratech <[email protected]> |
||
| 6 | * |
||
| 7 | * For the full copyright and license information, please view the LICENSE |
||
| 8 | * file that was distributed with this source code. |
||
| 9 | ******************************************************************************/ |
||
| 10 | |||
| 11 | namespace Ynlo\GraphQLBundle\Behat\Context; |
||
| 12 | |||
| 13 | use Behat\Behat\Context\Context; |
||
| 14 | use Behat\Behat\Definition\Call\Then; |
||
| 15 | use Behat\Behat\Hook\Scope\BeforeScenarioScope; |
||
| 16 | use PHPUnit\Framework\Assert; |
||
| 17 | use PHPUnit\Framework\AssertionFailedError; |
||
| 18 | use Symfony\Component\HttpFoundation\Response; |
||
| 19 | use Ynlo\GraphQLBundle\Behat\Client\ClientAwareInterface; |
||
| 20 | use Ynlo\GraphQLBundle\Behat\Client\ClientAwareTrait; |
||
| 21 | use Ynlo\GraphQLBundle\Util\Json; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Context to work with latest response |
||
| 25 | */ |
||
| 26 | final class ResponseContext implements Context, ClientAwareInterface |
||
| 27 | { |
||
| 28 | use ClientAwareTrait; |
||
| 29 | |||
| 30 | /** @var GraphQLContext */ |
||
| 31 | private $graphQLContext; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @BeforeScenario |
||
| 35 | */ |
||
| 36 | public function gatherContexts(BeforeScenarioScope $scope) |
||
| 37 | { |
||
| 38 | $environment = $scope->getEnvironment(); |
||
| 39 | |||
| 40 | $this->graphQLContext = $environment->getContext('Ynlo\GraphQLBundle\Behat\Context\GraphQLContext'); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Checks, that current response status is equal to specified |
||
| 45 | * Example: Then the response status code should be 200 |
||
| 46 | * Example: And the response status code should be 400 |
||
| 47 | * |
||
| 48 | * @Then /^the response status code should be (?P<code>\d+)$/ |
||
| 49 | */ |
||
| 50 | public function assertResponseStatus($code) |
||
| 51 | { |
||
| 52 | Assert::assertEquals($this->client->getResponse()->getStatusCode(), $code); |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Assert that latest response is a GraphQL error with the given message |
||
| 57 | * |
||
| 58 | * @Then /^the response is GraphQL error with "([^"]*)"$/ |
||
| 59 | */ |
||
| 60 | public function theResponseIsGraphQLErrorWith(string $message) |
||
| 61 | { |
||
| 62 | $this->assertResponseStatus(Response::HTTP_OK); |
||
| 63 | |||
| 64 | //success GraphQL response should not contains errors |
||
| 65 | if ($this->client->getGraphQL()) { |
||
| 66 | if ($this->isValidGraphQLResponse() && $errors = $this->getGraphQLResponseError()) { |
||
| 67 | $errorsStack = ''; |
||
| 68 | foreach ($errors as $error) { |
||
| 69 | $errorsStack .= $error->message."\n"; |
||
| 70 | } |
||
| 71 | |||
| 72 | Assert::assertContains($message, $errorsStack); |
||
| 73 | } else { |
||
| 74 | $this->graphQLContext->debugLastQuery(); |
||
| 75 | throw new AssertionFailedError('The response is not the expected error response.'); |
||
| 76 | } |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Assert that latest response is a GraphQL error with the given code |
||
| 82 | * |
||
| 83 | * @Then /^the response is GraphQL error with code "([^"]*)"$/ |
||
| 84 | */ |
||
| 85 | public function theResponseIsGraphQLErrorWithCode(string $code) |
||
| 86 | { |
||
| 87 | $this->assertResponseStatus(Response::HTTP_OK); |
||
| 88 | |||
| 89 | //success GraphQL response should not contains errors |
||
| 90 | if ($this->client->getGraphQL()) { |
||
| 91 | if ($this->isValidGraphQLResponse() && $errors = $this->getGraphQLResponseError()) { |
||
| 92 | $errorsStack = ''; |
||
| 93 | foreach ($errors as $error) { |
||
| 94 | $errorsStack .= $error->code."\n"; |
||
| 95 | } |
||
| 96 | |||
| 97 | Assert::assertContains($code, $errorsStack); |
||
| 98 | } else { |
||
| 99 | $this->graphQLContext->debugLastQuery(); |
||
| 100 | throw new AssertionFailedError('The response is not the expected error response.'); |
||
| 101 | } |
||
| 102 | } |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @Then the response is OK |
||
| 107 | */ |
||
| 108 | public function assertResponseIsOk() |
||
| 109 | { |
||
| 110 | $this->assertResponseStatus(Response::HTTP_OK); |
||
| 111 | |||
| 112 | //success GraphQL response should not contains errors |
||
| 113 | if ($this->client->getGraphQL()) { |
||
| 114 | if (!$this->isValidGraphQLResponse() || $this->getGraphQLResponseError()) { |
||
| 115 | $this->graphQLContext->debugLastQuery(); |
||
| 116 | throw new AssertionFailedError('The response is not OK, invalid response or contains some error.'); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | protected function isValidGraphQLResponse() |
||
| 122 | { |
||
| 123 | $content = $this->client->getResponse()->getContent(); |
||
| 124 | Assert::assertJson((string) $content, 'Invalid server response'); |
||
| 125 | $response = json_decode($content, true); |
||
| 126 | |||
| 127 | return $response && (isset($response['errors']) || isset($response['data'])); |
||
| 128 | } |
||
| 129 | |||
| 130 | protected function getGraphQLResponseError() |
||
| 131 | { |
||
| 132 | if ($this->isValidGraphQLResponse()) { |
||
| 133 | return Json::getValue($this->client->getResponse(), 'errors'); |
||
| 134 | } |
||
| 135 | |||
| 136 | return null; |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 |