ResponseFormatSame.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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;
  22. private $format;
  23. public function __construct(Request $request, ?string $format)
  24. {
  25. $this->request = $request;
  26. $this->format = $format;
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function toString(): string
  32. {
  33. return 'format is '.($this->format ?? 'null');
  34. }
  35. /**
  36. * @param Response $response
  37. *
  38. * {@inheritdoc}
  39. */
  40. protected function matches($response): bool
  41. {
  42. return $this->format === $this->request->getFormat($response->headers->get('Content-Type'));
  43. }
  44. /**
  45. * @param Response $response
  46. *
  47. * {@inheritdoc}
  48. */
  49. protected function failureDescription($response): string
  50. {
  51. return 'the Response '.$this->toString();
  52. }
  53. /**
  54. * @param Response $response
  55. *
  56. * {@inheritdoc}
  57. */
  58. protected function additionalFailureDescription($response): string
  59. {
  60. return (string) $response;
  61. }
  62. }