Skip to content

Commit d6f0586

Browse files
authored
Merge pull request #4 from clawrock/dev
Minor fixes and db profiler command
2 parents 2e192a0 + 525ea2e commit d6f0586

File tree

11 files changed

+153
-31
lines changed

11 files changed

+153
-31
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
composer.lock
2+
/vendor

.travis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ install:
1010
- composer install --prefer-dist
1111

1212
script:
13-
- php vendor/bin/phpcs --standard=PSR2 Api Controller Exception Helper Logger Model Observer Plugin Serializer Test
14-
- php vendor/bin/phpmd Api,Controller,Exception,Helper,Logger,Model,Observer,Plugin,Serializer text phpmd.xml
15-
- php vendor/bin/phpcpd Api Controller Exception Helper Logger Model Observer Plugin Serializer
13+
- php vendor/bin/phpcs --standard=PSR2 --extensions=php --ignore=etc,view,vendor .
14+
- php vendor/bin/phpmd . text phpmd.xml --suffixes=php
15+
- php vendor/bin/phpcpd . --exclude=etc --exclude=Test --exclude=view --exclude=vendor
1616
- php vendor/bin/phpunit --coverage-clover build/logs/clover.xml
1717

1818
after_script:
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace ClawRock\Debug\Console\Command;
4+
5+
use Symfony\Component\Console\Command\Command;
6+
use Symfony\Component\Console\Input\InputInterface;
7+
use Symfony\Component\Console\Output\OutputInterface;
8+
9+
class DatabaseProfilerDisableCommand extends Command
10+
{
11+
/**
12+
* @var \ClawRock\Debug\Model\Config\Database\ProfilerWriter
13+
*/
14+
private $profilerWriter;
15+
16+
public function __construct(
17+
\ClawRock\Debug\Model\Config\Database\ProfilerWriter $profilerWriter
18+
) {
19+
parent::__construct('debug:db-profiler:disable');
20+
21+
$this->profilerWriter = $profilerWriter;
22+
}
23+
24+
protected function configure()
25+
{
26+
parent::configure();
27+
28+
$this->setDescription('Disable database profiler required for database collector.');
29+
}
30+
31+
/**
32+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
33+
*/
34+
protected function execute(InputInterface $input, OutputInterface $output)
35+
{
36+
$this->profilerWriter->save(false);
37+
38+
$output->writeLn('<info>Database profiler disabled!</info>');
39+
}
40+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace ClawRock\Debug\Console\Command;
4+
5+
use Symfony\Component\Console\Command\Command;
6+
use Symfony\Component\Console\Input\InputInterface;
7+
use Symfony\Component\Console\Output\OutputInterface;
8+
9+
class DatabaseProfilerEnableCommand extends Command
10+
{
11+
/**
12+
* @var \ClawRock\Debug\Model\Config\Database\ProfilerWriter
13+
*/
14+
private $profilerWriter;
15+
16+
public function __construct(
17+
\ClawRock\Debug\Model\Config\Database\ProfilerWriter $profilerWriter
18+
) {
19+
parent::__construct('debug:db-profiler:enable');
20+
21+
$this->profilerWriter = $profilerWriter;
22+
}
23+
24+
protected function configure()
25+
{
26+
parent::configure();
27+
28+
$this->setDescription('Enable database profiler required for database collector.');
29+
}
30+
31+
/**
32+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
33+
*/
34+
protected function execute(InputInterface $input, OutputInterface $output)
35+
{
36+
$this->profilerWriter->save(true);
37+
38+
$output->writeLn('<info>Database profiler enabled!</info>');
39+
}
40+
}

Helper/Config.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use ClawRock\Debug\Model\Config\Source\ErrorHandler;
77
use Magento\Framework\App\Area;
88
use Magento\Framework\App\Config\ScopeConfigInterface;
9+
use Magento\Framework\Exception\LocalizedException;
910

1011
/**
1112
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
@@ -48,6 +49,11 @@ class Config
4849
*/
4950
private $scopeConfig;
5051

52+
/**
53+
* @var \Magento\Framework\App\DeploymentConfig
54+
*/
55+
private $deploymentConfig;
56+
5157
/**
5258
* @var \ClawRock\Debug\Model\Storage\HttpStorage
5359
*/
@@ -57,11 +63,13 @@ public function __construct(
5763
\Magento\Framework\PhraseFactory $phraseFactory,
5864
\Magento\Framework\App\State $appState,
5965
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
66+
\Magento\Framework\App\DeploymentConfig $deploymentConfig,
6067
\ClawRock\Debug\Model\Storage\HttpStorage $httpStorage
6168
) {
6269
$this->phraseFactory = $phraseFactory;
6370
$this->appState = $appState;
6471
$this->scopeConfig = $scopeConfig;
72+
$this->deploymentConfig = $deploymentConfig;
6573
$this->httpStorage = $httpStorage;
6674
}
6775

@@ -84,8 +92,12 @@ public function isEnabled(): bool
8492
return false;
8593
}
8694

87-
if ($this->appState->getAreaCode() === Area::AREA_ADMINHTML && !$this->isAdminhtmlActive()) {
88-
return false;
95+
try {
96+
if ($this->appState->getAreaCode() === Area::AREA_ADMINHTML && !$this->isAdminhtmlActive()) {
97+
return false;
98+
}
99+
} catch (LocalizedException $e) {
100+
return true;
89101
}
90102

91103
return true;
@@ -191,10 +203,10 @@ public function isCustomerCollectorEnabled(): bool
191203

192204
public function isDatabaseCollectorEnabled(): bool
193205
{
194-
return (bool) $this->scopeConfig->getValue(
206+
return $this->scopeConfig->getValue(
195207
self::CONFIG_COLLECTOR_DATABASE,
196208
ScopeConfigInterface::SCOPE_TYPE_DEFAULT
197-
);
209+
) && $this->deploymentConfig->get('db/connection/default/profiler/enabled');
198210
}
199211

200212
public function isEventCollectorEnabled(): bool

Helper/Database.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public function replaceQueryParameters($query, array $parameters)
9797
{
9898
$i = !array_key_exists(0, $parameters) && array_key_exists(1, $parameters) ? 1 : 0;
9999

100-
$result = preg_replace_callback('/\?|((?<!:):[a-z0-9_]+)/i', function ($matches) use ($parameters, &$i) {
100+
$result = preg_replace_callback('/\?|((?<!:):[a-zA-Z]\w*)/i', function ($matches) use ($parameters, &$i) {
101101
$value = array_key_exists($i, $parameters) ? $parameters[$i] : $parameters[$matches[0]];
102102
$result = $this->resourceConnection->getConnection(ResourceConnection::DEFAULT_CONNECTION)->quote($value);
103103
$i++;

Readme.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
[![Packagist](https://img.shields.io/packagist/v/clawrock/m2-debug.svg)](https://packagist.org/packages/clawrock/m2-debug)
2-
[![Packagist](https://img.shields.io/packagist/dt/clawrock/m2-debug.svg)](https://packagist.org/packages/clawrock/m2-debug)
3-
[![Build Status](https://travis-ci.org/clawrock/m2-debug.svg?branch=master)](https://travis-ci.org/clawrock/m2-debug)
4-
[![Coverage Status](https://coveralls.io/repos/github/clawrock/m2-debug/badge.svg)](https://coveralls.io/github/clawrock/m2-debug)
1+
[![Packagist](https://img.shields.io/packagist/v/clawrock/magento2-debug.svg)](https://packagist.org/packages/clawrock/magento2-debug)
2+
[![Packagist](https://img.shields.io/packagist/dt/clawrock/magento2-debug.svg)](https://packagist.org/packages/clawrock/magento2-debug)
3+
[![Build Status](https://travis-ci.org/clawrock/magento2-debug.svg?branch=master)](https://travis-ci.org/clawrock/magento2-debug)
4+
[![Coverage Status](https://coveralls.io/repos/github/clawrock/magento2-debug/badge.svg)](https://coveralls.io/github/clawrock/magento2-debug)
55

66
# Magento 2 - Debug module
77
Module for debugging Magento 2 performance. It works without overwriting any core files and it can be installed with composer.

Test/Unit/Helper/ConfigTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ class ConfigTest extends TestCase
2626
*/
2727
private $scopeConfigMock;
2828

29+
private $deploymentConfigMock;
30+
2931
private $httpStorageMock;
3032

3133
/**
@@ -52,6 +54,10 @@ protected function setUp()
5254

5355
$this->scopeConfigMock = $this->getMockForAbstractClass(ScopeConfigInterface::class);
5456

57+
$this->deploymentConfigMock = $this->getMockBuilder(\Magento\Framework\App\DeploymentConfig::class)
58+
->disableOriginalConstructor()
59+
->getMock();
60+
5561
$this->httpStorageMock = $this->getMockBuilder(HttpStorage::class)
5662
->disableOriginalConstructor()
5763
->getMock();
@@ -60,6 +66,7 @@ protected function setUp()
6066
'phraseFactory' => $this->phraseFactoryMock,
6167
'appState' => $this->appStateMock,
6268
'scopeConfig' => $this->scopeConfigMock,
69+
'deploymentConfig' => $this->deploymentConfigMock,
6370
'httpStorage' => $this->httpStorageMock,
6471
]);
6572
}
@@ -91,6 +98,11 @@ public function testIsDatabaseCollectorEnabled()
9198
$this->scopeConfigMock->expects($this->once())->method('getValue')
9299
->with(Config::CONFIG_COLLECTOR_DATABASE, ScopeConfigInterface::SCOPE_TYPE_DEFAULT)
93100
->willReturn('1');
101+
102+
$this->deploymentConfigMock->expects($this->once())->method('get')
103+
->with('db/connection/default/profiler/enabled')
104+
->willReturn(true);
105+
94106
$this->assertTrue($this->config->isDatabaseCollectorEnabled());
95107
}
96108

Test/Unit/Helper/DatabaseTest.php

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -62,31 +62,35 @@ public function testGetQueryId()
6262
$this->assertEquals(md5($query . implode(',', $params)), $this->database->getQueryId($profilerQueryMock));
6363
}
6464

65-
public function testReplaceQueryParameters()
65+
/**
66+
* @dataProvider queryParametersProvider
67+
*/
68+
public function testReplaceQueryParameters($query, array $parameters, $result)
6669
{
67-
$query = 'SELECT * FROM core_config_data WHERE path = :path';
68-
$parameters = [':path' => 'web/secure/base_url'];
69-
$result = 'SELECT * FROM core_config_data WHERE path = web/secure/base_url';
70-
$this->resourceConnectionMock->expects($this->once())->method('getConnection')
70+
$this->resourceConnectionMock->expects($this->exactly(count($parameters)))->method('getConnection')
7171
->willReturn($this->connectionMock);
72-
$this->connectionMock->expects($this->once())->method('quote')
73-
->with($parameters[':path'])
74-
->willReturn($parameters[':path']);
72+
73+
$this->connectionMock->expects($this->exactly(count($parameters)))->method('quote')
74+
->willReturnCallback(function () {
75+
$args = func_get_args();
76+
return $args[0];
77+
});
7578

7679
$this->assertEquals($result, $this->database->replaceQueryParameters($query, $parameters));
7780
}
7881

79-
public function testReplaceQueryParameters2()
82+
public function queryParametersProvider()
8083
{
81-
$query = 'SELECT * FROM core_config_data WHERE path = ?';
82-
$parameters = ['web/secure/base_url'];
83-
$result = 'SELECT * FROM core_config_data WHERE path = web/secure/base_url';
84-
$this->resourceConnectionMock->expects($this->once())->method('getConnection')
85-
->willReturn($this->connectionMock);
86-
$this->connectionMock->expects($this->once())->method('quote')
87-
->with($parameters[0])
88-
->willReturn($parameters[0]);
89-
90-
$this->assertEquals($result, $this->database->replaceQueryParameters($query, $parameters));
84+
return [
85+
['SELECT * FROM core_config_data WHERE path = ?', [
86+
'web/secure/base_url'
87+
], 'SELECT * FROM core_config_data WHERE path = web/secure/base_url'],
88+
['INSERT INTO `table` (`field1`, `field2`) VALUES (\'2000-01-01 10:00:00\', ?)', [
89+
'value',
90+
], 'INSERT INTO `table` (`field1`, `field2`) VALUES (\'2000-01-01 10:00:00\', value)'],
91+
['SELECT * FROM core_config_data WHERE path = :path', [
92+
':path' => 'web/unsecure/base_url'
93+
], 'SELECT * FROM core_config_data WHERE path = web/unsecure/base_url'],
94+
];
9195
}
9296
}

etc/di.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,12 @@
8484
</argument>
8585
</arguments>
8686
</type>
87+
<type name="Magento\Framework\Console\CommandListInterface">
88+
<arguments>
89+
<argument name="commands" xsi:type="array">
90+
<item name="ClawRock_Debug::enable_database_profiler" xsi:type="object">ClawRock\Debug\Console\Command\DatabaseProfilerEnableCommand</item>
91+
<item name="ClawRock_Debug::disable_database_profiler" xsi:type="object">ClawRock\Debug\Console\Command\DatabaseProfilerDisableCommand</item>
92+
</argument>
93+
</arguments>
94+
</type>
8795
</config>

phpmd.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,8 @@
1616
<exclude name="LongVariable" />
1717
</rule>
1818
<rule ref="rulesets/unusedcode.xml" />
19+
<exclude-pattern>etc</exclude-pattern>
20+
<exclude-pattern>Test</exclude-pattern>
21+
<exclude-pattern>view</exclude-pattern>
22+
<exclude-pattern>vendor</exclude-pattern>
1923
</ruleset>

0 commit comments

Comments
 (0)