Spaces:
Runtime error
Runtime error
File size: 5,329 Bytes
7a66774 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | <?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Test;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Component\HttpClient\DataCollector\HttpClientDataCollector;
/**
* @author Mathieu Santostefano <msantostefano@protonmail.com>
*/
trait HttpClientAssertionsTrait
{
public static function assertHttpClientRequest(string $expectedUrl, string $expectedMethod = 'GET', string|array|null $expectedBody = null, array $expectedHeaders = [], string $httpClientId = 'http_client'): void
{
/** @var KernelBrowser $client */
$client = static::getClient();
if (!($profile = $client->getProfile())) {
static::fail('The Profiler must be enabled for the current request. Please ensure to call "$client->enableProfiler()" before making the request.');
}
/** @var HttpClientDataCollector $httpClientDataCollector */
$httpClientDataCollector = $profile->getCollector('http_client');
$expectedRequestHasBeenFound = false;
if (!\array_key_exists($httpClientId, $httpClientDataCollector->getClients())) {
static::fail(\sprintf('HttpClient "%s" is not registered.', $httpClientId));
}
foreach ($httpClientDataCollector->getClients()[$httpClientId]['traces'] as $trace) {
if (($expectedUrl !== $trace['info']['url'] && $expectedUrl !== $trace['url'])
|| $expectedMethod !== $trace['method']
) {
continue;
}
if (null !== $expectedBody) {
$actualBody = null;
if (null !== $trace['options']['body'] && null === $trace['options']['json']) {
$actualBody = \is_string($trace['options']['body']) ? $trace['options']['body'] : $trace['options']['body']->getValue(true);
}
if (null === $trace['options']['body'] && null !== $trace['options']['json']) {
$actualBody = $trace['options']['json']->getValue(true);
}
if (!$actualBody) {
continue;
}
if ($expectedBody === $actualBody) {
$expectedRequestHasBeenFound = true;
if (!$expectedHeaders) {
break;
}
}
}
if ($expectedHeaders) {
$actualHeaders = $trace['options']['headers'] ?? [];
foreach ($actualHeaders as $headerKey => $actualHeader) {
if (\array_key_exists($headerKey, $expectedHeaders)
&& $expectedHeaders[$headerKey] === $actualHeader->getValue(true)
) {
$expectedRequestHasBeenFound = true;
break 2;
}
}
}
$expectedRequestHasBeenFound = true;
break;
}
self::assertTrue($expectedRequestHasBeenFound, 'The expected request has not been called: "'.$expectedMethod.'" - "'.$expectedUrl.'"');
}
public function assertNotHttpClientRequest(string $unexpectedUrl, string $expectedMethod = 'GET', string $httpClientId = 'http_client'): void
{
/** @var KernelBrowser $client */
$client = static::getClient();
if (!$profile = $client->getProfile()) {
static::fail('The Profiler must be enabled for the current request. Please ensure to call "$client->enableProfiler()" before making the request.');
}
/** @var HttpClientDataCollector $httpClientDataCollector */
$httpClientDataCollector = $profile->getCollector('http_client');
$unexpectedUrlHasBeenFound = false;
if (!\array_key_exists($httpClientId, $httpClientDataCollector->getClients())) {
static::fail(\sprintf('HttpClient "%s" is not registered.', $httpClientId));
}
foreach ($httpClientDataCollector->getClients()[$httpClientId]['traces'] as $trace) {
if (($unexpectedUrl === $trace['info']['url'] || $unexpectedUrl === $trace['url'])
&& $expectedMethod === $trace['method']
) {
$unexpectedUrlHasBeenFound = true;
break;
}
}
self::assertFalse($unexpectedUrlHasBeenFound, \sprintf('Unexpected URL called: "%s" - "%s"', $expectedMethod, $unexpectedUrl));
}
public static function assertHttpClientRequestCount(int $count, string $httpClientId = 'http_client'): void
{
/** @var KernelBrowser $client */
$client = static::getClient();
if (!($profile = $client->getProfile())) {
static::fail('The Profiler must be enabled for the current request. Please ensure to call "$client->enableProfiler()" before making the request.');
}
/** @var HttpClientDataCollector $httpClientDataCollector */
$httpClientDataCollector = $profile->getCollector('http_client');
self::assertCount($count, $httpClientDataCollector->getClients()[$httpClientId]['traces']);
}
}
|