Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f0c97d8

Browse files
[FrameworkBundle] Fix mocking decorated services in tests
1 parent 2fb2fd1 commit f0c97d8

6 files changed

Lines changed: 55 additions & 9 deletions

File tree

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/TestServiceContainerWeakRefPass.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ public function process(ContainerBuilder $container)
3434
$definitions = $container->getDefinitions();
3535

3636
foreach ($definitions as $id => $definition) {
37-
if ($id && '.' !== $id[0] && (!$definition->isPublic() || $definition->isPrivate() || $definition->hasTag('container.private')) && !$definition->hasErrors() && !$definition->isAbstract()) {
37+
if ($inner = $definition->getTag('container.decorator')[0]['inner'] ?? null) {
38+
$privateServices[$inner] = new Reference($inner, ContainerBuilder::IGNORE_ON_UNINITIALIZED_REFERENCE);
39+
}
40+
if ($id && '.' !== $id[0] && ($definition->isPrivate() || $definition->hasTag('container.private')) && !$definition->hasErrors() && !$definition->isAbstract()) {
3841
$privateServices[$id] = new Reference($id, ContainerBuilder::IGNORE_ON_UNINITIALIZED_REFERENCE);
3942
}
4043
}

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,11 @@ public function set(string $id, mixed $service): void
7171
$container = $this->getPublicContainer();
7272
$renamedId = $this->renamedIds[$id] ?? $id;
7373

74-
try {
74+
if (!$this->getPrivateContainer()->has($renamedId)) {
7575
$container->set($renamedId, $service);
76-
} catch (InvalidArgumentException $e) {
77-
if (!str_starts_with($e->getMessage(), "The \"$renamedId\" service is private")) {
78-
throw $e;
79-
}
80-
if (isset($container->privates[$renamedId])) {
81-
throw new InvalidArgumentException(\sprintf('The "%s" service is already initialized, you cannot replace it.', $id));
82-
}
76+
} elseif (isset($container->privates[$renamedId])) {
77+
throw new InvalidArgumentException(\sprintf('The "%s" service is already initialized, you cannot replace it.', $id));
78+
} else {
8379
$container->privates[$renamedId] = $service;
8480
}
8581
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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 HttpClientMockingController
18+
{
19+
public function __invoke(HttpClientInterface $httpClient): Response
20+
{
21+
$response = $httpClient->request('GET', 'https://example.com/');
22+
23+
return new Response($response->getContent());
24+
}
25+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ http_client_call:
6565
path: /http_client_call
6666
defaults: { _controller: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\HttpClientController::index }
6767

68+
http_client_mock:
69+
path: /http_client_mock
70+
defaults: { _controller: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\HttpClientMockingController }
71+
6872
uid:
6973
resource: "../../Controller/UidController.php"
7074
type: "attribute"

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/HttpClientTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;
1313

14+
use Symfony\Contracts\HttpClient\HttpClientInterface;
15+
use Symfony\Component\HttpClient\MockHttpClient;
16+
use Symfony\Component\HttpClient\Response\MockResponse;
17+
1418
class HttpClientTest extends AbstractWebTestCase
1519
{
1620
public function testHttpClientAssertions()
@@ -30,4 +34,15 @@ public function testHttpClientAssertions()
3034

3135
$this->assertHttpClientRequestCount(6, 'symfony.http_client');
3236
}
37+
38+
public function testHttpClientCanBeOverriddenInWebTestCase()
39+
{
40+
$browser = $this->createClient(['test_case' => 'HttpClient', 'root_config' => 'config.yml', 'debug' => true]);
41+
$mockedContent = 'Request Mocked successfully!';
42+
static::getContainer()->set(HttpClientInterface::class, new MockHttpClient(new MockResponse($mockedContent)));
43+
44+
$browser->request('GET', '/http_client_mock');
45+
46+
self::assertSame($mockedContent, $browser->getResponse()->getContent());
47+
}
3348
}

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/HttpClient/services.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,8 @@ services:
55
Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\HttpClientController:
66
tags: ['controller.service_arguments']
77

8+
Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Controller\HttpClientMockingController:
9+
tags: ['controller.service_arguments']
10+
811
Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Tests\MockClientCallback:
912
class: Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\Tests\MockClientCallback

0 commit comments

Comments
 (0)