E60D Add HttpClientAssertionsTrait which provide shortcut to assert HTTP c… · symfony/symfony@92b819c · GitHub
[go: up one dir, main page]

Skip to content

Commit 92b819c

Browse files
committed
Add HttpClientAssertionsTrait which provide shortcut to assert HTTP call was triggered
1 parent 52a9292 commit 92b819c

File tree

10 files changed

+158
-0
lines changed

10 files changed

+158
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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 ($urls as $i => $url) {
33+
self::assertSame($url, $httpClientDataCollector->getClients()[$httpClientId]['traces'][$i]['info']['url']);
34+
}
35+
}
36+
}

src/Symfony/Bundle/FrameworkBundle/Test/WebTestAssertionsTrait.php

+1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ trait WebTestAssertionsTrait
1515
{
1616
use BrowserKitAssertionsTrait;
1717
use DomCrawlerAssertionsTrait;
18+
use HttpClientAssertionsTrait;
1819
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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\Tests\Functional\Bundle\TestBundle\Controller;
13+
14+
use Symfony\Component\HttpFoundation\Response;
15+
use Symfony\Contracts\HttpClient\HttpClientInterface;
16+
17+
class HttpClientController
18+
{
19+
public function index(HttpClientInterface $symfonyHttpClient): Response
20+
{
21+
$symfonyHttpClient->request('GET', '/');
22+
$symfonyHttpClient->request('GET', '/doc/current/index.html');
23+
24+
return new Response();
25+
}
26+
}

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Resources/config/routing.yml

+4
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ send_email:
6161
path: /send_email
6262
defaults: { _controller: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\EmailController::indexAction }
6363

64+
http_client_call:
65+
path: /http_client_call
66+
defaults: { _controller: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\HttpClientController::index }
67+
6468
uid:
6569
resource: "../../Controller/UidController.php"
6670
type: "annotation"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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\Tests\Functional\Bundle\TestBundle\Tests;
13+
14+
use Symfony\Component\HttpClient\Response\MockResponse;
15+
use Symfony\Contracts\HttpClient\ResponseInterface;
16+
17+
class MockClientCallback
18+
{
19+
public function __invoke(string $method, string $url, array $options = []): ResponseInterface
20+
{
21+
return new MockResponse('foo');
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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\Tests\Functional;
13+
14+
class HttpClientTest extends AbstractWebTestCase
15+
{
16+
public function testHttpClientAssertions()
17+
{
18+
$client = $this->createClient(['test_case' => 'HttpClient', 'root_config' => 'config.yml', 'debug' => true]);
19+
$client->enableProfiler();
20+
$client->request('GET', '/http_client_call');
21+
22+
$this->assertHttpClientHasBeenCalled($client, 'symfony.http_client', [
23+
'https://symfony.com/',
24+
'https://symfony.com/doc/current/index.html',
25+
]);
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
13+
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestBundle;
14+
15+
return [
16+
new FrameworkBundle(),
17+
new TestBundle(),
18+
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
imports:
2+
- { resource: ../config/default.yml }
3+
- { resource: services.yml }
4+
5+
framework:
6+
http_method_override: false
7+
profiler: ~
8+
http_client:
9+
mock_response_factory: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Tests\MockClientCallback
10+
scoped_clients:
11+
symfony.http_client:
12+
base_uri: 'https://symfony.com'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
_emailtest_bundle:
2+
resource: '@TestBundle/Resources/config/routing.yml'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
services:
2+
_defaults:
3+
public: true
4+
5+
Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\HttpClientController:
6+
tags: ['controller.service_arguments']
7+
8+
Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Tests\MockClientCallback:
9+
class: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Tests\MockClientCallback

0 commit comments

Comments
 (0)
0