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