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

[VarDumper][PhpUnitBridge] VarDumperServerListener #26696

wants to merge 2 commits into from

Conversation

ogizanagi
Copy link
Contributor
@ogizanagi ogizanagi commented Mar 28, 2018
Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets N/A
License MIT
Doc PR N/A

Based on previous PR (#26695) about easy server dumper as main handler registration.

Keeping exploring different usages for the server dumper feature, this provides a PhpUnit listener you can configure to automatically register the server dumper (allowing to get better context about dumps, cli & html format & avoid mixing the test suite output with wild cli dumps).

Considering the following changes in the symfony-demo
diff --git a/phpunit.xml.dist b/phpunit.xml.dist
index d545bf4..90e9a18 100644
--- a/phpunit.xml.dist
+++ b/phpunit.xml.dist
@@ -34,5 +34,6 @@
         <!-- it begins a database transaction before every testcase and rolls it back after
              the test finished, so tests can manipulate the database without affecting other tests -->
         <listener class="\DAMA\DoctrineTestBundle\PHPUnit\PHPUnitListener" />
+        <listener class="\Symfony\Bridge\PhpUnit\VarDumperServerListener" />
     </listeners>
 </phpunit>
diff --git a/src/Controller/BlogController.php b/src/Controller/BlogController.php
index e3e30aa..e2f4633 100644
--- a/src/Controller/BlogController.php
+++ b/src/Controller/BlogController.php
@@ -50,6 +50,7 @@ class BlogController extends AbstractController
      */
     public function index(int $page, string $_format, PostRepository $posts): Response
     {
+        dump(__METHOD__);
         $la
8000
testPosts = $posts->findLatest($page);

         // Every template name also has two extensions that specify the format and
diff --git a/tests/Controller/BlogControllerTest.php b/tests/Controller/BlogControllerTest.php
index 822cd43..04d5e1c 100644
--- a/tests/Controller/BlogControllerTest.php
+++ b/tests/Controller/BlogControllerTest.php
@@ -28,14 +28,19 @@ class BlogControllerTest extends WebTestCase
 {
     public function testIndex()
     {
+        dump('Start '.__METHOD__);
         $client = static::createClient();
         $crawler = $client->request('GET', '/en/blog/');

+        dump('After request '.__METHOD__);
+
         $this->assertCount(
             Post::NUM_ITEMS,
             $crawler->filter('article.post'),
             'The homepage displays the right number of posts.'
         );
+
+        dump('End '.__METHOD__);
     }

     public function testRss()

running vendor/bin/simple-phpunit --filter=BlogControllerTest::testIndex and with server up will generate:

CLI HTML
capture d ecran 2018-03-28 a 14 50 39 capture d ecran 2018-03-28 a 14 49 17 (Note: the HTML output shows most recent dumps first)

giving proper context about the test case in dedicated output instead of this mess:

PHPUnit 6.5.7 by Sebastian Bergmann and contributors.

Testing Project Test Suite
"Start App\Tests\Controller\BlogControllerTest::testIndex"
"App\Controller\BlogController::index"
"After request App\Tests\Controller\BlogControllerTest::testIndex"
"End App\Tests\Controller\BlogControllerTest::testIndex"
.                                                                   1 / 1 (100%)

Time: 267 ms, Memory: 24.00MB

(of course the output is quite simple here)

However, by setting the debug.dump_destination: on test env too, and by not locking the handler from the PhpUnit listener, you'll get better context about the controller's dump:

capture d ecran 2018-03-28 a 14 56 06

but right after the request is executed, the server dumper in use still is the one registered by the DebugBundle, so we're now loosing the PhpUnit context.
So, right now, I don't know how to get the best of both worlds. ogizanagi@23a80bb is an attempt to this, but is not bullet-proof.

Any feedback welcome.

@Simperfit
Copy link
Contributor

Travis failure is not linked to the changes.

@ogizanagi
Copy link
Contributor Author

Closing for now as I didn't find a satisfying enough solution at the time I wrote this PR and didn't take the time to explore more options.

@ogizanagi ogizanagi closed this Jul 13, 2018
@ogizanagi ogizanagi deleted the feature/phpunit_var_dumper_server_listener branch July 13, 2018 10:47
@nicolas-grekas nicolas-grekas modified the milestones: next, 4.2 Nov 1, 2018
fabpot added a commit that referenced this pull request Aug 13, 2020
…nagi)

This PR was squashed before being merged into the 5.2-dev branch.

Discussion
----------

[VarDumper] Add VAR_DUMPER_FORMAT=server format

| Q             | A
| ------------- | ---
| Branch?       | master<!-- see below -->
| Bug fix?      | no
| New feature?  | yes<!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       | Fix #35801 <!-- prefix each issue number with "Fix #", if any -->
| License       | MIT
| Doc PR        | TODO <!-- required for new features -->

This PR follows discussion in #35801 and adds support for a `server` value for the existing `VAR_DUMPER_FORMAT` env var.

It comes as well with two more things:
- ~~the handler is registered as soon as the `VAR_DUMPER_FORMAT` env var is detected~~ we prevent registering another handler as soon as the `VAR_DUMPER_FORMAT` env var is set, instead of checking if there was a previous handler (which could make this env var useless in some conditions where the handler was already set by another "process")
- the handler registered this way cannot be replaced. This prevents another "process" to takeover dump handling while undesired. E.g: a phpunit functional test booting the kernel to call an endpoint => the handler is replaced. It's (in a sense) a satisfying alternative to #26696

This PR means anyone can use dump with a server in any context, without changing a single line of code in the project by:
- starting the server using `./vendor/bin/var-dump-server --format=html > dumps.html`
- using the env var: `VAR_DUMPER_FORMAT=server [your-cli-command]`

---
Previous related PRs:
- #26695
- #26696

Commits
-------

82df6db [VarDumper] Add VAR_DUMPER_FORMAT=server format
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0