Skip to content

Commit

Permalink
[VarDumper] Some tweaks after Nicolas' commit & ServerDumperPlacehold…
Browse files Browse the repository at this point in the history
…erCommand
  • Loading branch information
ogizanagi committed Mar 23, 2018
1 parent a5cc0d4 commit 1980802
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 28 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -7,7 +7,7 @@ CHANGELOG
* added a `ServerDumper` to send serialized Data clones to a server
* added a `ServerDumpCommand` and `DumpServer` to run a server collecting
and displaying dumps on a single place with multiple formats support
* added `CliDescriptor` and `HtmlDescriptor` descriptors for `server:dump` cli and html formats support
* added `CliDescriptor` and `HtmlDescriptor` descriptors for `server:dump` CLI and HTML formats support

4.0.0
-----
Expand Down
2 changes: 1 addition & 1 deletion Command/Descriptor/CliDescriptor.php
Expand Up @@ -36,7 +36,7 @@ public function __construct(CliDumper $dumper)

public function describe(OutputInterface $output, Data $data, array $context, int $clientId): void
{
$io = new SymfonyStyle(new ArrayInput(array()), $output);
$io = $output instanceof SymfonyStyle ? $output : new SymfonyStyle(new ArrayInput(array()), $output);

$rows = array(array('date', date('r', $context['timestamp'])));
$lastIdentifier = $this->lastIdentifier;
Expand Down
18 changes: 8 additions & 10 deletions Command/ServerDumpCommand.php
Expand Up @@ -11,7 +11,6 @@

namespace Symfony\Component\VarDumper\Command;

use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -37,14 +36,14 @@ class ServerDumpCommand extends Command
{
protected static $defaultName = 'server:dump';

private $logger;
private $server;

/** @var DumpDescriptorInterface[] */
private $descriptors;

public function __construct(array $descriptors = array(), LoggerInterface $logger = null)
public function __construct(DumpServer $server, array $descriptors = array())
{
$this->logger = $logger;
$this->server = $server;
$this->descriptors = $descriptors + array(
'cli' => new CliDescriptor(new CliDumper()),
'html' => new HtmlDescriptor(new HtmlDumper()),
Expand All @@ -58,7 +57,7 @@ protected function configure()
$availableFormats = implode(', ', array_keys($this->descriptors));

$this
->addOption('format', null, InputOption::VALUE_REQUIRED, "The output format ($availableFormats)", 'cli')
->addOption('format', null, InputOption::VALUE_REQUIRED, sprintf('The output format (%s)', $availableFormats), 'cli')
->setDescription('Starts a dump server that collects and displays dumps in a single place')
->setHelp(<<<'EOF'
<info>%command.name%</info> starts a dump server that collects and displays
Expand Down Expand Up @@ -88,14 +87,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
$errorIo = $io->getErrorStyle();
$errorIo->title('Symfony Var Dumper Server');

$server = new DumpServer(null, $this->logger);
$server->start();
$this->server->start();

$errorIo->success(sprintf('Server listening on %s', $server->getHost()));
$errorIo->success(sprintf('Server listening on %s', $this->server->getHost()));
$errorIo->comment('Quit the server with CONTROL-C.');

$server->listen(function (Data $data, array $context, int $clientId) use ($descriptor, $output) {
$descriptor->describe($output, $data, $context, $clientId);
$this->server->listen(function (Data $data, array $context, int $clientId) use ($descriptor, $io) {
$descriptor->describe($io, $data, $context, $clientId);
});
}
}
3 changes: 1 addition & 2 deletions Dumper/ServerDumper.php
Expand Up @@ -13,7 +13,6 @@

use Symfony\Component\VarDumper\Cloner\Data;
use Symfony\Component\VarDumper\Dumper\ContextProvider\ContextProviderInterface;
use Symfony\Component\VarDumper\Server\DumpServer;

/**
* ServerDumper forwards serialized Data clones to a server.
Expand Down Expand Up @@ -43,7 +42,7 @@ public function __construct(string $host, DataDumperInterface $wrappedDumper = n
$this->contextProviders = $contextProviders;
}

public function getContextProviders(): ?array
public function getContextProviders(): array
{
return $this->contextProviders;
}
Expand Down
18 changes: 5 additions & 13 deletions Tests/Dumper/ServerDumperTest.php
Expand Up @@ -21,21 +21,13 @@

class ServerDumperTest extends TestCase
{
public static function setUpBeforeClass()
{
putenv('VAR_DUMPER_SERVER=tcp://127.0.0.1:9913');
}

public static function tearDownAfterClass()
{
putenv('VAR_DUMPER_SERVER');
}
private const VAR_DUMPER_SERVER = 'tcp://127.0.0.1:9913';

public function testDumpForwardsToWrappedDumperWhenServerIsUnavailable()
{
$wrappedDumper = $this->getMockBuilder(DataDumperInterface::class)->getMock();

$dumper = new ServerDumper(null, $wrappedDumper);
$dumper = new ServerDumper(self::VAR_DUMPER_SERVER, $wrappedDumper);

$cloner = new VarCloner();
$data = $cloner->cloneVar('foo');
Expand All @@ -47,7 +39,7 @@ public function testDumpForwardsToWrappedDumperWhenServerIsUnavailable()

public function testIsServerListening()
{
$dumper = new ServerDumper();
$dumper = new ServerDumper(self::VAR_DUMPER_SERVER);

$this->assertFalse($dumper->isServerListening());

Expand All @@ -73,7 +65,7 @@ public function testDump()

$cloner = new VarCloner();
$data = $cloner->cloneVar('foo');
$dumper = new ServerDumper(null, $wrappedDumper, array(
$dumper = new ServerDumper(self::VAR_DUMPER_SERVER, $wrappedDumper, array(
'foo_provider' => new class() implements ContextProviderInterface {
public function getContext(): ?array
{
Expand Down Expand Up @@ -117,7 +109,7 @@ private function getServerProcess(): Process
{
$process = new PhpProcess(file_get_contents(__DIR__.'/../Fixtures/dump_server.php'), null, array(
'COMPONENT_ROOT' => __DIR__.'/../../',
'VAR_DUMPER_SERVER' => 'tcp://127.0.0.1:9913',
'VAR_DUMPER_SERVER' => self::VAR_DUMPER_SERVER,
));
$process->inheritEnvironmentVariables(true);

Expand Down
2 changes: 1 addition & 1 deletion Tests/Fixtures/dump_server.php
Expand Up @@ -25,7 +25,7 @@
$dumper->dump($data);
});

$server = new DumpServer();
$server = new DumpServer(getenv('VAR_DUMPER_SERVER'));

$server->start();

Expand Down

0 comments on commit 1980802

Please sign in to comment.