| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\HttpFoundation\Test\Constraint; |
| 13 |
|
| 14 |
use PHPUnit\Framework\Constraint\Constraint; |
| 15 |
use Symfony\Component\HttpFoundation\Request; |
| 16 |
use Symfony\Component\HttpFoundation\Response; |
| 17 |
|
| 18 |
final class ResponseHeaderLocationSame extends Constraint |
| 19 |
{ |
| 20 |
public function __construct(private Request $request, private string $expectedValue) |
| 21 |
{ |
| 22 |
} |
| 23 |
|
| 24 |
public function toString(): string |
| 25 |
{ |
| 26 |
return \sprintf('has header "Location" matching "%s"', $this->expectedValue); |
| 27 |
} |
| 28 |
|
| 29 |
protected function matches($other): bool |
| 30 |
{ |
| 31 |
if (!$other instanceof Response) { |
| 32 |
return false; |
| 33 |
} |
| 34 |
|
| 35 |
$location = $other->headers->get('Location'); |
| 36 |
|
| 37 |
if (null === $location) { |
| 38 |
return false; |
| 39 |
} |
| 40 |
|
| 41 |
return $this->toFullUrl($this->expectedValue) === $this->toFullUrl($location); |
| 42 |
} |
| 43 |
|
| 44 |
protected function failureDescription($other): string |
| 45 |
{ |
| 46 |
return 'the Response '.$this->toString(); |
| 47 |
} |
| 48 |
|
| 49 |
private function toFullUrl(string $url): string |
| 50 |
{ |
| 51 |
if (null === parse_url($url, \PHP_URL_PATH)) { |
| 52 |
$url .= '/'; |
| 53 |
} |
| 54 |
|
| 55 |
if (str_starts_with($url, '//')) { |
| 56 |
return \sprintf('%s:%s', $this->request->getScheme(), $url); |
| 57 |
} |
| 58 |
|
| 59 |
if (str_starts_with($url, '/')) { |
| 60 |
return $this->request->getSchemeAndHttpHost().$url; |
| 61 |
} |
| 62 |
|
| 63 |
return $url; |
| 64 |
} |
| 65 |
} |