|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2015 Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Config\Test\Unit\Model\Compiler; |
| 7 | + |
| 8 | +use Magento\Framework\App\Filesystem\DirectoryList; |
| 9 | + |
| 10 | +/** |
| 11 | + * Class IncludeElementTest |
| 12 | + */ |
| 13 | +class IncludeElementTest extends \PHPUnit_Framework_TestCase |
| 14 | +{ |
| 15 | + /** |
| 16 | + * @var \Magento\Config\Model\Config\Compiler\IncludeElement |
| 17 | + */ |
| 18 | + protected $includeElement; |
| 19 | + |
| 20 | + /** |
| 21 | + * @var \Magento\Framework\Module\Dir\Reader|\PHPUnit_Framework_MockObject_MockObject |
| 22 | + */ |
| 23 | + protected $moduleReaderMock; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var \Magento\Framework\Filesystem|\PHPUnit_Framework_MockObject_MockObject |
| 27 | + */ |
| 28 | + protected $filesystemMock; |
| 29 | + |
| 30 | + /** |
| 31 | + * Set up |
| 32 | + * |
| 33 | + * @return void |
| 34 | + */ |
| 35 | + protected function setUp() |
| 36 | + { |
| 37 | + $this->moduleReaderMock = $this->getMockBuilder('Magento\Framework\Module\Dir\Reader') |
| 38 | + ->disableOriginalConstructor() |
| 39 | + ->getMock(); |
| 40 | + $this->filesystemMock = $this->getMockBuilder('Magento\Framework\Filesystem') |
| 41 | + ->disableOriginalConstructor() |
| 42 | + ->getMock(); |
| 43 | + |
| 44 | + $this->includeElement = new \Magento\Config\Model\Config\Compiler\IncludeElement( |
| 45 | + $this->moduleReaderMock, |
| 46 | + $this->filesystemMock |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * @return void |
| 52 | + */ |
| 53 | + public function testCompileSuccess() |
| 54 | + { |
| 55 | + $xmlContent = '<rootConfig><include path="Module_Name::path/to/file.xml"/></rootConfig>'; |
| 56 | + |
| 57 | + $document = new \DOMDocument(); |
| 58 | + $document->loadXML($xmlContent); |
| 59 | + |
| 60 | + $compilerMock = $this->getMockBuilder('Magento\Framework\View\TemplateEngine\Xhtml\CompilerInterface') |
| 61 | + ->getMockForAbstractClass(); |
| 62 | + $processedObjectMock = $this->getMockBuilder('Magento\Framework\Object') |
| 63 | + ->disableOriginalConstructor() |
| 64 | + ->getMock(); |
| 65 | + |
| 66 | + $this->getContentStep(); |
| 67 | + |
| 68 | + $compilerMock->expects($this->exactly(2)) |
| 69 | + ->method('compile') |
| 70 | + ->with($this->isInstanceOf('\DOMElement'), $processedObjectMock, $processedObjectMock); |
| 71 | + |
| 72 | + $this->includeElement->compile( |
| 73 | + $compilerMock, |
| 74 | + $document->documentElement->firstChild, |
| 75 | + $processedObjectMock, |
| 76 | + $processedObjectMock |
| 77 | + ); |
| 78 | + |
| 79 | + $this->assertEquals( |
| 80 | + '<?xml version="1.0"?><rootConfig><item id="1"><test/></item><item id="2"/></rootConfig>', |
| 81 | + str_replace(PHP_EOL, '', $document->saveXML()) |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @expectedException \Magento\Framework\Exception\LocalizedException |
| 87 | + * @expectedExceptionMessage The file "relative/path/to/file.xml" does not exist |
| 88 | + */ |
| 89 | + public function testCompileException() |
| 90 | + { |
| 91 | + $xmlContent = '<rootConfig><include path="Module_Name::path/to/file.xml"/></rootConfig>'; |
| 92 | + |
| 93 | + $document = new \DOMDocument(); |
| 94 | + $document->loadXML($xmlContent); |
| 95 | + |
| 96 | + $compilerMock = $this->getMockBuilder('Magento\Framework\View\TemplateEngine\Xhtml\CompilerInterface') |
| 97 | + ->getMockForAbstractClass(); |
| 98 | + $processedObjectMock = $this->getMockBuilder('Magento\Framework\Object') |
| 99 | + ->disableOriginalConstructor() |
| 100 | + ->getMock(); |
| 101 | + |
| 102 | + $this->getContentStep(false); |
| 103 | + |
| 104 | + $compilerMock->expects($this->never()) |
| 105 | + ->method('compile') |
| 106 | + ->with($this->isInstanceOf('\DOMElement'), $processedObjectMock, $processedObjectMock); |
| 107 | + |
| 108 | + $this->includeElement->compile( |
| 109 | + $compilerMock, |
| 110 | + $document->documentElement->firstChild, |
| 111 | + $processedObjectMock, |
| 112 | + $processedObjectMock |
| 113 | + ); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * @param bool $check |
| 118 | + */ |
| 119 | + protected function getContentStep($check = true) |
| 120 | + { |
| 121 | + $resultPath = 'relative/path/to/file.xml'; |
| 122 | + $includeXmlContent = '<config><item id="1"><test/></item><item id="2"></item></config>'; |
| 123 | + |
| 124 | + $modulesDirectoryMock = $this->getMockBuilder('Magento\Framework\Filesystem\Directory\ReadInterface') |
| 125 | + ->getMockForAbstractClass(); |
| 126 | + |
| 127 | + $this->filesystemMock->expects($this->once()) |
| 128 | + ->method('getDirectoryRead') |
| 129 | + ->with(DirectoryList::MODULES) |
| 130 | + ->willReturn($modulesDirectoryMock); |
| 131 | + |
| 132 | + $this->moduleReaderMock->expects($this->once()) |
| 133 | + ->method('getModuleDir') |
| 134 | + ->with('etc', 'Module_Name') |
| 135 | + ->willReturn('path/in/application/module'); |
| 136 | + |
| 137 | + $modulesDirectoryMock->expects($this->once()) |
| 138 | + ->method('getRelativePath') |
| 139 | + ->with('path/in/application/module/adminhtml/path/to/file.xml') |
| 140 | + ->willReturn($resultPath); |
| 141 | + |
| 142 | + $modulesDirectoryMock->expects($this->once()) |
| 143 | + ->method('isExist') |
| 144 | + ->with($resultPath) |
| 145 | + ->willReturn($check); |
| 146 | + |
| 147 | + if ($check) { |
| 148 | + $modulesDirectoryMock->expects($this->once()) |
| 149 | + ->method('isFile') |
| 150 | + ->with($resultPath) |
| 151 | + ->willReturn($check); |
| 152 | + $modulesDirectoryMock->expects($this->once()) |
| 153 | + ->method('readFile') |
| 154 | + ->with($resultPath) |
| 155 | + ->willReturn($includeXmlContent); |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments