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