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