Skip to content

Commit

Permalink
Order Adapter Changes (#7)
Browse files Browse the repository at this point in the history
* convert to correct payload for tracking API

* remove references of product adapter

* update tests
  • Loading branch information
rockwellll authored Feb 4, 2025
1 parent 57943ad commit 19518fa
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 70 deletions.
12 changes: 7 additions & 5 deletions src/Adapters/OrderAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ public function get () {
}

return array(
'source' => 'woo',
'delivery' => 'deliver',
'reference' => $this->order->get_id(),
'type' => 'order',
'products' => ( isset($this->products) && 0 < count($this->products) )
? $this->products
: $this->adapted_products(),
'items' => $this->adapted_products(),
'total' => ( new PriceAdapter($this->order->get_total(), $this->order->get_currency()) )->get(),
);
}
Expand All @@ -35,7 +34,10 @@ public function adapted_products () {
foreach ($items as $item) {
$product = $item->get_product();

$this->products[] = ( new ProductAdapter($product) )->get();
$this->products[] = [
'product' => ( new ProductAdapter($product) )->get(),
'quantity' => $item->get_quantity(),
];
}

return $this->products;
Expand Down
11 changes: 1 addition & 10 deletions src/Adapters/ProductAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@

class ProductAdapter {
public $product; // WooCommerce product
public $item; // Cart/Order item

public function __construct ($product, $item = null) {
public function __construct ($product) {
$this->product = is_numeric($product) ? wc_get_product( $product ) : $product;
$this->item = $item;
}

public function get () {
Expand All @@ -21,7 +19,6 @@ public function get () {
$response = array(
'reference' => $this->product->get_id(),
'source' => 'woo',
'type' => 'product',
'name' => $this->product->get_name(),
'categories' => wp_get_post_terms( $this->product->get_id(), 'product_cat', array( 'fields' => 'names' ) ),
'price' => ( new PriceAdapter($this->product->get_price()) )->get(),
Expand All @@ -30,12 +27,6 @@ public function get () {
'url' => get_permalink( $this->product->get_id() ),
);

if (isset($this->item)) {
$response['quantity'] = is_array($this->item)
? $this->item['quantity']
: $this->item->get_quantity();
}

$response = array_filter($response, function ($value) {
return null != $value && [] != $value;
});
Expand Down
7 changes: 1 addition & 6 deletions src/Adapters/RefundAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,10 @@ public function get () {

return array(
'reference' => $this->refund->get_id(),
'type' => 'refund',
'source' => 'woo',
'amount' => $this->refund->get_amount(),
'currency' => $this->refund->get_currency(),
'total' => ( new PriceAdapter($this->order->get_total(), $this->order->get_currency()) )->get(),
'refundable' => [
'type' => 'order',
'amount' => $this->order->get_total(),
'currency' => $this->order->get_currency(),
]
);
}

Expand Down
26 changes: 7 additions & 19 deletions tests/Unit/Adapters/OrderAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,44 +28,32 @@

expect($result)->toBeArray();
expect($result)->toHaveKey('reference');
expect($result)->toHaveKey('type');
expect($result)->toHaveKey('products');
expect($result)->toHaveKey('items');
expect($result)->toHaveKey('total');
});

test('has the correct type', function () {
$result = (new OrderAdapter($this->order))->get();

expect($result['type'])->toBe('order');
expect($result['source'])->toBe('woo');
});

test('finds the correct order if passed an ID', function () {
$result = (new OrderAdapter($this->order->get_id()))->get();

expect($result['reference'])->toBe($this->order->get_id());
});

test('has the products array', function () {
test('has the products array as items', function () {
$result = (new OrderAdapter($this->order))->get();

expect($result['products'])->toBeArray();
expect($result['items'])->toBeArray();
});

test('has the correct amount of products', function () {
$result = (new OrderAdapter($this->order))->get();

expect($result['products'])->toHaveLength(1);
expect($result['items'])->toHaveLength(1);
});

test('products have the correct type', function () {
$result = (new OrderAdapter($this->order))->get();

expect($result['products'][0]['type'])->toBe('product');
expect($result['items'][0])->toHaveKey('product');
});

test('will return the products passed in the constructor', function () {
$result = (new OrderAdapter($this->order, ['test']))->get();

expect($result['products'][0])->toBe('test');
expect($result['items'][0])->toBe('test');
});

16 changes: 0 additions & 16 deletions tests/Unit/Adapters/ProductAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,10 @@
expect($result)->toBeArray();
expect($result)->toHaveKey('reference');
expect($result)->toHaveKey('source');
expect($result)->toHaveKey('type');
expect($result)->toHaveKey('name');
expect($result)->toHaveKey('price');
});

test('has the correct type', function () {
$result = (new ProductAdapter($this->product))->get();

expect($result['type'])->toBe('product');
});

test('has the correct source', function () {
$result = (new ProductAdapter($this->product))->get();

Expand All @@ -44,12 +37,3 @@

expect($result['name'])->toBe($this->product->get_name());
});

test('sets the quantity if item is passed', function () {
$item = new WC_Order_Item_Product();
$item->set_quantity(2);

$result = (new ProductAdapter($this->product, $item))->get();

expect($result['quantity'])->toBe(2);
});
14 changes: 0 additions & 14 deletions tests/Unit/Adapters/RefundAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,6 @@

expect($result)->toBeArray();
expect($result)->toHaveKey('reference');
expect($result)->toHaveKey('type');
expect($result)->toHaveKey('amount');
expect($result)->toHaveKey('amount');
expect($result)->toHaveKey('refundable');
});

test('has the correct type', function () {
$result = (new RefundAdapter($this->refund, $this->order))->get();

expect($result['type'])->toBe('refund');
});

test('the refundable has the correct type', function () {
$result = (new RefundAdapter($this->refund, $this->order))->get();

expect($result['refundable']['type'])->toBe('order');
});

0 comments on commit 19518fa

Please sign in to comment.