Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PaymentFailuresServiceTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
14 
18 class PaymentFailuresServiceTest extends \PHPUnit\Framework\TestCase
19 {
23  private $paymentFailures;
24 
28  private $quote;
29 
33  private $cartRepositoryMock;
34 
38  protected function setUp()
39  {
40  $this->quote = Bootstrap::getObjectManager()->create(Quote::class);
41  $this->cartRepositoryMock = $this->getMockBuilder(CartRepositoryInterface::class)
42  ->disableOriginalConstructor()
43  ->setMethods(['get'])
44  ->getMockForAbstractClass();
45 
46  $this->paymentFailures = Bootstrap::getObjectManager()->create(
47  PaymentFailuresInterface::class,
48  [
49  'cartRepository' => $this->cartRepositoryMock,
50  ]
51  );
52  }
53 
62  public function testHandlerWithCustomer(): void
63  {
64  $errorMessage = __('Transaction declined.');
65  $checkoutType = 'custom_checkout';
66 
67  $this->quote->load('test01', 'reserved_order_id');
68  $this->cartRepositoryMock->method('get')
69  ->with($this->quote->getId())
70  ->willReturn($this->quote);
71 
72  $this->paymentFailures->handle((int)$this->quote->getId(), $errorMessage->render());
73 
74  $paymentReflection = new \ReflectionClass($this->paymentFailures);
75  $templateTimeMethod = $paymentReflection->getMethod('getLocaleDate');
76  $templateTimeMethod->setAccessible(true);
77 
78  $templateVarsMethod = $paymentReflection->getMethod('getTemplateVars');
79  $templateVarsMethod->setAccessible(true);
80 
81  $templateVars = $templateVarsMethod->invoke($this->paymentFailures, $this->quote, $errorMessage, $checkoutType);
82  $expectedVars = [
83  'reason' => $errorMessage,
84  'checkoutType' => $checkoutType,
85  'dateAndTime' => $templateTimeMethod->invoke($this->paymentFailures),
86  'customer' => 'John Smith',
87  'customerEmail' => '[email protected]',
88  'paymentMethod' => 'Some Title Of The Method',
89  'shippingMethod' => 'Some Shipping Method',
90  'items' => 'Simple Product x 2 USD 10<br />Custom Design Simple Product x 1 USD 10',
91  'total' => 'USD 30.0000',
92  'billingAddress' => $this->quote->getBillingAddress(),
93  'shippingAddress' => $this->quote->getShippingAddress(),
94  ];
95 
96  $this->assertEquals($expectedVars, $templateVars);
97  }
98 }
__()
Definition: __.php:13