Skip to content

Commit aa24a9e

Browse files
committed
MAGETWO-67626: Auto-generated classes behave differently in unit tests and application
- fixed unit test
1 parent cb44fdb commit aa24a9e

File tree

1 file changed

+88
-54
lines changed

1 file changed

+88
-54
lines changed

app/code/Magento/Sales/Test/Unit/Model/Order/Invoice/Total/ShippingTest.php

Lines changed: 88 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,105 @@
66

77
namespace Magento\Sales\Test\Unit\Model\Order\Invoice\Total;
88

9+
use Magento\Sales\Model\Order\Invoice\Total\Shipping;
10+
911
/**
1012
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1113
*/
1214
class ShippingTest extends \PHPUnit_Framework_TestCase
1315
{
16+
/**
17+
* @var Shipping
18+
*/
19+
private $total;
20+
21+
protected function setUp()
22+
{
23+
$this->total = new Shipping();
24+
}
25+
26+
/**
27+
* @dataProvider collectWithNoOrZeroPrevInvoiceDataProvider
28+
* @param array $prevInvoicesData
29+
* @param float $orderShipping
30+
* @param float $expectedShipping
31+
*/
32+
public function testCollectWithNoOrZeroPrevInvoice(array $prevInvoicesData, $orderShipping, $expectedShipping)
33+
{
34+
$invoice = $this->createInvoiceStub($prevInvoicesData, $orderShipping);
35+
$invoice->expects($this->exactly(2))
36+
->method('setShippingAmount')
37+
->withConsecutive([0], [$expectedShipping]);
38+
39+
$this->total->collect($invoice);
40+
}
41+
42+
/**
43+
* @return array
44+
*/
45+
public static function collectWithNoOrZeroPrevInvoiceDataProvider()
46+
{
47+
return [
48+
'no previous invoices' => [
49+
'prevInvoicesData' => [[]],
50+
'orderShipping' => 10.00,
51+
'expectedShipping' => 10.00,
52+
],
53+
'zero shipping in previous invoices' => [
54+
'prevInvoicesData' => [['shipping_amount' => '0.0000']],
55+
'orderShipping' => 10.00,
56+
'expectedShipping' => 10.00,
57+
],
58+
];
59+
}
60+
61+
public function testCollectWithPreviousInvoice()
62+
{
63+
$orderShipping = 10.00;
64+
$prevInvoicesData = [['shipping_amount' => '10.000']];
65+
$invoice = $this->createInvoiceStub($prevInvoicesData, $orderShipping);
66+
$invoice->expects($this->once())
67+
->method('setShippingAmount')
68+
->with(0);
69+
70+
$this->total->collect($invoice);
71+
}
72+
73+
/**
74+
* Create stub of an invoice
75+
*
76+
* @param array $prevInvoicesData
77+
* @param float $orderShipping
78+
* @return \Magento\Sales\Model\Order\Invoice|\PHPUnit_Framework_MockObject_MockObject
79+
*/
80+
private function createInvoiceStub(array $prevInvoicesData, $orderShipping)
81+
{
82+
$order = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
83+
->disableOriginalConstructor()
84+
->getMock();
85+
$order->expects($this->any())
86+
->method('getInvoiceCollection')
87+
->will($this->returnValue($this->getInvoiceCollection($prevInvoicesData)));
88+
$order->expects($this->any())
89+
->method('getShippingAmount')
90+
->willReturn($orderShipping);
91+
/** @var $invoice \Magento\Sales\Model\Order\Invoice|\PHPUnit_Framework_MockObject_MockObject */
92+
$invoice = $this->getMockBuilder(\Magento\Sales\Model\Order\Invoice::class)
93+
->disableOriginalConstructor()
94+
->getMock();
95+
$invoice->expects($this->any())
96+
->method('getOrder')
97+
->willReturn($order);
98+
return $invoice;
99+
}
100+
14101
/**
15102
* Retrieve new invoice collection from an array of invoices' data
16103
*
17104
* @param array $invoicesData
18105
* @return \Magento\Framework\Data\Collection
19106
*/
20-
protected function _getInvoiceCollection(array $invoicesData)
107+
private function getInvoiceCollection(array $invoicesData)
21108
{
22109
$className = \Magento\Sales\Model\Order\Invoice::class;
23110
$result = new \Magento\Framework\Data\Collection(
@@ -71,57 +158,4 @@ protected function _getInvoiceCollection(array $invoicesData)
71158
}
72159
return $result;
73160
}
74-
75-
/**
76-
* @dataProvider collectDataProvider
77-
* @param array $prevInvoicesData
78-
* @param float $orderShipping
79-
* @param float $expectedShipping
80-
*/
81-
public function testCollect(array $prevInvoicesData, $orderShipping, $expectedShipping)
82-
{
83-
$order = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
84-
->disableOriginalConstructor()
85-
->getMock();
86-
$order->expects($this->any())
87-
->method('getInvoiceCollection')
88-
->will($this->returnValue($this->_getInvoiceCollection($prevInvoicesData)));
89-
$order->expects($this->any())
90-
->method('getShippingAmount')
91-
->willReturn($orderShipping);
92-
/** @var $invoice \Magento\Sales\Model\Order\Invoice|\PHPUnit_Framework_MockObject_MockObject */
93-
$invoice = $this->getMockBuilder(\Magento\Sales\Model\Order\Invoice::class)
94-
->disableOriginalConstructor()
95-
->getMock();
96-
$invoice->expects($this->any())
97-
->method('getOrder')
98-
->willReturn($order);
99-
$invoice->expects($this->any())
100-
->method('setShippingAmount')
101-
->withConsecutive([0], [$expectedShipping]);
102-
103-
$total = new \Magento\Sales\Model\Order\Invoice\Total\Shipping();
104-
$total->collect($invoice);
105-
}
106-
107-
public static function collectDataProvider()
108-
{
109-
return [
110-
'no previous invoices' => [
111-
'prevInvoicesData' => [[]],
112-
'orderShipping' => 10.00,
113-
'expectedShipping' => 10.00,
114-
],
115-
'zero shipping in previous invoices' => [
116-
'prevInvoicesData' => [['shipping_amount' => '0.0000']],
117-
'orderShipping' => 10.00,
118-
'expectedShipping' => 10.00,
119-
],
120-
'non-zero shipping in previous invoices' => [
121-
'prevInvoicesData' => [['shipping_amount' => '10.000']],
122-
'orderShipping' => 10.00,
123-
'expectedShipping' => 0,
124-
]
125-
];
126-
}
127161
}

0 commit comments

Comments
 (0)