1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace HelePartnerSyncApi\Request; |
4
|
|
|
|
5
|
|
|
use HelePartnerSyncApi\Client; |
6
|
|
|
use PHPUnit_Framework_TestCase; |
7
|
|
|
|
8
|
|
|
class RequestFactoryTest extends PHPUnit_Framework_TestCase |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
public function testCreateRequest() |
12
|
|
|
{ |
13
|
|
|
$algo = 'sha1'; |
14
|
|
|
$secret = 'secret'; |
15
|
|
|
$arguments = array('value'); |
16
|
|
|
$method = 'fooMethod'; |
17
|
|
|
$data = array( |
18
|
|
|
'data' => $arguments, |
19
|
|
|
'expectedVersion' => Client::VERSION, |
20
|
|
|
'method' => $method, |
21
|
|
|
); |
22
|
|
|
|
23
|
|
|
$requestData = json_encode($data); |
24
|
|
|
$requestFactory = new RequestFactory($secret); |
25
|
|
|
$request = $requestFactory->createRequest($requestData, array( |
26
|
|
|
Client::HEADER_SIGNATURE => hash_hmac($algo, $requestData, $secret), |
27
|
|
|
Client::HEADER_SIGNATURE_ALGORITHM => 'sha1', |
28
|
|
|
)); |
29
|
|
|
|
30
|
|
|
$this->assertInstanceOf('HelePartnerSyncApi\Request\Request', $request); |
31
|
|
|
$this->assertSame($arguments, $request->getData()); |
32
|
|
|
$this->assertSame(Client::VERSION, $request->getExpectedVersion()); |
33
|
|
|
$this->assertSame($method, $request->getMethod()); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
View Code Duplication |
public function testMissingSignatureHeader() |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
$secret = 'secret'; |
39
|
|
|
$requestFactory = new RequestFactory($secret); |
40
|
|
|
|
41
|
|
|
try { |
42
|
|
|
$requestFactory->createRequest(json_encode(array()), array()); |
43
|
|
|
} catch (RequestException $e) { |
44
|
|
|
$this->assertSame('Missing X-Hele-Signature header in HTTP request', $e->getMessage()); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
View Code Duplication |
public function testMissingSignatureAlgorithmHeader() |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
$secret = 'secret'; |
51
|
|
|
$requestFactory = new RequestFactory($secret); |
52
|
|
|
|
53
|
|
|
try { |
54
|
|
|
$requestFactory->createRequest(json_encode(array()), array( |
55
|
|
|
Client::HEADER_SIGNATURE => 'fooSignature', |
56
|
|
|
)); |
57
|
|
|
} catch (RequestException $e) { |
58
|
|
|
$this->assertSame('Missing X-Hele-Signature-Algorithm header in HTTP request', $e->getMessage()); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.