Skip to content

Commit

Permalink
[HttpFoundation] Decrease response test constraint verbosity
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Appriou committed Feb 1, 2023
1 parent 61e6bff commit 8408eed
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpFoundation/CHANGELOG.md
Expand Up @@ -7,6 +7,7 @@ CHANGELOG
* Add `ParameterBag::getEnum()`
* Create migration for session table when pdo handler is used
* Add support for Relay PHP extension for Redis
* Add `verbose` argument to response test constraints

6.2
---
Expand Down
10 changes: 9 additions & 1 deletion src/Symfony/Component/HttpFoundation/Response.php
Expand Up @@ -236,7 +236,7 @@ public function __construct(?string $content = '', int $status = 200, array $hea
public function __toString(): string
{
return
sprintf('HTTP/%s %s %s', $this->version, $this->statusCode, $this->statusText)."\r\n".
$this->getCommandString()."\r\n".
$this->headers."\r\n".
$this->getContent();
}
Expand All @@ -249,6 +249,14 @@ public function __clone()
$this->headers = clone $this->headers;
}

/**
* @return string The first line of the actual HTTP request
*/
public function getCommandString(): string
{
return sprintf('HTTP/%s %s %s', $this->version, $this->statusCode, $this->statusText);
}

/**
* Prepares the Response before it is sent to the client.
*
Expand Down
Expand Up @@ -16,6 +16,13 @@

final class ResponseIsRedirected extends Constraint
{
/**
* @param booll $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 @@ -42,6 +49,6 @@ protected function failureDescription($response): string
*/
protected function additionalFailureDescription($response): string
{
return (string) $response;
return $this->verbose ? (string) $response : $response->getCommandString()."\r\n".$response->headers;
}
}
Expand Up @@ -16,6 +16,13 @@

final class ResponseIsSuccessful extends Constraint
{
/**
* @param booll $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 @@ -42,6 +49,6 @@ protected function failureDescription($response): string
*/
protected function additionalFailureDescription($response): string
{
return (string) $response;
return $this->verbose ? (string) $response : $response->getCommandString()."\r\n".$response->headers;
}
}
Expand Up @@ -16,6 +16,10 @@

final class ResponseIsUnprocessable extends Constraint
{
public function __construct(private readonly bool $verbose = true)
{
}

public function toString(): string
{
return 'is unprocessable';
Expand All @@ -24,24 +28,24 @@ public function toString(): string
/**
* @param Response $other
*/
protected function matches($other): bool
protected function matches($response): bool
{
return Response::HTTP_UNPROCESSABLE_ENTITY === $other->getStatusCode();
return Response::HTTP_UNPROCESSABLE_ENTITY === $response->getStatusCode();
}

/**
* @param Response $other
*/
protected function failureDescription($other): string
protected function failureDescription($response): string
{
return 'the Response '.$this->toString();
}

/**
* @param Response $other
*/
protected function additionalFailureDescription($other): string
protected function additionalFailureDescription($response): string
{
return (string) $other;
return $this->verbose ? (string) $response : $response->getCommandString()."\r\n".$response->headers;
}
}
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 Down Expand Up @@ -49,6 +49,6 @@ protected function failureDescription($response): string
*/
protected function additionalFailureDescription($response): string
{
return (string) $response;
return $this->verbose ? (string) $response : $response->getCommandString()."\r\n".$response->headers;
}
}
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;
}
}
}

0 comments on commit 8408eed

Please sign in to comment.