-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
----- | ||
|
||
|
71 changes: 71 additions & 0 deletions
71
src/Symfony/Component/HttpFoundation/Test/Constraint/ResponseFormatSame.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]> | ||
* | ||
* 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 <[email protected]> | ||
*/ | ||
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; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/Symfony/Component/HttpFoundation/Tests/Test/Constraint/ResponseFormatSameTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]> | ||
* | ||
* 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 <[email protected]> | ||
*/ | ||
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(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.