Skip to content

[FrameworkBundle][HttpFoundation] add assertResponseFormatSame() #39666

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CHANGELOG
* Added support for configuring PHP error level to log levels
* Added the `dispatcher` option to `debug:event-dispatcher`
* Added the `event_dispatcher.dispatcher` tag
* Added `assertResponseFormatSame()` in `BrowserKitAssertionsTrait`

5.2.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public static function assertResponseStatusCodeSame(int $expectedCode, string $m
self::assertThatForResponse(new ResponseConstraint\ResponseStatusCodeSame($expectedCode), $message);
}

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

public static function assertResponseRedirects(string $expectedLocation = null, int $expectedCode = null, string $message = ''): void
{
$constraint = new ResponseConstraint\ResponseIsRedirected();
Expand Down
19 changes: 19 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Tests/Test/WebTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Symfony\Component\HttpFoundation\Cookie as HttpFoundationCookie;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Test\Constraint\ResponseFormatSame;

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

public function testAssertResponseFormat()
{
if (!class_exists(ResponseFormatSame::class)) {
$this->markTestSkipped('Too old version of HttpFoundation.');
}

$this->getResponseTester(new Response('', 200, ['Content-Type' => 'application/vnd.myformat']))->assertResponseFormatSame('custom');
$this->getResponseTester(new Response('', 200, ['Content-Type' => 'application/ld+json']))->assertResponseFormatSame('jsonld');
$this->getResponseTester(new Response())->assertResponseFormatSame(null);
$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage("Failed asserting that the Response format is jsonld.\nHTTP/1.0 200 OK");
$this->getResponseTester(new Response())->assertResponseFormatSame('jsonld');
}

public function testAssertResponseHasHeader()
{
$this->getResponseTester(new Response())->assertResponseHasHeader('Date');
Expand Down Expand Up @@ -284,6 +299,10 @@ private function getResponseTester(Response $response): WebTestCase
$client = $this->createMock(KernelBrowser::class);
$client->expects($this->any())->method('getResponse')->willReturn($response);

$request = new Request();
$request->setFormat('custom', ['application/vnd.myformat']);
$client->expects($this->any())->method('getRequest')->willReturn($request);

return $this->getTester($client);
}

Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/HttpFoundation/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

5.3.0
-----

* added `ResponseFormatSame` PHPUnit constraint

5.2.0
-----

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\HttpFoundation\Test\Constraint;

use PHPUnit\Framework\Constraint\Constraint;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
* Asserts that the response is in the given format.
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
final class ResponseFormatSame extends Constraint
{
private $request;
private $format;

public function __construct(Request $request, ?string $format)
{
$this->request = $request;
$this->format = $format;
}

/**
* {@inheritdoc}
*/
public function toString(): string
{
return 'format is '.($this->format ?? 'null');
}

/**
* @param Response $response
*
* {@inheritdoc}
*/
protected function matches($response): bool
{
return $this->format === $this->request->getFormat($response->headers->get('Content-Type'));
}

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

/**
* @param Response $response
*
* {@inheritdoc}
*/
protected function additionalFailureDescription($response): string
{
return (string) $response;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

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

use PHPUnit\Framework\ExpectationFailedException;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\TestFailure;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Test\Constraint\ResponseFormatSame;

/**
* @author Kévin Dunglas <kevin@dunglas.fr>
*/
class ResponseFormatSameTest extends TestCase
{
public function testConstraint()
{
$request = new Request();
$request->setFormat('custom', ['application/vnd.myformat']);

$constraint = new ResponseFormatSame($request, 'custom');
$this->assertTrue($constraint->evaluate(new Response('', 200, ['Content-Type' => 'application/vnd.myformat']), '', true));
$this->assertFalse($constraint->evaluate(new Response(), '', true));

try {
$constraint->evaluate(new Response('', 200, ['Content-Type' => 'application/ld+json']));
} catch (ExpectationFailedException $e) {
$this->assertStringContainsString("Failed asserting that the Response format is custom.\nHTTP/1.0 200 OK", TestFailure::exceptionToString($e));

return;
}

$this->fail();
}

public function testNullFormat()
{
$constraint = new ResponseFormatSame(new Request(), null);
$this->assertTrue($constraint->evaluate(new Response(), '', true));

try {
$constraint->evaluate(new Response('', 200, ['Content-Type' => 'application/ld+json']));
} catch (ExpectationFailedException $e) {
$this->assertStringContainsString("Failed asserting that the Response format is null.\nHTTP/1.0 200 OK", TestFailure::exceptionToString($e));

return;
}

$this->fail();
}
}