|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2013-2017 Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Signifyd\Plugin; |
| 7 | + |
| 8 | +use Magento\Framework\Api\FilterBuilder; |
| 9 | +use Magento\Framework\Api\SearchCriteriaBuilder; |
| 10 | +use Magento\Framework\Registry; |
| 11 | +use Magento\Payment\Model\Info as PaymentInfo; |
| 12 | +use Magento\Paypal\Model\Api\Nvp; |
| 13 | +use Magento\Paypal\Model\Config; |
| 14 | +use Magento\Paypal\Model\Express; |
| 15 | +use Magento\Paypal\Model\Info; |
| 16 | +use Magento\Paypal\Model\Pro; |
| 17 | +use Magento\Paypal\Model\ProFactory; |
| 18 | +use Magento\Sales\Api\Data\OrderInterface; |
| 19 | +use Magento\Sales\Api\OrderRepositoryInterface; |
| 20 | +use Magento\Signifyd\Api\CaseRepositoryInterface; |
| 21 | +use Magento\Signifyd\Api\Data\CaseInterface; |
| 22 | +use Magento\Signifyd\Model\SignifydGateway\ApiClient; |
| 23 | +use Magento\TestFramework\Helper\Bootstrap; |
| 24 | +use Magento\TestFramework\ObjectManager; |
| 25 | +use PHPUnit_Framework_MockObject_MockObject as MockObject; |
| 26 | + |
| 27 | +/** |
| 28 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 29 | + */ |
| 30 | +class DenyPaymentTest extends \PHPUnit_Framework_TestCase |
| 31 | +{ |
| 32 | + /** |
| 33 | + * @var int |
| 34 | + */ |
| 35 | + private static $caseId = 123; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var ObjectManager |
| 39 | + */ |
| 40 | + private $objectManager; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var ApiClient|MockObject |
| 44 | + */ |
| 45 | + private $apiClient; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var Registry |
| 49 | + */ |
| 50 | + private $registry; |
| 51 | + |
| 52 | + /** |
| 53 | + * @inheritdoc |
| 54 | + */ |
| 55 | + protected function setUp() |
| 56 | + { |
| 57 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 58 | + |
| 59 | + $this->apiClient = $this->getMockBuilder(ApiClient::class) |
| 60 | + ->disableOriginalConstructor() |
| 61 | + ->setMethods(['makeApiCall']) |
| 62 | + ->getMock(); |
| 63 | + |
| 64 | + $this->registry = $this->objectManager->get(Registry::class); |
| 65 | + |
| 66 | + $this->objectManager->addSharedInstance($this->apiClient, ApiClient::class); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @inheritdoc |
| 71 | + */ |
| 72 | + protected function tearDown() |
| 73 | + { |
| 74 | + $this->objectManager->removeSharedInstance(ApiClient::class); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Checks a test case, when payment has been denied |
| 79 | + * and calls plugin to cancel Signifyd case guarantee. |
| 80 | + * |
| 81 | + * @covers \Magento\Signifyd\Plugin\PaymentPlugin::afterDenyPayment |
| 82 | + * @magentoDataFixture Magento/Signifyd/_files/approved_case.php |
| 83 | + * @magentoConfigFixture current_store fraud_protection/signifyd/active 1 |
| 84 | + */ |
| 85 | + public function testAfterDenyPayment() |
| 86 | + { |
| 87 | + $order = $this->getOrder(); |
| 88 | + $this->registry->register('current_order', $order); |
| 89 | + |
| 90 | + $this->apiClient->expects(self::once()) |
| 91 | + ->method('makeApiCall') |
| 92 | + ->with( |
| 93 | + self::equalTo('/cases/' . self::$caseId . '/guarantee'), |
| 94 | + 'PUT', |
| 95 | + [ |
| 96 | + 'guaranteeDisposition' => CaseInterface::GUARANTEE_CANCELED |
| 97 | + ] |
| 98 | + ) |
| 99 | + ->willReturn([ |
| 100 | + 'disposition' => CaseInterface::GUARANTEE_CANCELED |
| 101 | + ]); |
| 102 | + |
| 103 | + /** @var \Magento\Sales\Model\Order\Payment $payment */ |
| 104 | + $payment = $order->getPayment(); |
| 105 | + $payment->setData('method_instance', $this->getMethodInstance()); |
| 106 | + $payment->deny(); |
| 107 | + |
| 108 | + /** @var CaseRepositoryInterface $caseRepository */ |
| 109 | + $caseRepository = $this->objectManager->get(CaseRepositoryInterface::class); |
| 110 | + $case = $caseRepository->getByCaseId(self::$caseId); |
| 111 | + |
| 112 | + self::assertEquals(CaseInterface::GUARANTEE_CANCELED, $case->getGuaranteeDisposition()); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Get stored order. |
| 117 | + * |
| 118 | + * @return OrderInterface |
| 119 | + */ |
| 120 | + private function getOrder() |
| 121 | + { |
| 122 | + /** @var FilterBuilder $filterBuilder */ |
| 123 | + $filterBuilder = $this->objectManager->get(FilterBuilder::class); |
| 124 | + $filters = [ |
| 125 | + $filterBuilder->setField(OrderInterface::INCREMENT_ID) |
| 126 | + ->setValue('100000001') |
| 127 | + ->create() |
| 128 | + ]; |
| 129 | + |
| 130 | + /** @var SearchCriteriaBuilder $searchCriteriaBuilder */ |
| 131 | + $searchCriteriaBuilder = $this->objectManager->get(SearchCriteriaBuilder::class); |
| 132 | + $searchCriteria = $searchCriteriaBuilder->addFilters($filters) |
| 133 | + ->create(); |
| 134 | + |
| 135 | + $orderRepository = $this->objectManager->get(OrderRepositoryInterface::class); |
| 136 | + $orders = $orderRepository->getList($searchCriteria) |
| 137 | + ->getItems(); |
| 138 | + |
| 139 | + /** @var OrderInterface $order */ |
| 140 | + return array_pop($orders); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Gets payment method instance. |
| 145 | + * |
| 146 | + * @return Express |
| 147 | + */ |
| 148 | + private function getMethodInstance() |
| 149 | + { |
| 150 | + /** @var PaymentInfo $infoInstance */ |
| 151 | + $infoInstance = $this->objectManager->get(PaymentInfo::class); |
| 152 | + $infoInstance->setAdditionalInformation( |
| 153 | + Info::PAYMENT_STATUS_GLOBAL, |
| 154 | + Info::PAYMENTSTATUS_PENDING |
| 155 | + ); |
| 156 | + $infoInstance->setAdditionalInformation( |
| 157 | + Info::PENDING_REASON_GLOBAL, |
| 158 | + Info::PAYMENTSTATUS_PENDING |
| 159 | + ); |
| 160 | + |
| 161 | + /** @var Express $methodInstance */ |
| 162 | + $methodInstance = $this->objectManager->create( |
| 163 | + Express::class, |
| 164 | + ['proFactory' => $this->getProFactory()] |
| 165 | + ); |
| 166 | + $methodInstance->setData('info_instance', $infoInstance); |
| 167 | + |
| 168 | + return $methodInstance; |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * Gets Pro factory mock. |
| 173 | + * |
| 174 | + * @return ProFactory|MockObject |
| 175 | + */ |
| 176 | + protected function getProFactory() |
| 177 | + { |
| 178 | + $pro = $this->getMockBuilder(Pro::class) |
| 179 | + ->disableOriginalConstructor() |
| 180 | + ->setMethods(['getApi', 'setMethod', 'getConfig', '__wakeup', 'reviewPayment']) |
| 181 | + ->getMock(); |
| 182 | + $nvpClient = $this->getMockBuilder(Nvp::class) |
| 183 | + ->disableOriginalConstructor() |
| 184 | + ->getMock(); |
| 185 | + |
| 186 | + $pro->method('getConfig') |
| 187 | + ->willReturn($this->getConfig()); |
| 188 | + $pro->method('getApi') |
| 189 | + ->willReturn($nvpClient); |
| 190 | + $pro->method('reviewPayment') |
| 191 | + ->willReturn(true); |
| 192 | + |
| 193 | + $proFactory = $this->getMockBuilder(ProFactory::class) |
| 194 | + ->disableOriginalConstructor() |
| 195 | + ->getMock(); |
| 196 | + $proFactory->method('create') |
| 197 | + ->willReturn($pro); |
| 198 | + |
| 199 | + return $proFactory; |
| 200 | + } |
| 201 | + |
| 202 | + /** |
| 203 | + * Gets config mock. |
| 204 | + * |
| 205 | + * @return Config|MockObject |
| 206 | + */ |
| 207 | + protected function getConfig() |
| 208 | + { |
| 209 | + $config = $this->getMockBuilder(Config::class) |
| 210 | + ->disableOriginalConstructor() |
| 211 | + ->getMock(); |
| 212 | + $config->method('getValue') |
| 213 | + ->with('payment_action') |
| 214 | + ->willReturn(Config::PAYMENT_ACTION_AUTH); |
| 215 | + |
| 216 | + return $config; |
| 217 | + } |
| 218 | +} |
0 commit comments