PaymentFailuresServiceTest.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Sales\Model\Service;
  8. use Magento\Quote\Api\CartRepositoryInterface;
  9. use Magento\Quote\Model\Quote;
  10. use Magento\Sales\Api\PaymentFailuresInterface;
  11. use Magento\TestFramework\Helper\Bootstrap;
  12. /**
  13. * Tests \Magento\Sales\Api\PaymentFailuresInterface.
  14. */
  15. class PaymentFailuresServiceTest extends \PHPUnit\Framework\TestCase
  16. {
  17. /**
  18. * @var PaymentFailuresInterface
  19. */
  20. private $paymentFailures;
  21. /**
  22. * @var Quote
  23. */
  24. private $quote;
  25. /**
  26. * @var CartRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. private $cartRepositoryMock;
  29. /**
  30. * @inheritdoc
  31. */
  32. protected function setUp()
  33. {
  34. $this->quote = Bootstrap::getObjectManager()->create(Quote::class);
  35. $this->cartRepositoryMock = $this->getMockBuilder(CartRepositoryInterface::class)
  36. ->disableOriginalConstructor()
  37. ->setMethods(['get'])
  38. ->getMockForAbstractClass();
  39. $this->paymentFailures = Bootstrap::getObjectManager()->create(
  40. PaymentFailuresInterface::class,
  41. [
  42. 'cartRepository' => $this->cartRepositoryMock,
  43. ]
  44. );
  45. }
  46. /**
  47. * @magentoDataFixture Magento/Sales/_files/quote_with_two_products_and_customer.php
  48. * @magentoConfigFixture current_store payment/payflowpro/title Some Title Of The Method
  49. * @magentoConfigFixture current_store carriers/freeshipping/title Some Shipping Method
  50. * @magentoDbIsolation enabled
  51. * @magentoAppIsolation enabled
  52. * @return void
  53. */
  54. public function testHandlerWithCustomer(): void
  55. {
  56. $errorMessage = __('Transaction declined.');
  57. $checkoutType = 'custom_checkout';
  58. $this->quote->load('test01', 'reserved_order_id');
  59. $this->cartRepositoryMock->method('get')
  60. ->with($this->quote->getId())
  61. ->willReturn($this->quote);
  62. $this->paymentFailures->handle((int)$this->quote->getId(), $errorMessage->render());
  63. $paymentReflection = new \ReflectionClass($this->paymentFailures);
  64. $templateTimeMethod = $paymentReflection->getMethod('getLocaleDate');
  65. $templateTimeMethod->setAccessible(true);
  66. $templateVarsMethod = $paymentReflection->getMethod('getTemplateVars');
  67. $templateVarsMethod->setAccessible(true);
  68. $templateVars = $templateVarsMethod->invoke($this->paymentFailures, $this->quote, $errorMessage, $checkoutType);
  69. $expectedVars = [
  70. 'reason' => $errorMessage,
  71. 'checkoutType' => $checkoutType,
  72. 'dateAndTime' => $templateTimeMethod->invoke($this->paymentFailures),
  73. 'customer' => 'John Smith',
  74. 'customerEmail' => 'aaa@aaa.com',
  75. 'paymentMethod' => 'Some Title Of The Method',
  76. 'shippingMethod' => 'Some Shipping Method',
  77. 'items' => 'Simple Product x 2 USD 10<br />Custom Design Simple Product x 1 USD 10',
  78. 'total' => 'USD 30.0000',
  79. 'billingAddress' => $this->quote->getBillingAddress(),
  80. 'shippingAddress' => $this->quote->getShippingAddress(),
  81. ];
  82. $this->assertEquals($expectedVars, $templateVars);
  83. }
  84. }