Skip to content

Commit c3ec35c

Browse files
committed
MAGETWO-34177: Simplification of Payment Configuration
- create tests
1 parent 38052e9 commit c3ec35c

File tree

4 files changed

+268
-6
lines changed

4 files changed

+268
-6
lines changed

app/code/Magento/Config/Model/Config/Compiler/IncludeElement.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,21 @@ public function __construct(Reader $moduleReader, Filesystem $filesystem)
5454
public function compile(CompilerInterface $compiler, \DOMElement $node, Object $processedObject, Object $context)
5555
{
5656
$ownerDocument = $node->ownerDocument;
57+
$parentNode = $node->parentNode;
5758

5859
$document = new \DOMDocument();
5960
$document->loadXML($this->getContent($node->getAttribute(static::INCLUDE_PATH)));
6061

62+
foreach ($this->getChildNodes($document->documentElement) as $child) {
63+
$compiler->compile($child, $processedObject, $context);
64+
}
65+
6166
$newFragment = $ownerDocument->createDocumentFragment();
6267
foreach ($document->documentElement->childNodes as $child) {
6368
$newFragment->appendXML($document->saveXML($child));
6469
}
6570

66-
$node = $node->parentNode->replaceChild($newFragment, $node);
67-
foreach ($this->getChildNodes($node) as $child) {
68-
$compiler->compile($child, $processedObject, $context);
69-
}
71+
$parentNode->replaceChild($newFragment, $node);
7072
}
7173

7274
/**
@@ -106,6 +108,6 @@ protected function getContent($includePath)
106108
return $modulesDirectory->readFile($path);
107109
}
108110

109-
throw new LocalizedException(__('The file "' . $file . '" does not exist'));
111+
throw new LocalizedException(__('The file "' . $path . '" does not exist'));
110112
}
111113
}
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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\Config\Structure;
7+
8+
/**
9+
* Class ReaderTest
10+
*/
11+
class ReaderTest extends \PHPUnit_Framework_TestCase
12+
{
13+
/**
14+
* @var \Magento\Config\Model\Config\Structure\Reader
15+
*/
16+
protected $reader;
17+
18+
/**
19+
* @var \Magento\Framework\Config\FileResolverInterface|\PHPUnit_Framework_MockObject_MockObject
20+
*/
21+
protected $fileResolverMock;
22+
23+
/**
24+
* @var \Magento\Config\Model\Config\Structure\Converter|\PHPUnit_Framework_MockObject_MockObject
25+
*/
26+
protected $converterMock;
27+
28+
/**
29+
* @var \Magento\Config\Model\Config\SchemaLocator|\PHPUnit_Framework_MockObject_MockObject
30+
*/
31+
protected $schemaLocatorMock;
32+
33+
/**
34+
* @var \Magento\Framework\Config\ValidationStateInterface|\PHPUnit_Framework_MockObject_MockObject
35+
*/
36+
protected $validationStateMock;
37+
38+
/**
39+
* @var \Magento\Framework\View\TemplateEngine\Xhtml\CompilerInterface|\PHPUnit_Framework_MockObject_MockObject
40+
*/
41+
protected $compilerMock;
42+
43+
/**
44+
* Set up
45+
*
46+
* @return void
47+
*/
48+
protected function setUp()
49+
{
50+
$this->fileResolverMock = $this->getMockBuilder('Magento\Framework\Config\FileResolverInterface')
51+
->getMockForAbstractClass();
52+
$this->converterMock = $this->getMockBuilder('Magento\Config\Model\Config\Structure\Converter')
53+
->disableOriginalConstructor()
54+
->getMock();
55+
$this->schemaLocatorMock = $this->getMockBuilder('Magento\Config\Model\Config\SchemaLocator')
56+
->disableOriginalConstructor()
57+
->getMock();
58+
$this->validationStateMock = $this->getMockBuilder('Magento\Framework\Config\ValidationStateInterface')
59+
->getMockForAbstractClass();
60+
$this->compilerMock = $this->getMockBuilder('Magento\Framework\View\TemplateEngine\Xhtml\CompilerInterface')
61+
->getMockForAbstractClass();
62+
63+
$this->reader = new \Magento\Config\Model\Config\Structure\Reader(
64+
$this->fileResolverMock,
65+
$this->converterMock,
66+
$this->schemaLocatorMock,
67+
$this->validationStateMock,
68+
$this->compilerMock
69+
);
70+
}
71+
72+
/**
73+
* Test the successful execution of the 'read' method
74+
*
75+
* @return void
76+
*/
77+
public function testReadSuccessNotValidatedCase()
78+
{
79+
$content = '<config><item name="test1"></item><item name="test2"></item></config>';
80+
$expectedResult = ['result_data'];
81+
$fileList = ['file' => $content];
82+
83+
$this->fileResolverMock->expects($this->once())
84+
->method('get')
85+
->with('system.xml', 'global')
86+
->willReturn($fileList);
87+
88+
$this->compilerMock->expects($this->once())
89+
->method('compile')
90+
->with(
91+
$this->isInstanceOf('\DOMElement'),
92+
$this->isInstanceOf('Magento\Framework\Object'),
93+
$this->isInstanceOf('Magento\Framework\Object')
94+
);
95+
$this->converterMock->expects($this->once())
96+
->method('convert')
97+
->with($this->isInstanceOf('\DOMDocument'))
98+
->willReturn($expectedResult);
99+
100+
$this->assertEquals($expectedResult, $this->reader->read());
101+
}
102+
}

dev/tests/integration/phpunit.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
<!-- Semicolon-separated 'glob' patterns, that match global XML configuration files -->
4545
<const name="TESTS_GLOBAL_CONFIG_DIR" value="../../../app/etc"/>
4646
<!-- Whether to cleanup the application before running tests or not -->
47-
<const name="TESTS_CLEANUP" value="enabled"/>
47+
<const name="TESTS_CLEANUP" value="disabled"/>
4848
<!-- Memory usage and estimated leaks thresholds -->
4949
<!--<const name="TESTS_MEM_USAGE_LIMIT" value="1024M"/>-->
5050
<const name="TESTS_MEM_LEAK_LIMIT" value=""/>

0 commit comments

Comments
 (0)