Completed
Push — 1.5 ( c5bd05...970e37 )
by Rafał
10:47 queued 10s
created

WebhookContext::convertToJson()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
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 Behatch\Json\Json;
10
use GuzzleHttp\Client;
11
12
class WebhookContext implements Context
13
{
14
    /**
15
     * @Given The payload received by :url webhook should be equal to:
16
     */
17
    public function thePayloadReceivedByWebhookShouldBeEqualTo(string $url, PyStringNode $body): void
18
    {
19
        $client = new Client();
20
        $response = $client->request('GET', $url);
21
        $expected = str_replace('\\"', '"', $body);
22
        $actual = $response->getBody()->getContents();
23
24
        $expected = $this->convertToJson($expected);
25
        $actual = $this->convertToJson($actual);
26
27
        if ($expected !== $actual) {
28
            throw new \Exception("The body $actual is not equal to $expected");
29
        }
30
    }
31
32
    private function convertToJson(string $value): string
33
    {
34
        try {
35
            $value = new Json($value);
36
        } catch (\Exception $e) {
37
            throw new \Exception('The expected JSON is not a valid');
38
        }
39
40
        return (string) $value;
41
    }
42
}
43