CancelTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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\Billing\Agreement;
  7. class CancelTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Paypal\Controller\Billing\Agreement
  11. */
  12. protected $_controller;
  13. /**
  14. * @var \Magento\Framework\ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  15. */
  16. protected $_objectManager;
  17. /**
  18. * @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $_request;
  21. /**
  22. * @var \Magento\Framework\Registry|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $_registry;
  25. /**
  26. * @var \Magento\Customer\Model\Session|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $_session;
  29. /**
  30. * @var \Magento\Framework\Message\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected $_messageManager;
  33. /**
  34. * @var \Magento\Paypal\Model\Billing\Agreement|\PHPUnit_Framework_MockObject_MockObject
  35. */
  36. protected $_agreement;
  37. protected function setUp()
  38. {
  39. $this->_session = $this->createMock(\Magento\Customer\Model\Session::class);
  40. $this->_agreement = $this->createPartialMock(
  41. \Magento\Paypal\Model\Billing\Agreement::class,
  42. ['load', 'getId', 'getCustomerId', 'getReferenceId', 'canCancel', 'cancel', '__wakeup']
  43. );
  44. $this->_agreement->expects($this->once())->method('load')->with(15)->will($this->returnSelf());
  45. $this->_agreement->expects($this->once())->method('getId')->will($this->returnValue(15));
  46. $this->_agreement->expects($this->once())->method('getCustomerId')->will($this->returnValue(871));
  47. $this->_objectManager = $this->createMock(\Magento\Framework\ObjectManagerInterface::class);
  48. $this->_objectManager->expects(
  49. $this->atLeastOnce()
  50. )->method(
  51. 'get'
  52. )->will(
  53. $this->returnValueMap([[\Magento\Customer\Model\Session::class, $this->_session]])
  54. );
  55. $this->_objectManager->expects(
  56. $this->once()
  57. )->method(
  58. 'create'
  59. )->with(
  60. \Magento\Paypal\Model\Billing\Agreement::class
  61. )->will(
  62. $this->returnValue($this->_agreement)
  63. );
  64. $this->_request = $this->createMock(\Magento\Framework\App\RequestInterface::class);
  65. $this->_request->expects($this->once())->method('getParam')->with('agreement')->will($this->returnValue(15));
  66. $response = $this->createMock(\Magento\Framework\App\ResponseInterface::class);
  67. $redirect = $this->createMock(\Magento\Framework\App\Response\RedirectInterface::class);
  68. $this->_messageManager = $this->createMock(\Magento\Framework\Message\ManagerInterface::class);
  69. $context = $this->createMock(\Magento\Framework\App\Action\Context::class);
  70. $context->expects($this->any())->method('getObjectManager')->will($this->returnValue($this->_objectManager));
  71. $context->expects($this->any())->method('getRequest')->will($this->returnValue($this->_request));
  72. $context->expects($this->any())->method('getResponse')->will($this->returnValue($response));
  73. $context->expects($this->any())->method('getRedirect')->will($this->returnValue($redirect));
  74. $context->expects($this->any())->method('getMessageManager')->will($this->returnValue($this->_messageManager));
  75. $this->_registry = $this->createMock(\Magento\Framework\Registry::class);
  76. $this->_controller = new \Magento\Paypal\Controller\Billing\Agreement\Cancel(
  77. $context,
  78. $this->_registry
  79. );
  80. }
  81. public function testExecuteActionSuccess()
  82. {
  83. $this->_agreement->expects($this->once())->method('getReferenceId')->will($this->returnValue('r15'));
  84. $this->_agreement->expects($this->once())->method('canCancel')->will($this->returnValue(true));
  85. $this->_agreement->expects($this->once())->method('cancel');
  86. $noticeMessage = 'The billing agreement "r15" has been canceled.';
  87. $this->_session->expects($this->once())->method('getCustomerId')->will($this->returnValue(871));
  88. $this->_messageManager->expects($this->once())->method('addNoticeMessage')->with($noticeMessage);
  89. $this->_messageManager->expects($this->never())->method('addErrorMessage');
  90. $this->_registry->expects(
  91. $this->once()
  92. )->method(
  93. 'register'
  94. )->with(
  95. 'current_billing_agreement',
  96. $this->identicalTo($this->_agreement)
  97. );
  98. $this->_controller->execute();
  99. }
  100. public function testExecuteAgreementDoesNotBelongToCustomer()
  101. {
  102. $this->_agreement->expects($this->never())->method('canCancel');
  103. $this->_agreement->expects($this->never())->method('cancel');
  104. $errorMessage = 'Please specify the correct billing agreement ID and try again.';
  105. $this->_session->expects($this->once())->method('getCustomerId')->will($this->returnValue(938));
  106. $this->_messageManager->expects($this->once())->method('addErrorMessage')->with($errorMessage);
  107. $this->_registry->expects($this->never())->method('register');
  108. $this->_controller->execute();
  109. }
  110. public function testExecuteAgreementStatusDoesNotAllowToCancel()
  111. {
  112. $this->_agreement->expects($this->once())->method('canCancel')->will($this->returnValue(false));
  113. $this->_agreement->expects($this->never())->method('cancel');
  114. $this->_session->expects($this->once())->method('getCustomerId')->will($this->returnValue(871));
  115. $this->_messageManager->expects($this->never())->method('addNoticeMessage');
  116. $this->_messageManager->expects($this->never())->method('addErrorMessage');
  117. $this->_registry->expects(
  118. $this->once()
  119. )->method(
  120. 'register'
  121. )->with(
  122. 'current_billing_agreement',
  123. $this->identicalTo($this->_agreement)
  124. );
  125. $this->_controller->execute();
  126. }
  127. }