|
4 | 4 | * See COPYING.txt for license details.
|
5 | 5 | */
|
6 | 6 |
|
7 |
| -// @codingStandardsIgnoreFile |
8 |
| - |
9 | 7 | namespace Magento\Framework\Less\Test\Unit;
|
10 | 8 |
|
11 | 9 | class FileGeneratorTest extends \PHPUnit_Framework_TestCase
|
@@ -41,118 +39,134 @@ class FileGeneratorTest extends \PHPUnit_Framework_TestCase
|
41 | 39 | private $object;
|
42 | 40 |
|
43 | 41 | /**
|
44 |
| - * @var \Magento\Framework\App\View\Asset\Publisher|\PHPUnit_Framework_MockObject_MockObject |
| 42 | + * @var \Magento\Framework\Less\FileGenerator\RelatedGenerator|\PHPUnit_Framework_MockObject_MockObject |
| 43 | + */ |
| 44 | + private $relatedGenerator; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var \Magento\Framework\Less\Config|\PHPUnit_Framework_MockObject_MockObject |
45 | 48 | */
|
46 |
| - private $publisher; |
| 49 | + private $config; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var \Magento\Framework\Less\File\Temporary|\PHPUnit_Framework_MockObject_MockObject |
| 53 | + */ |
| 54 | + private $temporaryFile; |
47 | 55 |
|
48 | 56 | protected function setUp()
|
49 | 57 | {
|
50 |
| - $this->tmpDirectory = $this->getMockForAbstractClass('\Magento\Framework\Filesystem\Directory\WriteInterface'); |
51 |
| - $this->rootDirectory = $this->getMockForAbstractClass('\Magento\Framework\Filesystem\Directory\ReadInterface'); |
| 58 | + $this->tmpDirectory = $this->getMockForAbstractClass('Magento\Framework\Filesystem\Directory\WriteInterface'); |
| 59 | + $this->rootDirectory = $this->getMockForAbstractClass('Magento\Framework\Filesystem\Directory\ReadInterface'); |
52 | 60 | $this->rootDirectory->expects($this->any())
|
53 | 61 | ->method('getRelativePath')
|
54 | 62 | ->will($this->returnArgument(0));
|
55 | 63 | $this->rootDirectory->expects($this->any())
|
56 | 64 | ->method('readFile')
|
57 |
| - ->will($this->returnCallback(function ($file) { |
58 |
| - return "content of '$file'"; |
59 |
| - })); |
| 65 | + ->will( |
| 66 | + $this->returnCallback( |
| 67 | + function ($file) { |
| 68 | + return "content of '$file'"; |
| 69 | + } |
| 70 | + ) |
| 71 | + ); |
60 | 72 | $filesystem = $this->getMock('\Magento\Framework\Filesystem', [], [], '', false);
|
61 |
| - $filesystem->expects($this->exactly(2)) |
| 73 | + $filesystem->expects($this->once()) |
62 | 74 | ->method('getDirectoryWrite')
|
63 |
| - //->with(DirectoryList::VAR_DIR) |
64 | 75 | ->will($this->returnValue($this->tmpDirectory));
|
65 | 76 | $this->assetRepo = $this->getMock('\Magento\Framework\View\Asset\Repository', [], [], '', false);
|
66 | 77 | $this->magentoImport = $this->getMock(
|
67 |
| - '\Magento\Framework\Less\PreProcessor\Instruction\MagentoImport', [], [], '', false |
| 78 | + 'Magento\Framework\Less\PreProcessor\Instruction\MagentoImport', |
| 79 | + [], |
| 80 | + [], |
| 81 | + '', |
| 82 | + false |
68 | 83 | );
|
69 | 84 | $this->import = $this->getMock(
|
70 |
| - '\Magento\Framework\Less\PreProcessor\Instruction\Import', [], [], '', false |
| 85 | + 'Magento\Framework\Less\PreProcessor\Instruction\Import', |
| 86 | + [], |
| 87 | + [], |
| 88 | + '', |
| 89 | + false |
71 | 90 | );
|
72 | 91 |
|
73 | 92 | $assetSource = $this->getMock(
|
74 |
| - 'Magento\Framework\View\Asset\Source', [], [], '', false |
| 93 | + 'Magento\Framework\View\Asset\Source', |
| 94 | + [], |
| 95 | + [], |
| 96 | + '', |
| 97 | + false |
75 | 98 | );
|
76 | 99 |
|
77 |
| - $this->publisher = $this->getMock('Magento\Framework\App\View\Asset\Publisher', [], [], '', false); |
78 |
| - |
| 100 | + $this->relatedGenerator = $this->getMockBuilder('Magento\Framework\Less\FileGenerator\RelatedGenerator') |
| 101 | + ->disableOriginalConstructor() |
| 102 | + ->setMethods([]) |
| 103 | + ->getMock(); |
| 104 | + $this->config = $this->getMockBuilder('Magento\Framework\Less\Config') |
| 105 | + ->disableOriginalConstructor() |
| 106 | + ->setMethods([]) |
| 107 | + ->getMock(); |
| 108 | + $this->temporaryFile = $this->getMockBuilder('Magento\Framework\Less\File\Temporary') |
| 109 | + ->disableOriginalConstructor() |
| 110 | + ->setMethods([]) |
| 111 | + ->getMock(); |
79 | 112 | $this->object = new \Magento\Framework\Less\FileGenerator(
|
80 |
| - $filesystem, $this->assetRepo, $this->magentoImport, $this->import, $assetSource, $this->publisher |
| 113 | + $filesystem, |
| 114 | + $this->assetRepo, |
| 115 | + $this->magentoImport, |
| 116 | + $this->import, |
| 117 | + $assetSource, |
| 118 | + $this->relatedGenerator, |
| 119 | + $this->config, |
| 120 | + $this->temporaryFile |
81 | 121 | );
|
82 | 122 | }
|
83 | 123 |
|
84 | 124 | public function testGenerateLessFileTree()
|
85 | 125 | {
|
86 |
| - $originalContent = 'original content'; |
| 126 | + $lessDirectory = 'path/to/less'; |
87 | 127 | $expectedContent = 'updated content';
|
88 |
| - $expectedRelativePath = 'view_preprocessed/less/some/file.less'; |
89 |
| - $expectedPath = '/var/view_preprocessed/less/some/file.less'; |
| 128 | + $expectedRelativePath = 'some/file.less'; |
| 129 | + $expectedPath = $lessDirectory . '/some/file.less'; |
90 | 130 |
|
91 |
| - $asset = $this->getMock('\Magento\Framework\View\Asset\File', [], [], '', false); |
92 |
| - $asset->expects($this->exactly(2)) |
93 |
| - ->method('getPath') |
94 |
| - ->will($this->returnValue('some/file.css')); |
95 |
| - $chain = new \Magento\Framework\View\Asset\PreProcessor\Chain($asset, $originalContent, 'less'); |
| 131 | + |
| 132 | + $asset = $this->getMock('Magento\Framework\View\Asset\File', [], [], '', false); |
| 133 | + $chain = $this->getMock('Magento\Framework\View\Asset\PreProcessor\Chain', [], [], '', false); |
| 134 | + |
| 135 | + $this->config->expects($this->any()) |
| 136 | + ->method('getLessDirectory') |
| 137 | + ->willReturn($lessDirectory); |
| 138 | + $this->tmpDirectory->expects($this->once()) |
| 139 | + ->method('isExist') |
| 140 | + ->willReturn(true); |
96 | 141 |
|
97 | 142 | $this->magentoImport->expects($this->once())
|
98 | 143 | ->method('process')
|
99 | 144 | ->with($chain);
|
100 | 145 | $this->import->expects($this->once())
|
101 | 146 | ->method('process')
|
102 | 147 | ->with($chain);
|
| 148 | + $this->relatedGenerator->expects($this->once()) |
| 149 | + ->method('generate') |
| 150 | + ->with($this->import); |
103 | 151 |
|
104 |
| - $relatedAssetOne = $this->getMock('\Magento\Framework\View\Asset\File', [], [], '', false); |
105 |
| - $relatedAssetOne->expects($this->any()) |
| 152 | + $asset->expects($this->once()) |
106 | 153 | ->method('getPath')
|
107 |
| - ->will($this->returnValue('related/file_one.css')); |
108 |
| - $relatedAssetOne->expects($this->any()) |
109 |
| - ->method('getContent') |
110 |
| - ->will($this->returnValue("content of 'related/file_one.css'")); |
111 |
| - $relatedAssetTwo = $this->getMock('\Magento\Framework\View\Asset\File', [], [], '', false); |
112 |
| - $relatedAssetTwo->expects($this->any()) |
113 |
| - ->method('getPath') |
114 |
| - ->will($this->returnValue('related/file_two.css')); |
115 |
| - $relatedAssetTwo->expects($this->any()) |
| 154 | + ->will($this->returnValue('some/file.css')); |
| 155 | + $chain->expects($this->once()) |
116 | 156 | ->method('getContent')
|
117 |
| - ->will($this->returnValue("content of 'related/file_two.css'")); |
118 |
| - $assetsMap = [ |
119 |
| - ['related/file_one.css', $asset, $relatedAssetOne], |
120 |
| - ['related/file_two.css', $asset, $relatedAssetTwo], |
121 |
| - ]; |
122 |
| - $this->assetRepo->expects($this->any()) |
123 |
| - ->method('createRelated') |
124 |
| - ->will($this->returnValueMap($assetsMap)); |
125 |
| - |
126 |
| - $relatedFilesOne = [['related/file_one.css', $asset]]; |
127 |
| - $this->import->expects($this->at(1)) |
128 |
| - ->method('getRelatedFiles') |
129 |
| - ->will($this->returnValue($relatedFilesOne)); |
130 |
| - $relatedFilesTwo = [['related/file_two.css', $asset]]; |
131 |
| - $this->import->expects($this->at(3)) |
132 |
| - ->method('getRelatedFiles') |
133 |
| - ->will($this->returnValue($relatedFilesTwo)); |
134 |
| - $this->import->expects($this->at(5)) |
135 |
| - ->method('getRelatedFiles') |
136 |
| - ->will($this->returnValue([])); |
137 |
| - |
138 |
| - $writeMap = [ |
139 |
| - [$expectedRelativePath, $expectedContent], |
140 |
| - ['related/file_one.css', "content of 'related/file_one.css'"], |
141 |
| - ['related/file_two.css', "content of 'related/file_two.css'"], |
142 |
| - ]; |
143 |
| - $pathsMap = [ |
144 |
| - [$expectedRelativePath, $expectedPath], |
145 |
| - ['related/file_one.css', '/var/view_preprocessed/less/related/file_one.css'], |
146 |
| - ['related/file_two.css', '/var/view_preprocessed/less/related/file_two.css'], |
147 |
| - ]; |
148 |
| - $this->tmpDirectory->expects($this->any()) |
149 |
| - ->method('writeFile') |
150 |
| - ->will($this->returnValueMap($writeMap)); |
151 |
| - $this->tmpDirectory->expects($this->any()) |
152 |
| - ->method('getAbsolutePath') |
153 |
| - ->will($this->returnValueMap($pathsMap)); |
154 |
| - |
155 |
| - $actual = $this->object->generateFileTree($chain); |
156 |
| - $this->assertSame($expectedPath, $actual); |
| 157 | + ->willReturn($expectedContent); |
| 158 | + $chain->expects($this->once()) |
| 159 | + ->method('getAsset') |
| 160 | + ->willReturn($asset); |
| 161 | + |
| 162 | + $this->temporaryFile->expects($this->once()) |
| 163 | + ->method('createFile') |
| 164 | + ->with( |
| 165 | + $expectedRelativePath, |
| 166 | + $expectedContent |
| 167 | + ) |
| 168 | + ->willReturn($expectedPath); |
| 169 | + |
| 170 | + $this->assertSame($expectedPath, $this->object->generateFileTree($chain)); |
157 | 171 | }
|
158 | 172 | }
|
0 commit comments