/
MockResponseTest.php
68 lines (57 loc) · 2.23 KB
/
MockResponseTest.php
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
<?php
namespace Symfony\Component\HttpClient\Tests\Response;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpClient\Exception\JsonException;
use Symfony\Component\HttpClient\Response\MockResponse;
/**
* Test methods from Symfony\Component\HttpClient\Response\ResponseTrait.
*/
class MockResponseTest extends TestCase
{
public function testToArray()
{
$data = ['color' => 'orange', 'size' => 42];
$response = new MockResponse(json_encode($data));
$response = MockResponse::fromRequest('GET', 'https://example.com/file.json', [], $response);
$this->assertSame($data, $response->toArray());
}
/**
* @dataProvider toArrayErrors
*/
public function testToArrayError($content, $responseHeaders, $message)
{
$this->expectException(JsonException::class);
$this->expectExceptionMessage($message);
$response = new MockResponse($content, ['response_headers' => $responseHeaders]);
$response = MockResponse::fromRequest('GET', 'https://example.com/file.json', [], $response);
$response->toArray();
}
public function toArrayErrors()
{
yield [
'content' => '{}',
'responseHeaders' => ['content-type' => 'plain/text'],
'message' => 'Response content-type is "plain/text" while a JSON-compatible one was expected for "https://example.com/file.json".',
];
yield [
'content' => 'not json',
'responseHeaders' => [],
'message' => 'Syntax error for "https://example.com/file.json".',
];
yield [
'content' => '[1,2}',
'responseHeaders' => [],
'message' => 'State mismatch (invalid or malformed JSON) for "https://example.com/file.json".',
];
yield [
'content' => '"not an array"',
'responseHeaders' => [],
'message' => 'JSON content was expected to decode to an array, string returned for "https://example.com/file.json".',
];
yield [
'content' => '8',
'responseHeaders' => [],
'message' => 'JSON content was expected to decode to an array, integer returned for "https://example.com/file.json".',
];
}
}