Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PaymentInformationManagementTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
10 use Braintree\Result\Error;
21 use PHPUnit\Framework\TestCase;
22 use PHPUnit_Framework_MockObject_MockObject as MockObject;
23 
29 class PaymentInformationManagementTest extends TestCase
30 {
34  private $objectManager;
35 
39  private $client;
40 
44  protected function setUp()
45  {
46  $this->objectManager = Bootstrap::getObjectManager();
47 
48  $this->client = $this->getMockBuilder(TransactionSale::class)
49  ->disableOriginalConstructor()
50  ->getMock();
51  $this->objectManager->addSharedInstance($this->client, TransactionSale::class);
52  }
53 
57  protected function tearDown()
58  {
59  $this->objectManager->removeSharedInstance(TransactionSale::class);
60  parent::tearDown();
61  }
62 
78  public function testSavePaymentInformationAndPlaceOrderWithErrors(
79  string $area,
80  array $testErrorCodes,
81  string $expectedOutput
82  ) {
84  $state = $this->objectManager->get(State::class);
85  $state->setAreaCode($area);
86 
87  $quote = $this->getQuote('test_order_1');
88 
90  $payment = $this->objectManager->create(PaymentInterface::class);
91  $payment->setMethod(ConfigProvider::CODE);
92 
93  $errors = ['errors' => []];
94 
95  foreach ($testErrorCodes as $testErrorCode) {
96  array_push($errors['errors'], ['code' => $testErrorCode]);
97  }
98 
99  $response = new Error(['errors' => $errors]);
100 
101  $this->client->method('placeRequest')
102  ->willReturn(['object' => $response]);
103 
104  $this->expectExceptionMessage($expectedOutput);
105 
107  $paymentInformationManagement = $this->objectManager->get(PaymentInformationManagementInterface::class);
108  $paymentInformationManagement->savePaymentInformationAndPlaceOrder(
109  $quote->getId(),
110  $payment
111  );
112  }
113 
119  public function getErrorPerAreaDataProvider()
120  {
121  $testErrorGlobal = ['code' => 81802, 'message' => 'Company is too long.'];
122  $testErrorAdmin = ['code' => 91511, 'message' => 'Customer does not have any credit cards.'];
123  $testErrorFake = ['code' => 'fake_code', 'message' => 'Error message should not be mapped.'];
124 
125  return [
126  [
128  [$testErrorAdmin['code'], $testErrorFake['code']],
129  'Transaction has been declined. Please try again later.'
130  ], [
132  [$testErrorGlobal['code'], $testErrorAdmin['code'], $testErrorFake['code']],
133  $testErrorGlobal['message']
134  ], [
136  [$testErrorGlobal['code'], $testErrorAdmin['code'], $testErrorFake['code']],
137  $testErrorGlobal['message'] . PHP_EOL . $testErrorAdmin['message']
138  ],
139  ];
140  }
141 
148  private function getQuote(string $reservedOrderId): CartInterface
149  {
151  $searchCriteriaBuilder = $this->objectManager->get(SearchCriteriaBuilder::class);
152  $searchCriteria = $searchCriteriaBuilder->addFilter('reserved_order_id', $reservedOrderId)
153  ->create();
154 
156  $quoteRepository = $this->objectManager->get(CartRepositoryInterface::class);
158  ->getItems();
159 
160  return array_pop($items);
161  }
162 }
$response
Definition: 404.php:11
$quote
$searchCriteria
$payment
Definition: order.php:17
$searchCriteriaBuilder
$quoteRepository
$errors
Definition: overview.phtml:9
$items