Completed
Push — master ( 179b7b...4a62fb )
by Rafael
08:47
created

ResponseContext::isValidGraphQLResponse()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
ccs 0
cts 6
cp 0
rs 9.4285
cc 3
eloc 4
nc 3
nop 0
crap 12
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
The method getContext() does not exist on Behat\Testwork\Environment\Environment. It seems like you code against a sub-type of Behat\Testwork\Environment\Environment such as Behat\Behat\Context\Envi...lizedContextEnvironment or Behat\Behat\Context\Envi...lizedContextEnvironment. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

40
        /** @scrutinizer ignore-call */ 
41
        $this->graphQLContext = $environment->getContext('Ynlo\GraphQLBundle\Behat\Context\GraphQLContext');
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($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
     * @Then the response is OK
82
     */
83
    public function assertResponseIsOk()
84
    {
85
        $this->assertResponseStatus(Response::HTTP_OK);
86
87
        //success GraphQL response should not contains errors
88
        if ($this->client->getGraphQL()) {
89
            if (!$this->isValidGraphQLResponse() || $this->getGraphQLResponseError()) {
90
                $this->graphQLContext->debugLastQuery();
91
                throw new AssertionFailedError('The response is not OK, invalid response or contains some error.');
92
            }
93
        }
94
    }
95
96
    protected function isValidGraphQLResponse()
97
    {
98
        $content = $this->client->getResponse()->getContent();
99
        Assert::assertJson((string) $content, 'Invalid server response');
100
        $response = json_decode($content, true);
101
102
        return $response && (isset($response['errors']) || isset($response['data']));
103
    }
104
105
    protected function getGraphQLResponseError()
106
    {
107
        if ($this->isValidGraphQLResponse()) {
108
            return Json::getValue($this->client->getResponse(), 'errors');
109
        }
110
111
        return null;
112
    }
113
}
114
115