Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change shipment data endpoint #127

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Endpoints/Insurance.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function getInsuranceContract(
throw new RequestException($response->errorMessage, null, $response);
}

// @todo is it a possible case, or do we need to throw an exception
// Is it a possible case, or do we need to throw an exception
if (!$response->json) {
return null;
}
Expand Down
16 changes: 10 additions & 6 deletions src/Endpoints/Orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,25 @@ public function update($orderId, $orderData)

/**
* @param string $orderId
* @param string|null $carrier
* @param string|null $trackingNumber
* @param string $carrier
* @param string $trackingNumber
* @param string|null $trackingUrl
* @return Order
* @return void
* @throws AlmaException
*/
public function updateTracking($orderId, $carrier = null, $trackingNumber = null, $trackingUrl = null)
public function addTracking($orderId, $carrier, $trackingNumber, $trackingUrl = null)
{
$trackingData = array_filter([
'carrier' => $carrier,
'tracking_number' => $trackingNumber,
'tracking_url' => $trackingUrl
]);
$response = $this->request(self::ORDERS_PATH . "/{$orderId}")->setRequestBody($trackingData)->put();
return new Order($response->json);
$response = $this->request(self::ORDERS_PATH_V2 . "/{$orderId}/shipment")
->setRequestBody($trackingData)
->post();
if ($response->isError()) {
throw new RequestException($response->errorMessage, null, $response);
}
}

/**
Expand Down
36 changes: 0 additions & 36 deletions src/Entities/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,6 @@

class Order
{
/** @var string | null Order carrier */
private $carrier;

/** @var string | null Order carrier tracking number */
private $trackingNumber;

/** @var string | null Order carrier tracking URL */
private $trackingUrl;

/** @var string ID of the Payment owning this Order
* @deprecated
*/
Expand Down Expand Up @@ -105,7 +96,6 @@ class Order

public function __construct($orderDataArray)
{
$this->carrier = $orderDataArray['carrier'];
$this->comment = $orderDataArray['comment'];
$this->createdAt = $orderDataArray['created'];
$this->customerUrl = $orderDataArray['customer_url'];
Expand All @@ -119,35 +109,9 @@ public function __construct($orderDataArray)
$this->merchantUrl = $orderDataArray['merchant_url'];
$this->payment = $orderDataArray['payment'];
$this->paymentId = $orderDataArray['payment'];
$this->trackingNumber = $orderDataArray['tracking_number'];
$this->trackingUrl = $orderDataArray['tracking_url'];
$this->updatedAt = isset($orderDataArray['updated']) ? $orderDataArray['updated'] : null;
}

/**
* @return string|null
*/
public function getCarrier()
{
return $this->carrier;
}

/**
* @return string|null
*/
public function getTrackingNumber()
{
return $this->trackingNumber;
}

/**
* @return string|null
*/
public function getTrackingUrl()
{
return $this->trackingUrl;
}

/**
* @return string
*/
Expand Down
40 changes: 19 additions & 21 deletions tests/Integration/Endpoints/OrdersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Alma\API\Tests\Integration\Endpoints;

use Alma\API\Entities\Order;
use Alma\API\Exceptions\AlmaException;
use Alma\API\Exceptions\RequestException;
use Alma\API\Tests\Integration\TestHelpers\ClientTestHelper;
use Alma\API\Tests\Integration\TestHelpers\PaymentTestHelper;
use PHPUnit\Framework\TestCase;
Expand All @@ -29,31 +31,27 @@ public function testCanCreateANewOrder()
$this->assertEquals('ABC-123-NEW', $newOrder->getMerchantReference());
}

public function testCanUpdateOrderTracking()
public function testAddOrderTrackingThrowErrorWithBadData()
{
$this->expectException(RequestException::class);
$payment = OrdersTest::$payment;
$order = $payment->orders[0];
$this->assertInstanceOf(Order::class, $order);
$this->assertNull($order->getCarrier());
$this->assertNull($order->getTrackingUrl());
$this->assertNull($order->getTrackingNumber());

$updatedOrder = OrdersTest::$almaClient->orders->updateTracking($order->getExternalId(), null,null , 'https://tracking.com');
$this->assertInstanceOf(Order::class, $updatedOrder);
$this->assertNull($order->getCarrier());
$this->assertNull($updatedOrder->getTrackingNumber());
$this->assertEquals('https://tracking.com', $updatedOrder->getTrackingUrl());

$updatedOrder = OrdersTest::$almaClient->orders->updateTracking($order->getExternalId(), 'UPS');
$this->assertInstanceOf(Order::class, $updatedOrder);
$this->assertEquals('UPS', $updatedOrder->getCarrier());
$this->assertNull($updatedOrder->getTrackingNumber());
$this->assertEquals('https://tracking.com', $updatedOrder->getTrackingUrl());
OrdersTest::$almaClient->orders->addTracking($order->getExternalId(), 'UPS', null);
}

$updatedOrder = OrdersTest::$almaClient->orders->updateTracking($order->getExternalId(), 'LAPOSTE','123456789' , 'https://laposte.com');
$this->assertInstanceOf(Order::class, $updatedOrder);
$this->assertEquals('LAPOSTE', $updatedOrder->getCarrier());
$this->assertEquals('123456789', $updatedOrder->getTrackingNumber());
$this->assertEquals('https://laposte.com', $updatedOrder->getTrackingUrl());
public function testAddOrderTracking()
{
$payment = OrdersTest::$payment;
$order = $payment->orders[0];
$this->assertInstanceOf(Order::class, $order);
$this->assertNull(
OrdersTest::$almaClient->orders->addTracking(
$order->getExternalId(),
'UPS',
'UPS_123456',
'https://tracking.com'
)
);
}
}
98 changes: 32 additions & 66 deletions tests/Unit/Endpoints/OrdersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,91 +186,57 @@ public function testSendStatusWithException()
));
}

public function testUpdateTrackingThrowsAlmaException()
public function testAddTrackingThrowsAlmaException()
{
$this->expectException(AlmaException::class);
$this->requestObject->shouldReceive('put')->andThrow(new RequestError());
$this->requestObject->shouldReceive('post')->andThrow(new RequestError());
$this->requestObject->shouldReceive('setRequestBody')->andReturn($this->requestObject);
$this->orderEndpoint->shouldReceive('request')
->with('/v1/orders/123')
->with('/v2/orders/123/shipment')
->once()
->andReturn($this->requestObject);
$this->orderEndpoint->updateTracking('123', 'ups', '123456', 'myUrl');
$this->orderEndpoint->addTracking('123', 'ups', '123456', 'myUrl');
}
public function testAddTrackingThrowsAlmaExceptionForBadData()
{
$this->expectException(RequestException::class);
$this->responseMock->shouldReceive('isError')->once()->andReturn(true);
$this->requestObject->shouldReceive('post')->once()->andReturn($this->responseMock);
$this->requestObject->shouldReceive('setRequestBody')->andReturn($this->requestObject);
$this->orderEndpoint->shouldReceive('request')
->with('/v2/orders/123/shipment')
->once()
->andReturn($this->requestObject);
$this->orderEndpoint->addTracking('123', 'ups', null);
}


/**
* @dataProvider updateTrackingDataProvider
* @param $carrier
* @param $trackingNumber
* @param $trackingUrl
* @param $trackingData
* @return void
* @throws AlmaException
*/
public function testUpdateTracking($carrier, $trackingNumber, $trackingUrl, $trackingData)
public function testAddTracking()
{
$this->responseMock->json = $this->orderDataFactory($carrier, $trackingNumber, $trackingUrl);
$this->requestObject->shouldReceive('put')->andReturn($this->responseMock);
$trackingData = [
'carrier' => 'UPS',
'tracking_number' => 'UPS_123456',
'tracking_url' => 'https://tracking.com'
];
$this->responseMock->shouldReceive('isError')->once()->andReturn(false);
$this->requestObject->shouldReceive('post')->once()->andReturn($this->responseMock);
$this->requestObject->shouldReceive('setRequestBody')->with($trackingData)->andReturn($this->requestObject);

$this->orderEndpoint->shouldReceive('request')
->with('/v1/orders/123')
->with('/v2/orders/123/shipment')
->once()
->andReturn($this->requestObject);
$order = $this->orderEndpoint->updateTracking('123', $carrier, $trackingNumber, $trackingUrl);
$this->assertInstanceOf(Order::class, $order);
$this->assertEquals($carrier, $order->getCarrier());
$this->assertEquals($trackingNumber, $order->getTrackingNumber());
$this->assertEquals($trackingUrl, $order->getTrackingUrl());
}

/**
* @return array[]
*/
public static function updateTrackingDataProvider()
{
return [
'no data' => [
null,
null,
null,
[]
],
'Only Carrier' => [
'ups',
null,
null,
['carrier' => 'ups']
],
'Only Url' => [
null,
null,
'myUrl',
['tracking_url' => 'myUrl']
],
'Carrier and Url' => [
'ups',
null,
'myUrl',
['carrier' => 'ups', 'tracking_url' => 'myUrl']
],
'All params' => [
'ups',
'123456',
'myUrl',
['carrier' => 'ups', 'tracking_number' => '123456', 'tracking_url' => 'myUrl']
],
];
$this->orderEndpoint->addTracking(
'123',
$trackingData['carrier'],
$trackingData['tracking_number'],
$trackingData['tracking_url']
);
}

public function orderDataFactory(
$carrier,
$tracking_number,
$tracking_url
)
{
return OrderTest::orderDataFactory($carrier, $tracking_number, $tracking_url);
}


}
9 changes: 0 additions & 9 deletions tests/Unit/Entities/OrderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ public function testOrderGetters()
$orderData = $this->orderDataFactory();
$order = new Order($orderData);

$this->assertEquals($orderData['carrier'], $order->getCarrier());
$this->assertEquals($orderData['tracking_number'], $order->getTrackingNumber());
$this->assertEquals($orderData['tracking_url'], $order->getTrackingUrl());
$this->assertEquals($orderData['payment'], $order->payment);
$this->assertEquals($orderData['payment'], $order->getPaymentId());
$this->assertEquals($orderData['merchant_reference'], $order->merchant_reference);
Expand All @@ -34,9 +31,6 @@ public function testOrderGetters()


public static function orderDataFactory(
$carrier = 'ups',
$tracking_number = 'ups_123456',
$tracking_url = 'http://tracking.url',
$comment = 'my comment',
$created = 1715331839,
$customer_url = 'http://customer.url',
Expand All @@ -49,7 +43,6 @@ public static function orderDataFactory(
)
{
return [
'carrier' => $carrier,
'comment' => $comment,
'created' => $created,
'customer_url' => $customer_url,
Expand All @@ -58,8 +51,6 @@ public static function orderDataFactory(
'merchant_reference' => $merchant_reference,
'merchant_url' => $merchant_url,
'payment' => $payment,
'tracking_number' => $tracking_number,
'tracking_url' => $tracking_url,
'updated' => $updated
];
}
Expand Down
Loading