|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\Ui\Test\Unit\TemplateEngine\Xhtml; |
| 10 | + |
| 11 | +use Magento\Framework\App\State; |
| 12 | +use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; |
| 13 | +use Magento\Framework\View\Element\UiComponentInterface; |
| 14 | +use Magento\Framework\View\TemplateEngine\Xhtml\CompilerInterface; |
| 15 | +use Magento\Framework\View\TemplateEngine\Xhtml\Template; |
| 16 | +use Magento\Ui\Component\Listing; |
| 17 | +use Magento\Ui\TemplateEngine\Xhtml\Result; |
| 18 | +use PHPUnit\Framework\MockObject\MockObject; |
| 19 | +use PHPUnit\Framework\TestCase; |
| 20 | +use Psr\Log\LoggerInterface; |
| 21 | + |
| 22 | +/** |
| 23 | + * Test for \Magento\Ui\TemplateEngine\Xhtml\Result. |
| 24 | + */ |
| 25 | +class ResultTest extends TestCase |
| 26 | +{ |
| 27 | + /** |
| 28 | + * Stub simple html element |
| 29 | + */ |
| 30 | + private const STUB_HTML_ELEMENT = '<div id="id"></div>'; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var Result |
| 34 | + */ |
| 35 | + private $model; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var ObjectManagerHelper|MockObject |
| 39 | + */ |
| 40 | + private $objectManagerHelper; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var Template|MockObject |
| 44 | + */ |
| 45 | + private $templateMock; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var CompilerInterface|MockObject |
| 49 | + */ |
| 50 | + private $compilerMock; |
| 51 | + |
| 52 | + /** |
| 53 | + * @var UiComponentInterface|MockObject |
| 54 | + */ |
| 55 | + private $componentMock; |
| 56 | + |
| 57 | + /** |
| 58 | + * @var LoggerInterface|MockObject |
| 59 | + */ |
| 60 | + private $loggerMock; |
| 61 | + |
| 62 | + /** |
| 63 | + * @var State|MockObject |
| 64 | + */ |
| 65 | + private $stateMock; |
| 66 | + |
| 67 | + /** |
| 68 | + * @inheritdoc |
| 69 | + */ |
| 70 | + protected function setUp() |
| 71 | + { |
| 72 | + $this->templateMock = $this->createMock(Template::class); |
| 73 | + $this->compilerMock = $this->createMock(CompilerInterface::class); |
| 74 | + $this->componentMock = $this->createMock(Listing::class); |
| 75 | + $this->loggerMock = $this->createMock(LoggerInterface::class); |
| 76 | + $this->stateMock = $this->createMock(State::class); |
| 77 | + |
| 78 | + $this->objectManagerHelper = new ObjectManagerHelper($this); |
| 79 | + $this->model = $this->objectManagerHelper->getObject( |
| 80 | + Result::class, |
| 81 | + [ |
| 82 | + 'template' => $this->templateMock, |
| 83 | + 'compiler' => $this->compilerMock, |
| 84 | + 'component' => $this->componentMock, |
| 85 | + 'logger' => $this->loggerMock, |
| 86 | + 'state' => $this->stateMock, |
| 87 | + ] |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * To string method with exception message |
| 93 | + * |
| 94 | + * @return void |
| 95 | + */ |
| 96 | + public function testToStringWithException(): void |
| 97 | + { |
| 98 | + $e = new \Exception(); |
| 99 | + |
| 100 | + $this->templateMock->expects($this->once()) |
| 101 | + ->method('getDocumentElement') |
| 102 | + ->willThrowException($e); |
| 103 | + $this->stateMock->expects($this->once()) |
| 104 | + ->method('getMode') |
| 105 | + ->willReturn(State::MODE_DEVELOPER); |
| 106 | + |
| 107 | + $this->loggerMock->expects($this->once()) |
| 108 | + ->method('critical') |
| 109 | + ->with($e); |
| 110 | + $this->assertEquals( |
| 111 | + '<pre><code>Exception in ' . $e->getFile() . ':' . $e->getLine() . '</code></pre>', |
| 112 | + $this->model->__toString() |
| 113 | + ); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * To string method |
| 118 | + * |
| 119 | + * @return void |
| 120 | + */ |
| 121 | + public function testToString(): void |
| 122 | + { |
| 123 | + $domElementMock = $this->getMockBuilder(\DOMElement::class) |
| 124 | + ->setConstructorArgs(['arg']) |
| 125 | + ->getMock(); |
| 126 | + |
| 127 | + $this->templateMock->expects($this->once()) |
| 128 | + ->method('getDocumentElement') |
| 129 | + ->willReturn($domElementMock); |
| 130 | + $this->compilerMock->expects($this->once()) |
| 131 | + ->method('compile') |
| 132 | + ->with( |
| 133 | + $this->isInstanceOf(\DOMElement::class), |
| 134 | + $this->componentMock, |
| 135 | + $this->componentMock |
| 136 | + ); |
| 137 | + $this->templateMock->expects($this->once())->method('__toString'); |
| 138 | + $this->compilerMock->expects($this->once()) |
| 139 | + ->method('postprocessing') |
| 140 | + ->willReturn(self::STUB_HTML_ELEMENT); |
| 141 | + |
| 142 | + $this->assertEquals(self::STUB_HTML_ELEMENT, $this->model->__toString()); |
| 143 | + } |
| 144 | +} |
0 commit comments