Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
IpnTest.php
Go to the documentation of this file.
1 <?php
11 
13 
14 class IpnTest extends \PHPUnit\Framework\TestCase
15 {
19  protected $_ipn;
20 
24  protected $_orderMock;
25 
29  protected $_paypalInfo;
30 
34  protected $configFactory;
35 
39  protected $curlFactory;
40 
41  protected function setUp()
42  {
43  $methods = [
44  'create',
45  'loadByIncrementId',
46  'canFetchPaymentReviewUpdate',
47  'getId',
48  'getPayment',
49  'getMethod',
50  'getStoreId',
51  'update',
52  'getAdditionalInformation',
53  'getEmailSent',
54  'save',
55  'getState',
56  ];
57  $this->_orderMock = $this->createPartialMock(\Magento\Sales\Model\OrderFactory::class, $methods);
58  $this->_orderMock->expects($this->any())->method('create')->will($this->returnSelf());
59  $this->_orderMock->expects($this->any())->method('loadByIncrementId')->will($this->returnSelf());
60  $this->_orderMock->expects($this->any())->method('getId')->will($this->returnSelf());
61  $this->_orderMock->expects($this->any())->method('getMethod')->will($this->returnSelf());
62  $this->_orderMock->expects($this->any())->method('getStoreId')->will($this->returnSelf());
63  $this->_orderMock->expects($this->any())->method('getEmailSent')->will($this->returnValue(true));
64 
65  $this->configFactory = $this->createPartialMock(\Magento\Paypal\Model\ConfigFactory::class, ['create']);
66  $configMock = $this->getMockBuilder(\Magento\Paypal\Model\Config::class)
67  ->disableOriginalConstructor()
68  ->getMock();
69  $this->configFactory->expects($this->any())->method('create')->willReturn($configMock);
70  $configMock->expects($this->any())->method('isMethodActive')->will($this->returnValue(true));
71  $configMock->expects($this->any())->method('isMethodAvailable')->will($this->returnValue(true));
72  $configMock->expects($this->any())->method('getValue')->will($this->returnValue(null));
73  $configMock->expects($this->any())->method('getPayPalIpnUrl')
74  ->will($this->returnValue('https://ipnpb_paypal_url'));
75 
76  $this->curlFactory = $this->createPartialMock(
77  \Magento\Framework\HTTP\Adapter\CurlFactory::class,
78  ['create', 'setConfig', 'write', 'read']
79  );
80  $this->curlFactory->expects($this->any())->method('create')->will($this->returnSelf());
81  $this->curlFactory->expects($this->any())->method('setConfig')->will($this->returnSelf());
82  $this->curlFactory->expects($this->any())->method('write')->will($this->returnSelf());
83  $this->curlFactory->expects($this->any())->method('read')->will($this->returnValue(
84  '
85  VERIFIED'
86  ));
87  $this->_paypalInfo = $this->createPartialMock(
88  \Magento\Paypal\Model\Info::class,
89  ['importToPayment', 'getMethod', 'getAdditionalInformation']
90  );
91  $this->_paypalInfo->expects($this->any())->method('getMethod')->will($this->returnValue('some_method'));
92  $objectHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
93  $this->_ipn = $objectHelper->getObject(
94  \Magento\Paypal\Model\Ipn::class,
95  [
96  'configFactory' => $this->configFactory,
97  'curlFactory' => $this->curlFactory,
98  'orderFactory' => $this->_orderMock,
99  'paypalInfo' => $this->_paypalInfo,
100  'data' => ['payment_status' => 'Pending', 'pending_reason' => 'authorization']
101  ]
102  );
103  }
104 
106  {
107  $this->_orderMock->expects($this->any())->method('canFetchPaymentReviewUpdate')->will(
108  $this->returnValue(false)
109  );
110  $methods = [
111  'setPreparedMessage',
112  '__wakeup',
113  'setTransactionId',
114  'setParentTransactionId',
115  'setIsTransactionClosed',
116  'registerAuthorizationNotification',
117  ];
118  $payment = $this->createPartialMock(\Magento\Sales\Model\Order\Payment::class, $methods);
119  $payment->expects($this->any())->method('setPreparedMessage')->will($this->returnSelf());
120  $payment->expects($this->any())->method('setTransactionId')->will($this->returnSelf());
121  $payment->expects($this->any())->method('setParentTransactionId')->will($this->returnSelf());
122  $payment->expects($this->any())->method('setIsTransactionClosed')->will($this->returnSelf());
123  $this->_orderMock->expects($this->any())->method('getPayment')->will($this->returnValue($payment));
124  $this->_orderMock->expects($this->any())->method('getAdditionalInformation')->will($this->returnValue([]));
125 
126  $this->_paypalInfo->expects($this->once())->method('importToPayment');
127  $this->_ipn->processIpnRequest();
128  }
129 
131  {
132  $this->_orderMock->expects($this->any())->method('getPayment')->will($this->returnSelf());
133  $this->_orderMock->expects($this->any())->method('canFetchPaymentReviewUpdate')->will($this->returnValue(true));
134  $this->_orderMock->expects($this->once())->method('update')->with(true)->will($this->returnSelf());
135  $this->_ipn->processIpnRequest();
136  }
137 
139  {
140  $paymentMock = $this->createPartialMock(
141  \Magento\Sales\Model\Order\Payment::class,
142  ['getAdditionalInformation', '__wakeup', 'registerCaptureNotification']
143  );
144  $paymentMock->expects($this->any())
145  ->method('getAdditionalInformation')
146  ->will($this->returnValue([]));
147  $paymentMock->expects($this->any())
148  ->method('registerCaptureNotification')
149  ->will($this->returnValue(true));
150  $this->_orderMock->expects($this->any())->method('getPayment')->will($this->returnValue($paymentMock));
151  $this->_orderMock->expects($this->any())->method('canFetchPaymentReviewUpdate')->will($this->returnValue(true));
152  $this->_orderMock->expects($this->once())->method('getState')->will(
153  $this->returnValue(Order::STATE_PENDING_PAYMENT)
154  );
155  $this->_paypalInfo->expects($this->once())
156  ->method('importToPayment')
157  ->with(
158  [
159  'payment_status' => 'pending',
160  'pending_reason' => 'fraud',
161  'collected_fraud_filters' => ['Maximum Transaction Amount'],
162  ],
163  $paymentMock
164  );
165  $objectHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
166  $this->_ipn = $objectHelper->getObject(
167  \Magento\Paypal\Model\Ipn::class,
168  [
169  'configFactory' => $this->configFactory,
170  'curlFactory' => $this->curlFactory,
171  'orderFactory' => $this->_orderMock,
172  'paypalInfo' => $this->_paypalInfo,
173  'data' => [
174  'payment_status' => 'Pending',
175  'pending_reason' => 'fraud',
176  'fraud_management_pending_filters_1' => 'Maximum Transaction Amount',
177  ]
178  ]
179  );
180  $this->_ipn->processIpnRequest();
181  $this->assertEquals('IPN "Pending"', $paymentMock->getPreparedMessage());
182  }
183 
184  public function testRegisterPaymentDenial()
185  {
187  $paymentMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Payment::class)
188  ->setMethods([
189  'getAdditionalInformation',
190  'setTransactionId',
191  'setNotificationResult',
192  'setIsTransactionClosed',
193  'deny'
194  ])
195  ->disableOriginalConstructor()
196  ->getMock();
197 
198  $paymentMock->expects($this->exactly(6))->method('getAdditionalInformation')->willReturn([]);
199  $paymentMock->expects($this->once())->method('setTransactionId')->willReturnSelf();
200  $paymentMock->expects($this->once())->method('setNotificationResult')->willReturnSelf();
201  $paymentMock->expects($this->once())->method('setIsTransactionClosed')->willReturnSelf();
202  $paymentMock->expects($this->once())->method('deny')->with(false)->willReturnSelf();
203 
204  $this->_orderMock->expects($this->exactly(4))->method('getPayment')->will($this->returnValue($paymentMock));
205 
206  $this->_paypalInfo->expects($this->once())
207  ->method('importToPayment')
208  ->with(['payment_status' => 'denied'], $paymentMock);
209 
210  $objectHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
211  $this->_ipn = $objectHelper->getObject(
212  \Magento\Paypal\Model\Ipn::class,
213  [
214  'configFactory' => $this->configFactory,
215  'curlFactory' => $this->curlFactory,
216  'orderFactory' => $this->_orderMock,
217  'paypalInfo' => $this->_paypalInfo,
218  'data' => [
219  'payment_status' => 'Denied',
220  ]
221  ]
222  );
223 
224  $this->_ipn->processIpnRequest();
225  }
226 }
$payment
Definition: order.php:17
$methods
Definition: billing.phtml:71