1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Loevgaard\DandomainStockBundle\Tests\Entity; |
4
|
|
|
|
5
|
|
|
use Loevgaard\DandomainFoundation\Entity\OrderLine; |
6
|
|
|
use Loevgaard\DandomainFoundation\Entity\Product; |
7
|
|
|
use Loevgaard\DandomainStockBundle\Entity\StockMovement; |
8
|
|
|
use Loevgaard\DandomainStockBundle\Exception\CurrencyMismatchException; |
9
|
|
|
use Loevgaard\DandomainStockBundle\Exception\UnsetCurrencyException; |
10
|
|
|
use Money\Currency; |
11
|
|
|
use Money\Money; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
|
14
|
|
|
class StockMovementTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @throws \Loevgaard\DandomainStockBundle\Exception\CurrencyMismatchException |
18
|
|
|
* @throws \Loevgaard\DandomainStockBundle\Exception\UnsetCurrencyException |
19
|
|
|
*/ |
20
|
|
|
public function testGettersSetters() |
21
|
|
|
{ |
22
|
|
|
$stockMovement = $this->getMockForAbstractClass(StockMovement::class); |
23
|
|
|
|
24
|
|
|
$product = new Product(); |
25
|
|
|
$orderLine = new OrderLine(); |
26
|
|
|
|
27
|
|
|
$qty = -3; |
28
|
|
|
$retailPrice = new Money(10396, new Currency('DKK')); |
29
|
|
|
$totalRetailPrice = new Money(31188, new Currency('DKK')); |
30
|
|
|
$price = new Money(7999, new Currency('DKK')); |
31
|
|
|
$totalPrice = new Money(23997, new Currency('DKK')); |
32
|
|
|
$discount = new Money(2397, new Currency('DKK')); |
33
|
|
|
$totalDiscount = new Money(7191, new Currency('DKK')); |
34
|
|
|
|
35
|
|
|
// with vat |
36
|
|
|
$retailPriceInclVat = new Money(12995, new Currency('DKK')); |
37
|
|
|
$totalRetailPriceInclVat = new Money(38985, new Currency('DKK')); |
38
|
|
|
$priceInclVat = new Money(9999, new Currency('DKK')); |
39
|
|
|
$totalPriceInclVat = new Money(29996, new Currency('DKK')); |
40
|
|
|
$discountInclVat = new Money(2996, new Currency('DKK')); |
41
|
|
|
$totalDiscountInclVat = new Money(8989, new Currency('DKK')); |
42
|
|
|
|
43
|
|
|
$stockMovement |
44
|
|
|
->setId(1) |
45
|
|
|
->setQuantity($qty) |
46
|
|
|
->setRetailPrice($retailPrice) |
47
|
|
|
->setPrice($price) |
48
|
|
|
->setType(StockMovement::TYPE_SALE) |
49
|
|
|
->setReference('order') |
50
|
|
|
->setComplaint(false) |
51
|
|
|
->setVatPercentage(25.0) |
52
|
|
|
->setOrderLine($orderLine) |
53
|
|
|
->setProduct($product) |
54
|
|
|
; |
55
|
|
|
|
56
|
|
|
$stockMovement->validate(); |
57
|
|
|
|
58
|
|
|
$this->assertSame(1, $stockMovement->getId()); |
59
|
|
|
$this->assertSame($qty, $stockMovement->getQuantity()); |
60
|
|
|
$this->assertSame('DKK', $stockMovement->getCurrency()); |
61
|
|
|
$this->assertSame(StockMovement::TYPE_SALE, $stockMovement->getType()); |
62
|
|
|
$this->assertSame('order', $stockMovement->getReference()); |
63
|
|
|
$this->assertFalse($stockMovement->isComplaint()); |
64
|
|
|
$this->assertSame(25.0, $stockMovement->getVatPercentage()); |
65
|
|
|
$this->assertSame($product, $stockMovement->getProduct()); |
66
|
|
|
$this->assertSame($orderLine, $stockMovement->getOrderLine()); |
67
|
|
|
|
68
|
|
|
// test prices excl vat |
69
|
|
|
$this->assertEquals($retailPrice, $stockMovement->getRetailPrice()); |
70
|
|
|
$this->assertEquals($totalRetailPrice, $stockMovement->getTotalRetailPrice()); |
71
|
|
|
$this->assertEquals($price, $stockMovement->getPrice()); |
72
|
|
|
$this->assertEquals($totalPrice, $stockMovement->getTotalPrice()); |
73
|
|
|
$this->assertEquals($discount, $stockMovement->getDiscount()); |
74
|
|
|
$this->assertEquals($totalDiscount, $stockMovement->getTotalDiscount()); |
75
|
|
|
|
76
|
|
|
// test prices incl vat |
77
|
|
|
$this->assertEquals($retailPriceInclVat, $stockMovement->getRetailPriceInclVat()); |
78
|
|
|
$this->assertEquals($totalRetailPriceInclVat, $stockMovement->getTotalRetailPriceInclVat()); |
79
|
|
|
$this->assertEquals($priceInclVat, $stockMovement->getPriceInclVat()); |
80
|
|
|
$this->assertEquals($totalPriceInclVat, $stockMovement->getTotalPriceInclVat()); |
81
|
|
|
$this->assertEquals($discountInclVat, $stockMovement->getDiscountInclVat()); |
82
|
|
|
$this->assertEquals($totalDiscountInclVat, $stockMovement->getTotalDiscountInclVat()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testMagicCall1() |
86
|
|
|
{ |
87
|
|
|
$this->expectException(\InvalidArgumentException::class); |
88
|
|
|
$stockMovement = $this->getMockForAbstractClass(StockMovement::class); |
89
|
|
|
$stockMovement->getTest(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function testMagicCall2() |
93
|
|
|
{ |
94
|
|
|
$this->expectException(\InvalidArgumentException::class); |
95
|
|
|
$stockMovement = $this->getMockForAbstractClass(StockMovement::class); |
96
|
|
|
$stockMovement->getTestInclVat(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @throws CurrencyMismatchException |
101
|
|
|
*/ |
102
|
|
|
public function testUpdateCurrency() |
103
|
|
|
{ |
104
|
|
|
$this->expectException(CurrencyMismatchException::class); |
105
|
|
|
$stockMovement = $this->getMockForAbstractClass(StockMovement::class); |
106
|
|
|
$stockMovement->setPrice(new Money(100, new Currency('DKK'))); |
107
|
|
|
$stockMovement->setRetailPrice(new Money(100, new Currency('EUR'))); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @throws UnsetCurrencyException |
112
|
|
|
*/ |
113
|
|
|
public function testMoney() |
114
|
|
|
{ |
115
|
|
|
$this->expectException(UnsetCurrencyException::class); |
116
|
|
|
$stockMovement = $this->getMockForAbstractClass(StockMovement::class); |
117
|
|
|
$stockMovement->getPrice(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @throws CurrencyMismatchException |
122
|
|
|
* @throws UnsetCurrencyException |
123
|
|
|
* @throws \Loevgaard\DandomainStockBundle\Exception\StockMovementProductMismatchException |
124
|
|
|
*/ |
125
|
|
|
public function testDiff() |
126
|
|
|
{ |
127
|
|
|
$diffTests = [ |
128
|
|
|
['original' => 1, 'new' => 1, 'expected' => 0], |
129
|
|
|
['original' => 0, 'new' => 1, 'expected' => 1], |
130
|
|
|
['original' => 0, 'new' => 2, 'expected' => 2], |
131
|
|
|
['original' => -1, 'new' => 1, 'expected' => 2], |
132
|
|
|
|
133
|
|
|
['original' => 1, 'new' => -1, 'expected' => -2], |
134
|
|
|
['original' => 0, 'new' => -1, 'expected' => -1], |
135
|
|
|
['original' => 0, 'new' => -2, 'expected' => -2], |
136
|
|
|
['original' => -1, 'new' => -1, 'expected' => 0], |
137
|
|
|
|
138
|
|
|
['original' => 0, 'new' => 0, 'expected' => 0], |
139
|
|
|
|
140
|
|
|
['original' => 2, 'new' => 3, 'expected' => 1], |
141
|
|
|
['original' => 3, 'new' => 2, 'expected' => -1], |
142
|
|
|
|
143
|
|
|
['original' => -2, 'new' => -3, 'expected' => -1], |
144
|
|
|
['original' => -3, 'new' => -2, 'expected' => 1], |
145
|
|
|
|
146
|
|
|
['original' => 3, 'new' => 0, 'expected' => -3], |
147
|
|
|
['original' => -3, 'new' => 0, 'expected' => 3], |
148
|
|
|
]; |
149
|
|
|
|
150
|
|
|
foreach ($diffTests as $diffTest) { |
151
|
|
|
$originalStockMovement = $this->getMockForAbstractClass(StockMovement::class); |
152
|
|
|
$stockMovement = $this->getMockForAbstractClass(StockMovement::class); |
153
|
|
|
|
154
|
|
|
$product = new Product(); |
155
|
|
|
$product->setId(1); |
156
|
|
|
|
157
|
|
|
$price = new Money(100, new Currency('DKK')); |
158
|
|
|
|
159
|
|
|
$originalStockMovement->setQuantity($diffTest['original'])->setPrice($price)->setProduct($product); |
160
|
|
|
$stockMovement->setQuantity($diffTest['new'])->setPrice($price)->setProduct($product); |
161
|
|
|
|
162
|
|
|
$diff = $originalStockMovement->diff($stockMovement); |
163
|
|
|
|
164
|
|
|
$this->assertSame($diffTest['expected'], $diff->getQuantity()); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|