Passed
Push — master ( 319fcb...7a107a )
by Rafael
04:41
created

ResponseContext   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 15
dl 0
loc 81
c 0
b 0
f 0
ccs 0
cts 44
cp 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getGraphQLResponseError() 0 7 2
A gatherContexts() 0 5 1
A assertResponseIsOk() 0 9 4
A theResponseIsGraphQLErrorWith() 0 11 4
A isValidGraphQLResponse() 0 7 3
A assertResponseStatus() 0 3 1
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
                Assert::assertContains($message, $errors[0]->message ?? null);
68
            } else {
69
                $this->graphQLContext->debugLastQuery();
70
                throw new AssertionFailedError('The response is not the expected error response.');
71
            }
72
        }
73
    }
74
75
    /**
76
     * @Then the response is OK
77
     */
78
    public function assertResponseIsOk()
79
    {
80
        $this->assertResponseStatus(Response::HTTP_OK);
81
82
        //success GraphQL response should not contains errors
83
        if ($this->client->getGraphQL()) {
84
            if (!$this->isValidGraphQLResponse() || $this->getGraphQLResponseError()) {
85
                $this->graphQLContext->debugLastQuery();
86
                throw new AssertionFailedError('The response is not a success response.');
87
            }
88
        }
89
    }
90
91
    protected function isValidGraphQLResponse()
92
    {
93
        $content = $this->client->getResponse()->getContent();
94
        Assert::assertJson((string) $content, 'Invalid server response');
95
        $response = json_decode($content, true);
96
97
        return $response && (isset($response['errors']) || isset($response['data']));
98
    }
99
100
    protected function getGraphQLResponseError()
101
    {
102
        if ($this->isValidGraphQLResponse()) {
103
            return Json::getValue($this->client->getResponse(), 'errors');
104
        }
105
106
        return null;
107
    }
108
}
109
110