1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HelePartnerSyncApi\Response; |
4
|
|
|
|
5
|
|
|
use HelePartnerSyncApi\Client; |
6
|
|
|
use PHPUnit_Framework_TestCase; |
7
|
|
|
|
8
|
|
|
// @codingStandardsIgnoreStart |
9
|
|
|
function header($header) |
10
|
|
|
{ |
11
|
|
|
ResponseTest::$headers[$header] = true; |
12
|
|
|
} |
13
|
|
|
// @codingStandardsIgnoreEnd |
14
|
|
|
|
15
|
|
|
class ResponseTest extends PHPUnit_Framework_TestCase |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
public static $headers = array(); |
19
|
|
|
|
20
|
|
|
public function test() |
21
|
|
|
{ |
22
|
|
|
$secret = 'secret'; |
23
|
|
|
$data = array( |
24
|
|
|
'key' => 'value', |
25
|
|
|
); |
26
|
|
|
$jsonData = json_encode(array( |
27
|
|
|
Response::KEY_SUCCESS => null, |
28
|
|
|
Response::KEY_MESSAGE => null, |
29
|
|
|
Response::KEY_DATA => $data, |
30
|
|
|
), JSON_PRETTY_PRINT); |
31
|
|
|
|
32
|
|
|
$response = $this->createResponseMock($secret, $data); |
33
|
|
|
|
34
|
|
|
ob_start(); |
35
|
|
|
$response->render(); |
36
|
|
|
$responseJson = ob_get_clean(); |
37
|
|
|
|
38
|
|
|
$this->assertSame($jsonData, $responseJson); |
39
|
|
|
|
40
|
|
|
$this->assertArrayHasKey('HTTP/1.1 200', self::$headers); |
41
|
|
|
$this->assertArrayHasKey('Content-Type: application/json', self::$headers); |
42
|
|
|
$this->assertArrayHasKey(Client::HEADER_SIGNATURE . ': ' . hash_hmac(Response::SIGNATURE_ALGORITHM, $jsonData, $secret), self::$headers); |
43
|
|
|
$this->assertArrayHasKey(Client::HEADER_SIGNATURE_ALGORITHM . ': ' . Response::SIGNATURE_ALGORITHM, self::$headers); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $secret |
48
|
|
|
* @param array $returnData |
49
|
|
|
* @return Response |
50
|
|
|
*/ |
51
|
|
|
private function createResponseMock($secret, $returnData) |
52
|
|
|
{ |
53
|
|
|
$response = $this->getMockBuilder('HelePartnerSyncApi\Response\Response') |
54
|
|
|
->setConstructorArgs(array($secret)) |
55
|
|
|
->getMockForAbstractClass(); |
56
|
|
|
$response->expects(self::once()) |
57
|
|
|
->method('getHttpCode') |
58
|
|
|
->willReturn(200); |
59
|
|
|
$response->expects(self::once()) |
60
|
|
|
->method('getData') |
61
|
|
|
->willReturn($returnData); |
62
|
|
|
|
63
|
|
|
return $response; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
} |
67
|
|
|
|