Completed
Push — master ( 170fde...562033 )
by Jan
03:08
created

CryptoServiceTest   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 12
c 3
b 0
f 0
lcom 1
cbo 7
dl 0
loc 150
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 7 1
B getSignDataData() 0 42 1
A testSignData() 0 10 2
A testExceptions() 0 23 3
B testExceptions2() 0 26 3
A testVerifyData() 0 8 2
1
<?php
2
3
namespace SlevomatCsobGateway\Crypto;
4
5
class CryptoServiceTest extends \PHPUnit_Framework_TestCase
6
{
7
8
	/**
9
	 * @var CryptoService
10
	 */
11
	private $cryptoService;
12
13
	protected function setUp()
14
	{
15
		$this->cryptoService = new CryptoService(
16
			__DIR__ . '/../../keys/client.key',
17
			__DIR__ . '/../../keys/client.pub'
18
		);
19
	}
20
21
	public function getSignDataData()
22
	{
23
		return [
24
			[
25
				[
26
					'name' => 'foo',
27
					'id' => 123,
28
					'cart' => [
29
						[
30
							'price' => 99,
31
							'name' => 'foo product',
32
						],
33
						[
34
							'name' => 'bar product',
35
						],
36
					],
37
					'description' => 'order description',
38
				],
39
				'VW1Ku7Ekvr2mxpF4bcAt8pViD60oMn7Ktifv7VO9CQ5aPoacICyRnx6csrtAhmhZ+3W5aA1PmSnlZEQ8+GkTpKk9pvS7vOQgyR1+62bsz5nlFigQ5esfoCAgcq4noWl1xQ2f9ScXn5gh/8iSDWTZ4BJvyddtgBP2eY4qzk8Lk7lrpksR8jbs1dLDLbYD8/LynZo4eD7OeKewwdG++bdSvYKKEHVg7CWT6JVFgucM1v59N1C+Qz+IihZ0gUuQbJxJWNe3vI/X6sTLdqqJTMw/MDghfMkBWC2L1yZbqOUd6LQwtg82KFZFZB71e71su60ci4TujvGErzLt1I+SJ7SWQg==',
40
				true,
41
				new SignatureDataFormatter([
42
					'id' => null,
43
					'name' => null,
44
					'cart' => [
45
						'name' => null,
46
						'price' => null,
47
					],
48
					'description' => null,
49
				]),
50
			],
51
			[
52
				[
53
					'merchantId' => '012345',
54
					'orderNo' => '5547',
55
					'dttm' => '20140425131559',
56
				],
57
				'invalidSignature',
58
				false,
59
				new SignatureDataFormatter([]),
60
			],
61
		];
62
	}
63
64
	/**
65
	 * @param mixed[] $data
66
	 * @param string $expectedSignature
67
	 * @param bool $valid
68
	 * @param SignatureDataFormatter $signatureDataFormatter
69
	 *
70
	 * @dataProvider getSignDataData
71
	 */
72
	public function testSignData(array $data, $expectedSignature, $valid, SignatureDataFormatter $signatureDataFormatter)
73
	{
74
		$signature = $this->cryptoService->signData($data, $signatureDataFormatter);
75
76
		if ($valid) {
77
			$this->assertSame($expectedSignature, $signature);
78
		} else {
79
			$this->assertNotSame($expectedSignature, $signature);
80
		}
81
	}
82
83
	public function testExceptions()
84
	{
85
		$cryptoService = new CryptoService(
86
			__DIR__ . '/invalid-key.key',
87
			__DIR__ . '/invalid-key.key'
88
		);
89
90
		try {
91
			$cryptoService->signData([], new SignatureDataFormatter([]));
92
			$this->fail();
93
94
		} catch (PrivateKeyFileException $e) {
95
			$this->assertSame(__DIR__ . '/invalid-key.key', $e->getPrivateKeyFile());
96
		}
97
98
		try {
99
			$cryptoService->verifyData([], 'fooSignature', new SignatureDataFormatter([]));
100
			$this->fail();
101
102
		} catch (PublicKeyFileException $e) {
103
			$this->assertSame(__DIR__ . '/invalid-key.key', $e->getPublicKeyFile());
104
		}
105
	}
106
107
	/**
108
	 * @runInSeparateProcess
109
	 */
110
	public function testExceptions2()
111
	{
112
		include __DIR__ . '/GlobalFunctionsMock.php';
113
114
		$cryptoService = new CryptoService(
115
			__DIR__ . '/../../keys/client.key',
116
			__DIR__ . '/../../keys/bank.pub'
117
		);
118
119
		try {
120
			$cryptoService->signData([], new SignatureDataFormatter([]));
121
			$this->fail();
122
123
		} catch (SigningFailedException $e) {
124
			$this->assertSame([], $e->getData());
125
		}
126
127
		try {
128
			$cryptoService->verifyData([], 'fooSignature', new SignatureDataFormatter([]));
129
			$this->fail();
130
131
		} catch (VerificationFailedException $e) {
132
			$this->assertSame([], $e->getData());
133
			$this->assertSame('error_message', $e->getErrorMessage());
134
		}
135
	}
136
137
	/**
138
	 * @param mixed[] $data
139
	 * @param string $signature
140
	 * @param bool $valid
141
	 * @param SignatureDataFormatter $signatureDataFormatter
142
	 *
143
	 * @dataProvider getSignDataData
144
	 */
145
	public function testVerifyData(array $data, $signature, $valid, SignatureDataFormatter $signatureDataFormatter)
146
	{
147
		if ($valid) {
148
			$this->assertTrue($this->cryptoService->verifyData($data, $signature, $signatureDataFormatter));
149
		} else {
150
			$this->assertFalse($this->cryptoService->verifyData($data, $signature, $signatureDataFormatter));
151
		}
152
	}
153
154
}
155