Skip to content

Commit cabb2dc

Browse files
Nicolas Apprioufabpot
Nicolas Appriou
authored andcommitted
[FrameworkBundle][HttpFoundation] reduce response constraints verbosity
1 parent c0ecee0 commit cabb2dc

10 files changed

+144
-33
lines changed

src/Symfony/Bundle/FrameworkBundle/Test/BrowserKitAssertionsTrait.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,24 @@
2828
*/
2929
trait BrowserKitAssertionsTrait
3030
{
31-
public static function assertResponseIsSuccessful(string $message = ''): void
31+
public static function assertResponseIsSuccessful(string $message = '', bool $verbose = true): void
3232
{
33-
self::assertThatForResponse(new ResponseConstraint\ResponseIsSuccessful(), $message);
33+
self::assertThatForResponse(new ResponseConstraint\ResponseIsSuccessful($verbose), $message);
3434
}
3535

36-
public static function assertResponseStatusCodeSame(int $expectedCode, string $message = ''): void
36+
public static function assertResponseStatusCodeSame(int $expectedCode, string $message = '', bool $verbose = true): void
3737
{
38-
self::assertThatForResponse(new ResponseConstraint\ResponseStatusCodeSame($expectedCode), $message);
38+
self::assertThatForResponse(new ResponseConstraint\ResponseStatusCodeSame($expectedCode, $verbose), $message);
3939
}
4040

4141
public static function assertResponseFormatSame(?string $expectedFormat, string $message = ''): void
4242
{
4343
self::assertThatForResponse(new ResponseConstraint\ResponseFormatSame(self::getRequest(), $expectedFormat), $message);
4444
}
4545

46-
public static function assertResponseRedirects(?string $expectedLocation = null, ?int $expectedCode = null, string $message = ''): void
46+
public static function assertResponseRedirects(?string $expectedLocation = null, ?int $expectedCode = null, string $message = '', bool $verbose = true): void
4747
{
48-
$constraint = new ResponseConstraint\ResponseIsRedirected();
48+
$constraint = new ResponseConstraint\ResponseIsRedirected($verbose);
4949
if ($expectedLocation) {
5050
if (class_exists(ResponseConstraint\ResponseHeaderLocationSame::class)) {
5151
$locationConstraint = new ResponseConstraint\ResponseHeaderLocationSame(self::getRequest(), $expectedLocation);
@@ -100,9 +100,9 @@ public static function assertResponseCookieValueSame(string $name, string $expec
100100
), $message);
101101
}
102102

103-
public static function assertResponseIsUnprocessable(string $message = ''): void
103+
public static function assertResponseIsUnprocessable(string $message = '', bool $verbose = true): void
104104
{
105-
self::assertThatForResponse(new ResponseConstraint\ResponseIsUnprocessable(), $message);
105+
self::assertThatForResponse(new ResponseConstraint\ResponseIsUnprocessable($verbose), $message);
106106
}
107107

108108
public static function assertBrowserHasCookie(string $name, string $path = '/', ?string $domain = null, string $message = ''): void

src/Symfony/Component/HttpFoundation/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ CHANGELOG
88
* Add `QueryParameterRequestMatcher`
99
* Add `HeaderRequestMatcher`
1010
* Add support for `\SplTempFileObject` in `BinaryFileResponse`
11+
* Add `verbose` argument to response test constraints
1112

1213
7.0
1314
---

src/Symfony/Component/HttpFoundation/Test/Constraint/ResponseIsRedirected.php

+13-5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616

1717
final class ResponseIsRedirected extends Constraint
1818
{
19+
/**
20+
* @param bool $verbose If true, the entire response is printed on failure. If false, the response body is omitted.
21+
*/
22+
public function __construct(private readonly bool $verbose = true)
23+
{
24+
}
25+
1926
public function toString(): string
2027
{
2128
return 'is redirected';
@@ -37,11 +44,12 @@ protected function failureDescription($response): string
3744
return 'the Response '.$this->toString();
3845
}
3946

40-
/**
41-
* @param Response $response
42-
*/
43-
protected function additionalFailureDescription($response): string
47+
protected function additionalFailureDescription($other): string
4448
{
45-
return (string) $response;
49+
if ($this->verbose || !($other instanceof Response)) {
50+
return (string) $other;
51+
} else {
52+
return explode("\r\n\r\n", (string) $other)[0];
53+
}
4654
}
4755
}

src/Symfony/Component/HttpFoundation/Test/Constraint/ResponseIsSuccessful.php

+13-5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616

1717
final class ResponseIsSuccessful extends Constraint
1818
{
19+
/**
20+
* @param bool $verbose If true, the entire response is printed on failure. If false, the response body is omitted.
21+
*/
22+
public function __construct(private readonly bool $verbose = true)
23+
{
24+
}
25+
1926
public function toString(): string
2027
{
2128
return 'is successful';
@@ -37,11 +44,12 @@ protected function failureDescription($response): string
3744
return 'the Response '.$this->toString();
3845
}
3946

40-
/**
41-
* @param Response $response
42-
*/
43-
protected function additionalFailureDescription($response): string
47+
protected function additionalFailureDescription($other): string
4448
{
45-
return (string) $response;
49+
if ($this->verbose || !($other instanceof Response)) {
50+
return (string) $other;
51+
} else {
52+
return explode("\r\n\r\n", (string) $other)[0];
53+
}
4654
}
4755
}

src/Symfony/Component/HttpFoundation/Test/Constraint/ResponseIsUnprocessable.php

+12-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616

1717
final class ResponseIsUnprocessable extends Constraint
1818
{
19+
/**
20+
* @param bool $verbose If true, the entire response is printed on failure. If false, the response body is omitted.
21+
*/
22+
public function __construct(private readonly bool $verbose = true)
23+
{
24+
}
25+
1926
public function toString(): string
2027
{
2128
return 'is unprocessable';
@@ -37,11 +44,12 @@ protected function failureDescription($other): string
3744
return 'the Response '.$this->toString();
3845
}
3946

40-
/**
41-
* @param Response $other
42-
*/
4347
protected function additionalFailureDescription($other): string
4448
{
45-
return (string) $other;
49+
if ($this->verbose || !($other instanceof Response)) {
50+
return (string) $other;
51+
} else {
52+
return explode("\r\n\r\n", (string) $other)[0];
53+
}
4654
}
4755
}

src/Symfony/Component/HttpFoundation/Test/Constraint/ResponseStatusCodeSame.php

+7-6
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ final class ResponseStatusCodeSame extends Constraint
1818
{
1919
private int $statusCode;
2020

21-
public function __construct(int $statusCode)
21+
public function __construct(int $statusCode, private readonly bool $verbose = true)
2222
{
2323
$this->statusCode = $statusCode;
2424
}
@@ -44,11 +44,12 @@ protected function failureDescription($response): string
4444
return 'the Response '.$this->toString();
4545
}
4646

47-
/**
48-
* @param Response $response
49-
*/
50-
protected function additionalFailureDescription($response): string
47+
protected function additionalFailureDescription($other): string
5148
{
52-
return (string) $response;
49+
if ($this->verbose || !($other instanceof Response)) {
50+
return (string) $other;
51+
} else {
52+
return explode("\r\n\r\n", (string) $other)[0];
53+
}
5354
}
5455
}

src/Symfony/Component/HttpFoundation/Tests/Test/Constraint/ResponseIsRedirectedTest.php

+20-2
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,27 @@ public function testConstraint()
2727
$this->assertFalse($constraint->evaluate(new Response(), '', true));
2828

2929
try {
30-
$constraint->evaluate(new Response());
30+
$constraint->evaluate(new Response('Body content'));
3131
} catch (ExpectationFailedException $e) {
32-
$this->assertStringContainsString("Failed asserting that the Response is redirected.\nHTTP/1.0 200 OK", TestFailure::exceptionToString($e));
32+
$exceptionMessage = TestFailure::exceptionToString($e);
33+
$this->assertStringContainsString("Failed asserting that the Response is redirected.\nHTTP/1.0 200 OK", $exceptionMessage);
34+
$this->assertStringContainsString('Body content', $exceptionMessage);
35+
36+
return;
37+
}
38+
39+
$this->fail();
40+
}
41+
42+
public function testReducedVerbosity()
43+
{
44+
$constraint = new ResponseIsRedirected(verbose: false);
45+
try {
46+
$constraint->evaluate(new Response('Body content'));
47+
} catch (ExpectationFailedException $e) {
48+
$exceptionMessage = TestFailure::exceptionToString($e);
49+
$this->assertStringContainsString("Failed asserting that the Response is redirected.\nHTTP/1.0 200 OK", $exceptionMessage);
50+
$this->assertStringNotContainsString('Body content', $exceptionMessage);
3351

3452
return;
3553
}

src/Symfony/Component/HttpFoundation/Tests/Test/Constraint/ResponseIsSuccessfulTest.php

+21-2
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,28 @@ public function testConstraint()
2727
$this->assertFalse($constraint->evaluate(new Response('', 404), '', true));
2828

2929
try {
30-
$constraint->evaluate(new Response('', 404));
30+
$constraint->evaluate(new Response('Response body', 404));
3131
} catch (ExpectationFailedException $e) {
32-
$this->assertStringContainsString("Failed asserting that the Response is successful.\nHTTP/1.0 404 Not Found", TestFailure::exceptionToString($e));
32+
$exceptionMessage = TestFailure::exceptionToString($e);
33+
$this->assertStringContainsString("Failed asserting that the Response is successful.\nHTTP/1.0 404 Not Found", $exceptionMessage);
34+
$this->assertStringContainsString('Response body', $exceptionMessage);
35+
36+
return;
37+
}
38+
39+
$this->fail();
40+
}
41+
42+
public function testReducedVerbosity()
43+
{
44+
$constraint = new ResponseIsSuccessful(verbose: false);
45+
46+
try {
47+
$constraint->evaluate(new Response('Response body', 404));
48+
} catch (ExpectationFailedException $e) {
49+
$exceptionMessage = TestFailure::exceptionToString($e);
50+
$this->assertStringContainsString("Failed asserting that the Response is successful.\nHTTP/1.0 404 Not Found", $exceptionMessage);
51+
$this->assertStringNotContainsString('Response body', $exceptionMessage);
3352

3453
return;
3554
}

src/Symfony/Component/HttpFoundation/Tests/Test/Constraint/ResponseIsUnprocessableTest.php

+31
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111

1212
namespace Symfony\Component\HttpFoundation\Tests\Test\Constraint;
1313

14+
use PHPUnit\Framework\ExpectationFailedException;
1415
use PHPUnit\Framework\TestCase;
16+
use PHPUnit\Framework\TestFailure;
1517
use Symfony\Component\HttpFoundation\Response;
1618
use Symfony\Component\HttpFoundation\Test\Constraint\ResponseIsUnprocessable;
1719

@@ -23,5 +25,34 @@ public function testConstraint()
2325

2426
$this->assertTrue($constraint->evaluate(new Response('', 422), '', true));
2527
$this->assertFalse($constraint->evaluate(new Response(), '', true));
28+
29+
try {
30+
$constraint->evaluate(new Response('Response body'));
31+
} catch (ExpectationFailedException $e) {
32+
$exceptionMessage = TestFailure::exceptionToString($e);
33+
$this->assertStringContainsString("Failed asserting that the Response is unprocessable.\nHTTP/1.0 200 OK", $exceptionMessage);
34+
$this->assertStringContainsString('Response body', $exceptionMessage);
35+
36+
return;
37+
}
38+
39+
$this->fail();
40+
}
41+
42+
public function testReducedVerbosity()
43+
{
44+
$constraint = new ResponseIsUnprocessable(verbose: false);
45+
46+
try {
47+
$constraint->evaluate(new Response('Response body'));
48+
} catch (ExpectationFailedException $e) {
49+
$exceptionMessage = TestFailure::exceptionToString($e);
50+
$this->assertStringContainsString("Failed asserting that the Response is unprocessable.\nHTTP/1.0 200 OK", $exceptionMessage);
51+
$this->assertStringNotContainsString('Response body', $exceptionMessage);
52+
53+
return;
54+
}
55+
56+
$this->fail();
2657
}
2758
}

src/Symfony/Component/HttpFoundation/Tests/Test/Constraint/ResponseStatusCodeSameTest.php

+18-1
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,30 @@ public function testConstraint()
2929

3030
$constraint = new ResponseStatusCodeSame(200);
3131
try {
32-
$constraint->evaluate(new Response('', 404));
32+
$constraint->evaluate(new Response('Response body', 404));
3333
} catch (ExpectationFailedException $e) {
34+
$exceptionMessage = TestFailure::exceptionToString($e);
3435
$this->assertStringContainsString("Failed asserting that the Response status code is 200.\nHTTP/1.0 404 Not Found", TestFailure::exceptionToString($e));
36+
$this->assertStringContainsString('Response body', $exceptionMessage);
3537

3638
return;
3739
}
3840

3941
$this->fail();
4042
}
43+
44+
public function testReducedVerbosity()
45+
{
46+
$constraint = new ResponseStatusCodeSame(200, verbose: false);
47+
48+
try {
49+
$constraint->evaluate(new Response('Response body', 404));
50+
} catch (ExpectationFailedException $e) {
51+
$exceptionMessage = TestFailure::exceptionToString($e);
52+
$this->assertStringContainsString("Failed asserting that the Response status code is 200.\nHTTP/1.0 404 Not Found", TestFailure::exceptionToString($e));
53+
$this->assertStringNotContainsString('Response body', $exceptionMessage);
54+
55+
return;
56+
}
57+
}
4158
}

0 commit comments

Comments
 (0)