8000 do not mock the ContainerInterface by xabbuh · Pull Request #53535 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

do not mock the ContainerInterface #53535

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 1 commit into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
do not mock the ContainerInterface
  • Loading branch information
xabbuh committed Jan 17, 2024
commit 016e6d1c58175a331c923f7e2203d85e99b6ef0e
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@
namespace Symfony\Bundle\FrameworkBundle\Tests\CacheWarmer;

use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Symfony\Bundle\FrameworkBundle\CacheWarmer\RouterCacheWarmer;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
use Symfony\Component\Routing\RouterInterface;

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

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

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

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

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

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

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

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

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

$routerMock = $this->getMockBuilder(testRouterInterfaceWithoutWarmableInterface::class)->onlyMethods(['match', 'generate', 'getContext', 'setContext', 'getRouteCollection'])->getMock();
$containerMock->expects($this->any())->method('get')->with('router')->willReturn($routerMock);
$routerCacheWarmer = new RouterCacheWarmer($containerMock);
$container->set('router', $routerMock);
$routerCacheWarmer = new RouterCacheWarmer($container);
$preload = $routerCacheWarmer->warmUp('/tmp');
self::assertSame([], $preload);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
use Symfony\Component\Console\Tester\CommandCompletionTester;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\HttpKernel\CacheClearer\Psr6CacheClearer;
use Symfony\Component\HttpKernel\KernelInterface;

Expand Down Expand Up @@ -54,13 +54,11 @@ public static function provideCompletionSuggestions(): iterable

private function getKernel(): MockObject&KernelInterface
{
$container = $this->createMock(ContainerInterface::class);

$kernel = $this->createMock(KernelInterface::class);
$kernel
->expects($this->any())
->method('getContainer')
->willReturn($container);
->willReturn(new Container());

$kernel
->expects($this->once())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
use Symfony\Component\Console\Tester\CommandCompletionTester;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\HttpKernel\CacheClearer\Psr6CacheClearer;
use Symfony\Component\HttpKernel\KernelInterface;

Expand Down Expand Up @@ -108,13 +108,11 @@ public static function provideCompletionSuggestions(): iterable

private function getKernel(): MockObject&KernelInterface
{
$container = $this->createMock(ContainerInterface::class);

$kernel = $this->createMock(KernelInterface::class);
$kernel
->expects($this->any())
->method('getContainer')
->willReturn($container);
->willReturn(new Container());

$kernel
->expects($this->once())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Symfony\Component\Cache\PruneableInterface;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\HttpKernel\KernelInterface;

class CachePruneCommandTest extends TestCase
Expand Down Expand Up @@ -50,13 +50,11 @@ private function getEmptyRewindableGenerator(): RewindableGenerator

private function getKernel(): MockObject&KernelInterface
{
$container = $this->createMock(ContainerInterface::class);

$kernel = $this->createMock(KernelInterface::class);
$kernel
->expects($this->any())
->method('getContainer')
->willReturn($container);
->willReturn(new Container());

$kernel
->expects($this->once())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Symfony\Bundle\FrameworkBundle\Command\RouterMatchCommand;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Route;
Expand Down Expand Up @@ -72,24 +72,11 @@ private function getRouter()

private function getKernel()
{
$container = $this->createMock(ContainerInterface::class);
$container
->expects($this->atLeastOnce())
->method('has')
->willReturnCallback(fn ($id) => 'console.command_loader' !== $id)
;
$container
->expects($this->any())
->method('get')
->with('router')
->willReturn($this->getRouter())
;

$kernel = $this->createMock(KernelInterface::class);
$kernel
->expects($this->any())
->method('getContainer')
->willReturn($container)
->willReturn(new Container())
;
$kernel
->expects($this->once())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Tester\ApplicationTester;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
use Symfony\Component\HttpKernel\KernelInterface;
Expand Down Expand Up @@ -241,7 +241,10 @@ private function createEventForSuggestingPackages(string $command, array $altern

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

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

$container->expects($this->atLeastOnce())
->method('get')
->willReturnMap([
['.virtual_request_stack', 2, new RequestStack()],
['event_dispatcher', 1, $dispatcher],
])
;
$container->set('event_dispatcher', $dispatcher);
}

$container
->expects($this->exactly(2))
->method('hasParameter')
->willReturnCallback(function (...$args) {
static $series = [
['console.command.ids'],
['console.lazy_command.ids'],
];

$this->assertSame(array_shift($series), $args);

return true;
})
;

$container
->expects($this->exactly(2))
->method('getParameter')
->willReturnCallback(function (...$args) {
static $series = [
['console.lazy_command.ids'],
['console.command.ids'],
];

$this->assertSame(array_shift($series), $args);

return [];
})
;

$kernel = $this->createMock(KernelInterface::class);
$kernel->expects($this->once())->method('boot');
$kernel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Tests\Controller\ContainerControllerResolverTest;

Expand Down Expand Up @@ -107,7 +106,7 @@ class_exists(AbstractControllerTest::class);
protected function createControllerResolver(LoggerInterface $logger = null, Psr11ContainerInterface $container = null)
{
if (!$container) {
$container = $this->createMockContainer();
$container = new Container();
}

return new ControllerResolver($container, $logger);
Expand All @@ -117,11 +116,6 @@ protected function createMockParser()
{
return $this->createMock(ControllerNameParser::class);
}

protected function createMockContainer()
{
return $this->createMock(ContainerInterface::class);
}
}

class DummyController extends AbstractController
Expand Down
48 changes: 16 additions & 32 deletions src/Symfony/Bundle/FrameworkBundle/Tests/Routing/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBag;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\Routing\Loader\YamlFileLoader;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
Expand Down Expand Up @@ -406,7 +408,8 @@ public function testExceptionOnNonStringParameter()
$routes->add('foo', new Route('/%object%'));

$sc = $this->getPsr11ServiceContainer($routes);
$parameters = $this->getParameterBag(['object' => new \stdClass()]);
$parameters = new Container();
$parameters->set('object', new \stdClass());

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

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

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

$pc = $this->createMock(ContainerInterface::class);
$pc
->expects($this->once())
->method('get')
->willReturn(new \stdClass())
;
$pc = new Container();
$pc->set('object', new \stdClass());

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

$this->expectException(RuntimeException::class);
$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".');

$router->getRouteCollection()->get('foo');
$router->getRouteCollection();
}

/**
Expand Down Expand Up @@ -483,7 +482,9 @@ public function testGetRouteCollectionAddsContainerParametersResource()

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

$router->getRouteCollection();
$routeCollection = $router->getRouteCollection();

$this->assertEquals([new ContainerParametersResource(['locale' => 'en'])], $routeCollection->getResources());
}

public function testGetRouteCollectionAddsContainerParametersResourceWithSfContainer()
Expand Down Expand Up @@ -617,13 +618,8 @@ private function getServiceContainer(RouteCollection $routes): Container
->willReturn($routes)
;

$sc = $this->getMockBuilder(Container::class)->onlyMethods(['get'])->getMock();

$sc
->expects($this->once())
->method('get')
->willReturn($loader)
;
$sc = new Container();
$sc->set('routing.loader', $loader);

return $sc;
}
Expand All @@ -638,26 +634,14 @@ private function getPsr11ServiceContainer(RouteCollection $routes): ContainerInt
->willReturn($routes)
;

$sc = $this->createMock(ContainerInterface::class);

$sc
->expects($this->once())
->method('get')
->willReturn($loader)
;
$container = new Container();
$container->set('routing.loader', $loader);

return $sc;
return $container;
}

private function getParameterBag(array $params = []): ContainerInterface
{
$bag = $this->createMock(ContainerInterface::class);
$bag
->expects($this->any())
->method('get')
->willReturnCallback(fn ($key) => $params[$key] ?? null)
;

return $bag;
return new ContainerBag(new Container(new ParameterBag($params)));
}
}
Loading
0