Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SilentPostTest.php
Go to the documentation of this file.
1 <?php
7 
20 use PHPUnit\Framework\MockObject_MockObject as MockObject;
21 
23 {
27  private $gateway;
28 
32  private $orderSender;
33 
37  protected function setUp()
38  {
39  parent::setUp();
40 
41  $this->gateway = $this->getMockBuilder(Gateway::class)
42  ->disableOriginalConstructor()
43  ->getMock();
44 
45  $this->orderSender = $this->getMockBuilder(OrderSender::class)
46  ->disableOriginalConstructor()
47  ->getMock();
48 
49  $this->_objectManager->addSharedInstance($this->gateway, Gateway::class);
50  $this->_objectManager->addSharedInstance($this->orderSender, OrderSender::class);
51  }
52 
56  protected function tearDown()
57  {
58  $this->_objectManager->removeSharedInstance(Gateway::class);
59  $this->_objectManager->removeSharedInstance(OrderSender::class);
60  parent::tearDown();
61  }
62 
72  public function testSuccessfulNotification($resultCode, $orderState, $orderStatus)
73  {
74  $orderIncrementId = '000000045';
75  $this->withRequest($orderIncrementId, $resultCode);
76  $this->withGatewayResponse($orderIncrementId, $resultCode);
77 
78  $this->dispatch('paypal/payflow/silentPost');
79 
80  self::assertEquals(200, $this->getResponse()->getStatusCode());
81 
82  $order = $this->getOrder($orderIncrementId);
83  self::assertEquals($orderState, $order->getState());
84  self::assertEquals($orderStatus, $order->getStatus());
85  }
86 
93  public function responseCodeDataProvider()
94  {
95  return [
96  [Payflowlink::RESPONSE_CODE_APPROVED, Order::STATE_PROCESSING, Order::STATE_PROCESSING],
97  [Payflowlink::RESPONSE_CODE_FRAUDSERVICE_FILTER, Order::STATE_PAYMENT_REVIEW, Order::STATUS_FRAUD],
98  ];
99  }
100 
107  public function testFraudulentNotification()
108  {
109  $orderIncrementId = '000000045';
110  $resultCode = Payflowlink::RESPONSE_CODE_DECLINED_BY_FILTER;
111  $this->withRequest($orderIncrementId, $resultCode);
112  $this->withGatewayResponse($orderIncrementId, $resultCode);
113 
114  $logger = $this->getMockBuilder(Monolog::class)
115  ->disableOriginalConstructor()
116  ->getMock();
117  $this->_objectManager->addSharedInstance($logger, Monolog::class);
118 
119  $exception = new CommandException(__('Response message from PayPal gateway'));
120  $logger->expects(self::once())
121  ->method('critical')
122  ->with(self::equalTo($exception));
123 
124  $this->dispatch('paypal/payflow/silentPost');
125 
126  self::assertEquals(200, $this->getResponse()->getStatusCode());
127 
128  $this->_objectManager->removeSharedInstance(Monolog::class);
129  }
130 
138  private function withRequest($orderIncrementId, $resultCode)
139  {
140  $data = [
141  'INVNUM' => $orderIncrementId,
142  'AMT' => 100,
143  'PNREF' => 'A21CP234KLB8',
144  'USER2' => 'cf7i85d01ed7c92223031afb4rdl2f1f',
145  'RESULT' => $resultCode,
146  'TYPE' => 'A',
147  ];
148  $this->getRequest()->setPostValue($data);
149  }
150 
158  private function withGatewayResponse($orderIncrementId, $resultCode)
159  {
160  $response = new DataObject([
161  'custref' => $orderIncrementId,
162  'origresult' => $resultCode,
163  'respmsg' => 'Response message from PayPal gateway'
164  ]);
165  $this->gateway->method('postRequest')
166  ->willReturn($response);
167  }
168 
175  private function getOrder($incrementId)
176  {
178  $filterBuilder = $this->_objectManager->get(FilterBuilder::class);
179  $filters = [
180  $filterBuilder->setField(OrderInterface::INCREMENT_ID)
181  ->setValue($incrementId)
182  ->create()
183  ];
184 
186  $searchCriteriaBuilder = $this->_objectManager->get(SearchCriteriaBuilder::class);
188  ->create();
189 
190  $orderRepository = $this->_objectManager->get(OrderRepositoryInterface::class);
192  ->getItems();
193 
195  return array_pop($orders);
196  }
197 }
$response
Definition: 404.php:11
testSuccessfulNotification($resultCode, $orderState, $orderStatus)
$orderRepository
Definition: order.php:69
$order
Definition: order.php:55
__()
Definition: __.php:13
$logger
$searchCriteria
setUp()
$orderStatus
Definition: order_status.php:9
responseCodeDataProvider()
testFraudulentNotification()
$filters
Definition: uploader.phtml:11
$searchCriteriaBuilder
tearDown()
dispatch(RequestInterface $request)
Definition: Action.php:92