|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace HelePartnerSyncApi\Request; |
|
4
|
|
|
|
|
5
|
|
|
use HelePartnerSyncApi\Client; |
|
6
|
|
|
use PHPUnit_Framework_TestCase; |
|
7
|
|
|
|
|
8
|
|
|
class RequestTest extends PHPUnit_Framework_TestCase |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
public function testSuccess() |
|
12
|
|
|
{ |
|
13
|
|
|
$secret = 'foo secret'; |
|
14
|
|
|
|
|
15
|
|
|
$body = json_encode(array( |
|
16
|
|
|
'data' => array('foo'), |
|
17
|
|
|
'method' => 'bar', |
|
18
|
|
|
'expectedVersion' => Client::VERSION, |
|
19
|
|
|
)); |
|
20
|
|
|
|
|
21
|
|
|
$signature = hash_hmac(Client::SIGNATURE_ALGORITHM, $body, $secret); |
|
22
|
|
|
|
|
23
|
|
|
$request = new Request($body, $secret, $signature, Client::SIGNATURE_ALGORITHM); |
|
24
|
|
|
$this->assertSame(array('foo'), $request->getData()); |
|
25
|
|
|
$this->assertSame('bar', $request->getMethod()); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function getTestExceptionData() |
|
29
|
|
|
{ |
|
30
|
|
|
return array( |
|
31
|
|
|
array( |
|
32
|
|
|
'', |
|
33
|
|
|
'', |
|
34
|
|
|
'md5', |
|
35
|
|
|
'Invalid JSON in request', |
|
36
|
|
|
), |
|
37
|
|
|
array( |
|
38
|
|
|
'{}', |
|
39
|
|
|
'9b9585ab4f87eff122c8cd8e6fd94d358ed56f22', |
|
40
|
|
|
'sha1', |
|
41
|
|
|
'Invalid request: Missing keys', |
|
42
|
|
|
), |
|
43
|
|
|
array( |
|
44
|
|
|
'{"data":[], "method": "foo", "expectedVersion": "1.0.0"}', |
|
45
|
|
|
'abc', |
|
46
|
|
|
'fooAlgo', |
|
47
|
|
|
'Unknown signature algorithm (fooAlgo) in request', |
|
48
|
|
|
), |
|
49
|
|
|
array( |
|
50
|
|
|
'{"data":[], "method": "foo", "expectedVersion": "1.0.0"}', |
|
51
|
|
|
'abc', |
|
52
|
|
|
'sha1', |
|
53
|
|
|
'Signature in request is invalid', |
|
54
|
|
|
), |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param string $body |
|
60
|
|
|
* @param string $signature |
|
61
|
|
|
* @param string $signatureAlgorithm |
|
62
|
|
|
* @param string $message |
|
63
|
|
|
* |
|
64
|
|
|
* @dataProvider getTestExceptionData |
|
65
|
|
|
*/ |
|
66
|
|
|
public function testException($body, $signature, $signatureAlgorithm, $message) |
|
67
|
|
|
{ |
|
68
|
|
|
try { |
|
69
|
|
|
new Request($body, '', $signature, $signatureAlgorithm); |
|
70
|
|
|
$this->fail('Exception was expected!'); |
|
71
|
|
|
|
|
72
|
|
|
} catch (RequestException $e) { |
|
73
|
|
|
$this->assertContains($message, $e->getMessage()); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|