Skip to content

Instantly share code, notes, and snippets.

@E1101
Last active February 9, 2023 17:05
Show Gist options
  • Save E1101/4ce13900133d68517f8b0a45f83372c2 to your computer and use it in GitHub Desktop.
Save E1101/4ce13900133d68517f8b0a45f83372c2 to your computer and use it in GitHub Desktop.
<?php
final class OrderLevelShippingCalculatorTest extends TestCase
{
public function testItEnsuresOrderIncludeShippingFeeCollection()
{
$order = SalesOrderBuilder::initialOrder();
$invoice = InvoiceBuilder::initialInvoice();
$calculator = $this->createCalculator(
$this->calculatorGoingToAttachShippingFees($order),
);
self::assertTrue($order->getShippingFeeCollection()->isEmpty());
$calculator->calculate($order, $invoice);
}
public function testItShouldReturnNewInstanceOfInvoice()
{
$order = SalesOrderBuilder::withAttachedShippingFees();
$invoice = InvoiceBuilder::initialInvoice();
$calculator = $this->createCalculator();
$calculatedInvoice = $calculator->calculate($order, $invoice);
self::assertNotSame($calculatedInvoice, $invoice);
}
public function testWhenParcelGetShippedFirst()
{
$order = SalesOrderBuilder::orderWithParcelAndOneManHandling();
$invoice = InvoiceBuilder::invoiceWithShippedParcelItem();
$calculator = $this->createCalculator(
$this->calculatorToCalculateFirstInvoice(),
);
$calculatedInvoice = $calculator->calculate($order, $invoice);
self::assertEquals(5, $calculatedInvoice->getShippingAmount(), 'The invoice should consist of Parcel shipping fee');
self::assertEquals(55, $calculatedInvoice->getGrandTotal(), 'The invoice should consist of Parcel Item cost and shipping fee');
}
/**
* @param Closure ...$mockery fn(Orderprocessing_Model_Order $orderMock, DbTable_Sales_InvoiceTable $tableMock)
* @return OrderLevelShippingCalculator
*/
private function createCalculator(Closure ...$mockery): OrderLevelShippingCalculator
{
$orderModelMock = $this->createMock(Orderprocessing_Model_Order::class);
$invoiceTableMock = $this->createMock(DbTable_Sales_InvoiceTable::class);
$shippingTableMock = $this->createMock(DbTable_Sales_Order_Shipping_FeeTable::class);
$mockery[] = $this->calculatorToCalculateFirstInvoice(false);
$mockery[] = $this->calculatorBeAbleToUpdateInvoicedShippingFeeRow();
foreach ($mockery as $mocker) {
$mocker([
'orderModelMock' => $orderModelMock,
'invoiceTableMock' => $invoiceTableMock,
'shippingTableMock' => $shippingTableMock,
]);
}
return new OrderLevelShippingCalculator(
$orderModelMock,
$invoiceTableMock,
$shippingTableMock
);
}
private function calculatorGoingToAttachShippingFees($order): Closure
{
return function (array $dependencies) use ($order) {
/* @var Orderprocessing_Model_Order $orderModelMock */
['orderModelMock' => $orderModelMock] = $dependencies;
/* @var MockObject $orderModelMock */
$orderModelMock->expects(self::once())
->method('withAttachedShippingFees')
->willReturnCallback(fn () => SalesOrderBuilder::withAttachedShippingFees($order));
};
}
private function calculatorBeAbleToUpdateInvoicedShippingFeeRow(): Closure
{
return function (array $dependencies) {
/* @var DbTable_Sales_Order_Shipping_FeeTable $shippingTableMock */
['shippingTableMock' => $shippingTableMock] = $dependencies;
/* @var MockObject $shippingTableMock */
$shippingTableMock->method('update')
->with(
[
DbTable_Sales_Order_Shipping_FeeRow::STATUS => Statuses::INVOICED,
DbTable_Sales_Order_Shipping_FeeRow::FK_SALES_INVOICE => InvoiceBuilder::INVOICE_ID,
],
$this->anything()
);
};
}
private function calculatorToCalculateFirstInvoice(bool $isFirstInvoice = true): Closure
{
return function (array $dependencies) use ($isFirstInvoice) {
/* @var DbTable_Sales_InvoiceTable $invoiceTableMock */
['invoiceTableMock' => $invoiceTableMock] = $dependencies;
$select = $this->createMock(Zend_Db_Table_Select::class);
$select->method('where')->willReturnSelf();
$select->method('order')->willReturnSelf();
/* @var MockObject $invoiceTableMock */
$invoiceTableMock->method('select')
->willReturn($select);
$invoiceTableMock->method('fetchRowToArray')
->willReturn($isFirstInvoice ? [] : [DbTable_Sales_InvoiceRow::INVOICE_NR => false]);
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment