diff --git a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md index cf66d7ef1b113..3ba2ee8be2795 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md @@ -4,6 +4,7 @@ CHANGELOG 6.4 --- + * Add `HttpClientAssertionsTrait` * Add `AbstractController::renderBlock()` and `renderBlockView()` * Add native return type to `Translator` and to `Application::reset()` * Deprecate the integration of Doctrine annotations, either uninstall the `doctrine/annotations` package or disable the integration by setting `framework.annotations` to `false` diff --git a/src/Symfony/Bundle/FrameworkBundle/Test/HttpClientAssertionsTrait.php b/src/Symfony/Bundle/FrameworkBundle/Test/HttpClientAssertionsTrait.php new file mode 100644 index 0000000000000..bed835fa1e14a --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Test/HttpClientAssertionsTrait.php @@ -0,0 +1,134 @@ + + * + * 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 + */ + +trait HttpClientAssertionsTrait +{ + public static function assertHttpClientRequest(string $expectedUrl, string $expectedMethod = 'GET', string|array $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']); + } +} diff --git a/src/Symfony/Bundle/FrameworkBundle/Test/WebTestAssertionsTrait.php b/src/Symfony/Bundle/FrameworkBundle/Test/WebTestAssertionsTrait.php index 0f1742ee3e2ca..aebd4577b3d52 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Test/WebTestAssertionsTrait.php +++ b/src/Symfony/Bundle/FrameworkBundle/Test/WebTestAssertionsTrait.php @@ -15,4 +15,5 @@ trait WebTestAssertionsTrait { use BrowserKitAssertionsTrait; use DomCrawlerAssertionsTrait; + use HttpClientAssertionsTrait; } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Controller/HttpClientController.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Controller/HttpClientController.php new file mode 100644 index 0000000000000..47b9a2161fa2f --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Controller/HttpClientController.php @@ -0,0 +1,35 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller; + +use Symfony\Component\HttpFoundation\Response; +use Symfony\Contracts\HttpClient\HttpClientInterface; + +class HttpClientController +{ + public function index(HttpClientInterface $httpClient, HttpClientInterface $symfonyHttpClient): Response + { + $httpClient->request('GET', 'https://symfony.com/'); + + $symfonyHttpClient->request('GET', '/'); + $symfonyHttpClient->request('POST', '/', ['body' => 'foo']); + $symfonyHttpClient->request('POST', '/', ['body' => ['foo' => 'bar']]); + $symfonyHttpClient->request('POST', '/', ['json' => ['foo' => 'bar']]); + $symfonyHttpClient->request('POST', '/', [ + 'headers' => ['X-Test-Header' => 'foo'], + 'json' => ['foo' => 'bar'], + ]); + $symfonyHttpClient->request('GET', '/doc/current/index.html'); + + return new Response(); + } +} diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Resources/config/routing.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Resources/config/routing.yml index 5630ed621048f..163e5fd135225 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Resources/config/routing.yml +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Resources/config/routing.yml @@ -61,6 +61,10 @@ send_email: path: /send_email defaults: { _controller: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\EmailController::indexAction } +http_client_call: + path: /http_client_call + defaults: { _controller: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\HttpClientController::index } + uid: resource: "../../Controller/UidController.php" type: "annotation" diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Tests/MockClientCallback.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Tests/MockClientCallback.php new file mode 100644 index 0000000000000..6eb82e6dd4b71 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Tests/MockClientCallback.php @@ -0,0 +1,23 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Tests; + +use Symfony\Component\HttpClient\Response\MockResponse; +use Symfony\Contracts\HttpClient\ResponseInterface; + +class MockClientCallback +{ + public function __invoke(string $method, string $url, array $options = []): ResponseInterface + { + return new MockResponse('foo'); + } +} diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/HttpClientTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/HttpClientTest.php new file mode 100644 index 0000000000000..5302d4427d437 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/HttpClientTest.php @@ -0,0 +1,33 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\FrameworkBundle\Tests\Functional; + +class HttpClientTest extends AbstractWebTestCase +{ + public function testHttpClientAssertions() + { + $client = $this->createClient(['test_case' => 'HttpClient', 'root_config' => 'config.yml', 'debug' => true]); + $client->enableProfiler(); + $client->request('GET', '/http_client_call'); + + $this->assertHttpClientRequest('https://symfony.com/'); + $this->assertHttpClientRequest('https://symfony.com/', httpClientId: 'symfony.http_client'); + $this->assertHttpClientRequest('https://symfony.com/', 'POST', 'foo', httpClientId: 'symfony.http_client'); + $this->assertHttpClientRequest('https://symfony.com/', 'POST', ['foo' => 'bar'], httpClientId: 'symfony.http_client'); + $this->assertHttpClientRequest('https://symfony.com/', 'POST', ['foo' => 'bar'], httpClientId: 'symfony.http_client'); + $this->assertHttpClientRequest('https://symfony.com/', 'POST', ['foo' => 'bar'], ['X-Test-Header' => 'foo'], 'symfony.http_client'); + $this->assertHttpClientRequest('https://symfony.com/doc/current/index.html', httpClientId: 'symfony.http_client'); + $this->assertNotHttpClientRequest('https://laravel.com', httpClientId: 'symfony.http_client'); + + $this->assertHttpClientRequestCount(6, 'symfony.http_client'); + } +} diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/HttpClient/bundles.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/HttpClient/bundles.php new file mode 100644 index 0000000000000..15ff182c6fed5 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/HttpClient/bundles.php @@ -0,0 +1,18 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +use Symfony\Bundle\FrameworkBundle\FrameworkBundle; +use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestBundle; + +return [ + new FrameworkBundle(), + new TestBundle(), +]; diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/HttpClient/config.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/HttpClient/config.yml new file mode 100644 index 0000000000000..eba89d2f67194 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/HttpClient/config.yml @@ -0,0 +1,12 @@ +imports: + - { resource: ../config/default.yml } + - { resource: services.yml } + +framework: + http_method_override: false + profiler: ~ + http_client: + mock_response_factory: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Tests\MockClientCallback + scoped_clients: + symfony.http_client: + base_uri: 'https://symfony.com' diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/HttpClient/routing.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/HttpClient/routing.yml new file mode 100644 index 0000000000000..4fb9a95400e97 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/HttpClient/routing.yml @@ -0,0 +1,2 @@ +_emailtest_bundle: + resource: '@TestBundle/Resources/config/routing.yml' diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/HttpClient/services.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/HttpClient/services.yml new file mode 100644 index 0000000000000..5b1a19b53c52b --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/HttpClient/services.yml @@ -0,0 +1,9 @@ +services: + _defaults: + public: true + + Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\HttpClientController: + tags: ['controller.service_arguments'] + + Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Tests\MockClientCallback: + class: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Tests\MockClientCallback