|
1
|
|
|
<?php declare(strict_types = 1); |
|
2
|
|
|
|
|
3
|
|
|
namespace SlevomatEET; |
|
4
|
|
|
|
|
5
|
|
|
use SlevomatEET\Cryptography\CryptographyService; |
|
6
|
|
|
use SlevomatEET\Driver\DriverRequestFailedException; |
|
7
|
|
|
use SlevomatEET\Driver\SoapClientDriver; |
|
8
|
|
|
|
|
9
|
|
|
class ClientTest extends \PHPUnit\Framework\TestCase |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
/** @var \SlevomatEET\Cryptography\CryptographyService|\PHPUnit_Framework_MockObject_MockObject */ |
|
13
|
|
|
private $cryptographyService; |
|
14
|
|
|
|
|
15
|
|
|
/** @var \SlevomatEET\Configuration|\PHPUnit_Framework_MockObject_MockObject */ |
|
16
|
|
|
private $configuration; |
|
17
|
|
|
|
|
18
|
|
|
/** @var \SlevomatEET\Driver\SoapClientDriver|\PHPUnit_Framework_MockObject_MockObject */ |
|
19
|
|
|
private $soapClientDriver; |
|
20
|
|
|
|
|
21
|
|
|
public function setUp() |
|
22
|
|
|
{ |
|
23
|
|
|
$this->cryptographyService = $this->createMock(CryptographyService::class); |
|
24
|
|
|
$this->configuration = $configuration = new Configuration('CZ00000019', '273', '/5546/RO24', new EvidenceEnvironment(EvidenceEnvironment::PLAYGROUND), false); |
|
|
|
|
|
|
25
|
|
|
$this->soapClientDriver = $this->createMock(SoapClientDriver::class); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function testSendSuccess() |
|
29
|
|
|
{ |
|
30
|
|
|
$client = new Client( |
|
31
|
|
|
$this->cryptographyService, |
|
32
|
|
|
$this->configuration, |
|
33
|
|
|
$this->soapClientDriver |
|
34
|
|
|
); |
|
35
|
|
|
|
|
36
|
|
|
$soapClient = $this->createMock(SoapClient::class); |
|
37
|
|
|
(function () use ($soapClient) { |
|
38
|
|
|
$this->soapClient = $soapClient; |
|
|
|
|
|
|
39
|
|
|
})->call($client); |
|
40
|
|
|
|
|
41
|
|
|
$receipt = $this->getTestReceipt(); |
|
42
|
|
|
|
|
43
|
|
|
$responseData = (object) [ |
|
44
|
|
|
'Hlavicka' => (object) [ |
|
45
|
|
|
'uuid_zpravy' => '12345', |
|
46
|
|
|
'dat_prij' => '2016-11-21T08:00:00Z', |
|
47
|
|
|
], |
|
48
|
|
|
'Potvrzeni' => (object) [ |
|
49
|
|
|
'fik' => '888-88-88-8888-ff', |
|
50
|
|
|
], |
|
51
|
|
|
]; |
|
52
|
|
|
|
|
53
|
|
|
$soapClient->expects($this->once()) |
|
54
|
|
|
->method('OdeslaniTrzby') |
|
55
|
|
View Code Duplication |
->with($this->callback(function (array $request) { |
|
|
|
|
|
|
56
|
|
|
$this->assertArrayHasKey('Hlavicka', $request); |
|
57
|
|
|
$this->assertArrayHasKey('Data', $request); |
|
58
|
|
|
$this->assertArrayHasKey('KontrolniKody', $request); |
|
59
|
|
|
|
|
60
|
|
|
return true; |
|
61
|
|
|
})) |
|
62
|
|
|
->willReturn($responseData); |
|
63
|
|
|
|
|
64
|
|
|
$response = $client->send($receipt); |
|
65
|
|
|
|
|
66
|
|
|
$this->assertTrue($response->isValid()); |
|
67
|
|
|
$this->assertFalse($response->isTest()); |
|
68
|
|
|
$this->assertSame($response->getFik(), '888-88-88-8888-ff'); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function testSendInvalidResponse() |
|
72
|
|
|
{ |
|
73
|
|
|
$client = new Client( |
|
74
|
|
|
$this->cryptographyService, |
|
75
|
|
|
$this->configuration, |
|
76
|
|
|
$this->soapClientDriver |
|
77
|
|
|
); |
|
78
|
|
|
|
|
79
|
|
|
$soapClient = $this->createMock(SoapClient::class); |
|
80
|
|
|
(function () use ($soapClient) { |
|
81
|
|
|
$this->soapClient = $soapClient; |
|
|
|
|
|
|
82
|
|
|
})->call($client); |
|
83
|
|
|
|
|
84
|
|
|
$receipt = $this->getTestReceipt(); |
|
85
|
|
|
|
|
86
|
|
|
$responseData = (object) [ |
|
87
|
|
|
'Hlavicka' => (object) [ |
|
88
|
|
|
'uuid_zpravy' => '12345', |
|
89
|
|
|
'dat_odmit' => '2016-11-21T08:00:00Z', |
|
90
|
|
|
], |
|
91
|
|
|
'Chyba' => (object) [], |
|
92
|
|
|
]; |
|
93
|
|
|
|
|
94
|
|
|
$soapClient->expects($this->once()) |
|
95
|
|
|
->method('OdeslaniTrzby') |
|
96
|
|
View Code Duplication |
->with($this->callback(function (array $request) { |
|
|
|
|
|
|
97
|
|
|
$this->assertArrayHasKey('Hlavicka', $request); |
|
98
|
|
|
$this->assertArrayHasKey('Data', $request); |
|
99
|
|
|
$this->assertArrayHasKey('KontrolniKody', $request); |
|
100
|
|
|
|
|
101
|
|
|
return true; |
|
102
|
|
|
})) |
|
103
|
|
|
->willReturn($responseData); |
|
104
|
|
|
|
|
105
|
|
|
try { |
|
106
|
|
|
$client->send($receipt); |
|
107
|
|
|
$this->fail('Expected exception was not thrown'); |
|
108
|
|
|
} catch (InvalidResponseReceivedException $e) { |
|
109
|
|
|
$response = $e->getResponse(); |
|
110
|
|
|
$this->assertFalse($response->isValid()); |
|
111
|
|
|
$this->assertFalse($response->isTest()); |
|
112
|
|
|
$this->expectException(InvalidResponseWithoutFikException::class); |
|
113
|
|
|
$response->getFik(); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public function testSendFailedRequest() |
|
118
|
|
|
{ |
|
119
|
|
|
$this->soapClientDriver->expects($this->once()) |
|
|
|
|
|
|
120
|
|
|
->method('send') |
|
121
|
|
|
->with( |
|
122
|
|
|
$this->stringStartsWith('<?xml'), |
|
123
|
|
|
$this->identicalTo('https://pg.eet.cz:443/eet/services/EETServiceSOAP/v3'), |
|
124
|
|
|
$this->identicalTo('http://fs.mfcr.cz/eet/OdeslaniTrzby') |
|
125
|
|
|
) |
|
126
|
|
|
->willThrowException(new DriverRequestFailedException(new \Exception('Fail'))); |
|
127
|
|
|
$this->cryptographyService->method('addWSESignature') |
|
128
|
|
|
->willReturnArgument(0); |
|
129
|
|
|
|
|
130
|
|
|
$client = new Client( |
|
131
|
|
|
$this->cryptographyService, |
|
132
|
|
|
$this->configuration, |
|
133
|
|
|
$this->soapClientDriver |
|
134
|
|
|
); |
|
135
|
|
|
|
|
136
|
|
|
$receipt = $this->getTestReceipt(); |
|
137
|
|
|
|
|
138
|
|
|
try { |
|
139
|
|
|
$client->send($receipt); |
|
140
|
|
|
$this->fail('Expected exception was not thrown'); |
|
141
|
|
|
} catch (FailedRequestException $e) { |
|
142
|
|
|
$request = $e->getRequestData(); |
|
143
|
|
|
$this->assertSame('', $request->getBkpCode()); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
private function getTestReceipt(): Receipt |
|
148
|
|
|
{ |
|
149
|
|
|
$receipt = new Receipt( |
|
150
|
|
|
true, |
|
151
|
|
|
'CZ683555118', |
|
152
|
|
|
'0/6460/ZQ42', |
|
153
|
|
|
new \DateTimeImmutable('2016-11-01 00:30:12'), |
|
154
|
|
|
3411300 |
|
155
|
|
|
); |
|
156
|
|
|
|
|
157
|
|
|
return $receipt; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
} |
|
161
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.