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

Skip to content

Commit 6b0ec7c

Browse files
committed
do not base services on PHPUnit mocks
Using the service container to build services bypasses PHPUnit's mock system which means that there is no guarantuee that objects are initialized in a way expected by PHPUnit. Thus mocks may or may not work as expected.
1 parent fe74ff3 commit 6b0ec7c

File tree

1 file changed

+31
-9
lines changed

1 file changed

+31
-9
lines changed

src/Symfony/Bundle/WebProfilerBundle/Tests/DependencyInjection/WebProfilerExtensionTest.php

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@
1515
use Symfony\Bundle\WebProfilerBundle\Tests\TestCase;
1616
use Symfony\Component\DependencyInjection\Container;
1717
use Symfony\Component\DependencyInjection\ContainerBuilder;
18-
use Symfony\Component\DependencyInjection\Definition;
1918
use Symfony\Component\DependencyInjection\Reference;
2019
use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer;
2120
use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass;
2221
use Symfony\Component\EventDispatcher\EventDispatcher;
2322
use Symfony\Component\HttpKernel\DataCollector\DumpDataCollector;
2423
use Symfony\Component\HttpKernel\KernelInterface;
24+
use Symfony\Component\HttpKernel\Profiler\FileProfilerStorage;
2525
use Symfony\Component\HttpKernel\Profiler\Profiler;
26-
use Symfony\Component\HttpKernel\Profiler\ProfilerStorageInterface;
26+
use Symfony\Component\Routing\RequestContext;
27+
use Symfony\Component\Routing\RouteCollection;
2728
use Symfony\Component\Routing\RouterInterface;
2829

2930
class WebProfilerExtensionTest extends TestCase
@@ -58,15 +59,11 @@ protected function setUp(): void
5859

5960
$this->kernel = $this->createMock(KernelInterface::class);
6061

61-
$profiler = $this->createMock(Profiler::class);
62-
$profilerStorage = $this->createMock(ProfilerStorageInterface::class);
63-
$router = $this->createMock(RouterInterface::class);
64-
6562
$this->container = new ContainerBuilder();
6663
$this->container->register('data_collector.dump', DumpDataCollector::class)->setPublic(true);
6764
$this->container->register('error_handler.error_renderer.html', HtmlErrorRenderer::class)->setPublic(true);
6865
$this->container->register('event_dispatcher', EventDispatcher::class)->setPublic(true);
69-
$this->container->register('router', \get_class($router))->setPublic(true);
66+
$this->container->register('router', Router::class)->setPublic(true);
7067
$this->container->register('twig', 'Twig\Environment')->setPublic(true);
7168
$this->container->register('twig_loader', 'Twig\Loader\ArrayLoader')->addArgument([])->setPublic(true);
7269
$this->container->register('twig', 'Twig\Environment')->addArgument(new Reference('twig_loader'))->setPublic(true);
@@ -78,9 +75,10 @@ protected function setUp(): void
7875
$this->container->setParameter('kernel.charset', 'UTF-8');
7976
$this->container->setParameter('debug.file_link_format', null);
8077
$this->container->setParameter('profiler.class', ['Symfony\\Component\\HttpKernel\\Profiler\\Profiler']);
81-
$this->container->register('profiler', \get_class($profiler))
78+
$this->container->register('profiler', Profiler::class)
8279
->setPublic(true)
83-
->addArgument(new Definition(\get_class($profilerStorage)));
80+
->addArgument(new Reference('profiler.storage'));
81+
$this->container->register('profiler.storage', FileProfilerStorage::class)->addArgument('file:/'.sys_get_temp_dir());
8482
$this->container->setParameter('data_collector.templates', []);
8583
$this->container->set('kernel', $this->kernel);
8684
$this->container->addCompilerPass(new RegisterListenersPass());
@@ -212,3 +210,27 @@ private function getCompiledContainer()
212210
return $this->container;
213211
}
214212
}
213+
214+
class Router implements RouterInterface
215+
{
216+
public function setContext(RequestContext $context): void
217+
{
218+
}
219+
220+
public function getContext(): RequestContext
221+
{
222+
}
223+
224+
public function getRouteCollection(): RouteCollection
225+
{
226+
return new RouteCollection();
227+
}
228+
229+
public function generate(string $name, array $parameters = [], int $referenceType = self::ABSOLUTE_PATH): string
230+
{
231+
}
232+
233+
public function match(string $pathinfo): array
234+
{
235+
}
236+
}

0 commit comments

Comments
 (0)