diff --git a/src/Symfony/Bundle/FrameworkBundle/Test/BrowserKitAssertionsTrait.php b/src/Symfony/Bundle/FrameworkBundle/Test/BrowserKitAssertionsTrait.php index a6d4fed3377a4..1a67971d51d13 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Test/BrowserKitAssertionsTrait.php +++ b/src/Symfony/Bundle/FrameworkBundle/Test/BrowserKitAssertionsTrait.php @@ -79,7 +79,7 @@ public static function assertResponseHeaderSame(string $headerName, string $expe public static function assertResponseHeaderNotSame(string $headerName, string $expectedValue, string $message = ''): void { - self::assertThatForResponse(new LogicalNot(new ResponseConstraint\ResponseHeaderSame($headerName, $expectedValue)), $message); + self::assertThatForResponse(new LogicalNot(new ResponseConstraint\ResponseHeaderSame($headerName, $expectedValue, true)), $message); } public static function assertResponseHasCookie(string $name, string $path = '/', string $domain = null, string $message = ''): void diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Test/WebTestCaseTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Test/WebTestCaseTest.php index 54a8044b02473..539350858ebe5 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Test/WebTestCaseTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Test/WebTestCaseTest.php @@ -121,7 +121,7 @@ public function testAssertResponseHeaderSame() { $this->getResponseTester(new Response())->assertResponseHeaderSame('Cache-Control', 'no-cache, private'); $this->expectException(AssertionFailedError::class); - $this->expectExceptionMessage('Failed asserting that the Response has header "Cache-Control" with value "public".'); + $this->expectExceptionMessage('Failed asserting that the Response has header "Cache-Control" with value "public", value of header "Cache-Control" is "no-cache, private".'); $this->getResponseTester(new Response())->assertResponseHeaderSame('Cache-Control', 'public'); } diff --git a/src/Symfony/Component/HttpFoundation/Test/Constraint/ResponseHeaderSame.php b/src/Symfony/Component/HttpFoundation/Test/Constraint/ResponseHeaderSame.php index 8141df9722b64..df7f9b9f5eb60 100644 --- a/src/Symfony/Component/HttpFoundation/Test/Constraint/ResponseHeaderSame.php +++ b/src/Symfony/Component/HttpFoundation/Test/Constraint/ResponseHeaderSame.php @@ -18,16 +18,29 @@ final class ResponseHeaderSame extends Constraint { private string $headerName; private string $expectedValue; + private bool $logicalNot; + private ?string $actualValue = null; - public function __construct(string $headerName, string $expectedValue) + public function __construct(string $headerName, string $expectedValue, bool $logicalNot = false) { $this->headerName = $headerName; $this->expectedValue = $expectedValue; + $this->logicalNot = $logicalNot; } public function toString(): string { - return sprintf('has header "%s" with value "%s"', $this->headerName, $this->expectedValue); + $output = sprintf('has header "%s" with value "%s"', $this->headerName, $this->expectedValue); + + if (null === $this->actualValue) { + $output .= sprintf(', header "%s" is not set', $this->headerName); + } + + if (null !== $this->actualValue && !$this->logicalNot) { + $output .= sprintf(', value of header "%s" is "%s"', $this->headerName, $this->actualValue); + } + + return $output; } /** @@ -35,7 +48,9 @@ public function toString(): string */ protected function matches($response): bool { - return $this->expectedValue === $response->headers->get($this->headerName, null); + $this->actualValue = $response->headers->get($this->headerName); + + return $this->expectedValue === $this->actualValue; } /** diff --git a/src/Symfony/Component/HttpFoundation/Tests/Test/Constraint/ResponseHeaderSameTest.php b/src/Symfony/Component/HttpFoundation/Tests/Test/Constraint/ResponseHeaderSameTest.php index 17a3f2a99bc90..dff0acf98df13 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Test/Constraint/ResponseHeaderSameTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Test/Constraint/ResponseHeaderSameTest.php @@ -19,17 +19,48 @@ class ResponseHeaderSameTest extends TestCase { - public function testConstraint() + public function testResponseHeaderSameWithExpectedHeaderValueIsSame() { - $constraint = new ResponseHeaderSame('Cache-Control', 'no-cache, private'); - $this->assertTrue($constraint->evaluate(new Response(), '', true)); - $constraint = new ResponseHeaderSame('Cache-Control', 'public'); - $this->assertFalse($constraint->evaluate(new Response(), '', true)); + $constraint = new ResponseHeaderSame('X-Token', 'custom-token'); + + $response = new Response(); + $response->headers->set('X-Token', 'custom-token'); + + $this->assertTrue($constraint->evaluate($response, '', true)); + } + + public function testResponseHeaderSameWithExpectedHeaderValueIsDifferent() + { + $constraint = new ResponseHeaderSame('X-Token', 'custom-token'); + + $response = new Response(); + $response->headers->set('X-Token', 'default-token'); + + $this->assertFalse($constraint->evaluate($response, '', true)); + + try { + $constraint->evaluate($response); + } catch (ExpectationFailedException $e) { + $this->assertEquals("Failed asserting that the Response has header \"X-Token\" with value \"custom-token\", value of header \"X-Token\" is \"default-token\".\n", TestFailure::exceptionToString($e)); + + return; + } + + $this->fail(); + } + + public function testResponseHeaderSameWithExpectedHeaderIsNotPresent() + { + $constraint = new ResponseHeaderSame('X-Token', 'custom-token'); + + $response = new Response(); + + $this->assertFalse($constraint->evaluate($response, '', true)); try { - $constraint->evaluate(new Response()); + $constraint->evaluate($response); } catch (ExpectationFailedException $e) { - $this->assertEquals("Failed asserting that the Response has header \"Cache-Control\" with value \"public\".\n", TestFailure::exceptionToString($e)); + $this->assertEquals("Failed asserting that the Response has header \"X-Token\" with value \"custom-token\", header \"X-Token\" is not set.\n", TestFailure::exceptionToString($e)); return; }