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
7 
15 use Magento\Paypal\Model\Api\ProcessableException as ApiProcessableException;
22 use \PHPUnit_Framework_MockObject_MockObject as MockObject;
23 
28 class ExpressTest extends \PHPUnit\Framework\TestCase
29 {
33  protected $errorCodes = [
34  ApiProcessableException::API_INTERNAL_ERROR,
35  ApiProcessableException::API_UNABLE_PROCESS_PAYMENT_ERROR_CODE,
36  ApiProcessableException::API_DO_EXPRESS_CHECKOUT_FAIL,
37  ApiProcessableException::API_UNABLE_TRANSACTION_COMPLETE,
38  ApiProcessableException::API_TRANSACTION_EXPIRED,
39  ApiProcessableException::API_MAX_PAYMENT_ATTEMPTS_EXCEEDED,
40  ApiProcessableException::API_COUNTRY_FILTER_DECLINE,
41  ApiProcessableException::API_MAXIMUM_AMOUNT_FILTER_DECLINE,
42  ApiProcessableException::API_OTHER_FILTER_DECLINE,
43  ApiProcessableException::API_ADDRESS_MATCH_FAIL
44  ];
45 
49  private $model;
50 
54  private $checkoutSession;
55 
59  private $pro;
60 
64  private $nvp;
65 
69  private $helper;
70 
74  private $transactionBuilder;
75 
79  private $eventManager;
80 
81  protected function setUp()
82  {
83  $this->checkoutSession = $this->createPartialMock(
84  Session::class,
85  ['getPaypalTransactionData', 'setPaypalTransactionData']
86  );
87  $this->transactionBuilder = $this->getMockForAbstractClass(
88  BuilderInterface::class,
89  [],
90  '',
91  false,
92  false
93  );
94  $this->nvp = $this->createPartialMock(
95  Nvp::class,
96  [
97  'setProcessableErrors',
98  'setAmount',
99  'setCurrencyCode',
100  'setTransactionId',
101  'callDoAuthorization',
102  'setData',
103  ]
104  );
105  $this->pro = $this->createPartialMock(
106  Pro::class,
107  ['setMethod', 'getApi', 'importPaymentInfo', 'resetApi']
108  );
109  $this->eventManager = $this->getMockBuilder(ManagerInterface::class)
110  ->setMethods(['dispatch'])
111  ->getMockForAbstractClass();
112 
113  $this->pro->expects($this->any())->method('getApi')->will($this->returnValue($this->nvp));
114  $this->helper = new ObjectManager($this);
115  }
116 
117  public function testSetApiProcessableErrors()
118  {
119  $this->nvp->expects($this->once())->method('setProcessableErrors')->with($this->errorCodes);
120 
121  $this->model = $this->helper->getObject(
122  \Magento\Paypal\Model\Express::class,
123  [
124  'data' => [$this->pro],
125  'checkoutSession' => $this->checkoutSession,
126  'transactionBuilder' => $this->transactionBuilder,
127  ]
128  );
129  }
130 
136  public function testOrder()
137  {
138  $transactionData = ['TOKEN' => 'EC-7NJ4634216284232D'];
139  $this->checkoutSession
140  ->method('getPaypalTransactionData')
141  ->willReturn($transactionData);
142 
143  $order = $this->createPartialMock(Order::class, ['setActionFlag']);
144  $order->method('setActionFlag')
145  ->with(Order::ACTION_FLAG_INVOICE, false)
146  ->willReturnSelf();
147 
148  $paymentModel = $this->createPartialMock(Payment::class, ['getOrder']);
149  $paymentModel->method('getOrder')
150  ->willReturn($order);
151 
152  $this->model = $this->helper->getObject(
153  \Magento\Paypal\Model\Express::class,
154  [
155  'data' => [$this->pro],
156  'checkoutSession' => $this->checkoutSession,
157  ]
158  );
159 
160  $this->nvp->method('setData')
161  ->with($transactionData)
162  ->willReturnSelf();
163 
164  static::assertEquals($this->model, $this->model->order($paymentModel, 12.3));
165  }
166 
167  public function testAssignData()
168  {
169  $transportValue = 'something';
170 
171  $extensionAttribute = $this->getMockForAbstractClass(
172  \Magento\Quote\Api\Data\PaymentExtensionInterface::class,
173  [],
174  '',
175  false,
176  false
177  );
178 
179  $data = new DataObject(
180  [
182  Express\Checkout::PAYMENT_INFO_TRANSPORT_BILLING_AGREEMENT => $transportValue,
183  Express\Checkout::PAYMENT_INFO_TRANSPORT_PAYER_ID => $transportValue,
184  Express\Checkout::PAYMENT_INFO_TRANSPORT_TOKEN => $transportValue,
185  \Magento\Framework\Api\ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY => $extensionAttribute
186  ]
187  ]
188  );
189 
190  $this->model = $this->helper->getObject(
191  \Magento\Paypal\Model\Express::class,
192  [
193  'data' => [$this->pro],
194  'checkoutSession' => $this->checkoutSession,
195  'transactionBuilder' => $this->transactionBuilder,
196  'eventDispatcher' => $this->eventManager,
197  ]
198  );
199 
200  $paymentInfo = $this->createMock(InfoInterface::class);
201  $this->model->setInfoInstance($paymentInfo);
202 
203  $this->parentAssignDataExpectation($data);
204 
205  $paymentInfo->expects(static::exactly(3))
206  ->method('setAdditionalInformation')
207  ->withConsecutive(
208  [Express\Checkout::PAYMENT_INFO_TRANSPORT_BILLING_AGREEMENT, $transportValue],
209  [Express\Checkout::PAYMENT_INFO_TRANSPORT_PAYER_ID, $transportValue],
210  [Express\Checkout::PAYMENT_INFO_TRANSPORT_TOKEN, $transportValue]
211  );
212 
213  $this->model->assignData($data);
214  }
215 
219  private function parentAssignDataExpectation(DataObject $data)
220  {
221  $eventData = [
223  AbstractDataAssignObserver::MODEL_CODE => $this->model->getInfoInstance(),
225  ];
226 
227  $this->eventManager->expects(static::exactly(2))
228  ->method('dispatch')
229  ->willReturnMap(
230  [
231  [
232  'payment_method_assign_data_' . $this->model->getCode(),
233  $eventData,
234  ],
235  [
236  'payment_method_assign_data',
237  $eventData,
238  ]
239  ]
240  );
241  }
242 }
$paymentInfo
$order
Definition: order.php:55