Skip to content

Commit 6c64c85

Browse files
dunglasfabpot
authored andcommitted
[FrameworkBundle][HttpFoundation] add assertResponseFormatSame()
1 parent 4bbebb1 commit 6c64c85

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

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
-----

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();

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

0 commit comments

Comments
 (0)