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

CryptographyServiceTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 3
dl 0
loc 44
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetCodes() 0 10 1
A testWSESignature() 0 12 1
A getMockData() 0 12 1
1
<?php declare(strict_types = 1);
2
3
namespace SlevomatEET;
4
5
use SlevomatEET\Cryptography\CryptographyService;
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->getMockData();
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
	public function testWSESignature()
25
	{
26
		$data = $this->getMockData();
27
		$request = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns1=\"http://fs.mfcr.cz/eet/schema/v3\"><SOAP-ENV:Body><ns1:Trzba><ns1:Hlavicka uuid_zpravy=\"1a10c633-8c0b-4003-93cd-9987836b6d57\" dat_odesl=\"{$data['celk_trzba']}\" prvni_zaslani=\"true\" overeni=\"false\"/><ns1:Data dic_popl=\"{$data['dic_popl']}\" id_provoz=\"{$data['id_provoz']}\" id_pokl=\"{$data['id_pokl']}\" porad_cis=\"{$data['porad_cis']}\" dat_trzby=\"{$data['dat_trzby']}\" celk_trzba=\"{$data['celk_trzba']}\" rezim=\"0\"/><ns1:KontrolniKody><ns1:pkp digest=\"SHA256\" cipher=\"RSA2048\" encoding=\"base64\">" . self::EXPECTED_PKP . "</ns1:pkp><ns1:bkp digest=\"SHA1\" encoding=\"base16\">" . self::EXPECTED_BKP . "</ns1:bkp></ns1:KontrolniKody></ns1:Trzba></SOAP-ENV:Body></SOAP-ENV:Envelope>";
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $data 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...
Coding Style Comprehensibility introduced by
The string literal </ns1:pkp><ns1:bkp diges...\" encoding=\"base16\"> does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
Coding Style Comprehensibility introduced by
The string literal </ns1:bkp></ns1:Kontroln...dy></SOAP-ENV:Envelope> does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
28
29
		$crypto = new CryptographyService(__DIR__ . '/../../cert/EET_CA1_Playground_With_Password-CZ00000019.key', __DIR__ . '/../../cert/EET_CA1_Playground-CZ00000019.pub', 'eet');
30
		$this->assertNotEmpty($crypto->addWSESignature($request));
31
32
		$crypto = new CryptographyService(__DIR__ . '/../../cert/EET_CA1_Playground_With_Password-CZ00000019.key', __DIR__ . '/../../cert/EET_CA1_Playground-CZ00000019.pub');
33
		$this->expectException(\PHPUnit_Framework_Error::class);
34
		$crypto->addWSESignature($request);
35
	}
36
37
	protected function getMockData()
38
	{
39
		$data = [
40
			'dic_popl' => 'CZ00000019',
41
			'id_provoz' => '273',
42
			'id_pokl' => '/5546/RO24',
43
			'porad_cis' => '0/6460/ZQ42',
44
			'dat_trzby' => Formatter::formatDateTime(new \DateTimeImmutable('2016-08-05 00:30:12', new \DateTimeZone('Europe/Prague'))),
45
			'celk_trzba' => Formatter::formatAmount(3411300),
46
		];
47
		return $data;
48
	}
49
50
}
51