|
| 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 | +} |
0 commit comments