Skip to content

Commit ea880fc

Browse files
author
Joan He
committed
Merge remote-tracking branch 'origin/MAGETWO-94865' into BugFixPR
2 parents a416908 + c1e55f9 commit ea880fc

File tree

2 files changed

+94
-16
lines changed

2 files changed

+94
-16
lines changed

app/code/Magento/Paypal/Controller/Ipn/Index.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
namespace Magento\Paypal\Controller\Ipn;
99

1010
use Magento\Framework\App\CsrfAwareActionInterface;
11+
use Magento\Framework\App\ObjectManager;
1112
use Magento\Framework\App\Request\InvalidRequestException;
1213
use Magento\Framework\App\RequestInterface;
1314
use Magento\Framework\Exception\RemoteServiceUnavailableException;
15+
use Magento\Sales\Model\OrderFactory;
1416

1517
/**
1618
* Unified IPN controller for all supported PayPal methods
@@ -27,18 +29,26 @@ class Index extends \Magento\Framework\App\Action\Action implements CsrfAwareAct
2729
*/
2830
protected $_ipnFactory;
2931

32+
/**
33+
* @var OrderFactory
34+
*/
35+
private $orderFactory;
36+
3037
/**
3138
* @param \Magento\Framework\App\Action\Context $context
3239
* @param \Magento\Paypal\Model\IpnFactory $ipnFactory
3340
* @param \Psr\Log\LoggerInterface $logger
41+
* @param OrderFactory $orderFactory
3442
*/
3543
public function __construct(
3644
\Magento\Framework\App\Action\Context $context,
3745
\Magento\Paypal\Model\IpnFactory $ipnFactory,
38-
\Psr\Log\LoggerInterface $logger
46+
\Psr\Log\LoggerInterface $logger,
47+
OrderFactory $orderFactory = null
3948
) {
4049
$this->_logger = $logger;
4150
$this->_ipnFactory = $ipnFactory;
51+
$this->orderFactory = $orderFactory ?: ObjectManager::getInstance()->get(OrderFactory::class);
4252
parent::__construct($context);
4353
}
4454

@@ -74,6 +84,13 @@ public function execute()
7484
try {
7585
$data = $this->getRequest()->getPostValue();
7686
$this->_ipnFactory->create(['data' => $data])->processIpnRequest();
87+
$incrementId = $this->getRequest()->getPostValue()['invoice'];
88+
$this->_eventManager->dispatch(
89+
'paypal_checkout_success',
90+
[
91+
'order' => $this->orderFactory->create()->loadByIncrementId($incrementId)
92+
]
93+
);
7794
} catch (RemoteServiceUnavailableException $e) {
7895
$this->_logger->critical($e);
7996
$this->getResponse()->setStatusHeader(503, '1.1', 'Service Unavailable')->sendResponse();

app/code/Magento/Paypal/Test/Unit/Controller/Ipn/IndexTest.php

Lines changed: 76 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,44 +6,105 @@
66

77
namespace Magento\Paypal\Test\Unit\Controller\Ipn;
88

9+
use Magento\Framework\Event\ManagerInterface;
10+
use Magento\Paypal\Controller\Ipn\Index;
11+
use Magento\Paypal\Model\IpnFactory;
12+
use Magento\Paypal\Model\IpnInterface;
13+
use Magento\Sales\Model\Order;
14+
use Magento\Sales\Model\OrderFactory;
15+
16+
/**
17+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
18+
*/
919
class IndexTest extends \PHPUnit\Framework\TestCase
1020
{
1121
/** @var Index */
12-
protected $model;
22+
private $model;
1323

1424
/** @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject */
15-
protected $logger;
25+
private $loggerMock;
1626

1727
/** @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject */
18-
protected $request;
28+
private $requestMock;
1929

2030
/** @var \Magento\Framework\App\ResponseInterface|\PHPUnit_Framework_MockObject_MockObject */
21-
protected $response;
31+
private $responseMock;
32+
33+
/**
34+
* @var IpnFactory|\PHPUnit_Framework_MockObject_MockObject
35+
*/
36+
private $ipnFactoryMock;
37+
38+
/**
39+
* @var OrderFactory|\PHPUnit_Framework_MockObject_MockObject
40+
*/
41+
private $orderFactoryMock;
42+
43+
/**
44+
* @var ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
45+
*/
46+
private $eventManagerMock;
2247

2348
protected function setUp()
2449
{
25-
$this->logger = $this->createMock(\Psr\Log\LoggerInterface::class);
26-
$this->request = $this->createMock(\Magento\Framework\App\Request\Http::class);
27-
$this->response = $this->createMock(\Magento\Framework\App\Response\Http::class);
50+
$this->loggerMock = $this->createMock(\Psr\Log\LoggerInterface::class);
51+
$this->requestMock = $this->createMock(\Magento\Framework\App\Request\Http::class);
52+
$this->responseMock = $this->createMock(\Magento\Framework\App\Response\Http::class);
53+
$this->ipnFactoryMock = $this->createMock(IpnFactory::class);
54+
$this->orderFactoryMock = $this->createMock(OrderFactory::class);
55+
$this->eventManagerMock = $this->createMock(ManagerInterface::class);
2856

2957
$objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
3058
$this->model = $objectManagerHelper->getObject(
31-
\Magento\Paypal\Controller\Ipn\Index::class,
59+
Index::class,
3260
[
33-
'logger' => $this->logger,
34-
'request' => $this->request,
35-
'response' => $this->response,
61+
'logger' => $this->loggerMock,
62+
'request' => $this->requestMock,
63+
'response' => $this->responseMock,
64+
'ipnFactory' => $this->ipnFactoryMock,
65+
'orderFactory' => $this->orderFactoryMock,
66+
'eventManager' => $this->eventManagerMock
3667
]
3768
);
3869
}
3970

4071
public function testIndexActionException()
4172
{
42-
$this->request->expects($this->once())->method('isPost')->will($this->returnValue(true));
73+
$this->requestMock->expects($this->once())->method('isPost')->will($this->returnValue(true));
4374
$exception = new \Exception();
44-
$this->request->expects($this->once())->method('getPostValue')->will($this->throwException($exception));
45-
$this->logger->expects($this->once())->method('critical')->with($this->identicalTo($exception));
46-
$this->response->expects($this->once())->method('setHttpResponseCode')->with(500);
75+
$this->requestMock->expects($this->once())->method('getPostValue')->will($this->throwException($exception));
76+
$this->loggerMock->expects($this->once())->method('critical')->with($this->identicalTo($exception));
77+
$this->responseMock->expects($this->once())->method('setHttpResponseCode')->with(500);
78+
$this->model->execute();
79+
}
80+
81+
public function testIndexAction()
82+
{
83+
$this->requestMock->expects($this->once())->method('isPost')->will($this->returnValue(true));
84+
$incrementId = 'incrementId';
85+
$data = [
86+
'invoice' => $incrementId,
87+
'other' => 'other data'
88+
];
89+
$this->requestMock->expects($this->exactly(2))->method('getPostValue')->willReturn($data);
90+
$ipnMock = $this->createMock(IpnInterface::class);
91+
$this->ipnFactoryMock->expects($this->once())
92+
->method('create')
93+
->with(['data' => $data])
94+
->willReturn($ipnMock);
95+
$ipnMock->expects($this->once())
96+
->method('processIpnRequest');
97+
$orderMock = $this->createMock(Order::class);
98+
$this->orderFactoryMock->expects($this->once())
99+
->method('create')
100+
->willReturn($orderMock);
101+
$orderMock->expects($this->once())
102+
->method('loadByIncrementId')
103+
->with($incrementId)
104+
->willReturn($orderMock);
105+
$this->eventManagerMock->expects($this->once())
106+
->method('dispatch')
107+
->with('paypal_checkout_success', ['order' => $orderMock]);
47108
$this->model->execute();
48109
}
49110
}

0 commit comments

Comments
 (0)