1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Developer\Test\Unit\Console\Command;
use Magento\Framework\Validator\Locale;
use Magento\Framework\View\Asset\Repository;
use Magento\Framework\App\View\Asset\Publisher;
use Magento\Framework\View\Asset\LocalInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Magento\Developer\Console\Command\SourceThemeDeployCommand;
/**
* Class SourceThemeDeployCommandTest
*
* @see \Magento\Developer\Console\Command\SourceThemeDeployCommand
*/
class SourceThemeDeployCommandTest extends \PHPUnit\Framework\TestCase
{
const AREA_TEST_VALUE = 'area-test-value';
const LOCALE_TEST_VALUE = 'locale-test-value';
const THEME_TEST_VALUE = 'Vendor/theme';
const THEME_INCORRECT_FORMAT_VALUE = 'theme-value';
const THEME_NONEXISTING_VALUE = 'NonExistentVendor/theme';
const TYPE_TEST_VALUE = 'type-test-value';
const FILE_TEST_VALUE = 'file-test-value/test/file';
/**
* @var SourceThemeDeployCommand
*/
private $sourceThemeDeployCommand;
/**
* @var Locale|\PHPUnit_Framework_MockObject_MockObject
*/
private $validatorMock;
/**
* @var Publisher|\PHPUnit_Framework_MockObject_MockObject
*/
private $assetPublisherMock;
/**
* @var Repository|\PHPUnit_Framework_MockObject_MockObject
*/
private $assetRepositoryMock;
/**
* Set up
*/
protected function setUp()
{
$this->validatorMock = $this->getMockBuilder(Locale::class)
->disableOriginalConstructor()
->getMock();
$this->assetPublisherMock = $this->getMockBuilder(Publisher::class)
->disableOriginalConstructor()
->getMock();
$this->assetRepositoryMock = $this->getMockBuilder(Repository::class)
->disableOriginalConstructor()
->getMock();
$this->sourceThemeDeployCommand = new SourceThemeDeployCommand(
$this->validatorMock,
$this->assetPublisherMock,
$this->assetRepositoryMock
);
}
/**
* Run test for execute method
*/
public function testExecute()
{
/** @var OutputInterface|\PHPUnit_Framework_MockObject_MockObject $outputMock */
$outputMock = $this->getMockBuilder(OutputInterface::class)
->getMockForAbstractClass();
$assetMock = $this->getMockBuilder(LocalInterface::class)
->getMockForAbstractClass();
$this->validatorMock->expects(self::once())
->method('isValid')
->with(self::LOCALE_TEST_VALUE)
->willReturn(true);
$message = sprintf(
'<info>Processed Area: %s, Locale: %s, Theme: %s, File type: %s.</info>',
self::AREA_TEST_VALUE,
self::LOCALE_TEST_VALUE,
self::THEME_TEST_VALUE,
self::TYPE_TEST_VALUE
);
$outputMock->expects(self::at(0))
->method('writeln')
->with($message);
$outputMock->expects(self::at(1))
->method('writeln')
->with('<comment>-> file-test-value/test/file</comment>');
$outputMock->expects(self::at(2))
->method('writeln')
->with('<info>Successfully processed.</info>');
$this->assetRepositoryMock->expects(self::once())
->method('createAsset')
->with(
'file-test-value/test' . DIRECTORY_SEPARATOR . 'file' . '.' . self::TYPE_TEST_VALUE,
[
'area' => self::AREA_TEST_VALUE,
'theme' => self::THEME_TEST_VALUE,
'locale' => self::LOCALE_TEST_VALUE,
]
)->willReturn($assetMock);
$this->assetPublisherMock->expects(self::once())
->method('publish')
->with($assetMock);
$assetMock->expects(self::once())
->method('getFilePath')
->willReturn(self::FILE_TEST_VALUE);
$this->sourceThemeDeployCommand->run($this->getInputMock(), $outputMock);
}
/**
* Run test for execute method with incorrect theme value
*
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Value "theme-value" of the option "theme" has invalid format. The format should be
*/
public function testExecuteIncorrectThemeFormat()
{
/** @var OutputInterface|\PHPUnit_Framework_MockObject_MockObject $outputMock */
$outputMock = $this->getMockBuilder(OutputInterface::class)
->getMockForAbstractClass();
$this->validatorMock->expects(self::once())
->method('isValid')
->with(self::LOCALE_TEST_VALUE)
->willReturn(true);
$valueMap = [
['area', self::AREA_TEST_VALUE],
['locale', self::LOCALE_TEST_VALUE],
['theme', self::THEME_INCORRECT_FORMAT_VALUE],
['type', self::TYPE_TEST_VALUE]
];
$this->sourceThemeDeployCommand->run(
$this->getInputMock($valueMap),
$outputMock
);
}
/**
* Run test for execute method with non existing theme
*
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Verify entered values of the argument and options.
*/
public function testExecuteNonExistingValue()
{
/** @var OutputInterface|\PHPUnit_Framework_MockObject_MockObject $outputMock */
$outputMock = $this->getMockBuilder(OutputInterface::class)
->getMockForAbstractClass();
$assetMock = $this->getMockBuilder(LocalInterface::class)
->getMockForAbstractClass();
$this->validatorMock->expects(self::once())
->method('isValid')
->with(self::LOCALE_TEST_VALUE)
->willReturn(true);
$this->assetRepositoryMock->expects(self::once())
->method('createAsset')
->with(
'file-test-value/test' . DIRECTORY_SEPARATOR . 'file' . '.' . self::TYPE_TEST_VALUE,
[
'area' => self::AREA_TEST_VALUE,
'theme' => self::THEME_NONEXISTING_VALUE,
'locale' => self::LOCALE_TEST_VALUE,
]
)->willReturn($assetMock);
$this->assetPublisherMock->expects(self::once())
->method('publish')
->with($assetMock)
->willThrowException(new \Magento\Framework\View\Asset\File\NotFoundException);
$valueMap = [
['area', self::AREA_TEST_VALUE],
['locale', self::LOCALE_TEST_VALUE],
['theme', self::THEME_NONEXISTING_VALUE],
['type', self::TYPE_TEST_VALUE]
];
$this->sourceThemeDeployCommand->run(
$this->getInputMock($valueMap),
$outputMock
);
}
/**
* @return InputInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private function getInputMock(array $valueMap = [])
{
$inputMock = $this->getMockBuilder(InputInterface::class)
->getMockForAbstractClass();
$defaultValueMap = [
['area', self::AREA_TEST_VALUE],
['locale', self::LOCALE_TEST_VALUE],
['theme', self::THEME_TEST_VALUE],
['type', self::TYPE_TEST_VALUE]
];
$valueMap = empty($valueMap) ? $defaultValueMap : $valueMap;
$inputMock->expects(self::exactly(4))
->method('getOption')
->willReturnMap(
$valueMap
);
$inputMock->expects(self::once())
->method('getArgument')
->with('file')
->willReturn([self::FILE_TEST_VALUE]);
return $inputMock;
}
}