CompositeTest.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. use \Magento\Framework\Phrase\Renderer\Composite;
  8. class CompositeTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var Composite
  12. */
  13. protected $object;
  14. /**
  15. * @var \PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $rendererOne;
  18. /**
  19. * @var \PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $rendererTwo;
  22. protected function setUp()
  23. {
  24. $this->rendererOne = $this->createMock(\Magento\Framework\Phrase\RendererInterface::class);
  25. $this->rendererTwo = $this->createMock(\Magento\Framework\Phrase\RendererInterface::class);
  26. $this->object = new \Magento\Framework\Phrase\Renderer\Composite([$this->rendererOne, $this->rendererTwo]);
  27. }
  28. /**
  29. * @expectedException \InvalidArgumentException
  30. * @expectedExceptionMessage Instance of the phrase renderer is expected, got stdClass instead
  31. */
  32. public function testConstructorException()
  33. {
  34. new \Magento\Framework\Phrase\Renderer\Composite([new \stdClass()]);
  35. }
  36. public function testRender()
  37. {
  38. $text = 'some text';
  39. $arguments = ['arg1', 'arg2'];
  40. $resultAfterFirst = 'rendered text first';
  41. $resultAfterSecond = 'rendered text second';
  42. $this->rendererOne->expects(
  43. $this->once()
  44. )->method(
  45. 'render'
  46. )->with(
  47. [$text],
  48. $arguments
  49. )->will(
  50. $this->returnValue($resultAfterFirst)
  51. );
  52. $this->rendererTwo->expects(
  53. $this->once()
  54. )->method(
  55. 'render'
  56. )->with(
  57. [
  58. $text,
  59. $resultAfterFirst,
  60. ],
  61. $arguments
  62. )->will(
  63. $this->returnValue($resultAfterSecond)
  64. );
  65. $this->assertEquals($resultAfterSecond, $this->object->render([$text], $arguments));
  66. }
  67. public function testRenderException()
  68. {
  69. $message = 'something went wrong';
  70. $exception = new \Exception($message);
  71. $this->rendererOne->expects($this->once())
  72. ->method('render')
  73. ->willThrowException($exception);
  74. $this->expectException('Exception');
  75. $this->expectExceptionMessage($message);
  76. $this->object->render(['text'], []);
  77. }
  78. }