ResponseFormatSame.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpFoundation\Test\Constraint;
  11. use PHPUnit\Framework\Constraint\Constraint;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. /**
  15. * Asserts that the response is in the given format.
  16. *
  17. * @author Kévin Dunglas <dunglas@gmail.com>
  18. */
  19. final class ResponseFormatSame extends Constraint
  20. {
  21. private Request $request;
  22. private ?string $format;
  23. public function __construct(Request $request, ?string $format)
  24. {
  25. $this->request = $request;
  26. $this->format = $format;
  27. }
  28. public function toString(): string
  29. {
  30. return 'format is '.($this->format ?? 'null');
  31. }
  32. /**
  33. * @param Response $response
  34. */
  35. protected function matches($response): bool
  36. {
  37. return $this->format === $this->request->getFormat($response->headers->get('Content-Type'));
  38. }
  39. /**
  40. * @param Response $response
  41. */
  42. protected function failureDescription($response): string
  43. {
  44. return 'the Response '.$this->toString();
  45. }
  46. /**
  47. * @param Response $response
  48. */
  49. protected function additionalFailureDescription($response): string
  50. {
  51. return (string) $response;
  52. }
  53. }