CancelGuaranteeAbilityTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Signifyd\Test\Unit\Model\Guarantee;
  7. use Magento\Framework\Exception\NoSuchEntityException;
  8. use Magento\Sales\Api\Data\OrderInterface;
  9. use Magento\Sales\Api\OrderRepositoryInterface;
  10. use Magento\Signifyd\Api\Data\CaseInterface;
  11. use Magento\Signifyd\Model\CaseEntity;
  12. use Magento\Signifyd\Model\CaseManagement;
  13. use Magento\Signifyd\Model\Guarantee\CancelGuaranteeAbility;
  14. /**
  15. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  16. */
  17. class CancelGuaranteeAbilityTest extends \PHPUnit\Framework\TestCase
  18. {
  19. /**
  20. * @var OrderRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  21. */
  22. private $orderRepository;
  23. /**
  24. * @var CaseManagement|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. private $caseManagement;
  27. /**
  28. * @var CancelGuaranteeAbility
  29. */
  30. private $cancelGuaranteeAbility;
  31. /**
  32. * @inheritdoc
  33. */
  34. protected function setUp()
  35. {
  36. $this->orderRepository = $this->getMockBuilder(OrderRepositoryInterface::class)
  37. ->getMockForAbstractClass();
  38. $this->caseManagement = $this->getMockBuilder(CaseManagement::class)
  39. ->disableOriginalConstructor()
  40. ->getMock();
  41. $this->cancelGuaranteeAbility = new CancelGuaranteeAbility(
  42. $this->caseManagement,
  43. $this->orderRepository
  44. );
  45. }
  46. /**
  47. * Success test for Cancel Guarantee Request button
  48. */
  49. public function testIsAvailableSuccess()
  50. {
  51. $orderId = 123;
  52. /** @var CaseInterface|\PHPUnit_Framework_MockObject_MockObject $case */
  53. $case = $this->getMockBuilder(CaseInterface::class)
  54. ->disableOriginalConstructor()
  55. ->getMock();
  56. $case->expects($this->once())
  57. ->method('getGuaranteeDisposition')
  58. ->willReturn(CaseEntity::GUARANTEE_APPROVED);
  59. $this->caseManagement->expects($this->once())
  60. ->method('getByOrderId')
  61. ->with($orderId)
  62. ->willReturn($case);
  63. /** @var OrderInterface|\PHPUnit_Framework_MockObject_MockObject $order */
  64. $order = $this->getMockBuilder(OrderInterface::class)
  65. ->getMockForAbstractClass();
  66. $this->orderRepository->expects($this->once())
  67. ->method('get')
  68. ->with($orderId)
  69. ->willReturn($order);
  70. $this->assertTrue($this->cancelGuaranteeAbility->isAvailable($orderId));
  71. }
  72. /**
  73. * Tests case when Case entity doesn't exist for order
  74. */
  75. public function testIsAvailableWithNullCase()
  76. {
  77. $orderId = 123;
  78. $this->caseManagement->expects($this->once())
  79. ->method('getByOrderId')
  80. ->with($orderId)
  81. ->willReturn(null);
  82. $this->assertFalse($this->cancelGuaranteeAbility->isAvailable($orderId));
  83. }
  84. /**
  85. * Tests case when Guarantee Disposition has Canceled states.
  86. */
  87. public function testIsAvailableWithCanceledGuarantee()
  88. {
  89. $orderId = 123;
  90. /** @var CaseInterface|\PHPUnit_Framework_MockObject_MockObject $case */
  91. $case = $this->getMockBuilder(CaseInterface::class)
  92. ->disableOriginalConstructor()
  93. ->getMock();
  94. $case->expects($this->once())
  95. ->method('getGuaranteeDisposition')
  96. ->willReturn(CaseEntity::GUARANTEE_CANCELED);
  97. $this->caseManagement->expects($this->once())
  98. ->method('getByOrderId')
  99. ->with($orderId)
  100. ->willReturn($case);
  101. $this->assertFalse($this->cancelGuaranteeAbility->isAvailable($orderId));
  102. }
  103. /**
  104. * Tests case when order does not exist.
  105. */
  106. public function testIsAvailableWithNullOrder()
  107. {
  108. $orderId = 123;
  109. /** @var CaseInterface|\PHPUnit_Framework_MockObject_MockObject $case */
  110. $case = $this->getMockBuilder(CaseInterface::class)
  111. ->disableOriginalConstructor()
  112. ->getMock();
  113. $case->expects($this->once())
  114. ->method('getGuaranteeDisposition')
  115. ->willReturn(CaseEntity::GUARANTEE_APPROVED);
  116. $this->caseManagement->expects($this->once())
  117. ->method('getByOrderId')
  118. ->with($orderId)
  119. ->willReturn($case);
  120. $this->orderRepository->expects($this->once())
  121. ->method('get')
  122. ->with($orderId)
  123. ->willThrowException(new NoSuchEntityException());
  124. $this->assertFalse($this->cancelGuaranteeAbility->isAvailable($orderId));
  125. }
  126. }