Completed
Pull Request — master (#3)
by
unknown
03:45
created

testWSESignatureWithPrivateKeyPassword()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
1
<?php declare(strict_types = 1);
2
3
namespace SlevomatEET\Cryptography;
4
5
use SlevomatEET\Formatter;
6
7
class CryptographyServiceTest extends \PHPUnit\Framework\TestCase
8
{
9
10
	const EXPECTED_PKP = 'a0asEiJhFCBlVtptSspKvEZhcrvnzF7SQ55C4DhnStnSu1b37GUI2+Dlme9P94UCPZ1oCUPJdsYOBZ3IX6aEgEe0FJKXYX0kXraYCJKIo3g64wRchE7iblIOBCK1uHh8qqHA66Isnhb6hqBOOdlt2aWO/0jCzlfeQr0axpPF1mohMnP3h3ICaxZh0dnMdju5OmMrq+91PL5T9KkR7bfGHqAoWJ0kmxY/mZumtRfGil2/xf7I5pdVeYXPgDO/Tojzm6J95n68fPDOXTDrTzKYmqDjpg3kmWepLNQKFXRmkQrkBLToJWG1LDUDm3UTTmPWzq4c0XnGcXJDZglxfolGpA==';
11
	const EXPECTED_BKP = '9356D566-A3E48838-FB403790-D201244E-95DCBD92';
12
13
	public function testGetCodes()
14
	{
15
		$data = $this->getReceiptData();
16
		$crypto = new CryptographyService(__DIR__ . '/../../../cert/EET_CA1_Playground-CZ00000019.key', __DIR__ . '/../../../cert/EET_CA1_Playground-CZ00000019.pub');
17
18
		$expectedPkp = base64_decode(self::EXPECTED_PKP);
19
		$pkpCode = $crypto->getPkpCode($data);
20
		self::assertSame($expectedPkp, $pkpCode);
21
		self::assertSame(self::EXPECTED_BKP, $crypto->getBkpCode($pkpCode));
22
	}
23
24 View Code Duplication
	public function testExceptions()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
25
	{
26
		$cryptoService = new CryptographyService(
27
			__DIR__ . '/invalid-certificate.pem',
28
			__DIR__ . '/invalid-certificate.pem'
29
		);
30
31
		try {
32
			$cryptoService->getPkpCode($this->getReceiptData());
33
			$this->fail();
34
35
		} catch (PrivateKeyFileException $e) {
36
			$this->assertSame(__DIR__ . '/invalid-certificate.pem', $e->getPrivateKeyFile());
37
		}
38
	}
39
40
	/**
41
	 * @runInSeparateProcess
42
	 */
43 View Code Duplication
	public function testExceptions2()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
44
	{
45
		include __DIR__ . '/OpenSslFunctionsMock.php';
46
47
		$cryptoService = new CryptographyService(
48
			__DIR__ . '/../../../cert/EET_CA1_Playground-CZ00000019.key',
49
			__DIR__ . '/../../../cert/EET_CA1_Playground-CZ00000019.pub'
50
		);
51
52
		try {
53
			$cryptoService->getPkpCode($this->getReceiptData());
54
			$this->fail();
55
56
		} catch (SigningFailedException $e) {
57
			$this->assertSame(array_values($this->getReceiptData()), $e->getData());
58
		}
59
	}
60
61 View Code Duplication
	public function testWSESignatureWithoutPrivateKeyPassword()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
62
	{
63
		$request = $this->getRequestData();
64
		$crypto = new CryptographyService(
65
			__DIR__ . '/../../../cert/EET_CA1_Playground-CZ00000019.key',
66
			__DIR__ . '/../../../cert/EET_CA1_Playground-CZ00000019.pub'
67
		);
68
69
		$this->assertNotEmpty($crypto->addWSESignature($request));
70
	}
71
72 View Code Duplication
	public function testWSESignatureWithPrivateKeyPassword()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
73
	{
74
		$request = $this->getRequestData();
75
		$crypto = new CryptographyService(
76
			__DIR__ . '/../../../cert/EET_CA1_Playground_With_Password-CZ00000019.key',
77
			__DIR__ . '/../../../cert/EET_CA1_Playground-CZ00000019.pub',
78
			'eet'
79
		);
80
81
		$this->assertNotEmpty($crypto->addWSESignature($request));
82
	}
83
84
	public function testWSESignatureWithInvalidPrivateKeyPassword()
85
	{
86
		$request = $this->getRequestData();
87
		$crypto = new CryptographyService(
88
			__DIR__ . '/../../../cert/EET_CA1_Playground_With_Password-CZ00000019.key',
89
			__DIR__ . '/../../../cert/EET_CA1_Playground-CZ00000019.pub',
90
			'invalid'
91
		);
92
93
		$this->expectException(\PHPUnit_Framework_Error::class);
94
		$this->expectExceptionMessage('openssl_sign(): supplied key param cannot be coerced into a private key');
95
		$crypto->addWSESignature($request);
96
	}
97
98
	private function getReceiptData():array
99
	{
100
		return [
101
			'dic_popl' => 'CZ00000019',
102
			'id_provoz' => '273',
103
			'id_pokl' => '/5546/RO24',
104
			'porad_cis' => '0/6460/ZQ42',
105
			'dat_trzby' => Formatter::formatDateTime(new \DateTimeImmutable('2016-08-05 00:30:12', new \DateTimeZone('Europe/Prague'))),
106
			'celk_trzba' => Formatter::formatAmount(3411300),
107
		];
108
	}
109
110
	private function getRequestData():string
111
	{
112
		$requestTemplate = file_get_contents(__DIR__ . '/CZ00000019.template.3.1.xml');
113
114
		$data = $this->getReceiptData();
115
		$data += [
116
			'pkp' => self::EXPECTED_PKP,
117
			'bkp' => self::EXPECTED_BKP,
118
		];
119
120
		$patterns = array_map(function ($dataKey) {
121
			return "~{{$dataKey}}~";
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $dataKey instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
122
		}, array_keys($data));
123
		$replacements = array_values($data);
124
125
		$request = preg_replace($patterns, $replacements, $requestTemplate);
126
127
		return $request;
128
	}
129
130
}
131