1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Loevgaard\DandomainStockBundle\Tests\EventListener; |
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\Event\LifecycleEventArgs; |
8
|
|
|
use Doctrine\ORM\Events; |
9
|
|
|
use Loevgaard\DandomainFoundation\Entity\Generated\OrderInterface; |
10
|
|
|
use Loevgaard\DandomainFoundation\Entity\Generated\ProductInterface; |
11
|
|
|
use Loevgaard\DandomainFoundation\Entity\Order; |
12
|
|
|
use Loevgaard\DandomainFoundation\Entity\OrderLine; |
13
|
|
|
use Loevgaard\DandomainFoundation\Entity\State; |
14
|
|
|
use Loevgaard\DandomainStock\Exception\CurrencyMismatchException; |
15
|
|
|
use Loevgaard\DandomainStock\Exception\StockMovementProductMismatchException; |
16
|
|
|
use Loevgaard\DandomainStock\Exception\UndefinedPriceForCurrencyException; |
17
|
|
|
use Loevgaard\DandomainStock\Exception\UnsetCurrencyException; |
18
|
|
|
use Loevgaard\DandomainStock\Exception\UnsetProductException; |
19
|
|
|
use Loevgaard\DandomainStockBundle\EventListener\StockMovementSubscriber; |
20
|
|
|
use PHPUnit\Framework\TestCase; |
21
|
|
|
|
22
|
|
|
final class StockMovementSubscriberTest extends TestCase |
23
|
|
|
{ |
24
|
|
|
public function testEventsAndCorrespondingMethods() |
25
|
|
|
{ |
26
|
|
|
$subscriber = new StockMovementSubscriber([]); |
27
|
|
|
|
28
|
|
|
$events = [ |
29
|
|
|
Events::prePersist, |
30
|
|
|
Events::preUpdate, |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
$this->assertEquals($events, $subscriber->getSubscribedEvents()); |
34
|
|
|
|
35
|
|
|
$refl = new \ReflectionClass(StockMovementSubscriber::class); |
36
|
|
|
|
37
|
|
|
foreach ($events as $event) { |
38
|
|
|
$this->assertTrue($refl->hasMethod($event)); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @throws CurrencyMismatchException |
44
|
|
|
* @throws StockMovementProductMismatchException |
45
|
|
|
* @throws UndefinedPriceForCurrencyException |
46
|
|
|
* @throws UnsetCurrencyException |
47
|
|
|
* @throws UnsetProductException |
48
|
|
|
*/ |
49
|
|
|
public function testInstanceType() |
50
|
|
|
{ |
51
|
|
|
$lifecycleEventArgs = $this->getLifecycleEventArgs(new \stdClass()); |
52
|
|
|
|
53
|
|
|
$subscriber = new StockMovementSubscriber([]); |
54
|
|
|
$res = $subscriber->preUpdate($lifecycleEventArgs); |
|
|
|
|
55
|
|
|
|
56
|
|
|
$this->assertFalse($res); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @throws CurrencyMismatchException |
61
|
|
|
* @throws StockMovementProductMismatchException |
62
|
|
|
* @throws UndefinedPriceForCurrencyException |
63
|
|
|
* @throws UnsetCurrencyException |
64
|
|
|
* @throws UnsetProductException |
65
|
|
|
*/ |
66
|
|
|
public function testOrderStateCondition() |
67
|
|
|
{ |
68
|
|
|
$order = new Order(); |
69
|
|
|
|
70
|
|
|
$state = new State(); |
71
|
|
|
$state->setExternalId(1); |
72
|
|
|
$order->setState($state); |
73
|
|
|
|
74
|
|
|
$lifecycleEventArgs = $this->getLifecycleEventArgs($order); |
75
|
|
|
|
76
|
|
|
$subscriber = new StockMovementSubscriber([3]); |
77
|
|
|
$res = $subscriber->preUpdate($lifecycleEventArgs); |
|
|
|
|
78
|
|
|
|
79
|
|
|
$this->assertFalse($res); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @throws CurrencyMismatchException |
84
|
|
|
* @throws StockMovementProductMismatchException |
85
|
|
|
* @throws UndefinedPriceForCurrencyException |
86
|
|
|
* @throws UnsetCurrencyException |
87
|
|
|
* @throws UnsetProductException |
88
|
|
|
*/ |
89
|
|
|
public function testQuantityEqualsZero() |
90
|
|
|
{ |
91
|
|
|
$order = $this->getOrder(1, 0); |
92
|
|
|
|
93
|
|
|
$lifecycleEventArgs = $this->getLifecycleEventArgs($order); |
94
|
|
|
|
95
|
|
|
$subscriber = new StockMovementSubscriber([1]); |
96
|
|
|
$res = $subscriber->preUpdate($lifecycleEventArgs); |
|
|
|
|
97
|
|
|
|
98
|
|
|
$this->assertTrue($res); |
99
|
|
|
foreach ($order->getOrderLines() as $orderLine) { |
100
|
|
|
$this->assertCount(0, $orderLine->getStockMovements()); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @throws CurrencyMismatchException |
106
|
|
|
* @throws StockMovementProductMismatchException |
107
|
|
|
* @throws UndefinedPriceForCurrencyException |
108
|
|
|
* @throws UnsetCurrencyException |
109
|
|
|
* @throws UnsetProductException |
110
|
|
|
*/ |
111
|
|
|
public function testNoProduct() |
112
|
|
|
{ |
113
|
|
|
$orderStateId = 1; |
114
|
|
|
$order = $this->getOrder($orderStateId); |
115
|
|
|
|
116
|
|
|
$lifecycleEventArgs = $this->getLifecycleEventArgs($order); |
117
|
|
|
|
118
|
|
|
$subscriber = new StockMovementSubscriber([$orderStateId]); |
119
|
|
|
$res = $subscriber->preUpdate($lifecycleEventArgs); |
|
|
|
|
120
|
|
|
|
121
|
|
|
$this->assertTrue($res); |
122
|
|
|
foreach ($order->getOrderLines() as $orderLine) { |
123
|
|
|
$this->assertCount(0, $orderLine->getStockMovements()); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param object $object |
129
|
|
|
* |
130
|
|
|
* @return \PHPUnit\Framework\MockObject\MockObject|LifecycleEventArgs |
131
|
|
|
*/ |
132
|
|
|
private function getLifecycleEventArgs($object) |
133
|
|
|
{ |
134
|
|
|
$lifecycleEventArgs = $this->getMockBuilder(LifecycleEventArgs::class)->disableOriginalConstructor()->getMock(); |
135
|
|
|
$lifecycleEventArgs->method('getObject')->willReturn($object); |
136
|
|
|
|
137
|
|
|
return $lifecycleEventArgs; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
private function getOrder(int $orderStateId = 1, $orderLineQuantity = 1, ProductInterface $product = null): OrderInterface |
141
|
|
|
{ |
142
|
|
|
$order = new Order(); |
143
|
|
|
$order->setCreatedDate(new \DateTimeImmutable()); |
144
|
|
|
|
145
|
|
|
// set state |
146
|
|
|
$state = new State(); |
147
|
|
|
$state->setExternalId($orderStateId); |
148
|
|
|
$order->setState($state); |
149
|
|
|
|
150
|
|
|
// add order line |
151
|
|
|
$orderLine = new OrderLine(); |
152
|
|
|
$orderLine->setQuantity($orderLineQuantity); |
153
|
|
|
if ($product) { |
154
|
|
|
$orderLine->setProduct($product); |
155
|
|
|
} |
156
|
|
|
$order->addOrderLine($orderLine); |
157
|
|
|
|
158
|
|
|
return $order; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.