feat(130): erli shipments and labels
This commit is contained in:
130
tests/Unit/ErliExternalShipmentServiceTest.php
Normal file
130
tests/Unit/ErliExternalShipmentServiceTest.php
Normal file
@@ -0,0 +1,130 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use App\Modules\Orders\OrdersRepository;
|
||||
use App\Modules\Settings\CarrierDeliveryMethodMappingRepository;
|
||||
use App\Modules\Settings\ErliApiClient;
|
||||
use App\Modules\Settings\ErliExternalShipmentService;
|
||||
use App\Modules\Settings\ErliIntegrationRepository;
|
||||
use App\Modules\Shipments\ShipmentPackageRepository;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class ErliExternalShipmentServiceTest extends TestCase
|
||||
{
|
||||
private ErliIntegrationRepository&MockObject $erliRepository;
|
||||
private ErliApiClient&MockObject $apiClient;
|
||||
private CarrierDeliveryMethodMappingRepository&MockObject $deliveryMappings;
|
||||
private ShipmentPackageRepository&MockObject $packageRepository;
|
||||
private OrdersRepository&MockObject $ordersRepository;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->erliRepository = $this->createMock(ErliIntegrationRepository::class);
|
||||
$this->apiClient = $this->createMock(ErliApiClient::class);
|
||||
$this->deliveryMappings = $this->createMock(CarrierDeliveryMethodMappingRepository::class);
|
||||
$this->packageRepository = $this->createMock(ShipmentPackageRepository::class);
|
||||
$this->ordersRepository = $this->createMock(OrdersRepository::class);
|
||||
}
|
||||
|
||||
public function testSyncPackageRegistersExternalParcelWithMappedVendor(): void
|
||||
{
|
||||
$this->packageRepository
|
||||
->method('findById')
|
||||
->with(55)
|
||||
->willReturn([
|
||||
'id' => 55,
|
||||
'order_id' => 1001,
|
||||
'provider' => 'inpost',
|
||||
'carrier_id' => 'inpost',
|
||||
'tracking_number' => '1234567890',
|
||||
'payload_json' => '{}',
|
||||
]);
|
||||
$this->ordersRepository
|
||||
->method('findDetails')
|
||||
->with(1001)
|
||||
->willReturn([
|
||||
'order' => [
|
||||
'source' => 'erli',
|
||||
'source_order_id' => '240101x12345',
|
||||
'external_carrier_id' => 'Erli Paczkomat',
|
||||
],
|
||||
]);
|
||||
$this->deliveryMappings
|
||||
->method('findByOrderMethod')
|
||||
->with('erli', 0, 'Erli Paczkomat')
|
||||
->willReturn(['source_vendor_code' => 'inpost']);
|
||||
$this->erliRepository
|
||||
->method('getCredentials')
|
||||
->willReturn([
|
||||
'integration_id' => 12,
|
||||
'base_url' => 'https://erli.test',
|
||||
'api_key' => 'token',
|
||||
'timeout_seconds' => 15,
|
||||
'orders_fetch_enabled' => true,
|
||||
'orders_fetch_start_date' => null,
|
||||
]);
|
||||
$this->apiClient
|
||||
->expects($this->once())
|
||||
->method('createExternalParcel')
|
||||
->with(
|
||||
$this->anything(),
|
||||
[[
|
||||
'orderId' => '240101x12345',
|
||||
'vendor' => 'inpost',
|
||||
'status' => 'sent',
|
||||
'trackingNumber' => '1234567890',
|
||||
]]
|
||||
)
|
||||
->willReturn(['ok' => true, 'http_code' => 200, 'items' => [['id' => 777]], 'message' => 'OK']);
|
||||
$this->packageRepository
|
||||
->expects($this->once())
|
||||
->method('update')
|
||||
->with(
|
||||
55,
|
||||
$this->callback(static function (array $data): bool {
|
||||
$payload = $data['payload_json'] ?? [];
|
||||
return is_array($payload)
|
||||
&& isset($payload['erli_external_parcel'])
|
||||
&& ($payload['erli_external_parcel']['trackingNumber'] ?? '') === '1234567890';
|
||||
})
|
||||
);
|
||||
|
||||
$result = $this->createService()->syncPackage(55);
|
||||
|
||||
self::assertTrue($result['ok']);
|
||||
self::assertFalse($result['skipped']);
|
||||
}
|
||||
|
||||
public function testSyncPackageSkipsUntilTrackingNumberExists(): void
|
||||
{
|
||||
$this->packageRepository
|
||||
->method('findById')
|
||||
->willReturn([
|
||||
'id' => 55,
|
||||
'order_id' => 1001,
|
||||
'tracking_number' => '',
|
||||
]);
|
||||
$this->apiClient
|
||||
->expects($this->never())
|
||||
->method('createExternalParcel');
|
||||
|
||||
$result = $this->createService()->syncPackage(55);
|
||||
|
||||
self::assertTrue($result['ok']);
|
||||
self::assertTrue($result['skipped']);
|
||||
}
|
||||
|
||||
private function createService(): ErliExternalShipmentService
|
||||
{
|
||||
return new ErliExternalShipmentService(
|
||||
$this->erliRepository,
|
||||
$this->apiClient,
|
||||
$this->deliveryMappings,
|
||||
$this->packageRepository,
|
||||
$this->ordersRepository
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user