Skip to content

Commit

Permalink
Allow to view all orderItem properties (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aurelieluciani authored Sep 19, 2024
1 parent ea4fd64 commit cc2622a
Show file tree
Hide file tree
Showing 5 changed files with 251 additions and 60 deletions.
165 changes: 153 additions & 12 deletions src/Api/Order/OrderItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,21 @@
*/
class OrderItem
{
/**
* @var int
*/
private $id;

/**
* @var string
*/
private $reference;

/**
* @var string
*/
private $status;

/**
* @var int
*/
Expand All @@ -21,23 +31,90 @@ class OrderItem
*/
private $unitPrice;

/**
* @var null|float
*/
private $commission;

/**
* @var float
*/
private $taxAmount;

/**
* @var float
*/
private $ecotaxAmount;

/**
* @var string
*/
private $channelReference;

/**
* @var null|array
*/
private $additionalFields;

/**
* @var null|string
*/
private $name;

/**
* @var null|string
*/
private $image;

/**
* @param int $id The product's id
* @param string $reference The product's reference
* @param int $quantity Number product's occurrence in cart
* @param float $price The price for a single unit of the reference (not price * quantity)
* @param float $taxAmount The tax amount of the item price
* @param string $status The product's status
* @param int $quantity Number product's occurrence in cart
* @param float $price The price for a single unit of the reference (not price * quantity)
* @param null|float $commission The commission taken by the channel for this item
* @param float $taxAmount The tax amount of the item price
* @param float $ecotaxAmount The ecotax amount of the item price
* @param string $channelReference The channel reference of the product
* @param null|array $additionalFields A key / value object containing additional marketplace fields for the item
* @param null|string $name The product's name
* @param null|string $image The main image of the product
*/
public function __construct($reference, $quantity, $price, $taxAmount)
public function __construct(
$id,
$reference,
$status,
$quantity,
$price,
$commission,
$taxAmount,
$ecotaxAmount,
$channelReference,
$additionalFields,
$name,
$image
)
{
$this->reference = $reference;
$this->quantity = $quantity;
$this->unitPrice = $price;
$this->taxAmount = $taxAmount;
$this->id = $id;
$this->reference = $reference;
$this->status = $status;
$this->quantity = $quantity;
$this->unitPrice = $price;
$this->commission = $commission;
$this->taxAmount = $taxAmount;
$this->ecotaxAmount = $ecotaxAmount;
$this->channelReference = $channelReference;
$this->additionalFields = $additionalFields;
$this->name = $name;
$this->image = $image;
}

/**
* @return int
*/
public function getId()
{
return $this->id;
}

/**
Expand All @@ -48,6 +125,14 @@ public function getReference()
return $this->reference;
}

/**
* @return string
*/
public function getStatus()
{
return $this->status;
}

/**
* @return int
*/
Expand All @@ -64,6 +149,14 @@ public function getUnitPrice()
return $this->unitPrice;
}

/**
* @return null|float
*/
public function getCommission()
{
return $this->commission;
}

/**
* @return float
*/
Expand All @@ -72,6 +165,46 @@ public function getTaxAmount()
return $this->taxAmount;
}

/**
* @return float
*/
public function getEcotaxAmount()
{
return $this->ecotaxAmount;
}

/**
* @return string
*/
public function getChannelReference()
{
return $this->channelReference;
}

/**
* @return array
*/
public function getAdditionalFields()
{
return $this->additionalFields;
}

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

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

/**
* @return float|int
*/
Expand All @@ -86,10 +219,18 @@ public function getTotalPrice()
public function toArray()
{
return [
'reference' => $this->getReference(),
'quantity' => $this->getQuantity(),
'price' => $this->getUnitPrice(),
'taxAmount' => $this->getTaxAmount(),
'id' => $this->getId(),
'reference' => $this->getReference(),
'status' => $this->getStatus(),
'quantity' => $this->getQuantity(),
'price' => $this->getUnitPrice(),
'commission' => $this->getCommission(),
'taxAmount' => $this->getTaxAmount(),
'ecotaxAmount' => $this->getEcotaxAmount(),
'channelReference' => $this->getChannelReference(),
'additionalFields' => $this->getAdditionalFields(),
'name' => $this->getName(),
'image' => $this->getImage(),
];
}
}
16 changes: 15 additions & 1 deletion src/Api/Order/OrderItemCollection.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace ShoppingFeed\Sdk\Api\Order;

class OrderItemCollection implements \Countable, \IteratorAggregate
Expand All @@ -18,7 +19,20 @@ public static function fromProperties(array $items)
$instance = new self;
foreach ($items as $item) {
$instance->add(
new OrderItem($item['reference'], $item['quantity'], $item['price'], $item['taxAmount'])
new OrderItem(
$item['id'],
$item['reference'],
$item['status'],
$item['quantity'],
$item['price'],
$item['commission'],
$item['taxAmount'],
$item['ecotaxAmount'],
$item['channelReference'],
$item['additionalFields'],
$item['name'],
$item['image']
)
);
}

Expand Down
66 changes: 44 additions & 22 deletions tests/unit/Api/Order/OrderItemCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,33 @@ class OrderItemCollectionTest extends TestCase
{
private $items = [
[
'reference' => 'a',
'quantity' => 1,
'price' => 2,
'taxAmount' => 7.99,
'id' => 123,
'reference' => 'a',
'status' => '',
'quantity' => 1,
'price' => 2,
'commission' => null,
'taxAmount' => 7.99,
'ecotaxAmount' => 0,
'channelReference' => 'ref',
'additionalFields' => ['taxRate' => 20],
'name' => null,
'image' => null,
],
[
'reference' => 'b',
'quantity' => 2,
'price' => 3,
'taxAmount' => 4.99,
]
'id' => 456,
'reference' => 'b',
'status' => '',
'quantity' => 2,
'price' => 3,
'commission' => null,
'taxAmount' => 4.99,
'ecotaxAmount' => 0,
'channelReference' => 'ref2',
'additionalFields' => ['taxRate' => 45],
'name' => null,
'image' => null,
],
];

public function testCreateCollectionFromArrayOfRemoteProperties()
Expand All @@ -37,23 +53,29 @@ public function testItemsAreWellConstructedFromArray()
$instance = OrderItemCollection::fromProperties($this->items);
$items = $instance->getIterator()->getArrayCopy();

$firstOne = new OrderItem(
$this->items[0]['reference'],
$this->items[0]['quantity'],
$this->items[0]['price'],
$this->items[0]['taxAmount']
);
$firstOne = $this->createItem($this->items[0]);
$secondOne = $this->createItem($this->items[1]);

$this->assertEquals($items[0], $firstOne);
$this->assertEquals($items[1], $secondOne);
}

$secondOne = new OrderItem(
$this->items[1]['reference'],
$this->items[1]['quantity'],
$this->items[1]['price'],
$this->items[1]['taxAmount']
private function createItem(array $item): OrderItem
{
return new OrderItem(
$item['id'],
$item['reference'],
$item['status'],
$item['quantity'],
$item['price'],
$item['commission'],
$item['taxAmount'],
$item['ecotaxAmount'],
$item['channelReference'],
$item['additionalFields'],
$item['name'],
$item['image']
);

$this->assertEquals($items[1], $secondOne);
}

public function testCollectionCanBeRevertedBackToArray()
Expand Down
48 changes: 27 additions & 21 deletions tests/unit/Api/Order/OrderItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,37 @@ class OrderItemTest extends TestCase

public function setUp(): void
{
$this->instance = new OrderItem('a', 2, 9.99, 7.99);
$this->instance = new OrderItem(
123,
'a',
'',
2,
9.99,
null,
7.99,
0,
'A00001',
['taxRate' => 20],
'ASPIRO 2000',
'http://url.com/image.png'
);
}

public function testGetReference()
public function testGetters()
{
$this->assertSame('a', $this->instance->getReference(), 'Reference is the first constructor arg');
}

public function testGetQuantity()
{
$this->assertSame(2, $this->instance->getQuantity(), 'Quantity is the second constructor arg');
}
$this->assertSame(123, $this->instance->getId());
$this->assertSame('a', $this->instance->getReference());
$this->assertSame('', $this->instance->getStatus());
$this->assertSame(2, $this->instance->getQuantity());
$this->assertSame(9.99, $this->instance->getUnitPrice());
$this->assertNull($this->instance->getCommission());
$this->assertSame(7.99, $this->instance->getTaxAmount());
$this->assertSame(0, $this->instance->getEcotaxAmount());
$this->assertSame('A00001', $this->instance->getChannelReference());
$this->assertSame(['taxRate' => 20], $this->instance->getAdditionalFields());
$this->assertSame('ASPIRO 2000', $this->instance->getName());
$this->assertSame('http://url.com/image.png', $this->instance->getImage());

public function testGetUnitPrice()
{
$this->assertSame(9.99, $this->instance->getUnitPrice(), 'Unit Price is the third constructor arg');
}

public function testGetTaxAmount()
{
$this->assertSame(7.99, $this->instance->getTaxAmount(), 'TaxAmount is the last constructor arg');
}

public function testGetTotalPriceWithComputeRowPrice()
{
$this->assertSame(19.98, $this->instance->getTotalPrice());
}
}
Expand Down
Loading

0 comments on commit cc2622a

Please sign in to comment.