Skip to content

Commit 0259927

Browse files
author
Grzegorz Rajchman
authored
PLAT-3846 fix Critic client (#25)
1 parent 2a1e2e7 commit 0259927

File tree

2 files changed

+46
-30
lines changed

2 files changed

+46
-30
lines changed

src/Talis/Critic/Client.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ protected function getHeaders($clientId, $clientSecret, array $params = [])
148148
);
149149

150150
return [
151-
'Content-Type' => 'application/json',
151+
'Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8',
152152
'Authorization' => "Bearer {$token['access_token']}",
153153
];
154154
}

test/unit/Critic/ClientTest.php

+45-29
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,11 @@
88
class ClientTest extends \PHPUnit_Framework_TestCase
99
{
1010
private $criticBaseUrl;
11-
private $criticClient;
12-
private $personaConfig;
1311
private $postFields;
14-
private $cacheDriver;
1512

1613
protected function setUp()
1714
{
1815
$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-
3016
$this->postFields = ['listUri' => 'http://somelist'];
3117
}
3218

@@ -37,7 +23,6 @@ public function testCreateReviewSuccess()
3723
new \GuzzleHttp\Psr7\Response(201, [], json_encode(['id' => $id])),
3824
]);
3925

40-
$criticClient->setPersonaConnectValues($this->personaConfig);
4126
$this->assertEquals($id, $criticClient->createReview($this->postFields, '', ''));
4227
}
4328

@@ -52,7 +37,6 @@ public function testCreateReviewException()
5237
new \GuzzleHttp\Psr7\Response(200, [], json_encode(['id' => '1234'])),
5338
]);
5439

55-
$criticClient->setPersonaConnectValues($this->personaConfig);
5640
$criticClient->createReview($this->postFields, '', '');
5741
}
5842

@@ -67,8 +51,6 @@ public function testCreateReviewGuzzleException()
6751
new \GuzzleHttp\Psr7\Response(401, [], json_encode([])),
6852
]);
6953

70-
$criticClient->setPersonaConnectValues($this->personaConfig);
71-
7254
$criticClient->createReview(
7355
$this->postFields,
7456
'someClientId',
@@ -82,41 +64,75 @@ public function testCreateReviewGuzzleException()
8264
*/
8365
public function testCreateReviewWithInvalidPersonaConfigFails()
8466
{
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(
8876
$this->postFields,
8977
'someClientId',
9078
'someClientSecret'
9179
);
9280
}
9381

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+
94100
/**
95101
* Gets the client with mocked HTTP responses.
96102
*
97103
* @param \GuzzleHttp\Psr7\Response[] $responses The responses
104+
* @param array $history History middleware container
98105
* @return \Talis\Critic\Client|\PHPUnit_Framework_MockObject_MockObject The client.
99106
*/
100-
private function getClientWithMockResponses(array $responses)
107+
private function getClientWithMockResponses(array $responses, array &$history = null)
101108
{
102109
$mockHandler = new \GuzzleHttp\Handler\MockHandler($responses);
103110
$handlerStack = \GuzzleHttp\HandlerStack::create($mockHandler);
111+
112+
if (isset($history)) {
113+
$handlerStack->push(\GuzzleHttp\Middleware::history($history));
114+
}
115+
104116
$httpClient = new \GuzzleHttp\Client(['handler' => $handlerStack]);
105117

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 */
106126
$criticClient = $this->getMockBuilder(\Talis\Critic\Client::class)
107-
->setMethods(['getHTTPClient', 'getHeaders'])
127+
->setMethods(['getHTTPClient', 'getTokenClient'])
108128
->setConstructorArgs([$this->criticBaseUrl])
109129
->getMock();
110130

111131
$criticClient->method('getHTTPClient')
112132
->willReturn($httpClient);
113133

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);
120136

121137
return $criticClient;
122138
}

0 commit comments

Comments
 (0)