8000 [VarDumper][PhpUnitBridge] VarDumperServerListener by ogizanagi · Pull Request #26696 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[VarDumper][PhpUnitBridge] VarDumperServerListener #26696

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

8000
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8000 39 changes: 39 additions & 0 deletions src/Symfony/Bridge/PhpUnit/Legacy/VarDumperServerListenerForV5.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\PhpUnit\Legacy;

use Symfony\Component\VarDumper\Dumper\ContextProvider\ContextProviderInterface;

/**
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
*
* @internal
*/
class VarDumperServerListenerForV5 extends \PHPUnit_Framework_BaseTestListener implements ContextProviderInterface
{
private $trait;

public function __construct(string $host = null)
{
$this->trait = new VarDumperServerListenerTrait($host);
}

public function startTest(\PHPUnit_Framework_Test $test)
{
$this->trait->startTest($test);
}

public function getContext(): ?array
{
$this->trait->getContext();
}
}
41 changes: 41 additions & 0 deletions 8000 src/Symfony/Bridge/PhpUnit/Legacy/VarDumperServerListenerForV6.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\PhpUnit\Legacy;

use PHPUnit\Framework\BaseTestListener;
use PHPUnit\Framework\Test;
use Symfony\Component\VarDumper\Dumper\ContextProvider\ContextProviderInterface;

/**
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
*
* @internal
*/
class VarDumperServerListenerForV6 extends BaseTestListener implements ContextProviderInterface
{
private $trait;

public function __construct(string $host = null)
{
$this->trait = new VarDumperServerListenerTrait($host);
}

public function startTest(Test $test)
{
$this->trait->startTest($test);
}

public function getContext(): ?array
{
$this->trait->getContext();
}
}
44 changes: 44 additions & 0 deletions src/Symfony/Bridge/PhpUnit/Legacy/VarDumperServerListenerForV7.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\PhpUnit\Legacy;

use PHPUnit\Framework\TestListener;
use PHPUnit\Framework\TestListenerDefaultImplementation;
use PHPUnit\Framework\TestSuite;
use Symfony\Component\VarDumper\Dumper\ContextProvider\ContextProviderInterface;

/**
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
*
* @internal
*/
class VarDumperServerListenerForV7 implements TestListener, ContextProviderInterface
{
use TestListenerDefaultImplementation;

private $trait;

public function __construct(string $host = null)
{
$this->trait = new VarDumperServerListenerTrait($host);
}

public function startTestSuite(TestSuite $suite): void
{
$this->trait->startTest($suite);
}

public function getContext(): ?array
{
$this->trait->getContext();
}
}
60 changes: 60 additions & 0 deletions src/Symfony/Bridge/PhpUnit/Legacy/VarDumperServerListenerTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\PhpUnit\Legacy;

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\VarDumperServerListener;
use Symfony\Component\VarDumper\Dumper\ServerDumper;

/**
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
*
* @internal
*/
class VarDumperServerListenerTrait
{
/** @var TestCase|null */
private $currentTestCase;

public function __construct(string $host = null)
{
if (!class_exists(ServerDumper::class)) {
throw new \LogicException(sprintf('The "%s" class is required for using the "%s" listener. Install "symfony/var-dumper" version 4.1 or above.', ServerDumper::class, VarDumperServerListener::class));
}

ServerDumper::register($host, true, array('phpunit' => $this));
}

public function startTest($test)
{
if (!$test instanceof TestCase) {
$this->currentTestCase = null;

return;
}

$this->currentTestCase = $test;
}

public function getContext(): ?array
{
if (!$this->currentTestCase) {
return null;
}

return array(
'identifier' => spl_object_hash($this->currentTestCase),
'test_class' => \get_class($this->currentTestCase),
'test_case' => $this->currentTestCase->getName(),
);
}
}
26 changes: 26 additions & 0 deletions src/Symfony/Bridge/PhpUnit/VarDumperServerListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\PhpUnit;

if (class_exists('PHPUnit_Runner_Version') && version_compare(\PHPUnit_Runner_Version::id(), '6.0.0', '<')) {
class_alias('Symfony\Bridge\PhpUnit\Legacy\VarDumperServerListenerForV5', 'Symfony\Bridge\PhpUnit\VarDumperServerListener');
} elseif (version_compare(\PHPUnit\Runner\Version::id(), '7.0.0', '<')) {
class_alias('Symfony\Bridge\PhpUnit\Legacy\VarDumperServerListenerForV6', 'Symfony\Bridge\PhpUnit\VarDumperServerListener');
} else {
class_alias('Symfony\Bridge\PhpUnit\Legacy\VarDumperServerListenerForV7', 'Symfony\Bridge\PhpUnit\VarDumperServerListener');
}

if (false) {
class VarDumperServerListener
{
}
}
1 change: 1 addition & 0 deletions src/Symfony/Bridge/PhpUnit/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
},
"suggest": {
"symfony/debug": "For tracking deprecated interfaces usages at runtime with DebugClassLoader",
"symfony/var-dumper": "To use the VarDumperServerListener",
"ext-zip": "Zip support is required when using bin/simple-phpunit"
},
"conflict": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ public function describe(OutputInterface $output, Data $data, array $context, in
$this->lastIdentifier = $clientId;

$section = "Received from client #$clientId";
if (isset($context['request'])) {
if (isset($context['phpunit'])) {
$phpunit = $context['phpunit'];
$this->lastIdentifier = $phpunit['identifier'];
$section = sprintf('%s::%s', $phpunit['test_class'], $phpunit['test_case']);
} elseif (isset($context['request'])) {
$request = $context['request'];
$this->lastIdentifier = $request['identifier'];
$section = sprintf('%s %s', $request['method'], $request['uri']);
Expand Down
Original file line number Diff line number Diff line change
F438 Expand Up @@ -42,7 +42,11 @@ public function describe(OutputInterface $output, Data $data, array $context, in
}

$title = '-';
if (isset($context['request'])) {
if (isset($context['phpunit'])) {
$phpunit = $context['phpunit'];
$title = '<code>PhpUnit</code>'.sprintf('%s::%s', $phpunit['test_class'], $phpunit['test_case']);
$dedupIdentifier = $phpunit['identifier'];
} elseif (isset($context['request'])) {
$request = $context['request'];
$title = sprintf('<code>%s</code> <a href="%s">%s</a>', $request['method'], $uri = $request['uri'], $uri);
$dedupIdentifier = $request['identifier'];
Expand Down
44 changes: 44 additions & 0 deletions src/Symfony/Component/VarDumper/Dumper/ServerDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,16 @@

namespace Symfony\Component\VarDumper\Dumper;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Debug\FileLinkFormatter;
use Symfony\Component\VarDumper\Cloner\Data;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\ContextProvider\CliContextProvider;
use Symfony\Component\VarDumper\Dumper\ContextProvider\ContextProviderInterface;
use Symfony\Component\VarDumper\Dumper\ContextProvider\RequestContextProvider;
use Symfony\Component\VarDumper\Dumper\ContextProvider\SourceContextProvider;
use Symfony\Component\VarDumper\VarDumper;

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

/**
* @final
*/
public static function getDefaultContextProviders(Request $request = null, string $projectDir = null): array
{
$contextProviders = array();

if ('cli' !== PHP_SAPI && ($request || class_exists(Request::class))) {
$requestStack = new RequestStack();
$requestStack->push($request ?? Request::createFromGlobals());
$contextProviders['request'] = new RequestContextProvider($requestStack);
}

$fileLinkFormatter = class_exists(FileLinkFormatter::class) ? new FileLinkFormatter(null, $requestStack ?? null, $projectDir) : null;

return $contextProviders + array(
'cli' => new CliContextProvider(),
'source' => new SourceContextProvider(null, $projectDir, $fileLinkFormatter),
);
}

/**
* @final
*/
public static function register(string $host = null, bool $lock = false, array $contextProviders = array()): ?callable
{
$contextProviders += self::getDefaultContextProviders();
$host = $host ?? $_SERVER['VAR_DUMPER_SERVER'] ?? '127.0.0.1:9912';
$cloner = new VarCloner();
$dumper = new self($host, VarDumper::getDefaultDumper(), $contextProviders);

return VarDumper::setHandler(function ($var) use ($dumper, $cloner) {
$dumper->dump($cloner->cloneVar($var));
}, $lock);
}

public function getContextProviders(): array
{
return $this->contextProviders;
Expand Down
26 changes: 24 additions & 2 deletions src/Symfony/Component/VarDumper/VarDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\CliDumper;
use Symfony\Component\VarDumper\Dumper\DataDumperInterface;
use Symfony\Component\VarDumper\Dumper\HtmlDumper;

// Load the global dump() function
Expand All @@ -24,12 +25,13 @@
class VarDumper
{
private static $handler;
private static $locked = false;

public static function dump($var)
{
if (null === self::$handler) {
$cloner = new VarCloner();
$dumper = \in_array(PHP_SAPI, array('cli', 'phpdbg'), true) ? new CliDumper() : new HtmlDumper();
$dumper = self::getDefaultDumper();
self::$handler = function ($var) use ($cloner, $dumper) {
$dumper->dump($cloner->cloneVar($var));
};
Expand All @@ -38,11 +40,31 @@ public static function dump($var)
return call_user_func(self::$handler, $var);
}

public static function setHandler(callable $callable = null)
/**
* @final since 4.1
*/
public static function setHandler(callable $callable = null/*, bool $lock = false*/)/*: ?callable*/
{
$lock = \func_num_args() > 1 ? func_get_arg(1) : false;
$prevHandler = self::$handler;

if (self::$locked) {
return $prevHandler;
}
if ($lock) {
self::$locked = true;
}

self::$handler = $callable;

return $prevHandler;
}

/**
* @final
*/
public static function getDefaultDumper(): DataDumperInterface
{
return \in_array(PHP_SAPI, array('cli', 'phpdbg'), true) ? new CliDumper() : new HtmlDumper();
}
}
0