ResponseFormatSame.php 1.6 KB

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