From 0892d3ec5a56510144836dd25c5d32468554c597 Mon Sep 17 00:00:00 2001 From: Edie Lemoine Date: Fri, 27 Oct 2023 14:35:25 +0200 Subject: [PATCH] test: increase coverage --- .../Fulfilment/Repository/PostOrdersTest.php | 129 +++++------------- .../Fulfilment/Model/ShipmentFactory.php | 12 +- .../Shipment/Model/DeliveryOptionsFactory.php | 11 +- .../Shipment/Model/ShipmentFactory.php | 8 ++ 4 files changed, 66 insertions(+), 94 deletions(-) diff --git a/tests/Unit/Fulfilment/Repository/PostOrdersTest.php b/tests/Unit/Fulfilment/Repository/PostOrdersTest.php index ef1759b4c..c6d74e145 100644 --- a/tests/Unit/Fulfilment/Repository/PostOrdersTest.php +++ b/tests/Unit/Fulfilment/Repository/PostOrdersTest.php @@ -5,15 +5,18 @@ namespace MyParcelNL\Pdk\Fulfilment\Repository; -use MyParcelNL\Pdk\Carrier\Model\Carrier; +use MyParcelNL\Pdk\App\Order\Collection\PdkOrderCollection; +use MyParcelNL\Pdk\App\Order\Collection\PdkOrderCollectionFactory; +use MyParcelNL\Pdk\App\Order\Model\PdkOrder; use MyParcelNL\Pdk\Facade\Pdk; use MyParcelNL\Pdk\Fulfilment\Collection\OrderCollection; use MyParcelNL\Pdk\Fulfilment\Model\Order; -use MyParcelNL\Pdk\Shipment\Model\DeliveryOptions; +use MyParcelNL\Pdk\Shipment\Model\Shipment; use MyParcelNL\Pdk\Tests\Api\Response\ExampleGetOrdersResponse; use MyParcelNL\Pdk\Tests\Api\Response\ExamplePostOrdersResponse; use MyParcelNL\Pdk\Tests\Bootstrap\MockApi; use MyParcelNL\Pdk\Tests\Uses\UsesMockPdkInstance; +use function MyParcelNL\Pdk\Tests\factory; use function MyParcelNL\Pdk\Tests\usesShared; use function Spatie\Snapshots\assertMatchesJsonSnapshot; @@ -32,13 +35,19 @@ assertMatchesJsonSnapshot(json_encode($savedOrders->toArray())); })->with('fulfilmentOrders'); -it('creates order', function ($input, $path, $query) { +it('creates order', function (PdkOrderCollectionFactory $factory) { MockApi::enqueue(new ExampleGetOrdersResponse()); /** @var \MyParcelNL\Pdk\Fulfilment\Repository\OrderRepository $repository */ - $repository = Pdk::get(OrderRepository::class); - $order = new Order($input); - $orderCollection = (new OrderCollection())->push($order); + $repository = Pdk::get(OrderRepository::class); + + $pdkOrderCollection = $factory + ->store() + ->make(); + + $orderCollection = new OrderCollection($pdkOrderCollection->map(function (PdkOrder $pdkOrder) { + return Order::fromPdkOrder($pdkOrder); + })); /** @var OrderRepository $response */ $response = $repository->postOrders($orderCollection); @@ -46,94 +55,30 @@ $uri = $request->getUri(); - expect($uri->getQuery()) - ->toBe($query) - ->and($uri->getPath()) - ->toBe($path) + expect($uri->getPath()) + ->toBe('API/fulfilment/orders') ->and($response) ->toBeInstanceOf(OrderCollection::class); })->with([ - 'empty query with single shipment response' => [ - 'input' => [ - 'invoiceAddress' => [ - 'cc' => 'NL', - 'city' => 'Boskoop', - ], - 'language' => null, - 'orderDate' => '2022-08-22 00:00:00', - 'lines' => [ - [ - 'uuid' => '1234', - 'quantity' => 1, - 'price' => 250, - 'vat' => 10, - 'priceAfterVat' => 260, - 'product' => [ - 'uuid' => '12345', - 'sku' => '018234', - 'ean' => '018234', - 'externalIdentifier' => '018234', - 'name' => 'Paarse stofzuiger', - 'description' => 'Een paars object waarmee stof opgezogen kan worden', - 'width' => null, - 'length' => null, - 'height' => null, - 'weight' => 3500, - ], - ], - - ], - 'price' => 260, - 'shipment' => [ - 'carrier' => [ - 'id' => Carrier::CARRIER_POSTNL_ID, - ], - 'customsDeclaration' => null, - 'deliveryOptions' => [ - 'date' => '2022-08-22 00:00:00', - 'deliveryType' => DeliveryOptions::DELIVERY_TYPE_STANDARD_NAME, - 'packageType' => DeliveryOptions::PACKAGE_TYPE_PACKAGE_NAME, - 'shipmentOptions' => [ - 'ageCheck' => 1, - 'insurance' => [ - 'amount' => 0, - 'currency' => 'EUR', - ], - 'labelDescription' => null, - 'largeFormat' => 0, - 'onlyRecipient' => 0, - 'return' => 0, - 'sameDayDelivery' => 0, - 'signature' => 1, - ], - ], - 'dropOffPoint' => null, - 'physicalProperties' => [ - 'weight' => 3500, - ], - 'recipient' => [ - 'cc' => 'NL', - 'city' => 'Hoofddorp', - 'person' => 'Jaap Krekel', - 'postalCode' => '2132JE', - 'address1' => 'Antareslaan 31', - ], - 'sender' => [ - 'cc' => 'NL', - 'city' => 'Amsterdam', - 'person' => 'Willem Wever', - 'postalCode' => '4164ZF', - 'address1' => 'Werf 2', - ], - ], - 'shopId' => null, - 'status' => null, - 'type' => null, - 'updatedAt' => null, - 'uuid' => null, - 'vat' => null, - ], - 'path' => 'API/fulfilment/orders', - 'query' => '', + 'simple order' => [ + function () { + return factory(PdkOrderCollection::class); + }, + ], + + 'order with drop-off point' => [ + function () { + return factory(PdkOrderCollection::class)->push(factory(PdkOrder::class)->withShipments()); + }, + ], + + 'order with shipment to pickup location' => [ + function () { + return factory(PdkOrderCollection::class)->push( + factory(PdkOrder::class)->withShipments([ + factory(Shipment::class)->withDeliveryOptionsWithPickupLocation(), + ]) + ); + }, ], ]); diff --git a/tests/factories/Fulfilment/Model/ShipmentFactory.php b/tests/factories/Fulfilment/Model/ShipmentFactory.php index adb143822..e74dbd4db 100644 --- a/tests/factories/Fulfilment/Model/ShipmentFactory.php +++ b/tests/factories/Fulfilment/Model/ShipmentFactory.php @@ -14,6 +14,7 @@ use MyParcelNL\Pdk\Shipment\Model\RetailLocation; use MyParcelNL\Pdk\Shipment\Model\RetailLocationFactory; use MyParcelNL\Pdk\Tests\Factory\Model\AbstractModelFactory; +use function MyParcelNL\Pdk\Tests\factory; /** * @template T of Shipment @@ -21,7 +22,6 @@ * @method $this withCarrier(int $carrier) * @method $this withContractId(string $contractId) * @method $this withCustomsDeclaration(CustomsDeclaration|CustomsDeclarationFactory $customsDeclaration) - * @method $this withDropOffPoint(RetailLocation|RetailLocationFactory $dropOffPoint) * @method $this withOptions(ShipmentOptions|ShipmentOptionsFactory $options) * @method $this withPhysicalProperties(PhysicalProperties|PhysicalPropertiesFactory $physicalProperties) * @method $this withPickup(RetailLocation|RetailLocationFactory $pickup) @@ -33,4 +33,14 @@ public function getModel(): string { return Shipment::class; } + + /** + * @param array|RetailLocation|RetailLocationFactory $dropOffPoint + * + * @return $this + */ + public function withDropOffPoint($dropOffPoint = null): self + { + return $this->with(['dropOffPoint' => $dropOffPoint ?? factory(RetailLocation::class)]); + } } diff --git a/tests/factories/Shipment/Model/DeliveryOptionsFactory.php b/tests/factories/Shipment/Model/DeliveryOptionsFactory.php index c1e74daf0..08cc40653 100644 --- a/tests/factories/Shipment/Model/DeliveryOptionsFactory.php +++ b/tests/factories/Shipment/Model/DeliveryOptionsFactory.php @@ -20,7 +20,6 @@ * @method $this withDeliveryType(string $deliveryType) * @method $this withLabelAmount(int $labelAmount) * @method $this withPackageType(string $packageType) - * @method $this withPickupLocation(array|RetailLocation|RetailLocationFactory $pickupLocation) * @method $this withShipmentOptions(array|ShipmentOptions|ShipmentOptionsFactory $shipmentOptions) */ final class DeliveryOptionsFactory extends AbstractModelFactory @@ -35,6 +34,16 @@ public function withAllShipmentOptions(): self return $this->withShipmentOptions(factory(ShipmentOptions::class)->withAllOptions()); } + /** + * @param array|RetailLocation|RetailLocationFactory $pickupLocation + * + * @return $this + */ + public function withPickupLocation($pickupLocation = null): self + { + return $this->with(['pickupLocation' => $pickupLocation ?? factory(RetailLocation::class)]); + } + protected function createDefault(): FactoryInterface { return $this diff --git a/tests/factories/Shipment/Model/ShipmentFactory.php b/tests/factories/Shipment/Model/ShipmentFactory.php index f9039fbd1..9faf8e296 100644 --- a/tests/factories/Shipment/Model/ShipmentFactory.php +++ b/tests/factories/Shipment/Model/ShipmentFactory.php @@ -60,6 +60,14 @@ public function getModel(): string return Shipment::class; } + /** + * @return $this + */ + public function withDeliveryOptionsWithPickupLocation(): self + { + return $this->withDeliveryOptions(factory(DeliveryOptions::class)->withPickupLocation()); + } + /** * @param null|int $id *