-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Copy pathConfigShowCommandTest.php
269 lines (238 loc) · 8.35 KB
/
ConfigShowCommandTest.php
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<?php
/**
* Copyright 2017 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);
namespace Magento\Config\Test\Unit\Console\Command;
use Magento\Config\Console\Command\ConfigShow\ValueProcessor;
use Magento\Config\Console\Command\ConfigShowCommand;
use Magento\Config\Console\Command\EmulatedAdminhtmlAreaProcessor;
use Magento\Config\Console\Command\LocaleEmulatorInterface;
use Magento\Config\Model\Config\PathValidator;
use Magento\Config\Model\Config\PathValidatorFactory;
use Magento\Framework\App\Config\ConfigPathResolver;
use Magento\Framework\App\Config\ConfigSourceInterface;
use Magento\Framework\App\Scope\ValidatorInterface;
use Magento\Framework\Console\Cli;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Tester\CommandTester;
/**
* Test for \Magento\Config\Console\Command\ConfigShowCommand.
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class ConfigShowCommandTest extends TestCase
{
private const CONFIG_PATH = 'some/config/path';
private const SCOPE = 'some/config/path';
private const SCOPE_CODE = 'someScopeCode';
/**
* @var ConfigShowCommand
*/
private $model;
/**
* @var ValidatorInterface|MockObject
*/
private $scopeValidatorMock;
/**
* @var ConfigSourceInterface|MockObject
*/
private $configSourceMock;
/**
* @var ValueProcessor|MockObject
*/
private $valueProcessorMock;
/**
* @var ConfigPathResolver|MockObject
*/
private $pathResolverMock;
/**
* @var EmulatedAdminhtmlAreaProcessor|MockObject
*/
private $emulatedAreProcessorMock;
/**
* @var PathValidator|MockObject
*/
private $pathValidatorMock;
/**
* @var LocaleEmulatorInterface|MockObject
*/
private $localeEmulatorMock;
/**
* @inheritdoc
*/
protected function setUp(): void
{
$objectManager = new ObjectManager($this);
$this->valueProcessorMock = $this->getMockBuilder(ValueProcessor::class)
->disableOriginalConstructor()
->getMock();
$this->pathResolverMock = $this->getMockBuilder(ConfigPathResolver::class)
->disableOriginalConstructor()
->getMock();
$this->scopeValidatorMock = $this->getMockBuilder(ValidatorInterface::class)
->getMockForAbstractClass();
$this->configSourceMock = $this->getMockBuilder(ConfigSourceInterface::class)
->getMockForAbstractClass();
$this->pathValidatorMock = $this->getMockBuilder(PathValidator::class)
->disableOriginalConstructor()
->getMock();
$pathValidatorFactoryMock = $this->getMockBuilder(PathValidatorFactory::class)
->disableOriginalConstructor()
->getMock();
$pathValidatorFactoryMock->expects($this->atMost(1))
->method('create')
->willReturn($this->pathValidatorMock);
$this->emulatedAreProcessorMock = $this->getMockBuilder(EmulatedAdminhtmlAreaProcessor::class)
->disableOriginalConstructor()
->getMock();
$this->localeEmulatorMock = $this->getMockBuilder(LocaleEmulatorInterface::class)
->getMockForAbstractClass();
$this->model = $objectManager->getObject(
ConfigShowCommand::class,
[
'scopeValidator' => $this->scopeValidatorMock,
'configSource' => $this->configSourceMock,
'pathResolver' => $this->pathResolverMock,
'valueProcessor' => $this->valueProcessorMock,
'pathValidatorFactory' => $pathValidatorFactoryMock,
'emulatedAreaProcessor' => $this->emulatedAreProcessorMock,
'localeEmulator' => $this->localeEmulatorMock
]
);
}
/**
* Test get config value
*
* @return void
*/
public function testExecute(): void
{
$resolvedConfigPath = 'someScope/someScopeCode/some/config/path';
$this->scopeValidatorMock->expects($this->once())
->method('isValid')
->with(self::SCOPE, self::SCOPE_CODE)
->willReturn(true);
$this->pathResolverMock->expects($this->once())
->method('resolve')
->with(self::CONFIG_PATH, self::SCOPE, self::SCOPE_CODE)
->willReturn($resolvedConfigPath);
$this->configSourceMock->expects($this->once())
->method('get')
->with($resolvedConfigPath)
->willReturn('someValue');
$this->valueProcessorMock->expects($this->once())
->method('process')
->with(self::SCOPE, self::SCOPE_CODE, 'someValue', self::CONFIG_PATH)
->willReturn('someProcessedValue');
$this->emulatedAreProcessorMock->expects($this->once())
->method('process')
->willReturnCallback(function ($function) {
return $function();
});
$this->localeEmulatorMock->expects($this->once())
->method('emulate')
->willReturnCallback(function ($callback) {
return $callback();
});
$tester = $this->getConfigShowCommandTester(
self::CONFIG_PATH,
self::SCOPE,
self::SCOPE_CODE
);
$this->assertEquals(
Cli::RETURN_SUCCESS,
$tester->getStatusCode()
);
$this->assertStringContainsString(
'someProcessedValue',
$tester->getDisplay()
);
}
/**
* Test not valid scope or scope code
*
* @return void
*/
public function testNotValidScopeOrScopeCode(): void
{
$this->scopeValidatorMock->expects($this->once())
->method('isValid')
->with(self::SCOPE, self::SCOPE_CODE)
->willThrowException(new LocalizedException(__('error message')));
$this->emulatedAreProcessorMock->expects($this->once())
->method('process')
->willReturnCallback(function ($function) {
return $function();
});
$this->localeEmulatorMock->expects($this->once())
->method('emulate')
->willReturnCallback(function ($function) {
return $function();
});
$tester = $this->getConfigShowCommandTester(
self::CONFIG_PATH,
self::SCOPE,
self::SCOPE_CODE
);
$this->assertEquals(
Cli::RETURN_FAILURE,
$tester->getStatusCode()
);
$this->assertStringContainsString(
__('error message')->render(),
$tester->getDisplay()
);
}
/**
* Test get config value for not existed path.
*
* @return void
*/
public function testConfigPathNotExist(): void
{
$this->emulatedAreProcessorMock->expects($this->once())
->method('process')
->willReturnCallback(function ($function) {
return $function();
});
$this->localeEmulatorMock->expects($this->once())
->method('emulate')
->willReturnCallback(function ($function) {
return $function();
});
$tester = $this->getConfigShowCommandTester(self::CONFIG_PATH);
$this->assertEquals(
Cli::RETURN_FAILURE,
$tester->getStatusCode()
);
$this->assertStringContainsString(
__('The "%1" path doesn\'t exist. Verify and try again.', self::CONFIG_PATH)->render(),
$tester->getDisplay()
);
}
/**
* @param string $configPath
* @param null|string $scope
* @param null|string $scopeCode
* @return CommandTester
*/
private function getConfigShowCommandTester($configPath, $scope = null, $scopeCode = null)
{
$arguments = [
ConfigShowCommand::INPUT_ARGUMENT_PATH => $configPath
];
if ($scope !== null) {
$arguments['--' . ConfigShowCommand::INPUT_OPTION_SCOPE] = $scope;
}
if ($scopeCode !== null) {
$arguments['--' . ConfigShowCommand::INPUT_OPTION_SCOPE_CODE] = $scopeCode;
}
$tester = new CommandTester($this->model);
$tester->execute($arguments);
return $tester;
}
}