3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+
6
7
declare (strict_types=1 );
7
8
8
9
namespace Magento \Config \Test \Unit \Console \Command ;
9
10
10
11
use Magento \Config \Console \Command \ConfigShow \ValueProcessor ;
11
12
use Magento \Config \Console \Command \ConfigShowCommand ;
13
+ use Magento \Config \Console \Command \EmulatedAdminhtmlAreaProcessor ;
12
14
use Magento \Framework \App \Config \ConfigPathResolver ;
13
15
use Magento \Framework \App \Config \ConfigSourceInterface ;
14
16
use Magento \Framework \App \Scope \ValidatorInterface ;
15
17
use Magento \Framework \Console \Cli ;
16
18
use Magento \Framework \Exception \LocalizedException ;
19
+ use Magento \Config \Model \Config \PathValidatorFactory ;
20
+ use Magento \Config \Model \Config \PathValidator ;
21
+ use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
17
22
use PHPUnit \Framework \MockObject \MockObject ;
18
23
use PHPUnit \Framework \TestCase ;
19
24
use Symfony \Component \Console \Tester \CommandTester ;
20
25
26
+ /**
27
+ * Test for \Magento\Config\Console\Command\ConfigShowCommand.
28
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
29
+ */
21
30
class ConfigShowCommandTest extends TestCase
22
31
{
32
+ private const CONFIG_PATH = 'some/config/path ' ;
33
+ private const SCOPE = 'some/config/path ' ;
34
+ private const SCOPE_CODE = 'someScopeCode ' ;
35
+
36
+ /**
37
+ * @var ConfigShowCommand
38
+ */
39
+ private $ model ;
40
+
23
41
/**
24
42
* @var ValidatorInterface|MockObject
25
43
*/
@@ -41,12 +59,22 @@ class ConfigShowCommandTest extends TestCase
41
59
private $ pathResolverMock ;
42
60
43
61
/**
44
- * @var ConfigShowCommand
62
+ * @var EmulatedAdminhtmlAreaProcessor|MockObject
63
+ */
64
+ private $ emulatedAreProcessorMock ;
65
+
66
+ /**
67
+ * @var PathValidator|MockObject
45
68
*/
46
- private $ command ;
69
+ private $ pathValidatorMock ;
47
70
71
+ /**
72
+ * @inheritdoc
73
+ */
48
74
protected function setUp (): void
49
75
{
76
+ $ objectManager = new ObjectManager ($ this );
77
+
50
78
$ this ->valueProcessorMock = $ this ->getMockBuilder (ValueProcessor::class)
51
79
->disableOriginalConstructor ()
52
80
->getMock ();
@@ -57,40 +85,69 @@ protected function setUp(): void
57
85
->getMockForAbstractClass ();
58
86
$ this ->configSourceMock = $ this ->getMockBuilder (ConfigSourceInterface::class)
59
87
->getMockForAbstractClass ();
88
+ $ this ->pathValidatorMock = $ this ->getMockBuilder (PathValidator::class)
89
+ ->disableOriginalConstructor ()
90
+ ->getMock ();
91
+ $ pathValidatorFactoryMock = $ this ->getMockBuilder (PathValidatorFactory::class)
92
+ ->disableOriginalConstructor ()
93
+ ->getMock ();
94
+ $ pathValidatorFactoryMock ->expects ($ this ->atMost (1 ))
95
+ ->method ('create ' )
96
+ ->willReturn ($ this ->pathValidatorMock );
60
97
61
- $ this ->command = new ConfigShowCommand (
62
- $ this ->scopeValidatorMock ,
63
- $ this ->configSourceMock ,
64
- $ this ->pathResolverMock ,
65
- $ this ->valueProcessorMock
98
+ $ this ->emulatedAreProcessorMock = $ this ->getMockBuilder (EmulatedAdminhtmlAreaProcessor::class)
99
+ ->disableOriginalConstructor ()
100
+ ->getMock ();
101
+
102
+ $ this ->model = $ objectManager ->getObject (
103
+ ConfigShowCommand::class,
104
+ [
105
+ 'scopeValidator ' => $ this ->scopeValidatorMock ,
106
+ 'configSource ' => $ this ->configSourceMock ,
107
+ 'pathResolver ' => $ this ->pathResolverMock ,
108
+ 'valueProcessor ' => $ this ->valueProcessorMock ,
109
+ 'pathValidatorFactory ' => $ pathValidatorFactoryMock ,
110
+ 'emulatedAreaProcessor ' => $ this ->emulatedAreProcessorMock ,
111
+ ]
66
112
);
67
113
}
68
114
69
- public function testExecute ()
115
+ /**
116
+ * Test get config value
117
+ *
118
+ * @return void
119
+ */
120
+ public function testExecute (): void
70
121
{
71
- $ configPath = 'some/config/path ' ;
72
122
$ resolvedConfigPath = 'someScope/someScopeCode/some/config/path ' ;
73
- $ scope = 'someScope ' ;
74
- $ scopeCode = 'someScopeCode ' ;
75
123
76
124
$ this ->scopeValidatorMock ->expects ($ this ->once ())
77
125
->method ('isValid ' )
78
- ->with ($ scope , $ scopeCode )
126
+ ->with (self :: SCOPE , self :: SCOPE_CODE )
79
127
->willReturn (true );
80
128
$ this ->pathResolverMock ->expects ($ this ->once ())
81
129
->method ('resolve ' )
82
- ->with ($ configPath , $ scope , $ scopeCode )
130
+ ->with (self :: CONFIG_PATH , self :: SCOPE , self :: SCOPE_CODE )
83
131
->willReturn ($ resolvedConfigPath );
84
132
$ this ->configSourceMock ->expects ($ this ->once ())
85
133
->method ('get ' )
86
134
->with ($ resolvedConfigPath )
87
135
->willReturn ('someValue ' );
88
136
$ this ->valueProcessorMock ->expects ($ this ->once ())
89
137
->method ('process ' )
90
- ->with ($ scope , $ scopeCode , 'someValue ' , $ configPath )
138
+ ->with (self :: SCOPE , self :: SCOPE_CODE , 'someValue ' , self :: CONFIG_PATH )
91
139
->willReturn ('someProcessedValue ' );
92
-
93
- $ tester = $ this ->getConfigShowCommandTester ($ configPath , $ scope , $ scopeCode );
140
+ $ this ->emulatedAreProcessorMock ->expects ($ this ->once ())
141
+ ->method ('process ' )
142
+ ->willReturnCallback (function ($ function ) {
143
+ return $ function ();
144
+ });
145
+
146
+ $ tester = $ this ->getConfigShowCommandTester (
147
+ self ::CONFIG_PATH ,
148
+ self ::SCOPE ,
149
+ self ::SCOPE_CODE
150
+ );
94
151
95
152
$ this ->assertEquals (
96
153
Cli::RETURN_SUCCESS ,
@@ -102,18 +159,28 @@ public function testExecute()
102
159
);
103
160
}
104
161
105
- public function testNotValidScopeOrScopeCode ()
162
+ /**
163
+ * Test not valid scope or scope code
164
+ *
165
+ * @return void
166
+ */
167
+ public function testNotValidScopeOrScopeCode (): void
106
168
{
107
- $ configPath = 'some/config/path ' ;
108
- $ scope = 'someScope ' ;
109
- $ scopeCode = 'someScopeCode ' ;
110
-
111
169
$ this ->scopeValidatorMock ->expects ($ this ->once ())
112
170
->method ('isValid ' )
113
- ->with ($ scope , $ scopeCode )
171
+ ->with (self :: SCOPE , self :: SCOPE_CODE )
114
172
->willThrowException (new LocalizedException (__ ('error message ' )));
115
-
116
- $ tester = $ this ->getConfigShowCommandTester ($ configPath , $ scope , $ scopeCode );
173
+ $ this ->emulatedAreProcessorMock ->expects ($ this ->once ())
174
+ ->method ('process ' )
175
+ ->willReturnCallback (function ($ function ) {
176
+ return $ function ();
177
+ });
178
+
179
+ $ tester = $ this ->getConfigShowCommandTester (
180
+ self ::CONFIG_PATH ,
181
+ self ::SCOPE ,
182
+ self ::SCOPE_CODE
183
+ );
117
184
118
185
$ this ->assertEquals (
119
186
Cli::RETURN_FAILURE ,
@@ -125,17 +192,35 @@ public function testNotValidScopeOrScopeCode()
125
192
);
126
193
}
127
194
128
- public function testConfigPathNotExist ()
195
+ /**
196
+ * Test get config value for not existed path.
197
+ *
198
+ * @return void
199
+ */
200
+ public function testConfigPathNotExist (): void
129
201
{
130
- $ configPath = 'some/path ' ;
131
- $ tester = $ this ->getConfigShowCommandTester ($ configPath );
202
+ $ exception = new LocalizedException (
203
+ __ ('The "%1" path doesn \'t exist. Verify and try again. ' , self ::CONFIG_PATH )
204
+ );
205
+
206
+ $ this ->pathValidatorMock ->expects ($ this ->once ())
207
+ ->method ('validate ' )
208
+ ->with (self ::CONFIG_PATH )
209
+ ->willThrowException ($ exception );
210
+ $ this ->emulatedAreProcessorMock ->expects ($ this ->once ())
211
+ ->method ('process ' )
212
+ ->willReturnCallback (function ($ function ) {
213
+ return $ function ();
214
+ });
215
+
216
+ $ tester = $ this ->getConfigShowCommandTester (self ::CONFIG_PATH );
132
217
133
218
$ this ->assertEquals (
134
219
Cli::RETURN_FAILURE ,
135
220
$ tester ->getStatusCode ()
136
221
);
137
222
$ this ->assertStringContainsString (
138
- __ ('Configuration for path: "%1" doesn \'t exist ' , $ configPath )->render (),
223
+ __ ('The "%1" path doesn \'t exist. Verify and try again. ' , self :: CONFIG_PATH )->render (),
139
224
$ tester ->getDisplay ()
140
225
);
141
226
}
@@ -159,7 +244,7 @@ private function getConfigShowCommandTester($configPath, $scope = null, $scopeCo
159
244
$ arguments ['-- ' . ConfigShowCommand::INPUT_OPTION_SCOPE_CODE ] = $ scopeCode ;
160
245
}
161
246
162
- $ tester = new CommandTester ($ this ->command );
247
+ $ tester = new CommandTester ($ this ->model );
163
248
$ tester ->execute ($ arguments );
164
249
165
250
return $ tester ;
0 commit comments