InlineTest.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Phrase\Test\Unit\Renderer;
  7. class InlineTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Framework\TranslateInterface|\PHPUnit_Framework_MockObject_MockObject
  11. */
  12. protected $translator;
  13. /**
  14. * @var \Magento\Framework\Phrase\Renderer\Inline
  15. */
  16. protected $renderer;
  17. /**
  18. * @var \Magento\Framework\Translate\Inline\ProviderInterface|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $provider;
  21. /**
  22. * @var \Psr\Log\LoggerInterface|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $loggerMock;
  25. protected function setUp()
  26. {
  27. $this->translator = $this->createMock(\Magento\Framework\TranslateInterface::class);
  28. $this->provider = $this->createMock(\Magento\Framework\Translate\Inline\ProviderInterface::class);
  29. $this->loggerMock = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)
  30. ->getMock();
  31. $this->renderer = new \Magento\Framework\Phrase\Renderer\Inline(
  32. $this->translator,
  33. $this->provider,
  34. $this->loggerMock
  35. );
  36. }
  37. public function testRenderIfInlineTranslationIsAllowed()
  38. {
  39. $theme = 'theme';
  40. $text = 'test';
  41. $result = sprintf('{{{%s}}{{%s}}}', $text, $theme);
  42. $this->translator->expects($this->once())
  43. ->method('getTheme')
  44. ->will($this->returnValue($theme));
  45. $inlineTranslate = $this->createMock(\Magento\Framework\Translate\InlineInterface::class);
  46. $inlineTranslate->expects($this->once())
  47. ->method('isAllowed')
  48. ->will($this->returnValue(true));
  49. $this->provider->expects($this->once())
  50. ->method('get')
  51. ->will($this->returnValue($inlineTranslate));
  52. $this->assertEquals($result, $this->renderer->render([$text], []));
  53. }
  54. public function testRenderIfInlineTranslationIsNotAllowed()
  55. {
  56. $text = 'test';
  57. $inlineTranslate = $this->createMock(\Magento\Framework\Translate\InlineInterface::class);
  58. $inlineTranslate->expects($this->once())
  59. ->method('isAllowed')
  60. ->will($this->returnValue(false));
  61. $this->provider->expects($this->once())
  62. ->method('get')
  63. ->will($this->returnValue($inlineTranslate));
  64. $this->assertEquals($text, $this->renderer->render([$text], []));
  65. }
  66. public function testRenderException()
  67. {
  68. $message = 'something went wrong';
  69. $exception = new \Exception($message);
  70. $this->provider->expects($this->once())
  71. ->method('get')
  72. ->willThrowException($exception);
  73. $this->expectException('Exception');
  74. $this->expectExceptionMessage($message);
  75. $this->renderer->render(['text'], []);
  76. }
  77. }