Skip to content

Commit 984d6b9

Browse files
committed
Class import
Vars name
1 parent c540a59 commit 984d6b9

File tree

1 file changed

+132
-76
lines changed
  • app/code/Magento/Developer/Test/Unit/Model/XmlCatalog/Format

1 file changed

+132
-76
lines changed

app/code/Magento/Developer/Test/Unit/Model/XmlCatalog/Format/VsCodeTest.php

Lines changed: 132 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -8,72 +8,87 @@
88
use Magento\Framework\Filesystem\Directory\ReadFactory;
99
use Magento\Framework\Filesystem\Directory\ReadInterface;
1010
use Magento\Framework\Filesystem\DriverPool;
11+
use Magento\Framework\Filesystem\File\Read;
12+
use Magento\Framework\Filesystem\File\Write;
1113
use Magento\Framework\Filesystem\File\WriteFactory;
14+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
15+
use PHPUnit\Framework\MockObject\MockObject;
16+
use PHPUnit\Framework\TestCase;
1217

13-
class VsCodeTest extends \PHPUnit\Framework\TestCase
18+
class VsCodeTest extends TestCase
1419
{
1520
/**
16-
* @var Magento\Developer\Model\XmlCatalog\Format\VsCode
21+
* @var VsCode
1722
*/
18-
protected $vscodeFormat;
23+
private $vscodeFormat;
1924

2025
/**
21-
* @var Magento\Framework\Filesystem\Directory\ReadFactory
26+
* @var MockObject|ReadFactory
2227
*/
23-
protected $readFactory;
28+
private $readFactoryMock;
2429

2530
/**
26-
* @var Magento\Framework\Filesystem\File\WriteFactory
31+
* @var MockObject|WriteFactory
2732
*/
28-
protected $fileWriteFactory;
33+
private $fileWriteFactoryMock;
2934

3035
/**
31-
* @var Magento\Framework\DomDocument\DomDocumentFactory
36+
* @var DomDocumentFactory
3237
*/
33-
protected $domFactory;
38+
private $domFactory;
3439

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-
];
40+
/**
41+
* @var ObjectManager
42+
*/
43+
private $objectManagerHelper;
4144

4245
public function setUp()
4346
{
47+
$this->objectManagerHelper = new ObjectManager($this);
4448

45-
$currentDirRead = $this->createMock(ReadInterface::class);
46-
$currentDirRead->expects($this->any())
49+
$currentDirReadMock = $this->createMock(ReadInterface::class);
50+
$currentDirReadMock->expects($this->any())
4751
->method('getRelativePath')
4852
->willReturnCallback(function ($xsdPath) {
4953
return $xsdPath;
5054
});
5155

52-
$this->readFactory = $this->createMock(ReadFactory::class);
53-
$this->readFactory->expects($this->once())
56+
$this->readFactoryMock = $this->createMock(ReadFactory::class);
57+
$this->readFactoryMock->expects($this->once())
5458
->method('create')
5559
->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
60+
->willReturn($currentDirReadMock);
61+
62+
$this->fileWriteFactoryMock = $this->createMock(WriteFactory::class);
63+
$this->domFactory = $this->objectManagerHelper->getObject(DomDocumentFactory::class);
64+
65+
$vscodeFormatArgs = $this->objectManagerHelper->getConstructArguments(
66+
VsCode::class,
67+
[
68+
'readFactory' => $this->readFactoryMock,
69+
'fileWriteFactory' => $this->fileWriteFactoryMock,
70+
'domDocumentFactory' => $this->domFactory,
71+
]
6572
);
73+
74+
$this->vscodeFormat = $this->objectManagerHelper->getObject(VsCode::class, $vscodeFormatArgs);
6675
}
6776

68-
public function testGenerateNewValidCatalog()
77+
/**
78+
* Test generation of new valid catalog
79+
*
80+
* @param string $content
81+
* @param array $dictionary
82+
* @dataProvider dictionaryDataProvider
83+
* @return void
84+
*/
85+
public function testGenerateNewValidCatalog($content, $dictionary)
6986
{
7087
$configFile = 'test';
71-
$fixtureXmlFile = __DIR__ . '/_files/valid_catalog.xml';
72-
$content = file_get_contents($fixtureXmlFile);
7388

7489
$message = __("The \"%1.xml\" file doesn't exist.", $configFile);
7590

76-
$this->fileWriteFactory->expects($this->at(0))
91+
$this->fileWriteFactoryMock->expects($this->at(0))
7792
->method('create')
7893
->with(
7994
$configFile,
@@ -82,136 +97,177 @@ public function testGenerateNewValidCatalog()
8297
)
8398
->willThrowException(new FileSystemException($message));
8499

85-
$file = $this->createMock(\Magento\Framework\Filesystem\File\Write::class);
86-
$file->expects($this->once())
100+
$fileMock = $this->createMock(Write::class);
101+
$fileMock->expects($this->once())
87102
->method('write')
88103
->with($content);
89104

90-
$this->fileWriteFactory->expects($this->at(1))
105+
$this->fileWriteFactoryMock->expects($this->at(1))
91106
->method('create')
92107
->with(
93108
$configFile,
94109
DriverPool::FILE,
95110
VsCode::FILE_MODE_WRITE
96111
)
97-
->willReturn($file);
112+
->willReturn($fileMock);
98113

99-
$this->vscodeFormat->generateCatalog($this->dictionary, $configFile);
114+
$this->vscodeFormat->generateCatalog($dictionary, $configFile);
100115
}
101116

102-
public function testGenerateExistingValidCatalog()
117+
/**
118+
* Test modify existing valid catalog
119+
*
120+
* @param string $content
121+
* @param array $dictionary
122+
* @dataProvider dictionaryDataProvider
123+
* @return void
124+
*/
125+
public function testGenerateExistingValidCatalog($content, $dictionary)
103126
{
104127
$configFile = 'test';
105-
$fixtureXmlFile = __DIR__ . '/_files/valid_catalog.xml';
106-
$content = file_get_contents($fixtureXmlFile);
107128

108-
$file = $this->createMock(\Magento\Framework\Filesystem\File\Read::class);
109-
$file->expects($this->once())
129+
$fileMock = $this->createMock(Read::class);
130+
$fileMock->expects($this->once())
110131
->method('readAll')
111132
->withAnyParameters()
112133
->willReturn($content);
113134

114-
$this->fileWriteFactory->expects($this->at(0))
135+
$this->fileWriteFactoryMock->expects($this->at(0))
115136
->method('create')
116137
->with(
117138
$configFile,
118139
DriverPool::FILE,
119140
VsCode::FILE_MODE_READ
120141
)
121-
->willReturn($file);
142+
->willReturn($fileMock);
122143

123-
$file = $this->createMock(\Magento\Framework\Filesystem\File\Write::class);
124-
$file->expects($this->once())
144+
$fileMock = $this->createMock(Write::class);
145+
$fileMock->expects($this->once())
125146
->method('write')
126147
->with($content);
127148

128-
$this->fileWriteFactory->expects($this->at(1))
149+
$this->fileWriteFactoryMock->expects($this->at(1))
129150
->method('create')
130151
->with(
131152
$configFile,
132153
DriverPool::FILE,
133154
VsCode::FILE_MODE_WRITE
134155
)
135-
->willReturn($file);
156+
->willReturn($fileMock);
136157

137-
$this->vscodeFormat->generateCatalog($this->dictionary, $configFile);
158+
$this->vscodeFormat->generateCatalog($dictionary, $configFile);
138159
}
139160

140-
public function testGenerateExistingEmptyValidCatalog()
161+
/**
162+
* Test modify existing empty catalog
163+
*
164+
* @param string $content
165+
* @param array $dictionary
166+
* @dataProvider dictionaryDataProvider
167+
* @return void
168+
*/
169+
public function testGenerateExistingEmptyValidCatalog($content, $dictionary)
141170
{
142171
$configFile = 'test';
143-
$fixtureXmlFile = __DIR__ . '/_files/valid_catalog.xml';
144-
$content = file_get_contents($fixtureXmlFile);
145172

146-
$file = $this->createMock(\Magento\Framework\Filesystem\File\Read::class);
147-
$file->expects($this->once())
173+
$fileMock = $this->createMock(Read::class);
174+
$fileMock->expects($this->once())
148175
->method('readAll')
149176
->withAnyParameters()
150177
->willReturn('');
151178

152-
$this->fileWriteFactory->expects($this->at(0))
179+
$this->fileWriteFactoryMock->expects($this->at(0))
153180
->method('create')
154181
->with(
155182
$configFile,
156183
DriverPool::FILE,
157184
VsCode::FILE_MODE_READ
158185
)
159-
->willReturn($file);
186+
->willReturn($fileMock);
160187

161-
$file = $this->createMock(\Magento\Framework\Filesystem\File\Write::class);
162-
$file->expects($this->once())
188+
$fileMock = $this->createMock(Write::class);
189+
$fileMock->expects($this->once())
163190
->method('write')
164191
->with($content);
165192

166-
$this->fileWriteFactory->expects($this->at(1))
193+
$this->fileWriteFactoryMock->expects($this->at(1))
167194
->method('create')
168195
->with(
169196
$configFile,
170197
DriverPool::FILE,
171198
VsCode::FILE_MODE_WRITE
172199
)
173-
->willReturn($file);
200+
->willReturn($fileMock);
174201

175-
$this->vscodeFormat->generateCatalog($this->dictionary, $configFile);
202+
$this->vscodeFormat->generateCatalog($dictionary, $configFile);
176203
}
177204

178-
public function testGenerateExistingInvalidValidCatalog()
205+
/**
206+
* Test modify existing invalid catalog
207+
*
208+
* @param string $content
209+
* @param array $dictionary
210+
* @dataProvider dictionaryDataProvider
211+
* @return void
212+
*/
213+
public function testGenerateExistingInvalidValidCatalog($content, $dictionary, $invalidContent)
179214
{
180215
$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);
185216

186-
$file = $this->createMock(\Magento\Framework\Filesystem\File\Read::class);
187-
$file->expects($this->once())
217+
$fileMock = $this->createMock(Read::class);
218+
$fileMock->expects($this->once())
188219
->method('readAll')
189220
->withAnyParameters()
190221
->willReturn($invalidContent);
191222

192-
$this->fileWriteFactory->expects($this->at(0))
223+
$this->fileWriteFactoryMock->expects($this->at(0))
193224
->method('create')
194225
->with(
195226
$configFile,
196227
DriverPool::FILE,
197228
VsCode::FILE_MODE_READ
198229
)
199-
->willReturn($file);
230+
->willReturn($fileMock);
200231

201-
$file = $this->createMock(\Magento\Framework\Filesystem\File\Write::class);
202-
$file->expects($this->once())
232+
$fileMock = $this->createMock(Write::class);
233+
$fileMock->expects($this->once())
203234
->method('write')
204-
->with($validContent);
235+
->with($content);
205236

206-
$this->fileWriteFactory->expects($this->at(1))
237+
$this->fileWriteFactoryMock->expects($this->at(1))
207238
->method('create')
208239
->with(
209240
$configFile,
210241
DriverPool::FILE,
211242
VsCode::FILE_MODE_WRITE
212243
)
213-
->willReturn($file);
244+
->willReturn($fileMock);
245+
246+
$this->vscodeFormat->generateCatalog($dictionary, $configFile);
247+
}
248+
249+
/**
250+
* Data provider for test
251+
*
252+
* @return array
253+
*/
254+
public function dictionaryDataProvider()
255+
{
256+
$fixtureXmlFile = __DIR__ . '/_files/valid_catalog.xml';
257+
$content = file_get_contents($fixtureXmlFile);
258+
$invalidXmlFile = __DIR__ . '/_files/invalid_catalog.xml';
259+
$invalidContent = file_get_contents($invalidXmlFile);
214260

215-
$this->vscodeFormat->generateCatalog($this->dictionary, $configFile);
261+
return [
262+
[
263+
$content,
264+
['urn:magento:framework:Acl/etc/acl.xsd' => 'vendor/magento/framework/Acl/etc/acl.xsd',
265+
'urn:magento:module:Magento_Store:etc/config.xsd' => 'vendor/magento/module-store/etc/config.xsd',
266+
'urn:magento:module:Magento_Cron:etc/crontab.xsd' => 'vendor/magento/module-cron/etc/crontab.xsd',
267+
'urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd' => 'vendor/magento/framework/Setup/Declaration/Schema/etc/schema.xsd'],
268+
$invalidContent,
269+
],
270+
];
216271
}
272+
217273
}

0 commit comments

Comments
 (0)