Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ExpressTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
21 use PHPUnit\Framework\TestCase;
22 
26 class ExpressTest extends TestCase
27 {
31  private $express;
32 
36  private $payment;
37 
41  private $paymentInstance;
42 
46  private $pro;
47 
51  private $nvp;
52 
56  private $order;
57 
61  private $transactionRepository;
62 
66  private $transaction;
67 
68  protected function setUp()
69  {
70  $objectManager = new ObjectManager($this);
71  $this->nvp = $this->createPartialMock(
72  Nvp::class,
73  ['getData','setProcessableErrors', 'callDoAuthorization']
74  );
75  $this->nvp->method('getData')->willReturn([]);
76  $this->nvp->method('setProcessableErrors')->willReturnSelf();
77 
78  $this->pro = $this->createPartialMock(
79  Pro::class,
80  ['setMethod', 'getApi', 'importPaymentInfo']
81  );
82  $this->pro->method('getApi')->willReturn($this->nvp);
83 
84  $this->transaction = $this->getMockForAbstractClass(TransactionInterface::class);
85  $this->transactionRepository = $this->createPartialMock(
86  TransactionRepository::class,
87  ['getByTransactionType']
88  );
89  $this->transactionRepository->method('getByTransactionType')->willReturn($this->transaction);
90 
91  $this->express = $objectManager->getObject(
92  Express::class,
93  [
94  'data' => [$this->pro],
95  'transactionRepository' => $this->transactionRepository,
96  ]
97  );
98 
99  $this->paymentInstance = $this->getMockForAbstractClass(MethodInterface::class);
100  $this->payment = $this->createPartialMock(
101  Payment::class,
102  [
103  'getAmountAuthorized',
104  'getMethod',
105  'getMethodInstance',
106  'getId',
107  'getOrder',
108  'addTransaction',
109  'addTransactionCommentsToOrder',
110  'setAmountAuthorized',
111  ]
112  );
113  $this->payment->method('getMethodInstance')
114  ->willReturn($this->paymentInstance);
115 
116  $this->payment->method('addTransaction')
117  ->willReturn($this->transaction);
118  }
119 
125  public function testAuthorizeOrder()
126  {
127  $this->order = $this->createPartialMock(
128  Order::class,
129  ['getId', 'getPayment', 'getTotalDue', 'getBaseTotalDue']
130  );
131  $this->order->method('getPayment')
132  ->willReturn($this->payment);
133  $this->order->method('getId')
134  ->willReturn(1);
135 
136  $totalDue = 15;
137  $baseTotalDue = 10;
138 
139  $this->order->method('getTotalDue')
140  ->willReturn($totalDue);
141  $this->order->method('getBaseTotalDue')
142  ->willReturn($baseTotalDue);
143 
144  $this->payment->method('getMethod')
145  ->willReturn('paypal_express');
146  $this->payment->method('getId')
147  ->willReturn(1);
148  $this->payment->method('getOrder')
149  ->willReturn($this->order);
150  $this->payment->method('getAmountAuthorized')
151  ->willReturn(0);
152 
153  $this->paymentInstance->method('getConfigPaymentAction')
154  ->willReturn('order');
155 
156  $this->nvp->expects(static::once())
157  ->method('callDoAuthorization')
158  ->willReturnSelf();
159 
160  $this->payment->expects(static::once())
161  ->method('addTransaction')
162  ->with(Transaction::TYPE_AUTH)
163  ->willReturn($this->transaction);
164 
165  $this->payment->method('addTransactionCommentsToOrder')
166  ->with($this->transaction);
167 
168  $this->payment->method('setAmountAuthorized')
169  ->with($totalDue);
170 
171  $this->express->authorizeOrder($this->order);
172  }
173 
185  string $method,
186  string $action,
187  float $authorizedAmount,
188  bool $isAuthAllowed
189  ) {
190  $this->payment->method('getMethod')
191  ->willReturn($method);
192 
193  $this->paymentInstance->method('getConfigPaymentAction')
194  ->willReturn($action);
195 
196  $this->payment->method('getAmountAuthorized')
197  ->willReturn($authorizedAmount);
198 
199  static::assertEquals($isAuthAllowed, $this->express->isOrderAuthorizationAllowed($this->payment));
200  }
201 
207  public function paymentDataProvider(): array
208  {
209  return [
210  ['paypal_express', 'sale', 10, false],
211  ['paypal_express', 'order', 50, false],
212  ['paypal_express', 'capture', 0, false],
213  ['paypal_express', 'order', 0, true],
214  ['braintree', 'authorize', 10, false],
215  ['braintree', 'authorize', 0, false],
216  ];
217  }
218 }
$objectManager
Definition: bootstrap.php:17
testIsOrderAuthorizationAllowed(string $method, string $action, float $authorizedAmount, bool $isAuthAllowed)
$method
Definition: info.phtml:13