|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Config\Model\Config\Structure\Reader; |
| 9 | + |
| 10 | +use Magento\Config\Model\Config\SchemaLocator; |
| 11 | +use Magento\Framework\App\Utility\Files; |
| 12 | +use Magento\Framework\Config\Dom; |
| 13 | +use Magento\Framework\Config\FileResolverInterface; |
| 14 | +use Magento\Framework\Config\ValidationStateInterface; |
| 15 | +use Magento\Framework\ObjectManagerInterface; |
| 16 | +use Magento\Framework\View\TemplateEngine\Xhtml\CompilerInterface; |
| 17 | +use Magento\TestFramework\Helper\Bootstrap; |
| 18 | + |
| 19 | +/** |
| 20 | + * Class ReaderTest check Magento\Config\Model\Config\Structure\Reader::_readFiles() method. |
| 21 | + */ |
| 22 | +class ReaderTest extends \PHPUnit\Framework\TestCase |
| 23 | +{ |
| 24 | + /** |
| 25 | + * Test config location. |
| 26 | + * |
| 27 | + * @string |
| 28 | + */ |
| 29 | + const CONFIG = '/dev/tests/integration/testsuite/Magento/Config/Model/Config/Structure/Reader/_files/'; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var ObjectManagerInterface |
| 33 | + */ |
| 34 | + private $objectManager; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var Files |
| 38 | + */ |
| 39 | + private $fileUtility; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var ValidationStateInterface |
| 43 | + */ |
| 44 | + private $validationStateMock; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var \Magento\Framework\Config\SchemaLocatorInterface |
| 48 | + */ |
| 49 | + private $schemaLocatorMock; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var FileResolverInterface |
| 53 | + */ |
| 54 | + private $fileResolverMock; |
| 55 | + |
| 56 | + /** |
| 57 | + * @var ReaderStub |
| 58 | + */ |
| 59 | + private $reader; |
| 60 | + |
| 61 | + /** |
| 62 | + * @var ConverterStub |
| 63 | + */ |
| 64 | + private $converter; |
| 65 | + |
| 66 | + /** |
| 67 | + * @var CompilerInterface|\PHPUnit_Framework_MockObject_MockObject |
| 68 | + */ |
| 69 | + private $compiler; |
| 70 | + |
| 71 | + /** |
| 72 | + * @inheritdoc |
| 73 | + */ |
| 74 | + protected function setUp() |
| 75 | + { |
| 76 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 77 | + $this->fileUtility = Files::init(); |
| 78 | + |
| 79 | + $this->validationStateMock = $this->getMockBuilder(ValidationStateInterface::class) |
| 80 | + ->setMethods(['isValidationRequired']) |
| 81 | + ->getMockForAbstractClass(); |
| 82 | + $this->schemaLocatorMock = $this->getMockBuilder(SchemaLocator::class) |
| 83 | + ->disableOriginalConstructor() |
| 84 | + ->setMethods(['getPerFileSchema']) |
| 85 | + ->getMock(); |
| 86 | + $this->fileResolverMock = $this->getMockBuilder(FileResolverInterface::class) |
| 87 | + ->getMockForAbstractClass(); |
| 88 | + |
| 89 | + $this->validationStateMock->expects($this->atLeastOnce()) |
| 90 | + ->method('isValidationRequired') |
| 91 | + ->willReturn(false); |
| 92 | + $this->schemaLocatorMock->expects($this->atLeastOnce()) |
| 93 | + ->method('getPerFileSchema') |
| 94 | + ->willReturn(false); |
| 95 | + |
| 96 | + $this->converter = $this->objectManager->create(ConverterStub::class); |
| 97 | + |
| 98 | + //Isolate test from actual configuration, and leave only sample data. |
| 99 | + $this->compiler = $this->getMockBuilder(CompilerInterface::class) |
| 100 | + ->disableOriginalConstructor() |
| 101 | + ->setMethods(['compile']) |
| 102 | + ->getMockForAbstractClass(); |
| 103 | + |
| 104 | + $this->reader = $this->objectManager->create( |
| 105 | + ReaderStub::class, |
| 106 | + [ |
| 107 | + 'fileResolver' => $this->fileResolverMock, |
| 108 | + 'converter' => $this->converter, |
| 109 | + 'schemaLocator' => $this->schemaLocatorMock, |
| 110 | + 'validationState' => $this->validationStateMock, |
| 111 | + 'fileName' => 'no_existing_file.xml', |
| 112 | + 'compiler' => $this->compiler, |
| 113 | + 'domDocumentClass' => Dom::class |
| 114 | + ] |
| 115 | + ); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * The test checks the file structure after processing the nodes responsible for inserting content. |
| 120 | + * |
| 121 | + * @return void |
| 122 | + */ |
| 123 | + public function testXmlConvertedConfigurationAndCompereStructure() |
| 124 | + { |
| 125 | + $actual = $this->reader->readFiles(['actual' => $this->getContent()]); |
| 126 | + |
| 127 | + $document = new \DOMDocument(); |
| 128 | + $document->loadXML($this->getContent()); |
| 129 | + |
| 130 | + $expected = $this->converter->getArrayData($document); |
| 131 | + |
| 132 | + $this->assertEquals($expected, $actual); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Get config sample data for test. |
| 137 | + * |
| 138 | + * @return string |
| 139 | + */ |
| 140 | + protected function getContent() |
| 141 | + { |
| 142 | + $files = $this->fileUtility->getFiles([BP . static::CONFIG], 'config.xml'); |
| 143 | + |
| 144 | + return file_get_contents(reset($files)); |
| 145 | + } |
| 146 | +} |
0 commit comments