Completed
Pull Request — 1.5 (#767)
by Paweł
10:46
created

WebhookContext::normalizeJson()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
dl 9
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SWP\Behat\Contexts;
6
7
use Behat\Behat\Context\Context;
8
use Behat\Gherkin\Node\PyStringNode;
9
use GuzzleHttp\Client;
10
11
class WebhookContext implements Context
12
{
13
    /**
14
     * @Given The payload received by :url webhook should be equal to:
15
     */
16
    public function thePayloadReceivedByWebhookShouldBeEqualTo(string $url, PyStringNode $body): void
17
    {
18
        $client = new Client();
19
        $response = $client->request('GET', $url);
20
        $actual = $response->getBody()->getContents();
21
22
        $expected = $this->normalizeJson($body->getRaw());
23
        $actual = $this->normalizeJson($actual);
24
25
        if ($expected !== $actual) {
26
            throw new \Exception("The body $actual is not equal to $expected");
27
        }
28
    }
29
30 View Code Duplication
    private function normalizeJson(string $value): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
31
    {
32
        $value = \json_decode($value, true);
33
        if (JSON_ERROR_NONE !== \json_last_error()) {
34
            throw new \Exception("The string '$value' is not valid json");
35
        }
36
37
        return \json_encode($value);
38
    }
39
}
40