Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
IncludeElementTest.php
Go to the documentation of this file.
1 <?php
7 
11 class IncludeElementTest extends \PHPUnit\Framework\TestCase
12 {
16  protected $includeElement;
17 
21  protected $moduleReaderMock;
22 
26  protected $readFactoryMock;
27 
33  protected function setUp()
34  {
35  $this->moduleReaderMock = $this->getMockBuilder(\Magento\Framework\Module\Dir\Reader::class)
36  ->disableOriginalConstructor()
37  ->getMock();
38  $this->readFactoryMock = $this->getMockBuilder(\Magento\Framework\Filesystem\Directory\ReadFactory::class)
39  ->disableOriginalConstructor()
40  ->getMock();
41 
42  $this->includeElement = new \Magento\Config\Model\Config\Compiler\IncludeElement(
43  $this->moduleReaderMock,
44  $this->readFactoryMock
45  );
46  }
47 
51  public function testCompileSuccess()
52  {
53  $xmlContent = '<rootConfig><include path="Module_Name::path/to/file.xml"/></rootConfig>';
54 
55  $document = new \DOMDocument();
56  $document->loadXML($xmlContent);
57 
58  $compilerMock = $this->getMockBuilder(\Magento\Framework\View\TemplateEngine\Xhtml\CompilerInterface::class)
59  ->getMockForAbstractClass();
60  $processedObjectMock = $this->getMockBuilder(\Magento\Framework\DataObject::class)
61  ->disableOriginalConstructor()
62  ->getMock();
63 
64  $this->getContentStep();
65 
66  $compilerMock->expects($this->exactly(2))
67  ->method('compile')
68  ->with($this->isInstanceOf('\DOMElement'), $processedObjectMock, $processedObjectMock);
69 
70  $this->includeElement->compile(
71  $compilerMock,
72  $document->documentElement->firstChild,
73  $processedObjectMock,
74  $processedObjectMock
75  );
76 
77  $this->assertEquals(
78  '<?xml version="1.0"?><rootConfig><item id="1"><test/></item><item id="2"/></rootConfig>',
79  str_replace(PHP_EOL, '', $document->saveXML())
80  );
81  }
82 
87  public function testCompileException()
88  {
89  $xmlContent = '<rootConfig><include path="Module_Name::path/to/file.xml"/></rootConfig>';
90 
91  $document = new \DOMDocument();
92  $document->loadXML($xmlContent);
93 
94  $compilerMock = $this->getMockBuilder(\Magento\Framework\View\TemplateEngine\Xhtml\CompilerInterface::class)
95  ->getMockForAbstractClass();
96  $processedObjectMock = $this->getMockBuilder(\Magento\Framework\DataObject::class)
97  ->disableOriginalConstructor()
98  ->getMock();
99 
100  $this->getContentStep(false);
101 
102  $compilerMock->expects($this->never())
103  ->method('compile')
104  ->with($this->isInstanceOf('\DOMElement'), $processedObjectMock, $processedObjectMock);
105 
106  $this->includeElement->compile(
107  $compilerMock,
108  $document->documentElement->firstChild,
109  $processedObjectMock,
110  $processedObjectMock
111  );
112  }
113 
117  protected function getContentStep($check = true)
118  {
119  $resultPath = 'adminhtml/path/to/file.xml';
120  $includeXmlContent = '<config><item id="1"><test/></item><item id="2"></item></config>';
121 
122  $modulesDirectoryMock = $this->getMockBuilder(\Magento\Framework\Filesystem\Directory\ReadInterface::class)
123  ->getMockForAbstractClass();
124 
125  $this->readFactoryMock->expects($this->once())
126  ->method('create')
127  ->willReturn($modulesDirectoryMock);
128 
129  $this->moduleReaderMock->expects($this->once())
130  ->method('getModuleDir')
131  ->with('etc', 'Module_Name')
132  ->willReturn('path/in/application/module');
133 
134  $modulesDirectoryMock->expects($this->once())
135  ->method('isExist')
136  ->with($resultPath)
137  ->willReturn($check);
138 
139  if ($check) {
140  $modulesDirectoryMock->expects($this->once())
141  ->method('isFile')
142  ->with($resultPath)
143  ->willReturn($check);
144  $modulesDirectoryMock->expects($this->once())
145  ->method('readFile')
146  ->with($resultPath)
147  ->willReturn($includeXmlContent);
148  }
149  }
150 }