Skip to content
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

[FrameworkBundle][HttpFoundation] reduce response constraints verbosity #49184

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -28,24 +28,24 @@
*/
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
{
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);
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpFoundation/CHANGELOG.md
Expand Up @@ -8,6 +8,7 @@ CHANGELOG
* Add `QueryParameterRequestMatcher`
* Add `HeaderRequestMatcher`
* Add support for `\SplTempFileObject` in `BinaryFileResponse`
* Add `verbose` argument to response test constraints

7.0
---
Expand Down
Expand Up @@ -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';
Expand All @@ -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];
}
}
}
Expand Up @@ -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';
Expand All @@ -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];
}
}
}
Expand Up @@ -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';
Expand All @@ -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];
}
}
}
Expand Up @@ -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;
}
Expand All @@ -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];
}
}
}
Expand Up @@ -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;
}
Expand Down
Expand Up @@ -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;
}
Expand Down
Expand Up @@ -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;

Expand All @@ -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();
}
}
Expand Up @@ -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;
}
}
}