IndexTest.php 4.0 KB

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