-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[FrameworkBundle] Add HttpClientAssertionsTrait
which provide shortcuts to assert HTTP calls was triggered
#50662
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
src/Symfony/Bundle/FrameworkBundle/Test/HttpClientAssertionsTrait.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* 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 <[email protected]> | ||
*/ | ||
|
||
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) { | ||
welcoMattic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.'"'); | ||
welcoMattic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
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) { | ||
welcoMattic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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())) { | ||
welcoMattic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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']); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...le/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Controller/HttpClientController.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* 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(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...ny/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Tests/MockClientCallback.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* 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'); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/Symfony/Bundle/FrameworkBundle/Tests/Functional/HttpClientTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* 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'); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/HttpClient/bundles.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* 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(), | ||
]; |
12 changes: 12 additions & 0 deletions
12
src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/HttpClient/config.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' |
2 changes: 2 additions & 0 deletions
2
src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/HttpClient/routing.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
_emailtest_bundle: | ||
resource: '@TestBundle/Resources/config/routing.yml' |
9 changes: 9 additions & 0 deletions
9
src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/HttpClient/services.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.