8000 minor #53535 do not mock the ContainerInterface (xabbuh) · symfony/symfony@4440c5d · GitHub
[go: up one dir, main page]

Skip to content

Commit 4440c5d

Browse files
committed
minor #53535 do not mock the ContainerInterface (xabbuh)
This PR was merged into the 7.1 branch. Discussion ---------- do not mock the ContainerInterface | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? | no | New feature? | no | Deprecations? | no | Issues | | License | MIT Commits ------- 016e6d1 do not mock the ContainerInterface
2 parents 57bcdf6 + 016e6d1 commit 4440c5d

22 files changed

+226
-480
lines changed

src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/RouterCacheWarmerTest.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,20 @@
1212
namespace Symfony\Bundle\FrameworkBundle\Tests\CacheWarmer;
1313

1414
use PHPUnit\Framework\TestCase;
15-
use Psr\Container\ContainerInterface;
1615
use Symfony\Bundle\FrameworkBundle\CacheWarmer\RouterCacheWarmer;
16+
use Symfony\Component\DependencyInjection\Container;
1717
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
1818
use Symfony\Component\Routing\RouterInterface;
1919

2020
class RouterCacheWarmerTest extends TestCase
2121
{
2222
public function testWarmUpWithWarmableInterfaceWithBuildDir()
2323
{
24-
$containerMock = $this->getMockBuilder(ContainerInterface::class)->onlyMethods(['get', 'has'])->getMock();
24+
$container = new Container();
2525

2626
$routerMock = $this->getMockBuilder(testRouterInterfaceWithWarmableInterface::class)->onlyMethods(['match', 'generate', 'getContext', 'setContext', 'getRouteCollection', 'warmUp'])->getMock();
27-
$containerMock->expects($this->any())->method('get')->with('router')->willReturn($routerMock);
28-
$routerCacheWarmer = new RouterCacheWarmer($containerMock);
27+
$container->set('router', $routerMock);
28+
$routerCacheWarmer = new RouterCacheWarmer($container);
2929

3030
$routerCacheWarmer->warmUp('/tmp/cache', '/tmp/build');
3131
$routerMock->expects($this->any())->method('warmUp')->with('/tmp/cache', '/tmp/build')->willReturn([]);
@@ -34,23 +34,23 @@ public function testWarmUpWithWarmableInterfaceWithBuildDir()
3434

3535
public function testWarmUpWithoutWarmableInterfaceWithBuildDir()
3636
{
37-
$containerMock = $this->getMockBuilder(ContainerInterface::class)->onlyMethods(['get', 'has'])->getMock();
37+
$container = new Container();
3838

3939
$routerMock = $this->getMockBuilder(testRouterInterfaceWithoutWarmableInterface::class)->onlyMethods(['match', 'generate', 'getContext', 'setContext', 'getRouteCollection'])->getMock();
40-
$containerMock->expects($this->any())->method('get')->with('router')->willReturn($routerMock);
41-
$routerCacheWarmer = new RouterCacheWarmer($containerMock);
40+
$container->set('router', $routerMock);
41+
$routerCacheWarmer = new RouterCacheWarmer($container);
4242
$this->expectException(\LogicException::class);
4343
$this->expectExceptionMessage('cannot be warmed up because it does not implement "Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface"');
4444
$routerCacheWarmer->warmUp('/tmp/cache', '/tmp/build');
4545
}
4646

4747
public function testWarmUpWithWarmableInterfaceWithoutBuildDir()
4848
{
49-
$containerMock = $this->getMockBuilder(ContainerInterface::class)->onlyMethods(['get', 'has'])->getMock();
49+
$container = new Container();
5050

5151
$routerMock = $this->getMockBuilder(testRouterInterfaceWithWarmableInterface::class)->onlyMethods(['match', 'generate', 'getContext', 'setContext', 'getRouteCollection', 'warmUp'])->getMock();
52-
$containerMock->expects($this->any())->method('get')->with('router')->willReturn($routerMock);
53-
$routerCacheWarmer = new RouterCacheWarmer($containerMock);
52+
$container->set('router', $routerMock);
53+
$routerCacheWarmer = new RouterCacheWarmer($container);
5454

5555
$preload = $routerCacheWarmer->warmUp('/tmp');
5656
$routerMock->expects($this->never())->method('warmUp');
@@ -60,11 +60,11 @@ public function testWarmUpWithWarmableInterfaceWithoutBuildDir()
6060

6161
public function testWarmUpWithoutWarmableInterfaceWithoutBuildDir()
6262
{
63-
$containerMock = $this->getMockBuilder(ContainerInterface::class)->onlyMethods(['get', 'has'])->getMock();
63+
$container = new Container();
6464

6565
$routerMock = $this->getMockBuilder(testRouterInterfaceWithoutWarmableInterface::class)->onlyMethods(['match', 'generate', 'getContext', 'setContext', 'getRouteCollection'])->getMock();
66-
$containerMock->expects($this->any())->method('get')->with('router')->willReturn($routerMock);
67-
$routerCacheWarmer = new RouterCacheWarmer($containerMock);
66+
$container->set('router', $routerMock);
67+
$routerCacheWarmer = new RouterCacheWarmer($container);
6868
$preload = $routerCacheWarmer->warmUp('/tmp');
6969
self::assertSame([], $preload);
7070
}

src/Symfony/Bundle/FrameworkBundle/Tests/Command/CachePoolClearCommandTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use Symfony\Bundle\FrameworkBundle\Console\Application;
1818
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
1919
use Symfony\Component\Console\Tester\CommandCompletionTester;
20-
use Symfony\Component\DependencyInjection\ContainerInterface;
20+
use Symfony\Component\DependencyInjection\Container;
2121
use Symfony\Component\HttpKernel\CacheClearer\Psr6CacheClearer;
2222
use Symfony\Component\HttpKernel\KernelInterface;
2323

@@ -54,13 +54,11 @@ public static function provideCompletionSuggestions(): iterable
5454

5555
private function getKernel(): MockObject&KernelInterface
5656
{
57-
$container = $this->createMock(ContainerInterface::class);
58-
5957
$kernel = $this->createMock(KernelInterface::class);
6058
$kernel
6159
->expects($this->any())
6260
->method('getContainer')
63-
->willReturn($container);
61+
->willReturn(new Container());
6462

6563
$kernel
6664
->expects($this->once())

src/Symfony/Bundle/FrameworkBundle/Tests/Command/CachePoolDeleteCommandTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
1919
use Symfony\Component\Console\Tester\CommandCompletionTester;
2020
use Symfony\Component\Console\Tester\CommandTester;
21-
use Symfony\Component\DependencyInjection\ContainerInterface;
21+
use Symfony\Component\DependencyInjection\Container;
2222
use Symfony\Component\HttpKernel\CacheClearer\Psr6CacheClearer;
2323
use Symfony\Component\HttpKernel\KernelInterface;
2424

@@ -108,13 +108,11 @@ public static function provideCompletionSuggestions(): iterable
108108

109109
private function getKernel(): MockObject&KernelInterface
110110
{
111-
$container = $this->createMock(ContainerInterface::class);
112-
113111
$kernel = $this->createMock(KernelInterface::class);
114112
$kernel
115113
->expects($this->any())
116114
->method('getContainer')
117-
->willReturn($container);
115+
->willReturn(new Container());
118116

119117
$kernel
120118
->expects($this->once())

src/Symfony/Bundle/FrameworkBundle/Tests/Command/CachePruneCommandTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use Symfony\Component\Cache\PruneableInterface;
1919
use Symfony\Component\Console\Tester\CommandTester;
2020
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
21-
use Symfony\Component\DependencyInjection\ContainerInterface;
21+
use Symfony\Component\DependencyInjection\Container;
2222
use Symfony\Component\HttpKernel\KernelInterface;
2323

2424
class CachePruneCommandTest extends TestCase
@@ -50,13 +50,11 @@ private function getEmptyRewindableGenerator(): RewindableGenerator
5050

5151
private function getKernel(): MockObject&KernelInterface
5252
{
53-
$container = $this->createMock(ContainerInterface::class);
54-
5553
$kernel = $this->createMock(KernelInterface::class);
5654
$kernel
5755
->expects($this->any())
5856
->method('getContainer')
59-
->willReturn($container);
57+
->willReturn(new Container());
6058

6159
$kernel
6260
->expects($this->once())

src/Symfony/Bundle/FrameworkBundle/Tests/Command/RouterMatchCommandTest.php

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use Symfony\Bundle\FrameworkBundle\Command\RouterMatchCommand;
1717
use Symfony\Bundle\FrameworkBundle\Console\Application;
1818
use Symfony\Component\Console\Tester\CommandTester;
19-
use Symfony\Component\DependencyInjection\ContainerInterface;
19+
use Symfony\Component\DependencyInjection\Container;
2020
use Symfony\Component\HttpKernel\KernelInterface;
2121
use Symfony\Component\Routing\RequestContext;
2222
use Symfony\Component\Routing\Route;
@@ -72,24 +72,11 @@ private function getRouter()
7272

7373
private function getKernel()
7474
{
75-
$container = $this->createMock(ContainerInterface::class);
76-
$container
77-
->expects($this->atLeastOnce())
78-
->method('has')
79-
->willReturnCallback(fn ($id) => 'console.command_loader' !== $id)
80-
;
81-
$container
82-
->expects($this->any())
83-
->method('get')
84-
->with('router')
85-
->willReturn($this->getRouter())
86-
;
87-
8875
$kernel = $this->createMock(KernelInterface::class);
8976
$kernel
9077
->expects($this->any())
9178
->method('getContainer')
92-
->willReturn($container)
79+
->willReturn(new Container())
9380
;
9481
$kernel
9582
->expects($this->once())

src/Symfony/Bundle/FrameworkBundle/Tests/Console/ApplicationTest.php

Lines changed: 7 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
use Symfony\Component\Console\Output\NullOutput;
2323
use Symfony\Component\Console\Output\OutputInterface;
2424
use Symfony\Component\Console\Tester\ApplicationTester;
25+
use Symfony\Component\DependencyInjection\Container;
2526
use Symfony\Component\DependencyInjection\ContainerBuilder;
26-
use Symfony\Component\DependencyInjection\ContainerInterface;
27+
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
2728
use Symfony\Component\EventDispatcher\EventDispatcher;
2829
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
29-
use Symfony\Component\HttpFoundation\RequestStack;
3030
use Symfony\Component\HttpKernel\Bundle\Bundle;
3131
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
3232
use Symfony\Component\HttpKernel\KernelInterface;
@@ -241,7 +241,10 @@ private function createEventForSuggestingPackages(string $command, array $altern
241241

242242
private function getKernel(array $bundles, $useDispatcher = false)
243243
{
244-
$container = $this->createMock(ContainerInterface::class);
244+
$container = new Container(new ParameterBag([
245+
'console.command.ids' => [],
246+
'console.lazy_command.ids' => [],
247+
]));
245248

246249
if ($useDispatcher) {
247250
$dispatcher = $this->createMock(EventDispatcherInterface::class);
@@ -250,45 +253,9 @@ private function getKernel(array $bundles, $useDispatcher = false)
250253
->method('dispatch')
251254
;
252255

253-
$container->expects($this->atLeastOnce())
254-
->method('get')
255-
->willReturnMap([
256-
['.virtual_request_stack', 2, new RequestStack()],
257-
['event_dispatcher', 1, $dispatcher],
258-
])
259-
;
256+
$container->set('event_dispatcher', $dispatcher);
260257
}
261258

262-
$container
263-
->expects($this->exactly(2))
264-
->method('hasParameter')
265-
->willReturnCallback(function (...$args) {
266-
static $series = [
267-
['console.command.ids'],
268-
['console.lazy_command.ids'],
269-
];
270-
271-
$this->assertSame(array_shift($series), $args);
272-
273-
return true;
274-
})
275-
;
276-
277-
$container
278-
->expects($this->exactly(2))
279-
->method('getParameter')
280-
->willReturnCallback(function (...$args) {
281-
static $series = [
282-
['console.lazy_command.ids'],
283-
['console.command.ids'],
284-
];
285-
286-
$this->assertSame(array_shift($series), $args);
287-
288-
return [];
289-
})
290-
;
291-
292259
$kernel = $this->createMock(KernelInterface::class);
293260
$kernel->expects($this->once())->method('boot');
294261
$kernel

src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerResolverTest.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1717
use Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver;
1818
use Symfony\Component\DependencyInjection\Container;
19-
use Symfony\Component\DependencyInjection\ContainerInterface;
2019
use Symfony\Component\HttpFoundation\Request;
2120
use Symfony\Component\HttpKernel\Tests\Controller\ContainerControllerResolverTest;
2221

@@ -107,7 +106,7 @@ class_exists(AbstractControllerTest::class);
107106
protected function createControllerResolver(LoggerInterface $logger = null, Psr11ContainerInterface $container = null)
108107
{
109108
if (!$container) {
110-
$container = $this->createMockContainer();
109+
$container = new Container();
111110
}
112111

113112
return new ControllerResolver($container, $logger);
@@ -117,11 +116,6 @@ protected function createMockParser()
117116
{
118117
return $this->createMock(ControllerNameParser::class);
119118
}
120-
121-
protected function createMockContainer()
122-
{
123-
return $this->createMock(ContainerInterface::class);
124-
}
125119
}
126120

127121
class DummyController extends AbstractController

src/Symfony/Bundle/FrameworkBundle/Tests/Routing/RouterTest.php

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
use Symfony\Component\DependencyInjection\Container;
2424
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
2525
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
26+
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBag;
27+
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
2628
use Symfony\Component\Routing\Loader\YamlFileLoader;
2729
use Symfony\Component\Routing\Route;
2830
use Symfony\Component\Routing\RouteCollection;
@@ -406,7 +408,8 @@ public function testExceptionOnNonStringParameter()
406408
$routes->add('foo', new Route('/%object%'));
407409

408410
$sc = $this->getPsr11ServiceContainer($routes);
409-
$parameters = $this->getParameterBag(['object' => new \stdClass()]);
411+
$parameters = new Container();
412+
$parameters->set('object', new \stdClass());
410413

411414
$router = new Router($sc, 'foo', [], null, $parameters);
412415

@@ -424,19 +427,15 @@ public function testExceptionOnNonStringParameterWithSfContainer()
424427

425428
$sc = $this->getServiceContainer($routes);
426429

427-
$pc = $this->createMock(ContainerInterface::class);
428-
$pc
429-
->expects($this->once())
430-
->method('get')
431-
->willReturn(new \stdClass())
432-
;
430+
$pc = new Container();
431+
$pc->set('object', new \stdClass());
433432

434433
$router = new Router($sc, 'foo', [], null, $pc);
435434

436435
$this->expectException(RuntimeException::class);
437436
$this->expectExceptionMessage('The container parameter "object", used in the route configuration value "/%object%", must be a string or numeric, but it is of type "stdClass".');
438437

439-
$router->getRouteCollection()->get('foo');
438+
$router->getRouteCollection();
440439
}
441440

442441
/**
@@ -483,7 +482,9 @@ public function testGetRouteCollectionAddsContainerParametersResource()
483482

484483
$router = new Router($sc, 'foo', [], null, $parameters);
485484

486-
$router->getRouteCollection();
485+
$routeCollection = $router->getRouteCollection();
486+
487+
$this->assertEquals([new ContainerParametersResource(['locale' => 'en'])], $routeCollection->getResources());
487488
}
488489

489490
public function testGetRouteCollectionAddsContainerParametersResourceWithSfContainer()
@@ -617,13 +618,8 @@ private function getServiceContainer(RouteCollection $routes): Container
617618
->willReturn($routes)
618619
;
619620

620-
$sc = $this->getMockBuilder(Container::class)->onlyMethods(['get'])->getMock();
621-
622-
$sc
623-
->expects($this->once())
624-
->method('get')
625-
->willReturn($loader)
626-
;
621+
$sc = new Container();
622+
$sc->set('routing.loader', $loader);
627623

628624
return $sc;
629625
}
@@ -638,26 +634,14 @@ private function getPsr11ServiceContainer(RouteCollection $routes): ContainerInt
638634
->willReturn($routes)
639635
;
640636

641-
$sc = $this->createMock(ContainerInterface::class);
642-
643-
$sc
644-
->expects($this->once())
645-
->method('get')
646-
->willReturn($loader)
647-
;
637+
$container = new Container();
638+
$container->set('routing.loader', $loader);
648639

649-
return $sc;
640+
return $container;
650641
}
651642

652643
private function getParameterBag(array $params = []): ContainerInterface
653644
{
654-
$bag = $this->createMock(ContainerInterface::class);
655-
$bag
656-
->expects($this->any())
657-
->method('get')
658-
->willReturnCallback(fn ($key) => $params[$key] ?? null)
659-
;
660-
661-
return $bag;
645+
return new ContainerBag(new Container(new ParameterBag($params)));
662646
}
663647
}

0 commit comments

Comments
 (0)
0