Skip to content

Commit 8fffa2c

Browse files
dunglasfabpot
authored andcommitted
[FrameworkBundle][HttpFoundation] add assertResponseFormatSame()
1 parent e905b8d commit 8fffa2c

File tree

6 files changed

+162
-0
lines changed

6 files changed

+162
-0
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ CHANGELOG
77
* Added support for configuring PHP error level to log levels
88
* Added the `dispatcher` option to `debug:event-dispatcher`
99
* Added the `event_dispatcher.dispatcher` tag
10+
* Added `assertResponseFormatSame()` in `BrowserKitAssertionsTrait`
1011

1112
5.2.0
1213
-----

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ public static function assertResponseStatusCodeSame(int $expectedCode, string $m
3838
self::assertThatForResponse(new ResponseConstraint\ResponseStatusCodeSame($expectedCode), $message);
3939
}
4040

41+
public static function assertResponseFormatSame(?string $expectedFormat, string $message = ''): void
42+
{
43+
self::assertThatForResponse(new ResponseConstraint\ResponseFormatSame(self::getRequest(), $expectedFormat), $message);
44+
}
45+
4146
public static function assertResponseRedirects(string $expectedLocation = null, int $expectedCode = null, string $message = ''): void
4247
{
4348
$constraint = new ResponseConstraint\ResponseIsRedirected();

src/Symfony/Bundle/FrameworkBundle/Tests/Test/WebTestCaseTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Symfony\Component\HttpFoundation\Cookie as HttpFoundationCookie;
2424
use Symfony\Component\HttpFoundation\Request;
2525
use Symfony\Component\HttpFoundation\Response;
26+
use Symfony\Component\HttpFoundation\Test\Constraint\ResponseFormatSame;
2627

2728
class WebTestCaseTest extends TestCase
2829
{
@@ -75,6 +76,20 @@ public function testAssertResponseRedirectsWithLocationAndStatusCode()
7576
$this->getResponseTester(new Response('', 302))->assertResponseRedirects('https://example.com/', 301);
7677
}
7778

79+
public function testAssertResponseFormat()
80+
{
81+
if (!class_exists(ResponseFormatSame::class)) {
82+
$this->markTestSkipped('Too old version of HttpFoundation.');
83+
}
84+
85+
$this->getResponseTester(new Response('', 200, ['Content-Type' => 'application/vnd.myformat']))->assertResponseFormatSame('custom');
86+
$this->getResponseTester(new Response('', 200, ['Content-Type' => 'application/ld+json']))->assertResponseFormatSame('jsonld');
87+
$this->getResponseTester(new Response())->assertResponseFormatSame(null);
88+
$this->expectException(AssertionFailedError::class);
89+
$this->expectExceptionMessage("Failed asserting that the Response format is jsonld.\nHTTP/1.0 200 OK");
90+
$this->getResponseTester(new Response())->assertResponseFormatSame('jsonld');
91+
}
92+
7893
public function testAssertResponseHasHeader()
7994
{
8095
$this->getResponseTester(new Response())->assertResponseHasHeader('Date');
@@ -284,6 +299,10 @@ private function getResponseTester(Response $response): WebTestCase
284299
$client = $this->createMock(KernelBrowser::class);
285300
$client->expects($this->any())->method('getResponse')->willReturn($response);
286301

302+
$request = new Request();
303+
$request->setFormat('custom', ['application/vnd.myformat']);
304+
$client->expects($this->any())->method('getRequest')->willReturn($request);
305+
287306
return $this->getTester($client);
288307
}
289308

src/Symfony/Component/HttpFoundation/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5.3.0
5+
-----
6+
7+
* added `ResponseFormatSame` PHPUnit constraint
8+
49
5.2.0
510
-----
611

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpFoundation\Test\Constraint;
13+
14+
use PHPUnit\Framework\Constraint\Constraint;
15+
use Symfony\Component\HttpFoundation\Request;
16+
use Symfony\Component\HttpFoundation\Response;
17+
18+
/**
19+
* Asserts that the response is in the given format.
20+
*
21+
* @author Kévin Dunglas <dunglas@gmail.com>
22+
*/
23+
final class ResponseFormatSame extends Constraint
24+
{
25+
private $request;
26+
private $format;
27+
28+
public function __construct(Request $request, ?string $format)
29+
{
30+
$this->request = $request;
31+
$this->format = $format;
32+
}
33+
34+
/**
35+
* {@inheritdoc}
36+
*/
37+
public function toString(): string
38+
{
39+
return 'format is '.($this->format ?? 'null');
40+
}
41+
42+
/**
43+
* @param Response $response
44+
*
45+
* {@inheritdoc}
46+
*/
47+
protected function matches($response): bool
48+
{
49+
return $this->format === $this->request->getFormat($response->headers->get('Content-Type'));
50+
}
51+
52+
/**
53+
* @param Response $response
54+
*
55+
* {@inheritdoc}
56+
*/
57+
protected function failureDescription($response): string
58+
{
59+
return 'the Response '.$this->toString();
60+
}
61+
62+
/**
63+
* @param Response $response
64+
*
65+
* {@inheritdoc}
66+
*/
67+
protected function additionalFailureDescription($response): string
68+
{
69+
return (string) $response;
70+
}
71+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpFoundation\Tests\Test\Constraint;
13+
14+
use PHPUnit\Framework\ExpectationFailedException;
15+
use PHPUnit\Framework\TestCase;
16+
use PHPUnit\Framework\TestFailure;
17+
use Symfony\Component\HttpFoundation\Request;
18+
use Symfony\Component\HttpFoundation\Response;
19+
use Symfony\Component\HttpFoundation\Test\Constraint\ResponseFormatSame;
20+
21+
/**
22+
* @author Kévin Dunglas <kevin@dunglas.fr>
23+
*/
24+
class ResponseFormatSameTest extends TestCase
25+
{
26+
public function testConstraint()
27+
{
28+
$request = new Request();
29+
$request->setFormat('custom', ['application/vnd.myformat']);
30+
31+
$constraint = new ResponseFormatSame($request, 'custom');
32+
$this->assertTrue($constraint->evaluate(new Response('', 200, ['Content-Type' => 'application/vnd.myformat']), '', true));
33+
$this->assertFalse($constraint->evaluate(new Response(), '', true));
34+
35+
try {
36+
$constraint->evaluate(new Response('', 200, ['Content-Type' => 'application/ld+json']));
37+
} catch (ExpectationFailedException $e) {
38+
$this->assertStringContainsString("Failed asserting that the Response format is custom.\nHTTP/1.0 200 OK", TestFailure::exceptionToString($e));
39+
40+
return;
41+
}
42+
43+
$this->fail();
44+
}
45+
46+
public function testNullFormat()
47+
{
48+
$constraint = new ResponseFormatSame(new Request(), null);
49+
$this->assertTrue($constraint->evaluate(new Response(), '', true));
50+
51+
try {
52+
$constraint->evaluate(new Response('', 200, ['Content-Type' => 'application/ld+json']));
53+
} catch (ExpectationFailedException $e) {
54+
$this->assertStringContainsString("Failed asserting that the Response format is null.\nHTTP/1.0 200 OK", TestFailure::exceptionToString($e));
55+
56+
return;
57+
}
58+
59+
$this->fail();
60+
}
61+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy