Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/Symfony/Bundle/Test/ApiTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,20 @@ protected function captureExceptionHandlerStack(): void
$this->symfonyErrorHandlerWasRegistered = self::isSymfonyErrorHandlerRegistered();
}

/**
* Symfony >= 8.2 flips the default verbosity of BrowserKit response assertions to false, so a failing
* assertion (e.g. assertResponseStatusCodeSame()) no longer prints the response body. That default targets
* full HTML pages; API responses are compact JSON payloads whose body is exactly what you need to debug a
* failing test. Keep verbose output on by default here so upgrading Symfony does not silently degrade API
* test failures. Projects can still opt out per suite (setBrowserKitAssertionsAsVerbose(false) in setUp(),
* which runs after this hook) or per assertion (verbose: false).
*/
#[Before]
protected function keepBrowserKitAssertionsVerbose(): void
{
self::setBrowserKitAssertionsAsVerbose(true);
}

#[After]
protected function restoreExceptionHandlerStack(): void
{
Expand Down
17 changes: 17 additions & 0 deletions tests/Symfony/Bundle/Test/ApiTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,23 @@ public function testExplicitContentTypeIsPreserved(): void
$this->assertSame('application/json', $client->getKernelBrowser()->getRequest()->headers->get('Content-Type'));
}

public function testBrowserKitAssertionsStayVerboseByDefault(): void
{
// The trait's static property is flattened into ApiTestCase (which directly uses the assertions trait)
// and shared with subclasses, so read it there rather than on the trait or this subclass.
$verboseMode = new \ReflectionProperty(ApiTestCase::class, 'defaultVerboseMode');

// Simulate the Symfony >= 8.2 default (non-verbose) or a previous test that opted out.
self::setBrowserKitAssertionsAsVerbose(false);
fwrite(\STDOUT, \sprintf("[issue-8450] before ApiTestCase before-hook: defaultVerboseMode = %s\n", var_export($verboseMode->getValue(), true)));

// Re-run the exact before-hook ApiTestCase registers; reverting the fix removes it and this test fails.
$this->keepBrowserKitAssertionsVerbose();

fwrite(\STDOUT, \sprintf("[issue-8450] after ApiTestCase before-hook: defaultVerboseMode = %s\n", var_export($verboseMode->getValue(), true)));
$this->assertTrue($verboseMode->getValue(), 'ApiTestCase must keep BrowserKit assertions verbose so failing API tests still show the response body.');
}

public function testDoNotRebootKernelOnCreateClient(): void
{
self::$alwaysBootKernel = false;
Expand Down
Loading