|
| 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 | +/* |
| 15 | + * @author Mathieu Santostefano <msantostefano@protonmail.com> |
| 16 | + */ |
| 17 | + |
| 18 | +use Symfony\Bundle\FrameworkBundle\KernelBrowser; |
| 19 | +use Symfony\Component\HttpClient\DataCollector\HttpClientDataCollector; |
| 20 | + |
| 21 | +trait HttpClientAssertionsTrait |
| 22 | +{ |
| 23 | + public static function assertHttpClientHasBeenCalled(KernelBrowser $client, string $httpClientId, array $urls): void |
| 24 | + { |
| 25 | + if (($profile = $client->getProfile()) === false) { |
| 26 | + static::fail('The Profiler must be enabled for the current request. Please ensure you have called $client->enableProfiler() before making the request.'); |
| 27 | + } |
| 28 | + |
| 29 | + /** @var HttpClientDataCollector $httpClientDataCollector */ |
| 30 | + $httpClientDataCollector = $profile->getCollector('http_client'); |
| 31 | + |
| 32 | + foreach ($httpClientDataCollector->getClients()[$httpClientId]['traces'] as $trace) { |
| 33 | + $url = $trace['info']['url']; |
| 34 | + $method = $trace['method']; |
| 35 | + |
| 36 | + if (isset($urls[$url])) { |
| 37 | + self::assertSame($urls[$url]['method'], $method); |
| 38 | + unset($urls[$url]); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + if (\count($urls) > 0) { |
| 43 | + static::fail('The following URLs were not called with the expected method: "'.implode(', ', array_keys($urls)).'"'); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + public static function assertHttpClientCountRequests(KernelBrowser $client, string $httpClientId, int $count): void |
| 48 | + { |
| 49 | + if (($profile = $client->getProfile()) === false) { |
| 50 | + static::fail('The Profiler must be enabled for the current request. Please ensure you have called $client->enableProfiler() before making the request.'); |
| 51 | + } |
| 52 | + |
| 53 | + /** @var HttpClientDataCollector $httpClientDataCollector */ |
| 54 | + $httpClientDataCollector = $profile->getCollector('http_client'); |
| 55 | + |
| 56 | + self::assertCount($count, $httpClientDataCollector->getClients()[$httpClientId]['traces']); |
| 57 | + } |
| 58 | +} |
0 commit comments