|
6 | 6 |
|
7 | 7 | /**
|
8 | 8 | * Class PaymentTest
|
| 9 | + * |
9 | 10 | * @package Magento\Sales\Model\Order
|
10 | 11 | */
|
11 | 12 | class PaymentTest extends \PHPUnit_Framework_TestCase
|
12 | 13 | {
|
13 |
| - /** |
14 |
| - * @var Payment |
15 |
| - */ |
16 |
| - protected $payment; |
| 14 | + /** @var Payment */ |
| 15 | + private $payment; |
17 | 16 |
|
18 |
| - /** |
19 |
| - * @var \Magento\Payment\Helper\Data | \PHPUnit_Framework_MockObject_MockObject |
20 |
| - */ |
21 |
| - protected $helper; |
| 17 | + /** @var \Magento\Payment\Helper\Data | \PHPUnit_Framework_MockObject_MockObject */ |
| 18 | + private $helperMock; |
22 | 19 |
|
23 |
| - /** |
24 |
| - * @var \Magento\Framework\Event\Manager | \PHPUnit_Framework_MockObject_MockObject |
25 |
| - */ |
26 |
| - protected $eventManager; |
| 20 | + /** @var \Magento\Framework\Event\Manager | \PHPUnit_Framework_MockObject_MockObject */ |
| 21 | + private $eventManagerMock; |
| 22 | + |
| 23 | + /** @var \Magento\Directory\Model\PriceCurrency | \PHPUnit_Framework_MockObject_MockObject */ |
| 24 | + private $priceCurrencyMock; |
| 25 | + |
| 26 | + /** @var \Magento\Sales\Model\Order | \PHPUnit_Framework_MockObject_MockObject $orderMock */ |
| 27 | + private $orderMock; |
| 28 | + |
| 29 | + /** @var \Magento\Payment\Model\Method\AbstractMethod | \PHPUnit_Framework_MockObject_MockObject $orderMock */ |
| 30 | + private $paymentMethodMock; |
27 | 31 |
|
28 | 32 | protected function setUp()
|
29 | 33 | {
|
30 |
| - $objectManger = new \Magento\TestFramework\Helper\ObjectManager($this); |
| 34 | + $this->eventManagerMock = $this->getMockBuilder('Magento\Framework\Event\Manager') |
| 35 | + ->disableOriginalConstructor() |
| 36 | + ->getMock(); |
31 | 37 |
|
32 |
| - $this->eventManager = $this->getMock('Magento\Framework\Event\Manager', [], [], '', false); |
| 38 | + $context = $this->getMockBuilder('Magento\Framework\Model\Context') |
| 39 | + ->disableOriginalConstructor() |
| 40 | + ->getMock(); |
33 | 41 |
|
34 |
| - $context = $this->getMock('Magento\Framework\Model\Context', [], [], '', false); |
35 | 42 | $context->expects($this->once())
|
36 | 43 | ->method('getEventDispatcher')
|
37 |
| - ->will($this->returnValue($this->eventManager)); |
| 44 | + ->will($this->returnValue($this->eventManagerMock)); |
| 45 | + |
| 46 | + $this->helperMock = $this->getMockBuilder('Magento\Payment\Helper\Data') |
| 47 | + ->disableOriginalConstructor() |
| 48 | + ->setMethods(['getMethodInstance']) |
| 49 | + ->getMock(); |
| 50 | + |
| 51 | + $this->priceCurrencyMock = $this->getMockBuilder('Magento\Directory\Model\PriceCurrency') |
| 52 | + ->disableOriginalConstructor() |
| 53 | + ->setMethods(['format']) |
| 54 | + ->getMock(); |
| 55 | + |
| 56 | + $this->priceCurrencyMock->expects($this->any()) |
| 57 | + ->method('format') |
| 58 | + ->willReturnCallback( |
| 59 | + function ($value) { |
| 60 | + return $value; |
| 61 | + } |
| 62 | + ); |
| 63 | + |
| 64 | + $this->paymentMethodMock = $this->getMockBuilder('Magento\Payment\Model\Method\AbstractMethod') |
| 65 | + ->disableOriginalConstructor() |
| 66 | + ->setMethods([ |
| 67 | + 'canVoid', |
| 68 | + 'authorize', |
| 69 | + 'getConfigData', |
| 70 | + 'getConfigPaymentAction', |
| 71 | + 'validate', |
| 72 | + ]) |
| 73 | + ->getMock(); |
| 74 | + |
| 75 | + $this->helperMock->expects($this->once()) |
| 76 | + ->method('getMethodInstance') |
| 77 | + ->will($this->returnValue($this->paymentMethodMock)); |
38 | 78 |
|
39 |
| - $this->helper = $this->getMock('Magento\Payment\Helper\Data', ['getMethodInstance'], [], '', false); |
| 79 | + $this->orderMock = $this->getMockBuilder('Magento\Sales\Model\Order') |
| 80 | + ->disableOriginalConstructor() |
| 81 | + ->setMethods([ |
| 82 | + 'getConfig', |
| 83 | + 'setState', |
| 84 | + 'getStoreId', |
| 85 | + 'getBaseGrandTotal', |
| 86 | + 'getBaseCurrency', |
| 87 | + 'getBaseCurrencyCode', |
| 88 | + 'getTotalDue', |
| 89 | + 'getBaseTotalDue', |
| 90 | + ]) |
| 91 | + ->getMock(); |
40 | 92 |
|
41 |
| - $this->payment = $objectManger->getObject( |
| 93 | + $this->payment = (new \Magento\TestFramework\Helper\ObjectManager($this))->getObject( |
42 | 94 | 'Magento\Sales\Model\Order\Payment',
|
43 | 95 | [
|
44 |
| - 'paymentData' => $this->helper, |
45 |
| - 'context' => $context |
| 96 | + 'context' => $context, |
| 97 | + 'paymentData' => $this->helperMock, |
| 98 | + 'priceCurrency' => $this->priceCurrencyMock, |
46 | 99 | ]
|
47 | 100 | );
|
| 101 | + |
| 102 | + $this->payment->setMethod('any'); |
| 103 | + $this->payment->setOrder($this->orderMock); |
48 | 104 | }
|
49 | 105 |
|
50 | 106 | protected function tearDown()
|
51 | 107 | {
|
52 |
| - $this->payment = null; |
| 108 | + unset($this->payment); |
53 | 109 | }
|
54 | 110 |
|
55 | 111 | public function testCancel()
|
56 | 112 | {
|
57 |
| - $paymentMethod = $this->getMock('Magento\Payment\Model\Method\AbstractMethod', ['canVoid'], [], '', false); |
58 |
| - $this->helper->expects($this->once())->method('getMethodInstance')->will($this->returnValue($paymentMethod)); |
59 |
| - $this->payment->setMethod('any'); |
60 | 113 | // check fix for partial refunds in Payflow Pro
|
61 |
| - $paymentMethod->expects( |
62 |
| - $this->once() |
63 |
| - )->method( |
64 |
| - 'canVoid' |
65 |
| - )->with( |
66 |
| - new \PHPUnit_Framework_Constraint_IsIdentical($this->payment) |
67 |
| - )->will( |
68 |
| - $this->returnValue(false) |
69 |
| - ); |
| 114 | + $this->paymentMethodMock->expects($this->once()) |
| 115 | + ->method('canVoid') |
| 116 | + ->with($this->payment) |
| 117 | + ->willReturn(false); |
70 | 118 |
|
71 | 119 | $this->assertEquals($this->payment, $this->payment->cancel());
|
72 | 120 | }
|
73 | 121 |
|
74 | 122 | public function testPlace()
|
75 | 123 | {
|
76 | 124 | $newOrderStatus = 'new_status';
|
77 |
| - /** @var \Magento\Sales\Model\Order\Config | \PHPUnit_Framework_MockObject_MockObject $orderConfig */ |
78 |
| - $orderConfig = $this->getMock('Magento\Sales\Model\Order\Config', [], [], '', false); |
79 |
| - $orderConfig->expects($this->at(0)) |
| 125 | + |
| 126 | + /** @var \Magento\Sales\Model\Order\Config | \PHPUnit_Framework_MockObject_MockObject $orderConfigMock */ |
| 127 | + $orderConfigMock = $this->getMockBuilder('Magento\Sales\Model\Order\Config') |
| 128 | + ->disableOriginalConstructor() |
| 129 | + ->setMethods(['getStateStatuses', 'getStateDefaultStatus']) |
| 130 | + ->getMock(); |
| 131 | + |
| 132 | + $orderConfigMock->expects($this->once()) |
80 | 133 | ->method('getStateStatuses')
|
81 | 134 | ->with(\Magento\Sales\Model\Order::STATE_NEW)
|
82 | 135 | ->will($this->returnValue(['firstStatus', 'secondStatus']));
|
83 |
| - $orderConfig->expects($this->at(1)) |
| 136 | + |
| 137 | + $orderConfigMock->expects($this->once()) |
84 | 138 | ->method('getStateDefaultStatus')
|
85 | 139 | ->with(\Magento\Sales\Model\Order::STATE_NEW)
|
86 | 140 | ->will($this->returnValue($newOrderStatus));
|
87 |
| - /** @var \Magento\Sales\Model\Order | \PHPUnit_Framework_MockObject_MockObject $order */ |
88 |
| - $order = $this->getMock('Magento\Sales\Model\Order', [], [], '', false); |
89 |
| - $order->expects($this->any()) |
| 141 | + |
| 142 | + $this->orderMock->expects($this->exactly(2)) |
90 | 143 | ->method('getConfig')
|
91 |
| - ->will($this->returnValue($orderConfig)); |
92 |
| - $order->expects($this->once()) |
| 144 | + ->will($this->returnValue($orderConfigMock)); |
| 145 | + |
| 146 | + $this->orderMock->expects($this->once()) |
93 | 147 | ->method('setState')
|
94 | 148 | ->with(\Magento\Sales\Model\Order::STATE_NEW, $newOrderStatus);
|
95 |
| - $methodInstance = $this->getMock('Magento\Payment\Model\Method\AbstractMethod', [], [], '', false); |
96 | 149 |
|
97 |
| - $this->payment->setOrder($order); |
98 |
| - $this->payment->setMethodInstance($methodInstance); |
| 150 | + $this->paymentMethodMock->expects($this->once()) |
| 151 | + ->method('getConfigPaymentAction') |
| 152 | + ->willReturn(null); |
99 | 153 |
|
100 |
| - $this->eventManager->expects($this->at(0)) |
| 154 | + $this->eventManagerMock->expects($this->at(0)) |
101 | 155 | ->method('dispatch')
|
102 | 156 | ->with('sales_order_payment_place_start', ['payment' => $this->payment]);
|
103 |
| - $this->eventManager->expects($this->at(1)) |
| 157 | + |
| 158 | + $this->eventManagerMock->expects($this->at(1)) |
104 | 159 | ->method('dispatch')
|
105 | 160 | ->with('sales_order_payment_place_end', ['payment' => $this->payment]);
|
106 | 161 |
|
107 | 162 | $this->assertEquals($this->payment, $this->payment->place());
|
108 | 163 | }
|
| 164 | + |
| 165 | + public function testAuthorize() |
| 166 | + { |
| 167 | + $storeID = 1; |
| 168 | + $amount = 10; |
| 169 | + |
| 170 | + $baseCurrencyMock = $this->getMockBuilder('Magento\Directory\Model\Currency') |
| 171 | + ->disableOriginalConstructor() |
| 172 | + ->setMethods(['formatTxt']) |
| 173 | + ->getMock(); |
| 174 | + |
| 175 | + $baseCurrencyMock->expects($this->once()) |
| 176 | + ->method('formatTxt') |
| 177 | + ->willReturnCallback( |
| 178 | + function ($value) { |
| 179 | + return $value; |
| 180 | + } |
| 181 | + ); |
| 182 | + |
| 183 | + $this->orderMock->expects($this->once()) |
| 184 | + ->method('getStoreId') |
| 185 | + ->willReturn($storeID); |
| 186 | + |
| 187 | + $this->orderMock->expects($this->once()) |
| 188 | + ->method('getBaseGrandTotal') |
| 189 | + ->willReturn($amount); |
| 190 | + |
| 191 | + $this->orderMock->expects($this->once()) |
| 192 | + ->method('getBaseCurrency') |
| 193 | + ->willReturn($baseCurrencyMock); |
| 194 | + |
| 195 | + $this->orderMock->expects($this->once()) |
| 196 | + ->method('setState') |
| 197 | + ->with(\Magento\Sales\Model\Order::STATE_PROCESSING, true, 'Authorized amount of ' . $amount); |
| 198 | + |
| 199 | + $this->paymentMethodMock->expects($this->once()) |
| 200 | + ->method('authorize') |
| 201 | + ->with($this->payment) |
| 202 | + ->willReturnSelf(); |
| 203 | + |
| 204 | + $paymentResult = $this->payment->authorize(true, $amount); |
| 205 | + |
| 206 | + $this->assertInstanceOf('Magento\Sales\Model\Order\Payment', $paymentResult); |
| 207 | + $this->assertEquals($amount, $paymentResult->getBaseAmountAuthorized()); |
| 208 | + } |
| 209 | + |
| 210 | + public function testAuthorizeFraudDetected() |
| 211 | + { |
| 212 | + $storeID = 1; |
| 213 | + $amount = 10; |
| 214 | + |
| 215 | + $baseCurrencyMock = $this->getMockBuilder('Magento\Directory\Model\Currency') |
| 216 | + ->disableOriginalConstructor() |
| 217 | + ->setMethods(['formatTxt']) |
| 218 | + ->getMock(); |
| 219 | + |
| 220 | + $baseCurrencyMock->expects($this->once()) |
| 221 | + ->method('formatTxt') |
| 222 | + ->willReturnCallback( |
| 223 | + function ($value) { |
| 224 | + return $value; |
| 225 | + } |
| 226 | + ); |
| 227 | + |
| 228 | + $this->orderMock->expects($this->once()) |
| 229 | + ->method('getStoreId') |
| 230 | + ->willReturn($storeID); |
| 231 | + |
| 232 | + $this->orderMock->expects($this->once()) |
| 233 | + ->method('getBaseCurrencyCode') |
| 234 | + ->willReturn("USD"); |
| 235 | + |
| 236 | + $this->orderMock->expects($this->once()) |
| 237 | + ->method('getBaseCurrency') |
| 238 | + ->willReturn($baseCurrencyMock); |
| 239 | + |
| 240 | + $this->orderMock->expects($this->once()) |
| 241 | + ->method('setState') |
| 242 | + ->with( |
| 243 | + \Magento\Sales\Model\Order::STATE_PAYMENT_REVIEW, |
| 244 | + \Magento\Sales\Model\Order::STATUS_FRAUD, |
| 245 | + "Order is suspended as its authorizing amount $amount is suspected to be fraudulent." |
| 246 | + ); |
| 247 | + |
| 248 | + $this->paymentMethodMock->expects($this->once()) |
| 249 | + ->method('authorize') |
| 250 | + ->with($this->payment) |
| 251 | + ->willReturnSelf(); |
| 252 | + |
| 253 | + $this->payment->setCurrencyCode('GBP'); |
| 254 | + |
| 255 | + $paymentResult = $this->payment->authorize(true, $amount); |
| 256 | + |
| 257 | + $this->assertInstanceOf('Magento\Sales\Model\Order\Payment', $paymentResult); |
| 258 | + $this->assertEquals($amount, $paymentResult->getBaseAmountAuthorized()); |
| 259 | + $this->assertTrue($paymentResult->getIsFraudDetected()); |
| 260 | + } |
| 261 | + |
| 262 | + public function testAuthorizeTransactionPending() |
| 263 | + { |
| 264 | + $storeID = 1; |
| 265 | + $amount = 10; |
| 266 | + |
| 267 | + $baseCurrencyMock = $this->getMockBuilder('Magento\Directory\Model\Currency') |
| 268 | + ->disableOriginalConstructor() |
| 269 | + ->setMethods(['formatTxt']) |
| 270 | + ->getMock(); |
| 271 | + |
| 272 | + $baseCurrencyMock->expects($this->once()) |
| 273 | + ->method('formatTxt') |
| 274 | + ->willReturnCallback( |
| 275 | + function ($value) { |
| 276 | + return $value; |
| 277 | + } |
| 278 | + ); |
| 279 | + |
| 280 | + $this->orderMock->expects($this->once()) |
| 281 | + ->method('getStoreId') |
| 282 | + ->willReturn($storeID); |
| 283 | + |
| 284 | + $this->orderMock->expects($this->once()) |
| 285 | + ->method('getBaseGrandTotal') |
| 286 | + ->willReturn($amount); |
| 287 | + |
| 288 | + $this->orderMock->expects($this->once()) |
| 289 | + ->method('getBaseCurrency') |
| 290 | + ->willReturn($baseCurrencyMock); |
| 291 | + |
| 292 | + $this->orderMock->expects($this->once()) |
| 293 | + ->method('setState') |
| 294 | + ->with( |
| 295 | + \Magento\Sales\Model\Order::STATE_PAYMENT_REVIEW, |
| 296 | + true, |
| 297 | + "We will authorize $amount after the payment is approved at the payment gateway." |
| 298 | + ); |
| 299 | + |
| 300 | + $this->paymentMethodMock->expects($this->once()) |
| 301 | + ->method('authorize') |
| 302 | + ->with($this->payment) |
| 303 | + ->willReturnSelf(); |
| 304 | + |
| 305 | + $this->payment->setIsTransactionPending(true); |
| 306 | + |
| 307 | + $paymentResult = $this->payment->authorize(true, $amount); |
| 308 | + |
| 309 | + $this->assertInstanceOf('Magento\Sales\Model\Order\Payment', $paymentResult); |
| 310 | + $this->assertEquals($amount, $paymentResult->getBaseAmountAuthorized()); |
| 311 | + $this->assertTrue($paymentResult->getIsTransactionPending()); |
| 312 | + } |
109 | 313 | }
|
0 commit comments