1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Postpay\Tests\Http; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use Postpay\Exceptions\RESTfulException; |
7
|
|
|
use Postpay\Http\Request; |
8
|
|
|
use Postpay\Http\Response; |
9
|
|
|
|
10
|
|
|
class ResponseTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
protected $request; |
13
|
|
|
|
14
|
|
|
protected function setUp() |
15
|
|
|
{ |
16
|
|
|
$this->request = new Request('GET'); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function testGetRequest() |
20
|
|
|
{ |
21
|
|
|
$response = new Response($this->request); |
22
|
|
|
|
23
|
|
|
self::assertSame($this->request, $response->getRequest()); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function testGetStatusCode() |
27
|
|
|
{ |
28
|
|
|
$response = new Response($this->request, 200); |
29
|
|
|
self::assertSame(200, $response->getStatusCode()); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function testGetHeaders() |
33
|
|
|
{ |
34
|
|
|
$headers = ['test' => true]; |
35
|
|
|
$response = new Response($this->request, 200, $headers); |
36
|
|
|
|
37
|
|
|
self::assertSame($headers, $response->getHeaders()); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testJson() |
41
|
|
|
{ |
42
|
|
|
$json = ['test' => true]; |
43
|
|
|
$response = new Response($this->request, 200, [], json_encode($json)); |
44
|
|
|
|
45
|
|
|
self::assertSame($json, $response->json()); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function testIsError() |
49
|
|
|
{ |
50
|
|
|
$body = json_encode([RESTfulException::ERROR_KEY => true]); |
51
|
|
|
$response = new Response($this->request, 400, [], $body); |
52
|
|
|
|
53
|
|
|
self::assertTrue($response->isError()); |
54
|
|
|
|
55
|
|
|
self::assertInstanceOf( |
56
|
|
|
RESTfulException::class, |
57
|
|
|
$response->getThrownException() |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testDecodeNonJson() |
62
|
|
|
{ |
63
|
|
|
$response = new Response($this->request, 200, [], '-'); |
64
|
|
|
|
65
|
|
|
self::assertEmpty($response->json()); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|