1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ChadicusTest\Slim\OAuth2\Http; |
4
|
|
|
|
5
|
|
|
use Chadicus\Slim\OAuth2\Http\ResponseBridge; |
6
|
|
|
use OAuth2\Response; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Unit tests for the \Chadicus\Slim\OAuth2\Http\ResponseBridge class. |
10
|
|
|
* |
11
|
|
|
* @coversDefaultClass \Chadicus\Slim\OAuth2\Http\ResponseBridge |
12
|
|
|
* @covers ::<private> |
13
|
|
|
*/ |
14
|
|
|
final class ResponseBridgeTest extends \PHPUnit_Framework_TestCase |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Verify basic behavior of fromOAuth2() |
18
|
|
|
* |
19
|
|
|
* @test |
20
|
|
|
* @covers ::fromOAuth2 |
21
|
|
|
* |
22
|
|
|
* @return void |
23
|
|
|
*/ |
24
|
|
|
public function fromOAuth2() |
25
|
|
|
{ |
26
|
|
|
$oauth2Response = new Response( |
27
|
|
|
['foo' => 'bar', 'abc' => '123'], |
28
|
|
|
200, |
29
|
|
|
['Content-Type' => 'application/json', 'Accept-Encoding' => 'gzip, deflate'] |
30
|
|
|
); |
31
|
|
|
|
32
|
|
|
$slimResponse = ResponseBridge::fromOAuth2($oauth2Response); |
33
|
|
|
|
34
|
|
|
$this->assertSame(200, $slimResponse->getStatusCode()); |
35
|
|
|
$this->assertSame( |
36
|
|
|
[ |
37
|
|
|
'Content-Type' => [ |
38
|
|
|
'application/json', |
39
|
|
|
], |
40
|
|
|
'Accept-Encoding' => [ |
41
|
|
|
'gzip', |
42
|
|
|
'deflate', |
43
|
|
|
], |
44
|
|
|
], |
45
|
|
|
$slimResponse->getHeaders() |
46
|
|
|
); |
47
|
|
|
|
48
|
|
|
$this->assertSame(json_encode(['foo' => 'bar', 'abc' => '123']), (string)$slimResponse->getBody()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Verify behavior of fromOAuth2() with empty response body. |
53
|
|
|
* |
54
|
|
|
* @test |
55
|
|
|
* @covers ::fromOAuth2 |
56
|
|
|
* |
57
|
|
|
* @return void |
58
|
|
|
*/ |
59
|
|
View Code Duplication |
public function fromOAuth2EmptyBody() |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
$oauth2Response = new Response( |
62
|
|
|
[], |
63
|
|
|
204, |
64
|
|
|
['Content-Type' => 'application/json'] |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
$slimResponse = ResponseBridge::fromOAuth2($oauth2Response); |
68
|
|
|
|
69
|
|
|
$this->assertSame(204, $slimResponse->getStatusCode()); |
70
|
|
|
$this->assertSame( |
71
|
|
|
[ |
72
|
|
|
'Content-Type' => [ |
73
|
|
|
'application/json', |
74
|
|
|
], |
75
|
|
|
], |
76
|
|
|
$slimResponse->getHeaders() |
77
|
|
|
); |
78
|
|
|
|
79
|
|
|
$this->assertSame('', (string)$slimResponse->getBody()); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Verify response can be written to when empty. |
84
|
|
|
* |
85
|
|
|
* @test |
86
|
|
|
* @covers ::fromOAuth2 |
87
|
|
|
* |
88
|
|
|
* @return void |
89
|
|
|
*/ |
90
|
|
View Code Duplication |
public function fromOAuth2WritableEmptyBody() |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
$oauth2Response = new Response( |
93
|
|
|
[], |
94
|
|
|
204, |
95
|
|
|
['Content-Type' => 'application/json'] |
96
|
|
|
); |
97
|
|
|
|
98
|
|
|
$slimResponse = ResponseBridge::fromOAuth2($oauth2Response); |
99
|
|
|
|
100
|
|
|
$slimResponse->getBody()->write('I was here'); |
101
|
|
|
|
102
|
|
|
$this->assertSame(204, $slimResponse->getStatusCode()); |
103
|
|
|
$this->assertSame( |
104
|
|
|
[ |
105
|
|
|
'Content-Type' => [ |
106
|
|
|
'application/json', |
107
|
|
|
], |
108
|
|
|
], |
109
|
|
|
$slimResponse->getHeaders() |
110
|
|
|
); |
111
|
|
|
|
112
|
|
|
$this->assertSame('I was here', (string)$slimResponse->getBody()); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
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.