diff --git a/src/Symfony/Bundle/FrameworkBundle/Test/BrowserKitAssertionsTrait.php b/src/Symfony/Bundle/FrameworkBundle/Test/BrowserKitAssertionsTrait.php index 125aa45a74c0..7eea648cb6ac 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Test/BrowserKitAssertionsTrait.php +++ b/src/Symfony/Bundle/FrameworkBundle/Test/BrowserKitAssertionsTrait.php @@ -28,14 +28,14 @@ */ trait BrowserKitAssertionsTrait { - public static function assertResponseIsSuccessful(string $message = ''): void + public static function assertResponseIsSuccessful(string $message = '', bool $verbose = true): void { - self::assertThatForResponse(new ResponseConstraint\ResponseIsSuccessful(), $message); + self::assertThatForResponse(new ResponseConstraint\ResponseIsSuccessful($verbose), $message); } - public static function assertResponseStatusCodeSame(int $expectedCode, string $message = ''): void + public static function assertResponseStatusCodeSame(int $expectedCode, string $message = '', bool $verbose = true): void { - self::assertThatForResponse(new ResponseConstraint\ResponseStatusCodeSame($expectedCode), $message); + self::assertThatForResponse(new ResponseConstraint\ResponseStatusCodeSame($expectedCode, $verbose), $message); } public static function assertResponseFormatSame(?string $expectedFormat, string $message = ''): void @@ -43,9 +43,9 @@ public static function assertResponseFormatSame(?string $expectedFormat, string self::assertThatForResponse(new ResponseConstraint\ResponseFormatSame(self::getRequest(), $expectedFormat), $message); } - public static function assertResponseRedirects(?string $expectedLocation = null, ?int $expectedCode = null, string $message = ''): void + public static function assertResponseRedirects(?string $expectedLocation = null, ?int $expectedCode = null, string $message = '', bool $verbose = true): void { - $constraint = new ResponseConstraint\ResponseIsRedirected(); + $constraint = new ResponseConstraint\ResponseIsRedirected($verbose); if ($expectedLocation) { if (class_exists(ResponseConstraint\ResponseHeaderLocationSame::class)) { $locationConstraint = new ResponseConstraint\ResponseHeaderLocationSame(self::getRequest(), $expectedLocation); @@ -100,9 +100,9 @@ public static function assertResponseCookieValueSame(string $name, string $expec ), $message); } - public static function assertResponseIsUnprocessable(string $message = ''): void + public static function assertResponseIsUnprocessable(string $message = '', bool $verbose = true): void { - self::assertThatForResponse(new ResponseConstraint\ResponseIsUnprocessable(), $message); + self::assertThatForResponse(new ResponseConstraint\ResponseIsUnprocessable($verbose), $message); } public static function assertBrowserHasCookie(string $name, string $path = '/', ?string $domain = null, string $message = ''): void diff --git a/src/Symfony/Component/HttpFoundation/CHANGELOG.md b/src/Symfony/Component/HttpFoundation/CHANGELOG.md index 3ab25638c30d..5b66f8562592 100644 --- a/src/Symfony/Component/HttpFoundation/CHANGELOG.md +++ b/src/Symfony/Component/HttpFoundation/CHANGELOG.md @@ -8,6 +8,7 @@ CHANGELOG * Add `QueryParameterRequestMatcher` * Add `HeaderRequestMatcher` * Add support for `\SplTempFileObject` in `BinaryFileResponse` + * Add `verbose` argument to response test constraints 7.0 --- diff --git a/src/Symfony/Component/HttpFoundation/Test/Constraint/ResponseIsRedirected.php b/src/Symfony/Component/HttpFoundation/Test/Constraint/ResponseIsRedirected.php index bb0e53d0e6cd..b1abd905e424 100644 --- a/src/Symfony/Component/HttpFoundation/Test/Constraint/ResponseIsRedirected.php +++ b/src/Symfony/Component/HttpFoundation/Test/Constraint/ResponseIsRedirected.php @@ -16,6 +16,13 @@ final class ResponseIsRedirected extends Constraint { + /** + * @param bool $verbose If true, the entire response is printed on failure. If false, the response body is omitted. + */ + public function __construct(private readonly bool $verbose = true) + { + } + public function toString(): string { return 'is redirected'; @@ -37,11 +44,12 @@ protected function failureDescription($response): string return 'the Response '.$this->toString(); } - /** - * @param Response $response - */ - protected function additionalFailureDescription($response): string + protected function additionalFailureDescription($other): string { - return (string) $response; + if ($this->verbose || !($other instanceof Response)) { + return (string) $other; + } else { + return explode("\r\n\r\n", (string) $other)[0]; + } } } diff --git a/src/Symfony/Component/HttpFoundation/Test/Constraint/ResponseIsSuccessful.php b/src/Symfony/Component/HttpFoundation/Test/Constraint/ResponseIsSuccessful.php index 2c6b76806126..d7d6c502a17c 100644 --- a/src/Symfony/Component/HttpFoundation/Test/Constraint/ResponseIsSuccessful.php +++ b/src/Symfony/Component/HttpFoundation/Test/Constraint/ResponseIsSuccessful.php @@ -16,6 +16,13 @@ final class ResponseIsSuccessful extends Constraint { + /** + * @param bool $verbose If true, the entire response is printed on failure. If false, the response body is omitted. + */ + public function __construct(private readonly bool $verbose = true) + { + } + public function toString(): string { return 'is successful'; @@ -37,11 +44,12 @@ protected function failureDescription($response): string return 'the Response '.$this->toString(); } - /** - * @param Response $response - */ - protected function additionalFailureDescription($response): string + protected function additionalFailureDescription($other): string { - return (string) $response; + if ($this->verbose || !($other instanceof Response)) { + return (string) $other; + } else { + return explode("\r\n\r\n", (string) $other)[0]; + } } } diff --git a/src/Symfony/Component/HttpFoundation/Test/Constraint/ResponseIsUnprocessable.php b/src/Symfony/Component/HttpFoundation/Test/Constraint/ResponseIsUnprocessable.php index 52336a5b25db..c791a31bd63c 100644 --- a/src/Symfony/Component/HttpFoundation/Test/Constraint/ResponseIsUnprocessable.php +++ b/src/Symfony/Component/HttpFoundation/Test/Constraint/ResponseIsUnprocessable.php @@ -16,6 +16,13 @@ final class ResponseIsUnprocessable extends Constraint { + /** + * @param bool $verbose If true, the entire response is printed on failure. If false, the response body is omitted. + */ + public function __construct(private readonly bool $verbose = true) + { + } + public function toString(): string { return 'is unprocessable'; @@ -37,11 +44,12 @@ protected function failureDescription($other): string return 'the Response '.$this->toString(); } - /** - * @param Response $other - */ protected function additionalFailureDescription($other): string { - return (string) $other; + if ($this->verbose || !($other instanceof Response)) { + return (string) $other; + } else { + return explode("\r\n\r\n", (string) $other)[0]; + } } } diff --git a/src/Symfony/Component/HttpFoundation/Test/Constraint/ResponseStatusCodeSame.php b/src/Symfony/Component/HttpFoundation/Test/Constraint/ResponseStatusCodeSame.php index cd565ba987b6..8259e09fdf2f 100644 --- a/src/Symfony/Component/HttpFoundation/Test/Constraint/ResponseStatusCodeSame.php +++ b/src/Symfony/Component/HttpFoundation/Test/Constraint/ResponseStatusCodeSame.php @@ -18,7 +18,7 @@ final class ResponseStatusCodeSame extends Constraint { private int $statusCode; - public function __construct(int $statusCode) + public function __construct(int $statusCode, private readonly bool $verbose = true) { $this->statusCode = $statusCode; } @@ -44,11 +44,12 @@ protected function failureDescription($response): string return 'the Response '.$this->toString(); } - /** - * @param Response $response - */ - protected function additionalFailureDescription($response): string + protected function additionalFailureDescription($other): string { - return (string) $response; + if ($this->verbose || !($other instanceof Response)) { + return (string) $other; + } else { + return explode("\r\n\r\n", (string) $other)[0]; + } } } diff --git a/src/Symfony/Component/HttpFoundation/Tests/Test/Constraint/ResponseIsRedirectedTest.php b/src/Symfony/Component/HttpFoundation/Tests/Test/Constraint/ResponseIsRedirectedTest.php index 60590346e262..c4df3bc93a8c 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Test/Constraint/ResponseIsRedirectedTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Test/Constraint/ResponseIsRedirectedTest.php @@ -27,9 +27,27 @@ public function testConstraint() $this->assertFalse($constraint->evaluate(new Response(), '', true)); try { - $constraint->evaluate(new Response()); + $constraint->evaluate(new Response('Body content')); } catch (ExpectationFailedException $e) { - $this->assertStringContainsString("Failed asserting that the Response is redirected.\nHTTP/1.0 200 OK", TestFailure::exceptionToString($e)); + $exceptionMessage = TestFailure::exceptionToString($e); + $this->assertStringContainsString("Failed asserting that the Response is redirected.\nHTTP/1.0 200 OK", $exceptionMessage); + $this->assertStringContainsString('Body content', $exceptionMessage); + + return; + } + + $this->fail(); + } + + public function testReducedVerbosity() + { + $constraint = new ResponseIsRedirected(verbose: false); + try { + $constraint->evaluate(new Response('Body content')); + } catch (ExpectationFailedException $e) { + $exceptionMessage = TestFailure::exceptionToString($e); + $this->assertStringContainsString("Failed asserting that the Response is redirected.\nHTTP/1.0 200 OK", $exceptionMessage); + $this->assertStringNotContainsString('Body content', $exceptionMessage); return; } diff --git a/src/Symfony/Component/HttpFoundation/Tests/Test/Constraint/ResponseIsSuccessfulTest.php b/src/Symfony/Component/HttpFoundation/Tests/Test/Constraint/ResponseIsSuccessfulTest.php index 89c3045f16db..9ecafae7e888 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Test/Constraint/ResponseIsSuccessfulTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Test/Constraint/ResponseIsSuccessfulTest.php @@ -27,9 +27,28 @@ public function testConstraint() $this->assertFalse($constraint->evaluate(new Response('', 404), '', true)); try { - $constraint->evaluate(new Response('', 404)); + $constraint->evaluate(new Response('Response body', 404)); } catch (ExpectationFailedException $e) { - $this->assertStringContainsString("Failed asserting that the Response is successful.\nHTTP/1.0 404 Not Found", TestFailure::exceptionToString($e)); + $exceptionMessage = TestFailure::exceptionToString($e); + $this->assertStringContainsString("Failed asserting that the Response is successful.\nHTTP/1.0 404 Not Found", $exceptionMessage); + $this->assertStringContainsString('Response body', $exceptionMessage); + + return; + } + + $this->fail(); + } + + public function testReducedVerbosity() + { + $constraint = new ResponseIsSuccessful(verbose: false); + + try { + $constraint->evaluate(new Response('Response body', 404)); + } catch (ExpectationFailedException $e) { + $exceptionMessage = TestFailure::exceptionToString($e); + $this->assertStringContainsString("Failed asserting that the Response is successful.\nHTTP/1.0 404 Not Found", $exceptionMessage); + $this->assertStringNotContainsString('Response body', $exceptionMessage); return; } diff --git a/src/Symfony/Component/HttpFoundation/Tests/Test/Constraint/ResponseIsUnprocessableTest.php b/src/Symfony/Component/HttpFoundation/Tests/Test/Constraint/ResponseIsUnprocessableTest.php index 7f22bde345bf..a142ec4b02c4 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Test/Constraint/ResponseIsUnprocessableTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Test/Constraint/ResponseIsUnprocessableTest.php @@ -11,7 +11,9 @@ namespace Symfony\Component\HttpFoundation\Tests\Test\Constraint; +use PHPUnit\Framework\ExpectationFailedException; use PHPUnit\Framework\TestCase; +use PHPUnit\Framework\TestFailure; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Test\Constraint\ResponseIsUnprocessable; @@ -23,5 +25,34 @@ public function testConstraint() $this->assertTrue($constraint->evaluate(new Response('', 422), '', true)); $this->assertFalse($constraint->evaluate(new Response(), '', true)); + + try { + $constraint->evaluate(new Response('Response body')); + } catch (ExpectationFailedException $e) { + $exceptionMessage = TestFailure::exceptionToString($e); + $this->assertStringContainsString("Failed asserting that the Response is unprocessable.\nHTTP/1.0 200 OK", $exceptionMessage); + $this->assertStringContainsString('Response body', $exceptionMessage); + + return; + } + + $this->fail(); + } + + public function testReducedVerbosity() + { + $constraint = new ResponseIsUnprocessable(verbose: false); + + try { + $constraint->evaluate(new Response('Response body')); + } catch (ExpectationFailedException $e) { + $exceptionMessage = TestFailure::exceptionToString($e); + $this->assertStringContainsString("Failed asserting that the Response is unprocessable.\nHTTP/1.0 200 OK", $exceptionMessage); + $this->assertStringNotContainsString('Response body', $exceptionMessage); + + return; + } + + $this->fail(); } } diff --git a/src/Symfony/Component/HttpFoundation/Tests/Test/Constraint/ResponseStatusCodeSameTest.php b/src/Symfony/Component/HttpFoundation/Tests/Test/Constraint/ResponseStatusCodeSameTest.php index 80a4eebfc861..df840f1a821c 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Test/Constraint/ResponseStatusCodeSameTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Test/Constraint/ResponseStatusCodeSameTest.php @@ -29,13 +29,30 @@ public function testConstraint() $constraint = new ResponseStatusCodeSame(200); try { - $constraint->evaluate(new Response('', 404)); + $constraint->evaluate(new Response('Response body', 404)); } catch (ExpectationFailedException $e) { + $exceptionMessage = TestFailure::exceptionToString($e); $this->assertStringContainsString("Failed asserting that the Response status code is 200.\nHTTP/1.0 404 Not Found", TestFailure::exceptionToString($e)); + $this->assertStringContainsString('Response body', $exceptionMessage); return; } $this->fail(); } + + public function testReducedVerbosity() + { + $constraint = new ResponseStatusCodeSame(200, verbose: false); + + try { + $constraint->evaluate(new Response('Response body', 404)); + } catch (ExpectationFailedException $e) { + $exceptionMessage = TestFailure::exceptionToString($e); + $this->assertStringContainsString("Failed asserting that the Response status code is 200.\nHTTP/1.0 404 Not Found", TestFailure::exceptionToString($e)); + $this->assertStringNotContainsString('Response body', $exceptionMessage); + + return; + } + } }