Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProTest.php
Go to the documentation of this file.
1 <?php
12 
15 
20 class ProTest extends \PHPUnit\Framework\TestCase
21 {
25  protected $pro;
26 
28  protected $apiMock;
29 
30  protected function setUp()
31  {
32  $objectHelper = new ObjectManager($this);
33  $infoFactory = $this->getInfoFactory();
34 
35  $storeId = 33;
37  $apiFactory = $this->getApiFactory($objectHelper);
38  $args = $objectHelper->getConstructArguments(
39  \Magento\Paypal\Model\Pro::class,
40  [
41  'configFactory' => $configFactory,
42  'infoFactory' => $infoFactory,
43  'apiFactory' => $apiFactory
44  ]
45  );
47  $this->pro = $this->getMockBuilder(\Magento\Paypal\Model\Pro::class)
48  ->setMethods(['_isPaymentReviewRequired'])
49  ->setConstructorArgs($args)
50  ->getMock();
51  $this->pro->setMethod(PaypalConfig::METHOD_PAYMENT_PRO, $storeId);
52  }
53 
60  public function testCanReviewPayment($pendingReason, $isReviewRequired, $expected)
61  {
62  $this->pro->expects(
63  $this->any()
64  )->method(
65  '_isPaymentReviewRequired'
66  )->will(
67  $this->returnValue($isReviewRequired)
68  );
69  $payment = $this->getMockBuilder(
70  \Magento\Payment\Model\Info::class
71  )->disableOriginalConstructor()->setMethods(
72  ['getAdditionalInformation', '__wakeup']
73  )->getMock();
74  $payment->expects(
75  $this->once()
76  )->method(
77  'getAdditionalInformation'
78  )->with(
79  $this->equalTo(\Magento\Paypal\Model\Info::PENDING_REASON_GLOBAL)
80  )->will(
81  $this->returnValue($pendingReason)
82  );
83 
84  $this->assertEquals($expected, $this->pro->canReviewPayment($payment));
85  }
86 
90  public function canReviewPaymentDataProvider()
91  {
92  return [
95  ['another_pending_reason', false, false],
96  ['another_pending_reason', true, true]
97  ];
98  }
99 
103  public function testCapture()
104  {
105  $paymentMock = $this->getPaymentMock();
106  $orderMock = $this->getOrderMock();
107 
108  $this->apiMock->expects(static::any())
109  ->method('setAuthorizationId')
110  ->willReturnSelf();
111  $this->apiMock->expects(static::any())
112  ->method('setIsCaptureComplete')
113  ->willReturnSelf();
114  $this->apiMock->expects(static::any())
115  ->method('setAmount')
116  ->willReturnSelf();
117 
118  $paymentMock->expects(static::once())
119  ->method('getOrder')
120  ->willReturn($orderMock);
121 
122  $paymentMock->method('isCaptureFinal')
123  ->willReturn(true);
124 
125  $this->apiMock->expects(static::once())
126  ->method('getTransactionId')
127  ->willReturn(45);
128  $this->apiMock->expects(static::any())
129  ->method('getDataUsingMethod')
130  ->willReturn(false);
131 
132  $amount = 43.03;
133  $this->pro->capture($paymentMock, $amount);
134  }
135 
140  protected function getInfoFactory()
141  {
142  $infoFactory = $this->getMockBuilder(\Magento\Paypal\Model\InfoFactory::class)
143  ->disableOriginalConstructor()
144  ->setMethods(['create'])
145  ->getMock();
146  $infoMock = $this->getMockBuilder(\Magento\Paypal\Model\Info::class)
147  ->disableOriginalConstructor()
148  ->setMethods(['isPaymentReviewRequired'])
149  ->getMock();
150  $infoFactory->expects(static::any())->method('create')->willReturn($infoMock);
151  return $infoFactory;
152  }
153 
159  protected function getConfigFactory($storeId)
160  {
161  $configType = \Magento\Paypal\Model\Config::class;
162  $configMock = $this->getMockBuilder($configType)
163  ->disableOriginalConstructor()
164  ->getMock();
165  $configFactory = $this->getMockBuilder(\Magento\Paypal\Model\Config\Factory::class)
166  ->disableOriginalConstructor()
167  ->setMethods(['create'])
168  ->getMock();
169 
170  $configFactory->expects(static::any())
171  ->method('create')
172  ->with($configType, ['params' => [
173  PaypalConfig::METHOD_PAYMENT_PRO,
174  $storeId
175  ]])
176  ->willReturn($configMock);
177  return $configFactory;
178  }
179 
185  protected function getApiFactory(ObjectManager $objectHelper)
186  {
187  $apiFactory = $this->getMockBuilder(\Magento\Paypal\Model\Api\Type\Factory::class)
188  ->disableOriginalConstructor()
189  ->setMethods(['create'])
190  ->getMock();
191 
192  $httpClient = $this->getMockBuilder(\Magento\Framework\HTTP\Adapter\Curl::class)
193  ->disableOriginalConstructor()
194  ->getMock();
195 
196  $httpClient->expects(static::any())
197  ->method('read')
198  ->will(static::returnValue(
199  "\r\n" . 'ACK=Success&CORRELATIONID=32342431'
200  ));
201 
202  $curlFactory = $this->getMockBuilder(\Magento\Framework\HTTP\Adapter\CurlFactory::class)
203  ->disableOriginalConstructor()
204  ->setMethods(['create'])
205  ->getMock();
206  $curlFactory->expects(static::any())->method('create')->willReturn($httpClient);
207 
208  $apiType = \Magento\Paypal\Model\Api\Nvp::class;
209  $args = $objectHelper->getConstructArguments(
210  $apiType,
211  [
212  'curlFactory' => $curlFactory
213  ]
214  );
215  $this->apiMock = $this->getMockBuilder($apiType)
216  ->setConstructorArgs($args)
217  ->setMethods(
218  [
219  '__wakeup',
220  'getTransactionId',
221  'getDataUsingMethod',
222  'setAuthorizationId',
223  'setIsCaptureComplete',
224  'setAmount'
225  ]
226  )
227  ->getMock();
228 
229  $apiFactory->expects(static::any())->method('create')->with($apiType)->willReturn($this->apiMock);
230  return $apiFactory;
231  }
232 
237  protected function getPaymentMock()
238  {
239  $paymentMock = $this->getMockBuilder(\Magento\Payment\Model\Info::class)
240  ->disableOriginalConstructor()
241  ->setMethods([
242  'getParentTransactionId', 'getOrder', 'getShouldCloseParentTransaction', 'isCaptureFinal',
243  ])
244  ->getMock();
245  $parentTransactionId = 43;
246  $paymentMock->expects(static::once())
247  ->method('getParentTransactionId')
248  ->willReturn($parentTransactionId);
249 
250  return $paymentMock;
251  }
252 
257  protected function getOrderMock()
258  {
259  $orderData = [
260  'currency' => 'USD',
261  'id' => 4,
262  'increment_id' => '0000004'
263  ];
264  $orderMock = $this->getMockBuilder(\Magento\Sales\Model\Order::class)
265  ->disableOriginalConstructor()
266  ->setMethods(['getBaseCurrencyCode', 'getIncrementId', 'getId', 'getBillingAddress', 'getShippingAddress'])
267  ->getMock();
268 
269  $orderMock->expects(static::once())
270  ->method('getId')
271  ->willReturn($orderData['id']);
272  $orderMock->expects(static::once())
273  ->method('getBaseCurrencyCode')
274  ->willReturn($orderData['currency']);
275  $orderMock->expects(static::atLeastOnce())
276  ->method('getIncrementId')
277  ->willReturn($orderData['increment_id']);
278  return $orderMock;
279  }
280 }
$amount
Definition: order.php:14
$payment
Definition: order.php:17
testCanReviewPayment($pendingReason, $isReviewRequired, $expected)
Definition: ProTest.php:60
$configFactory
Definition: config_data.php:43
getApiFactory(ObjectManager $objectHelper)
Definition: ProTest.php:185