-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDpdCloudClientTest.php
More file actions
262 lines (210 loc) · 9.84 KB
/
Copy pathDpdCloudClientTest.php
File metadata and controls
262 lines (210 loc) · 9.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php
declare(strict_types=1);
namespace VeryCodeCom\DpdDe\Tests\Unit;
use PHPUnit\Framework\TestCase;
use VeryCodeCom\DpdDe\Dto\Address;
use VeryCodeCom\DpdDe\Dto\OrderItem;
use VeryCodeCom\DpdDe\Dto\Parcel;
use VeryCodeCom\DpdDe\Dto\ParcelShopQuery;
use VeryCodeCom\DpdDe\Dto\SearchAddress;
use VeryCodeCom\DpdDe\DpdCloudClient;
use VeryCodeCom\DpdDe\DpdCloudConfig;
use VeryCodeCom\DpdDe\Enum\ShipService;
use VeryCodeCom\DpdDe\Enum\TransportMode;
use VeryCodeCom\DpdDe\Exception\DpdCloudApiException;
use VeryCodeCom\DpdDe\Exception\DpdCloudAuthException;
use VeryCodeCom\DpdDe\Exception\DpdCloudTransportException;
use VeryCodeCom\DpdDe\Exception\DpdCloudValidationException;
use VeryCodeCom\DpdDe\Transport\TransportInterface;
use VeryCodeCom\DpdDe\Transport\TransportRequest;
use VeryCodeCom\DpdDe\Transport\TransportResponse;
/**
* Exercises DpdCloudClient's orchestration logic (validation -> transport -> parsing -> error
* classification) using a scripted fake TransportInterface, without any real network calls.
*/
final class DpdCloudClientTest extends TestCase
{
private function fixture(string $name): string
{
$contents = file_get_contents(__DIR__ . '/../Fixtures/' . $name);
self::assertIsString($contents, "Fixture {$name} could not be read.");
return $contents;
}
private function config(): DpdCloudConfig
{
return DpdCloudConfig::sandbox('Partner Name', 'partner-token', 123456, 'user-token');
}
private function makeItem(): OrderItem
{
return new OrderItem(
shipAddress: new Address(
name: 'Max Mustermann',
street: 'Musterstr.',
houseNo: '1',
country: 'DE',
zipCode: '12345',
city: 'Berlin',
),
parcelShopId: 0,
parcel: new Parcel(ShipService::Classic, weightKg: 2.5, yourInternalId: 'ORDER-1'),
);
}
// -- happy paths --------------------------------------------------
public function testCreateShipmentSuccess(): void
{
$transport = new FakeTransport([new TransportResponse(200, $this->fixture('set_order_success.xml'))]);
$client = new DpdCloudClient($this->config(), transport: $transport);
$result = $client->createShipment($this->makeItem());
self::assertSame('%PDF-1.4-fake-pdf-content', $result->labelPdf);
self::assertSame('01234567890123', $result->firstParcelNo());
self::assertCount(1, $transport->requests);
self::assertSame('POST', $transport->requests[0]->method);
self::assertStringContainsString('DPDCloudService.asmx', $transport->requests[0]->url);
}
public function testFetchZipCodeRulesSuccessSoap(): void
{
$transport = new FakeTransport([new TransportResponse(200, $this->fixture('get_zip_code_rules_success.xml'))]);
$client = new DpdCloudClient($this->config(), transport: $transport);
$rules = $client->fetchZipCodeRules();
self::assertSame('DE', $rules->country);
self::assertSame('12345', $rules->zipCode);
}
public function testFetchZipCodeRulesSuccessRest(): void
{
$transport = new FakeTransport([new TransportResponse(200, $this->fixture('get_zip_code_rules_success.json'))]);
$client = new DpdCloudClient($this->config(), TransportMode::Rest, $transport);
$rules = $client->fetchZipCodeRules();
self::assertSame('DE', $rules->country);
self::assertSame('GET', $transport->requests[0]->method);
}
public function testFindParcelShops(): void
{
$transport = new FakeTransport([new TransportResponse(200, $this->fixture('get_parcel_shop_finder_success.xml'))]);
$client = new DpdCloudClient($this->config(), transport: $transport);
$shops = $client->findParcelShops(ParcelShopQuery::byAddress(new SearchAddress(zipCode: '12345')));
self::assertCount(1, $shops);
self::assertSame(987654, $shops[0]->parcelShopId);
}
public function testFetchOrderStatus(): void
{
$transport = new FakeTransport([new TransportResponse(200, $this->fixture('get_order_status_success.xml'))]);
$client = new DpdCloudClient($this->config(), transport: $transport);
$status = $client->fetchOrderStatus('01234567890123');
self::assertSame('01234567890123', $status->parcelNo);
}
public function testFetchParcelLifeCycleAlwaysUsesSoapEvenInRestMode(): void
{
$transport = new FakeTransport([new TransportResponse(200, $this->fixture('get_parcel_life_cycle_success.xml'))]);
$client = new DpdCloudClient($this->config(), TransportMode::Rest, $transport);
$result = $client->fetchParcelLifeCycle('01234567890123');
self::assertNotNull($result->shipmentInfo);
// Despite Rest mode, getParcelLifeCycle must hit the SOAP endpoint.
self::assertStringContainsString('DPDCloudService.asmx', $transport->requests[0]->url);
}
public function testCheckOrderDataDoesNotThrowOnSuccess(): void
{
$transport = new FakeTransport([new TransportResponse(200, $this->fixture('set_order_success.xml'))]);
$client = new DpdCloudClient($this->config(), transport: $transport);
$client->checkOrderData([$this->makeItem()]);
self::assertCount(1, $transport->requests);
}
// -- local validation short-circuits before any network call --------------------------------------------------
public function testCreateShipmentThrowsValidationExceptionWithoutNetworkCall(): void
{
$transport = new FakeTransport([]);
$client = new DpdCloudClient($this->config(), transport: $transport);
$invalidItem = new OrderItem(
shipAddress: new Address(name: 'Max'),
parcelShopId: 0,
parcel: new Parcel(ShipService::Classic, weightKg: 999.0),
);
$this->expectException(DpdCloudValidationException::class);
try {
$client->createShipment($invalidItem);
} finally {
self::assertCount(0, $transport->requests, 'No network call should be made when local validation fails.');
}
}
public function testValidateLocallyReturnsErrorsWithoutThrowing(): void
{
$client = new DpdCloudClient($this->config(), transport: new FakeTransport([]));
$errors = $client->validateLocally([]);
self::assertNotEmpty($errors);
}
// -- error classification --------------------------------------------------
public function testAuthErrorIsClassifiedAsAuthException(): void
{
$transport = new FakeTransport([new TransportResponse(200, $this->fixture('get_zip_code_rules_auth_error.xml'))]);
$client = new DpdCloudClient($this->config(), transport: $transport);
$this->expectException(DpdCloudAuthException::class);
$client->fetchZipCodeRules();
}
public function testValidationApiErrorIsClassifiedAsApiException(): void
{
$transport = new FakeTransport([new TransportResponse(200, $this->fixture('set_order_validation_error.xml'))]);
$client = new DpdCloudClient($this->config(), transport: $transport);
try {
$client->createShipment($this->makeItem());
self::fail('Expected DpdCloudApiException to be thrown.');
} catch (DpdCloudApiException $e) {
self::assertTrue($e->hasCode('CLOUD_API_ORDER_WEIGHT'));
}
}
public function testHttpErrorIsClassifiedAsTransportException(): void
{
$transport = new FakeTransport([new TransportResponse(500, 'Internal Server Error')]);
$client = new DpdCloudClient($this->config(), transport: $transport);
$this->expectException(DpdCloudTransportException::class);
$client->fetchZipCodeRules();
}
public function testDebugModeAttachesRawResponseToException(): void
{
$config = DpdCloudConfig::fromArray([
'partner_name' => 'Partner Name',
'partner_token' => 'partner-token',
'user_id' => 123456,
'user_token' => 'user-token',
'env' => 'sandbox',
'debug' => true,
]);
$transport = new FakeTransport([new TransportResponse(200, $this->fixture('get_zip_code_rules_auth_error.xml'))]);
$client = new DpdCloudClient($config, transport: $transport);
try {
$client->fetchZipCodeRules();
self::fail('Expected DpdCloudAuthException to be thrown.');
} catch (DpdCloudAuthException $e) {
self::assertNotNull($e->getRawResponse());
self::assertStringContainsString('CLOUD_API_PARTNERCREDENTIALS', $e->getRawResponse() ?? '');
}
}
// -- named constructors --------------------------------------------------
public function testSandboxNamedConstructorUsesStageEndpoint(): void
{
$client = DpdCloudClient::sandbox('Partner', 'ptoken', 1, 'utoken');
self::assertInstanceOf(DpdCloudClient::class, $client);
}
public function testProductionNamedConstructorUsesProductionEndpoint(): void
{
$client = DpdCloudClient::production('Partner', 'ptoken', 1, 'utoken');
self::assertInstanceOf(DpdCloudClient::class, $client);
}
}
/** Scripted fake transport: returns queued responses in order and records every request sent. */
final class FakeTransport implements TransportInterface
{
private int $index = 0;
/** @var list<TransportRequest> */
public array $requests = [];
/** @param list<TransportResponse> $responses */
public function __construct(private readonly array $responses)
{
}
public function send(TransportRequest $request): TransportResponse
{
$this->requests[] = $request;
if (!isset($this->responses[$this->index])) {
throw new \RuntimeException('FakeTransport: no more scripted responses available.');
}
return $this->responses[$this->index++];
}
}