InfoTest.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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\AuthorizenetAcceptjs\Test\Unit\Block;
  8. use Magento\AuthorizenetAcceptjs\Block\Info;
  9. use Magento\AuthorizenetAcceptjs\Gateway\Config;
  10. use Magento\Framework\Phrase;
  11. use Magento\Framework\Phrase\RendererInterface;
  12. use Magento\Framework\View\Element\Template\Context;
  13. use Magento\Payment\Gateway\ConfigInterface;
  14. use Magento\Payment\Model\InfoInterface;
  15. use PHPUnit\Framework\MockObject\Builder\InvocationMocker;
  16. use PHPUnit\Framework\MockObject\MockObject;
  17. use PHPUnit\Framework\TestCase;
  18. class InfoTest extends TestCase
  19. {
  20. public function testLabelsAreTranslated()
  21. {
  22. /** @var Context|MockObject|InvocationMocker $contextMock */
  23. $contextMock = $this->createMock(Context::class);
  24. /** @var Config|MockObject|InvocationMocker $configMock */
  25. $configMock = $this->createMock(ConfigInterface::class);
  26. $block = new Info($contextMock, $configMock);
  27. /** @var InfoInterface|MockObject|InvocationMocker $payment */
  28. $payment = $this->createMock(InfoInterface::class);
  29. /** @var RendererInterface|MockObject|InvocationMocker $translationRenderer */
  30. $translationRenderer = $this->createMock(RendererInterface::class);
  31. // only foo should be used
  32. $configMock->method('getValue')
  33. ->willReturnMap([
  34. ['paymentInfoKeys', null, 'foo'],
  35. ['privateInfoKeys', null, '']
  36. ]);
  37. // Give more info to ensure only foo is translated
  38. $payment->method('getAdditionalInformation')
  39. ->willReturnCallback(function ($name = null) {
  40. $info = [
  41. 'foo' => 'bar',
  42. 'baz' => 'bash'
  43. ];
  44. if (empty($name)) {
  45. return $info;
  46. }
  47. return $info[$name];
  48. });
  49. // Foo should be translated to Super Cool String
  50. $translationRenderer->method('render')
  51. ->with(['foo'], [])
  52. ->willReturn('Super Cool String');
  53. $previousRenderer = Phrase::getRenderer();
  54. Phrase::setRenderer($translationRenderer);
  55. try {
  56. $block->setData('info', $payment);
  57. $info = $block->getSpecificInformation();
  58. } finally {
  59. // No matter what, restore the renderer
  60. Phrase::setRenderer($previousRenderer);
  61. }
  62. // Assert the label was correctly translated
  63. $this->assertSame($info['Super Cool String'], 'bar');
  64. $this->assertArrayNotHasKey('foo', $info);
  65. $this->assertArrayNotHasKey('baz', $info);
  66. }
  67. }