Skip to content

Commit cbfac52

Browse files
committed
Added unit tests for commands and LoggerProxy
1 parent 5d46ea8 commit cbfac52

File tree

6 files changed

+241
-2
lines changed

6 files changed

+241
-2
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ class QueryLogDisableCommand extends Command
2020
*/
2121
const COMMAND_NAME = 'dev:query-log:disable';
2222

23+
/**
24+
* Success message
25+
*/
26+
const SUCCESS_MESSAGE = "DB query logging disabled.";
27+
2328
/**
2429
* @var Writer
2530
*/
@@ -58,6 +63,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
5863
$data = [LoggerProxy::PARAM_ALIAS => LoggerProxy::LOGGER_ALIAS_DISABLED];
5964
$this->deployConfigWriter->saveConfig([ConfigFilePool::APP_ENV => $data]);
6065

61-
$output->writeln("<info>DB query logging disabled.</info>");
66+
$output->writeln("<info>". self::SUCCESS_MESSAGE . "</info>");
6267
}
6368
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ class QueryLogEnableCommand extends Command
3636
*/
3737
const COMMAND_NAME = 'dev:query-log:enable';
3838

39+
/**
40+
* Success message
41+
*/
42+
const SUCCESS_MESSAGE = "DB query logging enabled.";
43+
3944
/**
4045
* @var Writer
4146
*/
@@ -104,6 +109,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
104109

105110
$this->deployConfigWriter->saveConfig([ConfigFilePool::APP_ENV => $data]);
106111

107-
$output->writeln("<info>DB query logging enabled.</info>");
112+
$output->writeln("<info>". self::SUCCESS_MESSAGE . "</info>");
108113
}
109114
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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\QueryLogDisableCommand;
10+
use Magento\Framework\App\DeploymentConfig\Writer;
11+
use Magento\Framework\Config\File\ConfigFilePool;
12+
use Magento\Framework\DB\Logger\LoggerProxy;
13+
use Symfony\Component\Console\Tester\CommandTester;
14+
15+
/**
16+
* Class QueryLogDisableCommandTest
17+
*
18+
* Tests dev:query-log:disable command.
19+
* Tests that the correct configuration is passed to the deployment config writer.
20+
*/
21+
class QueryLogDisableCommandTest extends \PHPUnit_Framework_TestCase
22+
{
23+
/**
24+
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\DeploymentConfig\Writer
25+
*/
26+
private $configWriter;
27+
28+
/**
29+
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Developer\Console\Command\QueryLogEnableCommand
30+
*/
31+
private $command;
32+
33+
/**
34+
* {@inheritdoc}
35+
*/
36+
public function setUp()
37+
{
38+
$this->configWriter = $this->getMockBuilder(Writer::class)
39+
->disableOriginalConstructor()
40+
->getMock();
41+
$this->command = new QueryLogDisableCommand($this->configWriter);
42+
}
43+
44+
/**
45+
* Test execute()
46+
*/
47+
public function testExecute()
48+
{
49+
$data = [LoggerProxy::PARAM_ALIAS => LoggerProxy::LOGGER_ALIAS_DISABLED];
50+
51+
$this->configWriter
52+
->expects($this->once())
53+
->method('saveConfig')
54+
->with([ConfigFilePool::APP_ENV => $data]);
55+
56+
$commandTester = new CommandTester($this->command);
57+
$commandTester->execute([]);
58+
$this->assertSame(
59+
QueryLogDisableCommand::SUCCESS_MESSAGE . PHP_EOL,
60+
$commandTester->getDisplay()
61+
);
62+
}
63+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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\QueryLogEnableCommand;
10+
use Magento\Framework\App\DeploymentConfig\Writer;
11+
use Magento\Framework\Config\File\ConfigFilePool;
12+
use Magento\Framework\DB\Logger\LoggerProxy;
13+
use Symfony\Component\Console\Tester\CommandTester;
14+
15+
/**
16+
* Class QueryLogEnableCommandTest
17+
*
18+
* Tests dev:query-log:enable command.
19+
* Tests that the correct configuration is passed to the deployment config writer with and without parameters.
20+
*/
21+
class QueryLogEnableCommandTest extends \PHPUnit_Framework_TestCase
22+
{
23+
/**
24+
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\DeploymentConfig\Writer
25+
*/
26+
private $configWriter;
27+
28+
/**
29+
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Developer\Console\Command\QueryLogEnableCommand
30+
*/
31+
private $command;
32+
33+
/**
34+
* {@inheritdoc}
35+
*/
36+
public function setUp()
37+
{
38+
$this->configWriter = $this->getMockBuilder(Writer::class)
39+
->disableOriginalConstructor()
40+
->getMock();
41+
$this->command = new QueryLogEnableCommand($this->configWriter);
42+
}
43+
44+
/**
45+
* Test execute() without parameters.
46+
*/
47+
public function testExecuteWithNoParams()
48+
{
49+
$data = [LoggerProxy::PARAM_ALIAS => LoggerProxy::LOGGER_ALIAS_FILE];
50+
$data[LoggerProxy::PARAM_LOG_ALL] = 1;
51+
$data[LoggerProxy::PARAM_QUERY_TIME] = 0.001;
52+
$data[LoggerProxy::PARAM_CALL_STACK] = 1;
53+
54+
$this->configWriter = $this->getMockBuilder(Writer::class)
55+
->disableOriginalConstructor()
56+
->getMock();
57+
$this->configWriter
58+
->expects($this->any())
59+
->method('saveConfig')
60+
->with([ConfigFilePool::APP_ENV => $data]);
61+
62+
$commandTester = new CommandTester($this->command);
63+
$commandTester->execute([]);
64+
$this->assertSame(
65+
QueryLogEnableCommand::SUCCESS_MESSAGE . PHP_EOL,
66+
$commandTester->getDisplay()
67+
);
68+
}
69+
70+
/**
71+
* Test execute() with parameters.
72+
*/
73+
public function testExecuteWithParams()
74+
{
75+
$data = [LoggerProxy::PARAM_ALIAS => LoggerProxy::LOGGER_ALIAS_FILE];
76+
$data[LoggerProxy::PARAM_LOG_ALL] = 0;
77+
$data[LoggerProxy::PARAM_QUERY_TIME] = '0.05';
78+
$data[LoggerProxy::PARAM_CALL_STACK] = 0;
79+
80+
$this->configWriter = $this->getMockBuilder(Writer::class)
81+
->disableOriginalConstructor()
82+
->getMock();
83+
$this->configWriter
84+
->expects($this->any())
85+
->method('saveConfig')
86+
->with([ConfigFilePool::APP_ENV => $data]);
87+
88+
$commandTester = new CommandTester($this->command);
89+
$commandTester->execute(['false', '0.05', 'false']);
90+
$this->assertSame(
91+
QueryLogEnableCommand::SUCCESS_MESSAGE . PHP_EOL,
92+
$commandTester->getDisplay()
93+
);
94+
}
95+
}

lib/internal/Magento/Framework/DB/Logger/LoggerProxy.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ public function __construct(
7171
parent::__construct($logAllQueries, $logQueryTime, $logCallStack);
7272
}
7373

74+
/**
75+
* @return LoggerAbstract|Quiet
76+
*/
77+
public function getLogger()
78+
{
79+
return $this->logger;
80+
}
81+
7482
/**
7583
* Adds log record
7684
*
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Framework\Test\Unit\DB\Logger;
8+
9+
use Magento\Framework\DB\Logger\LoggerProxy;
10+
use Magento\Framework\DB\Logger\File;
11+
use Magento\Framework\DB\Logger\Quiet;
12+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
13+
14+
class LoggerProxyTest extends \PHPUnit_Framework_TestCase
15+
{
16+
/**
17+
* @var \Magento\Framework\DB\Logger\LoggerProxy
18+
*/
19+
private $loggerProxy;
20+
21+
/**
22+
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
23+
*/
24+
private $objectManager;
25+
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
public function setUp()
30+
{
31+
$this->objectManager = new ObjectManager($this);
32+
}
33+
34+
/**
35+
* Test new logger proxy with file alias
36+
*/
37+
public function testNewWithAliasFile()
38+
{
39+
$this->loggerProxy = $this->objectManager->getObject(
40+
LoggerProxy::class,
41+
[
42+
'loggerAlias' => LoggerProxy::LOGGER_ALIAS_FILE,
43+
]
44+
);
45+
46+
$this->assertInstanceOf(File::class, $this->loggerProxy->getLogger());
47+
}
48+
49+
/**
50+
* Test new logger proxy with disabled alias
51+
*/
52+
public function testNewWithAliasDisabled()
53+
{
54+
$this->loggerProxy = $this->objectManager->getObject(
55+
LoggerProxy::class,
56+
[
57+
'loggerAlias' => LoggerProxy::LOGGER_ALIAS_DISABLED,
58+
]
59+
);
60+
61+
$this->assertInstanceOf(Quiet::class, $this->loggerProxy->getLogger());
62+
}
63+
}

0 commit comments

Comments
 (0)