-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathInspectorCommandTest.php
More file actions
312 lines (256 loc) · 12.2 KB
/
Copy pathInspectorCommandTest.php
File metadata and controls
312 lines (256 loc) · 12.2 KB
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<?php
declare(strict_types=1);
namespace OpenForgeProject\MageForge\Test\Unit\Console\Command\Dev;
use Magento\Framework\App\Cache\Manager as CacheManager;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Config\Storage\WriterInterface;
use Magento\Framework\App\State;
use Magento\Framework\Console\Cli;
use OpenForgeProject\MageForge\Console\Command\Dev\InspectorCommand;
use OpenForgeProject\MageForge\Model\Config\Inspector as InspectorConfig;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Tester\CommandTester;
class InspectorCommandTest extends TestCase
{
/**
* @var WriterInterface&MockObject
*/
private $configWriter;
/**
* @var State&MockObject
*/
private $state;
/**
* @var CacheManager&MockObject
*/
private $cacheManager;
/**
* @var ScopeConfigInterface&MockObject
*/
private $scopeConfig;
/**
* @var InspectorCommand
*/
private InspectorCommand $command;
protected function setUp(): void
{
$this->configWriter = $this->createMock(WriterInterface::class);
$this->state = $this->createMock(State::class);
$this->cacheManager = $this->createMock(CacheManager::class);
$this->scopeConfig = $this->createMock(ScopeConfigInterface::class);
$this->command = new InspectorCommand(
$this->configWriter,
$this->state,
$this->cacheManager,
$this->scopeConfig,
);
}
public function testRejectsInvalidAction(): void
{
$tester = new CommandTester($this->command);
$exitCode = $tester->execute(['action' => 'bogus']);
$this->assertSame(Cli::RETURN_FAILURE, $exitCode);
$this->assertStringContainsString('Invalid action', $tester->getDisplay());
}
public function testEnableFailsOutsideDeveloperMode(): void
{
$this->state->method('getMode')->willReturn(State::MODE_PRODUCTION);
$this->configWriter->expects($this->never())->method('save');
$tester = new CommandTester($this->command);
$exitCode = $tester->execute(['action' => 'enable']);
$this->assertSame(Cli::RETURN_FAILURE, $exitCode);
$this->assertStringContainsString('developer mode', $tester->getDisplay());
}
public function testEnableSavesConfigAndCleansCacheInDeveloperMode(): void
{
$this->state->method('getMode')->willReturn(State::MODE_DEVELOPER);
$this->configWriter->expects($this->once())
->method('save')
->with(InspectorConfig::XML_PATH_ENABLED, '1');
$this->cacheManager->expects($this->once())
->method('clean')
->with(['config', 'layout', 'full_page', 'block_html']);
$tester = new CommandTester($this->command);
$exitCode = $tester->execute(['action' => 'ENABLE']);
$this->assertSame(Cli::RETURN_SUCCESS, $exitCode);
$this->assertStringContainsString('has been enabled', $tester->getDisplay());
}
public function testDisableSavesConfigAndCleansCache(): void
{
$this->state->method('getMode')->willReturn(State::MODE_DEVELOPER);
$this->configWriter->expects($this->once())
->method('save')
->with(InspectorConfig::XML_PATH_ENABLED, '0');
$tester = new CommandTester($this->command);
$exitCode = $tester->execute(['action' => 'disable']);
$this->assertSame(Cli::RETURN_SUCCESS, $exitCode);
$this->assertStringContainsString('has been disabled', $tester->getDisplay());
}
public function testStatusWarnsWhenNotInDeveloperMode(): void
{
$this->state->method('getMode')->willReturn(State::MODE_PRODUCTION);
$this->scopeConfig->method('isSetFlag')->willReturn(false);
$tester = new CommandTester($this->command);
$exitCode = $tester->execute(['action' => 'status']);
$this->assertSame(Cli::RETURN_SUCCESS, $exitCode);
$this->assertStringContainsString('requires developer mode', $tester->getDisplay());
}
public function testStatusNotesWhenDisabledInDeveloperMode(): void
{
$this->state->method('getMode')->willReturn(State::MODE_DEVELOPER);
$this->scopeConfig->method('isSetFlag')->willReturn(false);
$tester = new CommandTester($this->command);
$exitCode = $tester->execute(['action' => 'status']);
$this->assertSame(Cli::RETURN_SUCCESS, $exitCode);
$this->assertStringContainsString('Inspector is disabled', $tester->getDisplay());
}
public function testStatusReportsActiveWhenEnabledInDeveloperMode(): void
{
$this->state->method('getMode')->willReturn(State::MODE_DEVELOPER);
$this->scopeConfig->method('isSetFlag')->willReturn(true);
$tester = new CommandTester($this->command);
$exitCode = $tester->execute(['action' => 'status']);
$this->assertSame(Cli::RETURN_SUCCESS, $exitCode);
$this->assertStringContainsString('active and ready to use', $tester->getDisplay());
}
public function testCommandNameIsConfigured(): void
{
$this->assertSame('mageforge:theme:inspector', $this->command->getName());
}
// -------------------------------------------------------------------------
// Mutation hardening: exact messages and cache interactions
// -------------------------------------------------------------------------
public function testEnableFailureListsCurrentModeAndRemedy(): void
{
$this->state->method('getMode')->willReturn('production');
$tester = new CommandTester($this->command);
$tester->execute(['action' => 'enable']);
$display = (string) preg_replace('/\s+/', ' ', $tester->getDisplay());
$this->assertStringContainsString('Inspector can only be enabled/disabled in developer mode.', $display);
$this->assertStringContainsString('Current mode: production', $display);
$this->assertStringContainsString('bin/magento deploy:mode:set developer', $display);
}
public function testEnableAnnouncesUsageAndCleansExactCacheTypes(): void
{
$this->state->method('getMode')->willReturn(State::MODE_DEVELOPER);
$this->cacheManager
->expects($this->once())
->method('clean')
->with(['config', 'layout', 'full_page', 'block_html']);
$tester = new CommandTester($this->command);
$tester->execute(['action' => 'enable']);
$display = (string) preg_replace('/\s+/', ' ', $tester->getDisplay());
$this->assertStringContainsString('MageForge Inspector has been enabled!', $display);
$activeHint = 'The inspector will now be active on the frontend for allowed IPs.';
$this->assertStringContainsString($activeHint, $display);
$this->assertStringContainsString('Press Ctrl+Shift+I (or Cmd+Option+I on macOS)', $display);
$this->assertStringContainsString('Hover over elements to see their template information', $display);
$this->assertStringContainsString('Click to pin the inspector panel', $display);
$this->assertStringContainsString('Inspector only works in developer mode and for allowed IPs', $display);
$this->assertStringContainsString('Config, layout, full page & block HTML caches cleaned', $display);
}
public function testStatusShowsModeAndEnabledState(): void
{
$this->state->method('getMode')->willReturn(State::MODE_DEVELOPER);
$this->scopeConfig->method('isSetFlag')->willReturn(true);
$tester = new CommandTester($this->command);
$tester->execute(['action' => 'status']);
$display = (string) preg_replace('/\s+/', ' ', $tester->getDisplay());
$this->assertStringContainsString('MageForge Inspector Status', $display);
$this->assertStringContainsString('Mode: developer', $display);
$this->assertStringContainsString('Inspector: Enabled', $display);
$this->assertStringContainsString('Inspector is active and ready to use!', $display);
$this->assertStringNotContainsString('Inspector is disabled.', $display);
}
public function testDisabledStatusShowsDisabledState(): void
{
$this->state->method('getMode')->willReturn(State::MODE_DEVELOPER);
$this->scopeConfig->method('isSetFlag')->willReturn(false);
$tester = new CommandTester($this->command);
$tester->execute(['action' => 'status']);
$display = (string) preg_replace('/\s+/', ' ', $tester->getDisplay());
$this->assertStringContainsString('Inspector: Disabled', $display);
$this->assertStringContainsString(
'Inspector is disabled. Run "bin/magento mageforge:theme:inspector enable" to activate it.',
$display,
);
$this->assertStringNotContainsString('ready to use', $display);
}
public function testStatusUppercasesActionArgument(): void
{
$this->state->method('getMode')->willReturn(State::MODE_DEVELOPER);
$this->scopeConfig->method('isSetFlag')->willReturn(true);
$tester = new CommandTester($this->command);
$exitCode = $tester->execute(['action' => 'STATUS']);
$this->assertSame(Cli::RETURN_SUCCESS, $exitCode);
$this->assertStringContainsString('MageForge Inspector Status', $tester->getDisplay());
}
// -------------------------------------------------------------------------
// No-argument execution path (interactive menu / non-interactive fallback)
// -------------------------------------------------------------------------
public function testNoActionFallsBackToStatusInNonInteractiveMode(): void
{
$this->state->method('getMode')->willReturn(State::MODE_DEVELOPER);
$this->scopeConfig->method('isSetFlag')->willReturn(true);
$this->configWriter->expects($this->never())->method('save');
$tester = new CommandTester($this->command);
$exitCode = $tester->execute([]);
$this->assertSame(Cli::RETURN_SUCCESS, $exitCode);
$this->assertStringContainsString('MageForge Inspector Status', $tester->getDisplay());
}
public function testNoActionUsesSelectedActionFromInteractiveMenu(): void
{
$this->state->method('getMode')->willReturn(State::MODE_DEVELOPER);
$this->configWriter->expects($this->once())
->method('save')
->with(InspectorConfig::XML_PATH_ENABLED, '0');
$tester = new CommandTester($this->createInteractiveCommand('disable'));
$exitCode = $tester->execute([]);
$this->assertSame(Cli::RETURN_SUCCESS, $exitCode);
$this->assertStringContainsString('has been disabled', $tester->getDisplay());
}
public function testNoActionFailsWhenInteractiveMenuIsCancelled(): void
{
$this->configWriter->expects($this->never())->method('save');
$tester = new CommandTester($this->createInteractiveCommand(null));
$exitCode = $tester->execute([]);
$this->assertSame(Cli::RETURN_FAILURE, $exitCode);
}
/**
* Create a command double that always takes the interactive path and returns
* the given selection from the menu instead of rendering a real prompt
*
* @param string|null $selection
* @return InspectorCommand
*/
private function createInteractiveCommand(?string $selection): InspectorCommand
{
return new class(
$this->configWriter,
$this->state,
$this->cacheManager,
$this->scopeConfig,
$selection,
) extends InspectorCommand {
public function __construct(
WriterInterface $configWriter,
State $state,
CacheManager $cacheManager,
ScopeConfigInterface $scopeConfig,
private readonly ?string $selection,
) {
parent::__construct($configWriter, $state, $cacheManager, $scopeConfig);
}
protected function isInteractiveTerminal(OutputInterface $output): bool
{
return true;
}
protected function promptAction(): ?string
{
return $this->selection;
}
};
}
}