ResponseFormatSame.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. public function __construct(
  22. private Request $request,
  23. private ?string $format,
  24. private readonly bool $verbose = true,
  25. ) {
  26. }
  27. public function toString(): string
  28. {
  29. return 'format is '.($this->format ?? 'null');
  30. }
  31. /**
  32. * @param Response $response
  33. */
  34. protected function matches($response): bool
  35. {
  36. return $this->format === $this->request->getFormat($response->headers->get('Content-Type'));
  37. }
  38. /**
  39. * @param Response $response
  40. */
  41. protected function failureDescription($response): string
  42. {
  43. return 'the Response '.$this->toString();
  44. }
  45. /**
  46. * @param Response $response
  47. */
  48. protected function additionalFailureDescription($response): string
  49. {
  50. return $this->verbose ? (string) $response : explode("\r\n\r\n", (string) $response)[0];
  51. }
  52. }