8
8
class ClientTest extends \PHPUnit_Framework_TestCase
9
9
{
10
10
private $ criticBaseUrl ;
11
- private $ criticClient ;
12
- private $ personaConfig ;
13
11
private $ postFields ;
14
- private $ cacheDriver ;
15
12
16
13
protected function setUp ()
17
14
{
18
15
$ this ->criticBaseUrl = 'http://listreviews.talis.com/test/reviews ' ;
19
- $ this ->criticClient = new \Talis \Critic \Client ($ this ->criticBaseUrl );
20
-
21
- $ this ->cacheDriver = new \Doctrine \Common \Cache \ArrayCache ();
22
- $ this ->personaConfig = [
23
- 'userAgent ' => 'userAgentVal ' ,
24
- 'persona_host ' => 'persona_host_val ' ,
25
- 'persona_oauth_route ' => 'persona_oauth_route_val ' ,
26
- 'persona_oauth_route ' => 'persona_oauth_route_val ' ,
27
- 'cacheBackend ' => $ this ->cacheDriver ,
28
- ];
29
-
30
16
$ this ->postFields = ['listUri ' => 'http://somelist ' ];
31
17
}
32
18
@@ -37,7 +23,6 @@ public function testCreateReviewSuccess()
37
23
new \GuzzleHttp \Psr7 \Response (201 , [], json_encode (['id ' => $ id ])),
38
24
]);
39
25
40
- $ criticClient ->setPersonaConnectValues ($ this ->personaConfig );
41
26
$ this ->assertEquals ($ id , $ criticClient ->createReview ($ this ->postFields , '' , '' ));
42
27
}
43
28
@@ -52,7 +37,6 @@ public function testCreateReviewException()
52
37
new \GuzzleHttp \Psr7 \Response (200 , [], json_encode (['id ' => '1234 ' ])),
53
38
]);
54
39
55
- $ criticClient ->setPersonaConnectValues ($ this ->personaConfig );
56
40
$ criticClient ->createReview ($ this ->postFields , '' , '' );
57
41
}
58
42
@@ -67,8 +51,6 @@ public function testCreateReviewGuzzleException()
67
51
new \GuzzleHttp \Psr7 \Response (401 , [], json_encode ([])),
68
52
]);
69
53
70
- $ criticClient ->setPersonaConnectValues ($ this ->personaConfig );
71
-
72
54
$ criticClient ->createReview (
73
55
$ this ->postFields ,
74
56
'someClientId ' ,
@@ -82,41 +64,75 @@ public function testCreateReviewGuzzleException()
82
64
*/
83
65
public function testCreateReviewWithInvalidPersonaConfigFails ()
84
66
{
85
- $ this ->criticClient ->setPersonaConnectValues ($ this ->personaConfig );
86
-
87
- $ this ->criticClient ->createReview (
67
+ $ criticClient = new \Talis \Critic \Client ($ this ->criticBaseUrl );
68
+ $ criticClient ->setPersonaConnectValues ([
69
+ 'userAgent ' => 'userAgentVal ' ,
70
+ 'persona_host ' => 'persona_host_val ' ,
71
+ 'persona_oauth_route ' => 'persona_oauth_route_val ' ,
72
+ 'persona_oauth_route ' => 'persona_oauth_route_val ' ,
73
+ 'cacheBackend ' => new \Doctrine \Common \Cache \ArrayCache (),
74
+ ]);
75
+ $ criticClient ->createReview (
88
76
$ this ->postFields ,
89
77
'someClientId ' ,
90
78
'someClientSecret '
91
79
);
92
80
}
93
81
82
+ public function testCreateReviewRequestIsSentOutCorrectly ()
83
+ {
84
+ $ history = [];
85
+ $ criticClient = $ this ->getClientWithMockResponses ([
86
+ new \GuzzleHttp \Psr7 \Response (201 , [], json_encode (['id ' => '1234567890 ' ])),
87
+ ], $ history );
88
+ $ criticClient ->createReview ($ this ->postFields , '' , '' );
89
+
90
+ /** @var \Psr\Http\Message\RequestInterface $request */
91
+ $ request = array_pop ($ history )['request ' ];
92
+
93
+ $ this ->assertTrue ($ request ->hasHeader ('Content-Type ' ), 'Content-Type header is missing ' );
94
+ $ this ->assertStringStartsWith ('application/x-www-form-urlencoded ' , $ request ->getHeader ('Content-Type ' )[0 ]);
95
+ parse_str ((string ) $ request ->getBody (), $ body );
96
+ $ this ->assertInternalType ('array ' , $ body );
97
+ $ this ->assertEquals ($ this ->postFields , $ body );
98
+ }
99
+
94
100
/**
95
101
* Gets the client with mocked HTTP responses.
96
102
*
97
103
* @param \GuzzleHttp\Psr7\Response[] $responses The responses
104
+ * @param array $history History middleware container
98
105
* @return \Talis\Critic\Client|\PHPUnit_Framework_MockObject_MockObject The client.
99
106
*/
100
- private function getClientWithMockResponses (array $ responses )
107
+ private function getClientWithMockResponses (array $ responses, array & $ history = null )
101
108
{
102
109
$ mockHandler = new \GuzzleHttp \Handler \MockHandler ($ responses );
103
110
$ handlerStack = \GuzzleHttp \HandlerStack::create ($ mockHandler );
111
+
112
+ if (isset ($ history )) {
113
+ $ handlerStack ->push (\GuzzleHttp \Middleware::history ($ history ));
114
+ }
115
+
104
116
$ httpClient = new \GuzzleHttp \Client (['handler ' => $ handlerStack ]);
105
117
118
+ $ tokenClient = $ this ->getMockBuilder (\Talis \Persona \Client \Tokens::class)
119
+ ->disableOriginalConstructor ()
120
+ ->setMethods (['obtainNewToken ' ])
121
+ ->getMock ();
122
+ $ tokenClient ->method ('obtainNewToken ' )
123
+ ->willReturn (['access_token ' => 'TOKEN ' ]);
124
+
125
+ /** @var \Talis\Critic\Client|\PHPUnit_Framework_MockObject_MockObject */
106
126
$ criticClient = $ this ->getMockBuilder (\Talis \Critic \Client::class)
107
- ->setMethods (['getHTTPClient ' , 'getHeaders ' ])
127
+ ->setMethods (['getHTTPClient ' , 'getTokenClient ' ])
108
128
->setConstructorArgs ([$ this ->criticBaseUrl ])
109
129
->getMock ();
110
130
111
131
$ criticClient ->method ('getHTTPClient ' )
112
132
->willReturn ($ httpClient );
113
133
114
- $ criticClient ->expects ($ this ->once ())
115
- ->method ('getHeaders ' )
116
- ->willReturn ([
117
- 'Content-Type ' => 'application/json ' ,
118
- 'Authorization ' => 'Bearer TOKEN ' ,
119
- ]);
134
+ $ criticClient ->method ('getTokenClient ' )
135
+ ->willReturn ($ tokenClient );
120
136
121
137
return $ criticClient ;
122
138
}
0 commit comments