configured var dump server
parent
f0bddb321c
commit
02ba802d6b
@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Engelsystem\Helpers;
|
||||
|
||||
use Engelsystem\Config\Config;
|
||||
use Engelsystem\Container\ServiceProvider;
|
||||
use Symfony\Component\VarDumper\Cloner\VarCloner;
|
||||
use Symfony\Component\VarDumper\Dumper\CliDumper;
|
||||
use Symfony\Component\VarDumper\Dumper\ContextProvider\CliContextProvider;
|
||||
use Symfony\Component\VarDumper\Dumper\ContextProvider\SourceContextProvider;
|
||||
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
|
||||
use Symfony\Component\VarDumper\Dumper\ServerDumper;
|
||||
use Symfony\Component\VarDumper\VarDumper;
|
||||
|
||||
class DumpServerServiceProvider extends ServiceProvider
|
||||
{
|
||||
public function register(): void
|
||||
{
|
||||
$app = $this->app;
|
||||
|
||||
/** @var Config $config */
|
||||
$config = $app->get('config');
|
||||
// setup var dump server to use for easier debugging
|
||||
$varDumpServerConfig = $config->get('var_dump_server');
|
||||
|
||||
if (
|
||||
!$varDumpServerConfig['enable']
|
||||
|| $config->get('environment') !== 'development'
|
||||
|| !class_exists(ServerDumper::class)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
$dumper = new ServerDumper(
|
||||
'tcp://' . $varDumpServerConfig['host'] . ':' . $varDumpServerConfig['port'],
|
||||
in_array(PHP_SAPI, ['cli', 'phpdbg']) ? $app->get(CliDumper::class) : $app->get(HtmlDumper::class),
|
||||
[
|
||||
'cli' => new CliContextProvider(),
|
||||
'source' => new SourceContextProvider(),
|
||||
]
|
||||
);
|
||||
|
||||
$cloner = $app->get(VarCloner::class);
|
||||
|
||||
VarDumper::setHandler(
|
||||
// @codeCoverageIgnoreStart
|
||||
static function ($var) use ($cloner, $dumper) {
|
||||
$dumper->dump($cloner->cloneVar($var));
|
||||
}
|
||||
// @codeCoverageIgnoreEnd
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
namespace Engelsystem\Test\Unit\Helpers;
|
||||
|
||||
use Engelsystem\Config\Config;
|
||||
use Engelsystem\Helpers\DumpServerServiceProvider;
|
||||
use Engelsystem\Test\Unit\ServiceProviderTest;
|
||||
use Symfony\Component\VarDumper\Cloner\VarCloner;
|
||||
use Symfony\Component\VarDumper\Dumper\CliDumper;
|
||||
use Symfony\Component\VarDumper\Dumper\ServerDumper;
|
||||
|
||||
class DumpServerServiceProviderTest extends ServiceProviderTest
|
||||
{
|
||||
/**
|
||||
* @covers \Engelsystem\Helpers\DumpServerServiceProvider::register
|
||||
*/
|
||||
public function testRegisterIfClassExists(): void
|
||||
{
|
||||
if (class_exists(ServerDumper::class) === false) {
|
||||
self::markTestSkipped('ServerDumper class does not exist. Skipping.');
|
||||
}
|
||||
|
||||
$varDumpServerConfig = [
|
||||
'host' => 'localhost',
|
||||
'port' => 80,
|
||||
'enable' => true
|
||||
];
|
||||
|
||||
$config = new Config();
|
||||
$config->set('var_dump_server', $varDumpServerConfig);
|
||||
$config->set('environment', 'development');
|
||||
|
||||
// mock to test that the code has passed the enabled checks and started to configure the var dump server
|
||||
$app = $this->getApp(['get']);
|
||||
|
||||
$app->expects(self::exactly(3))
|
||||
->method('get')
|
||||
->withConsecutive(
|
||||
['config'],
|
||||
[CliDumper::class],
|
||||
[VarCloner::class]
|
||||
)->willReturnOnConsecutiveCalls(
|
||||
$config,
|
||||
new CliDumper(),
|
||||
new VarCloner()
|
||||
);
|
||||
|
||||
$dumpServiceProvider = new DumpServerServiceProvider($app);
|
||||
$dumpServiceProvider->register();
|
||||
}
|
||||
|
||||
public function notEnabledDataProvider(): array
|
||||
{
|
||||
return [
|
||||
[false, 'development'],
|
||||
[false, 'production'],
|
||||
[true, 'production'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \Engelsystem\Helpers\DumpServerServiceProvider::register
|
||||
* @dataProvider notEnabledDataProvider
|
||||
*
|
||||
* @param bool $enable
|
||||
* @param string $environment
|
||||
*/
|
||||
public function testRegisterShouldNotEnable(bool $enable, string $environment): void
|
||||
{
|
||||
$varDumpServerConfig = [
|
||||
'host' => 'localhost',
|
||||
'port' => 80,
|
||||
'enable' => $enable
|
||||
];
|
||||
|
||||
$config = new Config();
|
||||
$config->set('var_dump_server', $varDumpServerConfig);
|
||||
$config->set('environment', $environment);
|
||||
|
||||
// asset get is called once only
|
||||
$app = $this->getApp(['get']);
|
||||
$app->expects(self::once())
|
||||
->method('get')
|
||||
->willReturn($config);
|
||||
|
||||
$dumpServiceProvider = new DumpServerServiceProvider($app);
|
||||
$dumpServiceProvider->register();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue