1
|
|
|
<?php |
2
|
|
|
namespace Chadicus\Marvel\Api; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Defines unit tests for the Response class. |
6
|
|
|
* |
7
|
|
|
* @coversDefaultClass \Chadicus\Marvel\Api\Response |
8
|
|
|
*/ |
9
|
|
|
final class ResponseTest extends \PHPUnit_Framework_TestCase |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Verify basic functionality of the response object. |
13
|
|
|
* |
14
|
|
|
* @test |
15
|
|
|
* @covers ::__construct |
16
|
|
|
* @covers ::getHttpCode |
17
|
|
|
* @covers ::getHeaders |
18
|
|
|
* @covers ::getBody |
19
|
|
|
* |
20
|
|
|
* @return void |
21
|
|
|
*/ |
22
|
|
|
public function construct() |
23
|
|
|
{ |
24
|
|
|
$httpCode = 200; |
25
|
|
|
$headers = ['Content-Type' => 'text/json']; |
26
|
|
|
$body = ['doesnt' => 'matter']; |
27
|
|
|
$response = new Response($httpCode, $headers, $body); |
28
|
|
|
$this->assertSame($httpCode, $response->getHttpCode()); |
29
|
|
|
$this->assertSame($headers, $response->getHeaders()); |
30
|
|
|
$this->assertSame($body, $response->getBody()); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Verify basic behavior of getDataWrapper() |
35
|
|
|
* |
36
|
|
|
* @test |
37
|
|
|
* @covers ::getDataWrapper |
38
|
|
|
* |
39
|
|
|
* @return void |
40
|
|
|
*/ |
41
|
|
|
public function getDataWrapper() |
42
|
|
|
{ |
43
|
|
|
$body = [ |
44
|
|
|
'code' => 200, |
45
|
|
|
'status' => 'ok', |
46
|
|
|
'copyright' => 'a copyright', |
47
|
|
|
'attributionText' => 'a attributionText', |
48
|
|
|
'attributionHTML' => 'a attributionHTML', |
49
|
|
|
'etag' => 'a etag', |
50
|
|
|
]; |
51
|
|
|
|
52
|
|
|
$response = new Response(200, ['Content-Type' => 'application/json'], $body); |
53
|
|
|
|
54
|
|
|
$this->assertSame($body['code'], $response->getDataWrapper()->getCode()); |
55
|
|
|
$this->assertSame($body['status'], $response->getDataWrapper()->getStatus()); |
56
|
|
|
$this->assertSame($body['copyright'], $response->getDataWrapper()->getCopyRight()); |
57
|
|
|
$this->assertSame($body['attributionText'], $response->getDataWrapper()->getAttributionText()); |
58
|
|
|
$this->assertSame($body['attributionHTML'], $response->getDataWrapper()->getAttributionHTML()); |
59
|
|
|
$this->assertSame($body['etag'], $response->getDataWrapper()->getEtag()); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Verify invalid constructor parameters cause exceptions. |
64
|
|
|
* |
65
|
|
|
* @param integer $httpCode The http response code. |
66
|
|
|
* @param array $headers The response headers. |
67
|
|
|
* @param array $body The response body. |
68
|
|
|
* |
69
|
|
|
* @test |
70
|
|
|
* @covers ::__construct |
71
|
|
|
* @dataProvider constructorBadData |
72
|
|
|
* @expectedException \InvalidArgumentException |
73
|
|
|
* |
74
|
|
|
* @return void |
75
|
|
|
*/ |
76
|
|
|
public function constructWithInvalidParameters($httpCode, array $headers, array $body) |
77
|
|
|
{ |
78
|
|
|
new Response($httpCode, $headers, $body); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Data provider for constructWithInvalidParameters. |
83
|
|
|
* |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
|
View Code Duplication |
public function constructorBadData() |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
return [ |
89
|
|
|
'httpCode is not a number' => ['NaN', [], []], |
90
|
|
|
'httpCode is less than 100' => [99, [], []], |
91
|
|
|
'httpCode is greater than 600' => [601, [], []], |
92
|
|
|
]; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.