Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DenyPaymentTest.php
Go to the documentation of this file.
1 <?php
7 
10 use Magento\Payment\Model\Info as PaymentInfo;
16 use Magento\Paypal\Model\ProFactory;
24 use PHPUnit\Framework\MockObject_MockObject as MockObject;
25 
29 class DenyPaymentTest extends \PHPUnit\Framework\TestCase
30 {
34  private static $caseId = 123;
35 
39  private $objectManager;
40 
44  private $apiClient;
45 
49  private $registry;
50 
54  protected function setUp()
55  {
56  $this->objectManager = Bootstrap::getObjectManager();
57 
58  $this->apiClient = $this->getMockBuilder(ApiClient::class)
59  ->disableOriginalConstructor()
60  ->setMethods(['makeApiCall'])
61  ->getMock();
62 
63  $this->registry = $this->objectManager->get(Registry::class);
64 
65  $this->objectManager->addSharedInstance($this->apiClient, ApiClient::class);
66  }
67 
71  protected function tearDown()
72  {
73  $this->objectManager->removeSharedInstance(ApiClient::class);
74  }
75 
84  public function testAfterDenyPayment()
85  {
86  $order = $this->getOrder();
87  $this->registry->register('current_order', $order);
88 
89  $this->apiClient->expects(self::once())
90  ->method('makeApiCall')
91  ->with(
92  self::equalTo('/cases/' . self::$caseId . '/guarantee'),
93  'PUT',
94  [
95  'guaranteeDisposition' => CaseInterface::GUARANTEE_CANCELED
96  ]
97  )
98  ->willReturn([
99  'disposition' => CaseInterface::GUARANTEE_CANCELED
100  ]);
101 
103  $payment = $order->getPayment();
104  $payment->setData('method_instance', $this->getMethodInstance());
105  $payment->deny();
106 
108  $caseRepository = $this->objectManager->get(CaseRepositoryInterface::class);
109  $case = $caseRepository->getByCaseId(self::$caseId);
110 
111  self::assertEquals(CaseInterface::GUARANTEE_CANCELED, $case->getGuaranteeDisposition());
112  }
113 
119  private function getOrder()
120  {
122  $searchCriteriaBuilder = $this->objectManager->get(SearchCriteriaBuilder::class);
124  ->create();
125 
126  $orderRepository = $this->objectManager->get(OrderRepositoryInterface::class);
128  ->getItems();
129 
131  return array_pop($orders);
132  }
133 
139  private function getMethodInstance()
140  {
142  $infoInstance = $this->objectManager->get(PaymentInfo::class);
143  $infoInstance->setAdditionalInformation(
146  );
147  $infoInstance->setAdditionalInformation(
150  );
151 
153  $methodInstance = $this->objectManager->create(
154  Express::class,
155  ['proFactory' => $this->getProFactory()]
156  );
157  $methodInstance->setData('info_instance', $infoInstance);
158 
159  return $methodInstance;
160  }
161 
167  protected function getProFactory()
168  {
169  $pro = $this->getMockBuilder(Pro::class)
170  ->disableOriginalConstructor()
171  ->setMethods(['getApi', 'setMethod', 'getConfig', '__wakeup', 'reviewPayment'])
172  ->getMock();
173  $nvpClient = $this->getMockBuilder(Nvp::class)
174  ->disableOriginalConstructor()
175  ->getMock();
176 
177  $pro->method('getConfig')
178  ->willReturn($this->getConfig());
179  $pro->method('getApi')
180  ->willReturn($nvpClient);
181  $pro->method('reviewPayment')
182  ->willReturn(true);
183 
184  $proFactory = $this->getMockBuilder(ProFactory::class)
185  ->disableOriginalConstructor()
186  ->getMock();
187  $proFactory->method('create')
188  ->willReturn($pro);
189 
190  return $proFactory;
191  }
192 
198  protected function getConfig()
199  {
200  $config = $this->getMockBuilder(Config::class)
201  ->disableOriginalConstructor()
202  ->getMock();
203  $config->method('getValue')
204  ->with('payment_action')
205  ->willReturn(Config::PAYMENT_ACTION_AUTH);
206 
207  return $config;
208  }
209 }
$config
Definition: fraud_order.php:17
$orderRepository
Definition: order.php:69
$case
$order
Definition: order.php:55
$caseRepository
$searchCriteria
$payment
Definition: order.php:17
const PAYMENT_STATUS_GLOBAL
Definition: Info.php:62
const PENDING_REASON_GLOBAL
Definition: Info.php:64
$searchCriteriaBuilder