ResponseIsSuccessful.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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\Response;
  13. final class ResponseIsSuccessful extends Constraint
  14. {
  15. /**
  16. * @param bool $verbose If true, the entire response is printed on failure. If false, the response body is omitted.
  17. */
  18. public function __construct(private readonly bool $verbose = true)
  19. {
  20. }
  21. public function toString(): string
  22. {
  23. return 'is successful';
  24. }
  25. /**
  26. * @param Response $response
  27. */
  28. protected function matches($response): bool
  29. {
  30. return $response->isSuccessful();
  31. }
  32. /**
  33. * @param Response $response
  34. */
  35. protected function failureDescription($response): string
  36. {
  37. return 'the Response '.$this->toString();
  38. }
  39. /**
  40. * @param Response $response
  41. */
  42. protected function additionalFailureDescription($response): string
  43. {
  44. return $this->verbose ? (string) $response : explode("\r\n\r\n", (string) $response)[0];
  45. }
  46. }