public function ClientTest::testPostPurgeRequest in Akamai 7.3
Tests the postPurgeRequest method.
@covers Drupal\akamai\Ccu3Client::postPurgeRequest
File
- tests/
Ccu3ClientTest.php, line 70 - Unit tests for the Drupal\akamai\Ccu3Client class.
Class
Namespace
Drupal\akamai\TestsCode
public function testPostPurgeRequest() {
// Setup purge request parameters.
$hostname = 'www.example.com';
$path = '/robots.txt';
$operation = 'invalidate';
// Create stub for response class.
$response_stub = $this
->getMockBuilder('GuzzleHttp\\Psr7\\Response')
->disableOriginalConstructor()
->getMock();
$response_stub
->method('getBody')
->willReturn('{
"estimatedSeconds": 5,
"purgeId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"supportId": "xxxxxxxxxxxxxxxxxxxx-xxxxxxxxx",
"httpStatus": 201,
"detail": "Request accepted"
}');
// Create stub for the EdgeGrid client class.
$edgegrid_client = $this
->getMockBuilder('\\Akamai\\Open\\EdgeGrid\\Client')
->disableOriginalConstructor()
->setMethods([
'post',
])
->getMock();
$edgegrid_client
->method('post')
->willReturn($response_stub);
// Ensure that the `getBody` method of the response object is called.
$response_stub
->expects($this
->once())
->method('getBody');
// Ensure that the `post` method of the EdgeGrid client is called.
// Ensure that the `Content-Type: application/json` header is set.
// Also verify that the payload is encoded as JSON and contains the
// expected 'hostname' and 'objects' parameters.
$edgegrid_client
->expects($this
->once())
->method('post')
->with($this
->equalTo("/ccu/v3/{$operation}/url/production"), $this
->callback(function ($payload) use ($hostname, $path) {
if (!isset($payload['body'])) {
return FALSE;
}
if (!isset($payload['headers']) && !isset($payload['headers']['Content-Type'])) {
return FALSE;
}
if ($payload['headers']['Content-Type'] != 'application/json') {
return FALSE;
}
$decoded = json_decode($payload['body'], TRUE);
if (empty($decoded) || !is_array($decoded)) {
return FALSE;
}
if (!isset($decoded['hostname']) || !isset($decoded['objects'])) {
return FALSE;
}
return $decoded['hostname'] == $hostname && in_array($path, $decoded['objects']);
}));
$ccu_client = new Ccu3Client($edgegrid_client);
$result = $ccu_client
->postPurgeRequest($hostname, [
$path,
], 'invalidate');
$this
->assertSame($result->estimatedSeconds, 5, 'Method postPurgeRequest did not return decoded JSON response.');
}