|
6 | 6 |
|
7 | 7 | namespace Magento\Paypal\Test\Unit\Controller\Ipn;
|
8 | 8 |
|
| 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 | + |
9 | 16 | class IndexTest extends \PHPUnit\Framework\TestCase
|
10 | 17 | {
|
11 | 18 | /** @var Index */
|
12 |
| - protected $model; |
| 19 | + private $model; |
13 | 20 |
|
14 | 21 | /** @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject */
|
15 |
| - protected $logger; |
| 22 | + private $loggerMock; |
16 | 23 |
|
17 | 24 | /** @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject */
|
18 |
| - protected $request; |
| 25 | + private $requestMock; |
19 | 26 |
|
20 | 27 | /** @var \Magento\Framework\App\ResponseInterface|\PHPUnit_Framework_MockObject_MockObject */
|
21 |
| - protected $response; |
| 28 | + private $responseMock; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var IpnFactory|\PHPUnit_Framework_MockObject_MockObject |
| 32 | + */ |
| 33 | + private $ipnFactoryMock; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var OrderFactory|\PHPUnit_Framework_MockObject_MockObject |
| 37 | + */ |
| 38 | + private $orderFactoryMock; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var ManagerInterface|\PHPUnit_Framework_MockObject_MockObject |
| 42 | + */ |
| 43 | + private $eventManagerMock; |
22 | 44 |
|
23 | 45 | protected function setUp()
|
24 | 46 | {
|
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); |
| 47 | + $this->loggerMock = $this->createMock(\Psr\Log\LoggerInterface::class); |
| 48 | + $this->requestMock = $this->createMock(\Magento\Framework\App\Request\Http::class); |
| 49 | + $this->responseMock = $this->createMock(\Magento\Framework\App\Response\Http::class); |
| 50 | + $this->ipnFactoryMock = $this->createMock(IpnFactory::class); |
| 51 | + $this->orderFactoryMock = $this->createMock(OrderFactory::class); |
| 52 | + $this->eventManagerMock = $this->createMock(ManagerInterface::class); |
28 | 53 |
|
29 | 54 | $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
|
30 | 55 | $this->model = $objectManagerHelper->getObject(
|
31 |
| - \Magento\Paypal\Controller\Ipn\Index::class, |
| 56 | + Index::class, |
32 | 57 | [
|
33 |
| - 'logger' => $this->logger, |
34 |
| - 'request' => $this->request, |
35 |
| - 'response' => $this->response, |
| 58 | + 'logger' => $this->loggerMock, |
| 59 | + 'request' => $this->requestMock, |
| 60 | + 'response' => $this->responseMock, |
| 61 | + 'ipnFactory' => $this->ipnFactoryMock, |
| 62 | + 'orderFactory' => $this->orderFactoryMock, |
| 63 | + 'eventManager' => $this->eventManagerMock |
36 | 64 | ]
|
37 | 65 | );
|
38 | 66 | }
|
39 | 67 |
|
40 | 68 | public function testIndexActionException()
|
41 | 69 | {
|
42 |
| - $this->request->expects($this->once())->method('isPost')->will($this->returnValue(true)); |
| 70 | + $this->requestMock->expects($this->once())->method('isPost')->will($this->returnValue(true)); |
43 | 71 | $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); |
| 72 | + $this->requestMock->expects($this->once())->method('getPostValue')->will($this->throwException($exception)); |
| 73 | + $this->loggerMock->expects($this->once())->method('critical')->with($this->identicalTo($exception)); |
| 74 | + $this->responseMock->expects($this->once())->method('setHttpResponseCode')->with(500); |
| 75 | + $this->model->execute(); |
| 76 | + } |
| 77 | + |
| 78 | + public function testIndexAction() |
| 79 | + { |
| 80 | + $this->requestMock->expects($this->once())->method('isPost')->will($this->returnValue(true)); |
| 81 | + $incrementId = 'incrementId'; |
| 82 | + $data = [ |
| 83 | + 'invoice' => $incrementId, |
| 84 | + 'other' => 'other data' |
| 85 | + ]; |
| 86 | + $this->requestMock->expects($this->exactly(2))->method('getPostValue')->willReturn($data); |
| 87 | + $ipnMock = $this->createMock(IpnInterface::class); |
| 88 | + $this->ipnFactoryMock->expects($this->once()) |
| 89 | + ->method('create') |
| 90 | + ->with(['data' => $data]) |
| 91 | + ->willReturn($ipnMock); |
| 92 | + $ipnMock->expects($this->once()) |
| 93 | + ->method('processIpnRequest'); |
| 94 | + $orderMock = $this->createMock(Order::class); |
| 95 | + $this->orderFactoryMock->expects($this->once()) |
| 96 | + ->method('create') |
| 97 | + ->willReturn($orderMock); |
| 98 | + $orderMock->expects($this->once()) |
| 99 | + ->method('loadByIncrementId') |
| 100 | + ->with($incrementId) |
| 101 | + ->willReturn($orderMock); |
| 102 | + $this->eventManagerMock->expects($this->once()) |
| 103 | + ->method('dispatch') |
| 104 | + ->with('paypal_checkout_success', ['order' => $orderMock]); |
47 | 105 | $this->model->execute();
|
48 | 106 | }
|
49 | 107 | }
|
0 commit comments