Skip to content

Commit

Permalink
test: increase coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine authored and myparcel-bot[bot] committed Oct 27, 2023
1 parent 0ec9159 commit 0892d3e
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 94 deletions.
129 changes: 37 additions & 92 deletions tests/Unit/Fulfilment/Repository/PostOrdersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -32,108 +35,50 @@
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);
$request = MockApi::ensureLastRequest();

$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(),
])
);
},
],
]);
12 changes: 11 additions & 1 deletion tests/factories/Fulfilment/Model/ShipmentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
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
* @method Shipment make()
* @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)
Expand All @@ -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)]);
}
}
11 changes: 10 additions & 1 deletion tests/factories/Shipment/Model/DeliveryOptionsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 8 additions & 0 deletions tests/factories/Shipment/Model/ShipmentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down

0 comments on commit 0892d3e

Please sign in to comment.