forked from pavlokomarov/roach-php-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResponseTest.php
More file actions
135 lines (110 loc) · 3.62 KB
/
ResponseTest.php
File metadata and controls
135 lines (110 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
declare(strict_types=1);
/**
* Copyright (c) 2021 Kai Sassnowski
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* @see https://github.com/roach-php/roach
*/
namespace RoachPHP\Tests\Http;
use Generator;
use GuzzleHttp\Psr7\Stream;
use PHPUnit\Framework\TestCase;
use RoachPHP\Http\Response;
use RoachPHP\Support\DroppableInterface;
use RoachPHP\Tests\InteractsWithRequestsAndResponses;
use RoachPHP\Tests\Support\DroppableTest;
/**
* @internal
*/
final class ResponseTest extends TestCase
{
use DroppableTest;
use InteractsWithRequestsAndResponses;
public function testCanAccessDomCrawlerDirectlyFromResponse(): void
{
$response = $this->makeResponse(body: '<html lang="en"><body><a href="https://roach-php.dev">Docs</a></body></html>');
$links = $response->filter('a')->links();
self::assertCount(1, $links);
}
/**
* @dataProvider responseCodeProvider
*/
public function testCanRetrieveStatusCodeOfOriginalResponse(int $statusCode): void
{
$response = new Response(new \GuzzleHttp\Psr7\Response($statusCode), $this->makeRequest());
self::assertSame($statusCode, $response->getStatus());
}
/**
* @dataProvider responseBodyProvider
*/
public function testCanRetrieveHtmlBodyOfOriginalResponse(callable $getBody): void
{
$body = '<html lang="en"><body><p>Hello, world!</p></body>';
$response = new Response(
new \GuzzleHttp\Psr7\Response(body: $getBody($body)),
$this->makeRequest(),
);
self::assertSame($body, $response->getBody());
}
public function testCanUpdateResponseBody(): void
{
$originalBody = '<html lang="en"><body><p>Old</p></body></html>';
$newBody = '<html lang="en"><body><p>New</p></body></html>';
$response = new Response(
new \GuzzleHttp\Psr7\Response(body: $originalBody),
$this->makeRequest(),
);
$response = $response->withBody($newBody);
self::assertSame($newBody, $response->getBody());
}
public function testUpdatingResponseBodyUpdatesCrawler(): void
{
$originalBody = '<html lang="en"><body><p>Old</p></body></html>';
$newBody = '<html lang="en"><body><p>New</p></body></html>';
$response = new Response(
new \GuzzleHttp\Psr7\Response(body: $originalBody),
$this->makeRequest(),
);
$response = $response->withBody($newBody);
self::assertSame('New', $response->filter('p')->text(''));
}
public function responseCodeProvider(): Generator
{
yield from [
[200],
[300],
[301],
[302],
[400],
[404],
[500],
];
}
public function responseBodyProvider(): Generator
{
yield from [
'string' => [static fn (string $body) => $body],
'stream' => [static function (string $body) {
$stream = \fopen('php://memory', 'r+b');
\fwrite($stream, $body);
\rewind($stream);
return $stream;
}],
'StreamInterface' => [static function (string $body) {
$stream = \fopen('php://memory', 'r+b');
\fwrite($stream, $body);
\rewind($stream);
return new Stream($stream);
}],
];
}
protected function createDroppable(): DroppableInterface
{
return $this->makeResponse(
$this->makeRequest(),
);
}
}