Completed
Push — dev ( ce9639...c420bb )
by Josef
04:04
created

RequestTest::testSuccess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 0
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