Passed
Push — master ( 0838e7...46b6ee )
by Caen
04:34 queued 14s
created

TestResponse   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 33
dl 0
loc 91
rs 10
c 2
b 0
f 0
wmc 11

11 Methods

Rating   Name   Duplication   Size   Complexity  
A dd() 0 3 1
A assertStatus() 0 5 1
A ddText() 0 3 1
A extractText() 0 15 1
A ddHeaders() 0 3 1
A __construct() 0 6 1
A get() 0 7 1
A assertHeader() 0 6 1
A parsePageData() 0 4 1
A assertSeeText() 0 5 1
A assertJson() 0 6 1
1
<?php
2
3
namespace Hyde\RealtimeCompiler\Tests\Integration;
4
5
use GuzzleHttp\Client;
6
use Psr\Http\Message\ResponseInterface;
7
8
class TestResponse
9
{
10
    protected ResponseInterface $response;
11
    protected IntegrationTestCase $test;
12
13
    protected string $html;
14
    protected string $text;
15
16
    public static function get(IntegrationTestCase $test, string $uri): static
17
    {
18
        $guzzle = new Client();
19
20
        $response = $guzzle->get('http://localhost:8080'.$uri, ['http_errors' => false]);
21
22
        return new static($test, $response);
23
    }
24
25
    public function __construct(IntegrationTestCase $test, ResponseInterface $response)
26
    {
27
        $this->test = $test;
28
        $this->response = $response;
29
30
        $this->parsePageData();
31
    }
32
33
    protected function parsePageData(): void
34
    {
35
        $this->html = $this->response->getBody()->getContents();
36
        $this->text = $this->extractText($this->html);
37
    }
38
39
    protected function extractText(string $html): string
40
    {
41
        // Strip script and style tags
42
        $html = preg_replace('/<style\b[^>]*>(.*?)<\/style>/is', '', $html);
43
        $html = preg_replace('/<script\b[^>]*>(.*?)<\/script>/is', '', $html);
44
45
        $html = strip_tags($html);
46
47
        $html = implode("\n", array_map('trim', explode("\n", $html)));
48
49
        // Remove double spaces and double newlines
50
        $html = preg_replace('/\n{2,}/', "\n", $html);
51
        $html = preg_replace('/\s{2,}/', ' ', $html);
52
53
        return trim($html);
54
    }
55
56
    public function dd(): void
57
    {
58
        dd($this->html);
59
    }
60
61
    public function ddText(): void
62
    {
63
        dd($this->text);
64
    }
65
66
    public function ddHeaders(): void
67
    {
68
        dd($this->response->getHeaders());
69
    }
70
71
    public function assertStatus(int $code): static
72
    {
73
        $this->test->assertSame($code, $this->response->getStatusCode());
74
75
        return $this;
76
    }
77
78
    public function assertSeeText(string $text): static
79
    {
80
        $this->test->assertStringContainsString($text, $this->text);
81
82
        return $this;
83
    }
84
85
    public function assertHeader(string $header, string $value): static
86
    {
87
        $this->test->assertArrayHasKey($header, $this->response->getHeaders());
88
        $this->test->assertContains($value, $this->response->getHeader($header));
89
90
        return $this;
91
    }
92
93
    public function assertJson(array $data): static
94
    {
95
        $this->test->assertJson($this->html);
96
        $this->test->assertSame($data, json_decode($this->html, true));
97
98
        return $this;
99
    }
100
}
101