Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit 48baf59

Browse files
⏫ Forwardport of magento/magento2#11407 to 2.3-develop branch
1 parent 02620b0 commit 48baf59

File tree

3 files changed

+217
-4
lines changed

3 files changed

+217
-4
lines changed

app/code/Magento/Developer/Console/Command/ProfilerEnableCommand.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
7979
$type = self::TYPE_DEFAULT;
8080
}
8181

82-
if (!in_array($type, self::BUILT_IN_TYPES)) {
82+
if (!in_array($type, self::BUILT_IN_TYPES, true)) {
8383
$builtInTypes = implode(', ', self::BUILT_IN_TYPES);
8484
$output->writeln(
85-
'<comment>' . sprintf('Type %s is not one of the built-in output types (%s).', $type) .
86-
sprintf('Make sure the necessary class exists.', $type, $builtInTypes) . '</comment>'
85+
'<comment>'
86+
. sprintf('Type %s is not one of the built-in output types (%s).', $type, $builtInTypes) .
87+
'</comment>'
8788
);
8889
}
8990

@@ -100,9 +101,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
100101
);
101102
}
102103
$output->write(PHP_EOL);
104+
103105
return;
104106
}
105-
106107
$output->writeln('<error>Something went wrong while enabling the profiler.</error>');
107108
}
108109
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Developer\Test\Unit\Console\Command;
8+
9+
use Magento\Developer\Console\Command\ProfilerDisableCommand;
10+
use Magento\Framework\Filesystem\Io\File;
11+
use Magento\TestFramework\Helper\Bootstrap;
12+
use PHPUnit\Framework\TestCase;
13+
use Symfony\Component\Console\Tester\CommandTester;
14+
15+
16+
/**
17+
* Class ProfilerDisableCommandTest
18+
*
19+
* Tests dev:profiler:disable command.
20+
*/
21+
class ProfilerDisableCommandTest extends TestCase
22+
{
23+
/**
24+
* @var File|\PHPUnit_Framework_MockObject_MockObject
25+
*/
26+
private $filesystemMock;
27+
28+
/**
29+
* Test disabling the profiler by command.
30+
*
31+
* @param bool $fileExists
32+
* @param string $expectedOutput
33+
* @dataProvider commandDataProvider
34+
*/
35+
public function testCommand(bool $fileExists, string $expectedOutput)
36+
{
37+
$this->filesystemMock
38+
->expects($this->once())
39+
->method('rm')
40+
->with(BP . '/' . ProfilerDisableCommand::PROFILER_FLAG_FILE);
41+
$this->filesystemMock
42+
->expects($this->once())
43+
->method('fileExists')
44+
->with(BP . '/' . ProfilerDisableCommand::PROFILER_FLAG_FILE)
45+
->willReturn($fileExists);
46+
/** @var ProfilerDisableCommand $command */
47+
$command = Bootstrap::getObjectManager()->create(
48+
ProfilerDisableCommand::class,
49+
[
50+
'filesystem' => $this->filesystemMock,
51+
]
52+
);
53+
$commandTester = new CommandTester($command);
54+
$commandTester->execute([]);
55+
56+
self::assertEquals(
57+
$expectedOutput,
58+
trim(str_replace(PHP_EOL, ' ', $commandTester->getDisplay()))
59+
);
60+
}
61+
62+
/**
63+
* Data provider for testCommand.
64+
*
65+
* @return array
66+
*/
67+
public function commandDataProvider()
68+
{
69+
return [
70+
[true, 'Something went wrong while disabling the profiler.'],
71+
[false, 'Profiler disabled.'],
72+
];
73+
}
74+
75+
/**
76+
* @inheritdoc
77+
*/
78+
protected function setUp()
79+
{
80+
$this->filesystemMock = $this->getMockBuilder(File::class)
81+
->disableOriginalConstructor()
82+
->getMock();
83+
}
84+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Developer\Test\Unit\Console\Command;
8+
9+
use Magento\Developer\Console\Command\ProfilerEnableCommand;
10+
use Magento\Framework\Filesystem\Io\File;
11+
use Magento\TestFramework\Helper\Bootstrap;
12+
use PHPUnit\Framework\TestCase;
13+
use Symfony\Component\Console\Tester\CommandTester;
14+
15+
16+
/**
17+
* Class ProfilerEnableCommandTest
18+
*
19+
* Tests dev:profiler:enable command.
20+
*/
21+
class ProfilerEnableCommandTest extends TestCase
22+
{
23+
/**
24+
* @var File|\PHPUnit_Framework_MockObject_MockObject
25+
*/
26+
private $filesystemMock;
27+
28+
/**
29+
* Tests enabling the profiler by command.
30+
*
31+
* @param string $inputType
32+
* @param bool $fileExists
33+
* @param string $expectedOutput
34+
* @dataProvider commandDataProvider
35+
*/
36+
public function testCommand(string $inputType, bool $fileExists, string $expectedOutput)
37+
{
38+
$this->filesystemMock
39+
->expects($this->once())
40+
->method('write')
41+
->with(
42+
BP . '/' . ProfilerEnableCommand::PROFILER_FLAG_FILE,
43+
$inputType ?: ProfilerEnableCommand::TYPE_DEFAULT
44+
);
45+
$this->filesystemMock
46+
->expects($this->once())
47+
->method('fileExists')
48+
->with(BP . '/' . ProfilerEnableCommand::PROFILER_FLAG_FILE)
49+
->willReturn($fileExists);
50+
/** @var ProfilerEnableCommand $command */
51+
$command = Bootstrap::getObjectManager()->create(
52+
ProfilerEnableCommand::class,
53+
[
54+
'filesystem' => $this->filesystemMock,
55+
]
56+
);
57+
$commandTester = new CommandTester($command);
58+
$commandTester->execute(['type' => $inputType]);
59+
60+
self::assertEquals(
61+
$expectedOutput,
62+
trim(str_replace(PHP_EOL, ' ', $commandTester->getDisplay()))
63+
);
64+
}
65+
66+
/**
67+
* Data provider for testCommand.
68+
*
69+
* @return array
70+
*/
71+
public function commandDataProvider()
72+
{
73+
return [
74+
[
75+
'',
76+
true,
77+
'Profiler enabled with html output.'
78+
],
79+
[
80+
'',
81+
false,
82+
'Something went wrong while enabling the profiler.'
83+
],
84+
[
85+
'html',
86+
true,
87+
'Profiler enabled with html output.'
88+
],
89+
[
90+
'html',
91+
false,
92+
'Something went wrong while enabling the profiler.'
93+
],
94+
[
95+
'csvfile',
96+
true,
97+
'Profiler enabled with csvfile output. Output will be saved in /var/log/profiler.csv'
98+
],
99+
[
100+
'csvfile',
101+
false,
102+
'Something went wrong while enabling the profiler.'
103+
],
104+
[
105+
'xml',
106+
true,
107+
'Type xml is not one of the built-in output types (html, csvfile). ' .
108+
'Profiler enabled with xml output.'
109+
],
110+
[
111+
'xml',
112+
false,
113+
'Type xml is not one of the built-in output types (html, csvfile). ' .
114+
'Something went wrong while enabling the profiler.'
115+
],
116+
];
117+
}
118+
119+
/**
120+
* @inheritdoc
121+
*/
122+
protected function setUp()
123+
{
124+
$this->filesystemMock = $this->getMockBuilder(File::class)
125+
->disableOriginalConstructor()
126+
->getMock();
127+
}
128+
}

0 commit comments

Comments
 (0)