|
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 | + |
| 16 | +/** |
| 17 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 18 | + */ |
9 | 19 | class IndexTest extends \PHPUnit\Framework\TestCase
|
10 | 20 | {
|
11 | 21 | /** @var Index */
|
12 |
| - protected $model; |
| 22 | + private $model; |
13 | 23 |
|
14 | 24 | /** @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject */
|
15 |
| - protected $logger; |
| 25 | + private $loggerMock; |
16 | 26 |
|
17 | 27 | /** @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject */
|
18 |
| - protected $request; |
| 28 | + private $requestMock; |
19 | 29 |
|
20 | 30 | /** @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; |
22 | 47 |
|
23 | 48 | protected function setUp()
|
24 | 49 | {
|
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); |
28 | 56 |
|
29 | 57 | $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
|
30 | 58 | $this->model = $objectManagerHelper->getObject(
|
31 |
| - \Magento\Paypal\Controller\Ipn\Index::class, |
| 59 | + Index::class, |
32 | 60 | [
|
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 |
36 | 67 | ]
|
37 | 68 | );
|
38 | 69 | }
|
39 | 70 |
|
40 | 71 | public function testIndexActionException()
|
41 | 72 | {
|
42 |
| - $this->request->expects($this->once())->method('isPost')->will($this->returnValue(true)); |
| 73 | + $this->requestMock->expects($this->once())->method('isPost')->will($this->returnValue(true)); |
43 | 74 | $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]); |
47 | 108 | $this->model->execute();
|
48 | 109 | }
|
49 | 110 | }
|
0 commit comments