Skip to content

Commit c540a59

Browse files
committed
Add Unit Tests
1 parent 10c0bc6 commit c540a59

File tree

5 files changed

+302
-8
lines changed

5 files changed

+302
-8
lines changed

app/code/Magento/Developer/Model/XmlCatalog/Format/VsCode.php

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
class VsCode implements FormatInterface
2121
{
2222
private const PROJECT_PATH_IDENTIFIER = '..';
23-
private const FILE_MODE_READ = 'r';
24-
private const FILE_MODE_WRITE = 'w';
23+
public const XMLNS = 'urn:oasis:names:tc:entity:xmlns:xml:catalog';
24+
public const FILE_MODE_READ = 'r';
25+
public const FILE_MODE_WRITE = 'w';
2526

2627
/**
2728
* @var ReadInterface
@@ -46,7 +47,7 @@ class VsCode implements FormatInterface
4647
public function __construct(
4748
ReadFactory $readFactory,
4849
WriteFactory $fileWriteFactory,
49-
DomDocumentFactory $domDocumentFactory = null
50+
DomDocumentFactory $domDocumentFactory
5051
) {
5152
$this->currentDirRead = $readFactory->create(getcwd());
5253
$this->fileWriteFactory = $fileWriteFactory;
@@ -73,23 +74,36 @@ public function generateCatalog(array $dictionary, $configFile): void
7374
} else {
7475
$this->initEmptyFile($dom);
7576
}
76-
$xpath = new \DOMXPath($dom);
77-
$nodeList = $xpath->query('/catalog');
78-
$catalogNode = $nodeList->item(0);
77+
$catalogNode = $dom->getElementsByTagName('catalog')->item(0);
78+
79+
if ($catalogNode == null) {
80+
$dom = $this->domDocumentFactory->create();
81+
$catalogNode = $this->initEmptyFile($dom);
82+
}
7983
$file->close();
8084
} catch (FileSystemException $f) {
8185
//create file if does not exists
8286
$dom = $this->domDocumentFactory->create();
8387
$catalogNode = $this->initEmptyFile($dom);
8488
}
8589

90+
$xpath = new \DOMXPath($dom);
91+
$xpath->registerNamespace('xmlns', self::XMLNS);
92+
8693
foreach ($dictionary as $urn => $xsdPath) {
87-
$node = $dom->createElement('system');
94+
// Find an existing urn
95+
$existingNode = $xpath->query("/xmlns:catalog/xmlns:system[@systemId='" . $urn . "']")->item(0);
96+
$node = $existingNode ?? $dom->createElement('system');
8897
$node->setAttribute('systemId', $urn);
8998
$node->setAttribute('uri', $this->getFileLocationInProject($xsdPath));
9099
$catalogNode->appendChild($node);
91100
}
92101
$dom->formatOutput = true;
102+
$dom->preserveWhiteSpace = false;
103+
104+
// Reload to keep pretty format
105+
$dom->loadXML($dom->saveXML());
106+
93107
$file = $this->fileWriteFactory->create($configFile, DriverPool::FILE, self::FILE_MODE_WRITE);
94108
$file->write($dom->saveXML());
95109
$file->close();
@@ -105,7 +119,7 @@ private function initEmptyFile(\DOMDocument $dom): \DOMElement
105119
{
106120
$catalogNode = $dom->createElement('catalog');
107121

108-
$catalogNode->setAttribute('xmlns', 'urn:oasis:names:tc:entity:xmlns:xml:catalog');
122+
$catalogNode->setAttribute('xmlns', self::XMLNS);
109123
$dom->appendChild($catalogNode);
110124

111125
return $catalogNode;

app/code/Magento/Developer/Test/Unit/Console/Command/XmlCatalogGenerateCommandTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,58 @@ public function testExecuteBadType()
6666
$commandTester->execute([XmlCatalogGenerateCommand::IDE_FILE_PATH_ARGUMENT => 'test']);
6767
$this->assertEquals('', $commandTester->getDisplay());
6868
}
69+
70+
public function testExecuteVsCodeFormat()
71+
{
72+
$fixtureXmlFile = __DIR__ . '/_files/test.xml';
73+
74+
$filesMock = $this->createPartialMock(\Magento\Framework\App\Utility\Files::class, ['getXmlCatalogFiles']);
75+
$filesMock->expects($this->at(0))
76+
->method('getXmlCatalogFiles')
77+
->will($this->returnValue([[$fixtureXmlFile]]));
78+
$filesMock->expects($this->at(1))
79+
->method('getXmlCatalogFiles')
80+
->will($this->returnValue([]));
81+
$urnResolverMock = $this->createMock(\Magento\Framework\Config\Dom\UrnResolver::class);
82+
$urnResolverMock->expects($this->once())
83+
->method('getRealPath')
84+
->with($this->equalTo('urn:magento:framework:Module/etc/module.xsd'))
85+
->will($this->returnValue($fixtureXmlFile));
86+
87+
$vscodeFormatMock = $this->createMock(\Magento\Developer\Model\XmlCatalog\Format\VsCode::class);
88+
$vscodeFormatMock->expects($this->once())
89+
->method('generateCatalog')
90+
->with(
91+
$this->equalTo(['urn:magento:framework:Module/etc/module.xsd' => $fixtureXmlFile]),
92+
$this->equalTo('test')
93+
)->will($this->returnValue(null));
94+
95+
$formats = ['vscode' => $vscodeFormatMock];
96+
$readFactory = $this->createMock(\Magento\Framework\Filesystem\Directory\ReadFactory::class);
97+
$readDirMock = $this->createMock(\Magento\Framework\Filesystem\Directory\ReadInterface::class);
98+
99+
$content = file_get_contents($fixtureXmlFile);
100+
101+
$readDirMock->expects($this->once())
102+
->method('readFile')
103+
->with($this->equalTo('test.xml'))
104+
->will($this->returnValue($content));
105+
$readFactory->expects($this->once())
106+
->method('create')
107+
->will($this->returnValue($readDirMock));
108+
109+
$this->command = new XmlCatalogGenerateCommand(
110+
$filesMock,
111+
$urnResolverMock,
112+
$readFactory,
113+
$formats
114+
);
115+
116+
$commandTester = new CommandTester($this->command);
117+
$commandTester->execute([
118+
'--' . XmlCatalogGenerateCommand::IDE_OPTION => 'vscode',
119+
XmlCatalogGenerateCommand::IDE_FILE_PATH_ARGUMENT => 'test',
120+
]);
121+
$this->assertEquals('', $commandTester->getDisplay());
122+
}
69123
}
Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
<?php
2+
3+
namespace Magento\Developer\Test\Unit\Model\XmlCatalog\Format;
4+
5+
use Magento\Developer\Model\XmlCatalog\Format\VsCode;
6+
use Magento\Framework\DomDocument\DomDocumentFactory;
7+
use Magento\Framework\Exception\FileSystemException;
8+
use Magento\Framework\Filesystem\Directory\ReadFactory;
9+
use Magento\Framework\Filesystem\Directory\ReadInterface;
10+
use Magento\Framework\Filesystem\DriverPool;
11+
use Magento\Framework\Filesystem\File\WriteFactory;
12+
13+
class VsCodeTest extends \PHPUnit\Framework\TestCase
14+
{
15+
/**
16+
* @var Magento\Developer\Model\XmlCatalog\Format\VsCode
17+
*/
18+
protected $vscodeFormat;
19+
20+
/**
21+
* @var Magento\Framework\Filesystem\Directory\ReadFactory
22+
*/
23+
protected $readFactory;
24+
25+
/**
26+
* @var Magento\Framework\Filesystem\File\WriteFactory
27+
*/
28+
protected $fileWriteFactory;
29+
30+
/**
31+
* @var Magento\Framework\DomDocument\DomDocumentFactory
32+
*/
33+
protected $domFactory;
34+
35+
protected $dictionary = [
36+
'urn:magento:framework:Acl/etc/acl.xsd' => 'vendor/magento/framework/Acl/etc/acl.xsd',
37+
'urn:magento:module:Magento_Store:etc/config.xsd' => 'vendor/magento/module-store/etc/config.xsd',
38+
'urn:magento:module:Magento_Cron:etc/crontab.xsd' => 'vendor/magento/module-cron/etc/crontab.xsd',
39+
'urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd' => 'vendor/magento/framework/Setup/Declaration/Schema/etc/schema.xsd',
40+
];
41+
42+
public function setUp()
43+
{
44+
45+
$currentDirRead = $this->createMock(ReadInterface::class);
46+
$currentDirRead->expects($this->any())
47+
->method('getRelativePath')
48+
->willReturnCallback(function ($xsdPath) {
49+
return $xsdPath;
50+
});
51+
52+
$this->readFactory = $this->createMock(ReadFactory::class);
53+
$this->readFactory->expects($this->once())
54+
->method('create')
55+
->withAnyParameters()
56+
->willReturn($currentDirRead);
57+
58+
$this->fileWriteFactory = $this->createMock(WriteFactory::class);
59+
$this->domFactory = new DomDocumentFactory();
60+
61+
$this->vscodeFormat = new VsCode(
62+
$this->readFactory,
63+
$this->fileWriteFactory,
64+
$this->domFactory
65+
);
66+
}
67+
68+
public function testGenerateNewValidCatalog()
69+
{
70+
$configFile = 'test';
71+
$fixtureXmlFile = __DIR__ . '/_files/valid_catalog.xml';
72+
$content = file_get_contents($fixtureXmlFile);
73+
74+
$message = __("The \"%1.xml\" file doesn't exist.", $configFile);
75+
76+
$this->fileWriteFactory->expects($this->at(0))
77+
->method('create')
78+
->with(
79+
$configFile,
80+
DriverPool::FILE,
81+
VsCode::FILE_MODE_READ
82+
)
83+
->willThrowException(new FileSystemException($message));
84+
85+
$file = $this->createMock(\Magento\Framework\Filesystem\File\Write::class);
86+
$file->expects($this->once())
87+
->method('write')
88+
->with($content);
89+
90+
$this->fileWriteFactory->expects($this->at(1))
91+
->method('create')
92+
->with(
93+
$configFile,
94+
DriverPool::FILE,
95+
VsCode::FILE_MODE_WRITE
96+
)
97+
->willReturn($file);
98+
99+
$this->vscodeFormat->generateCatalog($this->dictionary, $configFile);
100+
}
101+
102+
public function testGenerateExistingValidCatalog()
103+
{
104+
$configFile = 'test';
105+
$fixtureXmlFile = __DIR__ . '/_files/valid_catalog.xml';
106+
$content = file_get_contents($fixtureXmlFile);
107+
108+
$file = $this->createMock(\Magento\Framework\Filesystem\File\Read::class);
109+
$file->expects($this->once())
110+
->method('readAll')
111+
->withAnyParameters()
112+
->willReturn($content);
113+
114+
$this->fileWriteFactory->expects($this->at(0))
115+
->method('create')
116+
->with(
117+
$configFile,
118+
DriverPool::FILE,
119+
VsCode::FILE_MODE_READ
120+
)
121+
->willReturn($file);
122+
123+
$file = $this->createMock(\Magento\Framework\Filesystem\File\Write::class);
124+
$file->expects($this->once())
125+
->method('write')
126+
->with($content);
127+
128+
$this->fileWriteFactory->expects($this->at(1))
129+
->method('create')
130+
->with(
131+
$configFile,
132+
DriverPool::FILE,
133+
VsCode::FILE_MODE_WRITE
134+
)
135+
->willReturn($file);
136+
137+
$this->vscodeFormat->generateCatalog($this->dictionary, $configFile);
138+
}
139+
140+
public function testGenerateExistingEmptyValidCatalog()
141+
{
142+
$configFile = 'test';
143+
$fixtureXmlFile = __DIR__ . '/_files/valid_catalog.xml';
144+
$content = file_get_contents($fixtureXmlFile);
145+
146+
$file = $this->createMock(\Magento\Framework\Filesystem\File\Read::class);
147+
$file->expects($this->once())
148+
->method('readAll')
149+
->withAnyParameters()
150+
->willReturn('');
151+
152+
$this->fileWriteFactory->expects($this->at(0))
153+
->method('create')
154+
->with(
155+
$configFile,
156+
DriverPool::FILE,
157+
VsCode::FILE_MODE_READ
158+
)
159+
->willReturn($file);
160+
161+
$file = $this->createMock(\Magento\Framework\Filesystem\File\Write::class);
162+
$file->expects($this->once())
163+
->method('write')
164+
->with($content);
165+
166+
$this->fileWriteFactory->expects($this->at(1))
167+
->method('create')
168+
->with(
169+
$configFile,
170+
DriverPool::FILE,
171+
VsCode::FILE_MODE_WRITE
172+
)
173+
->willReturn($file);
174+
175+
$this->vscodeFormat->generateCatalog($this->dictionary, $configFile);
176+
}
177+
178+
public function testGenerateExistingInvalidValidCatalog()
179+
{
180+
$configFile = 'test';
181+
$invalidXmlFile = __DIR__ . '/_files/invalid_catalog.xml';
182+
$invalidContent = file_get_contents($invalidXmlFile);
183+
$validXmlFile = __DIR__ . '/_files/valid_catalog.xml';
184+
$validContent = file_get_contents($validXmlFile);
185+
186+
$file = $this->createMock(\Magento\Framework\Filesystem\File\Read::class);
187+
$file->expects($this->once())
188+
->method('readAll')
189+
->withAnyParameters()
190+
->willReturn($invalidContent);
191+
192+
$this->fileWriteFactory->expects($this->at(0))
193+
->method('create')
194+
->with(
195+
$configFile,
196+
DriverPool::FILE,
197+
VsCode::FILE_MODE_READ
198+
)
199+
->willReturn($file);
200+
201+
$file = $this->createMock(\Magento\Framework\Filesystem\File\Write::class);
202+
$file->expects($this->once())
203+
->method('write')
204+
->with($validContent);
205+
206+
$this->fileWriteFactory->expects($this->at(1))
207+
->method('create')
208+
->with(
209+
$configFile,
210+
DriverPool::FILE,
211+
VsCode::FILE_MODE_WRITE
212+
)
213+
->willReturn($file);
214+
215+
$this->vscodeFormat->generateCatalog($this->dictionary, $configFile);
216+
}
217+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version="1.0"?>
2+
<root />
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0"?>
2+
<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">
3+
<system systemId="urn:magento:framework:Acl/etc/acl.xsd" uri="../vendor/magento/framework/Acl/etc/acl.xsd"/>
4+
<system systemId="urn:magento:module:Magento_Store:etc/config.xsd" uri="../vendor/magento/module-store/etc/config.xsd"/>
5+
<system systemId="urn:magento:module:Magento_Cron:etc/crontab.xsd" uri="../vendor/magento/module-cron/etc/crontab.xsd"/>
6+
<system systemId="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd" uri="../vendor/magento/framework/Setup/Declaration/Schema/etc/schema.xsd"/>
7+
</catalog>

0 commit comments

Comments
 (0)