8000 do not base services on PHPUnit mocks · symfony/symfony@6ed0797 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6ed0797

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 6ed0797

File tree

1 file changed

+57
-7
lines changed

1 file changed

+57
-7
lines changed

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

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@
2222
use Symfony\Component\EventDispatcher\EventDispatcher;
2323
use Symfony\Component\HttpKernel\DataCollector\DumpDataCollector;
2424
use Symfony\Component\HttpKernel\KernelInterface;
25+
use Symfony\Component\HttpKernel\Profiler\Profile;
2526
use Symfony\Component\HttpKernel\Profiler\Profiler;
2627
use Symfony\Component\HttpKernel\Profiler\ProfilerStorageInterface;
28+
use Symfony\Component\Routing\RequestContext;
29+
use Symfony\Component\Routing\RouteCollection;
2730
use Symfony\Component\Routing\RouterInterface;
2831

2932
class WebProfilerExtensionTest extends TestCase
@@ -58,15 +61,11 @@ protected function setUp(): void
5861

5962
$this->kernel = $this->createMock(KernelInterface::class);
6063

61-
$profiler = $this->createMock(Profiler::class);
62-
$profilerStorage = $this->createMock(ProfilerStorageInterface::class);
63-
$router = $this->createMock(RouterInterface::class);
64-
6564
$this->container = new ContainerBuilder();
6665
$this->container->register('data_collector.dump', DumpDataCollector::class)->setPublic(true);
6766
$this->container->register('error_handler.error_renderer.html', HtmlErrorRenderer::class)->setPublic(true);
6867
$this->container->register('event_dispatcher', EventDispatcher::class)->setPublic(true);
69-
$this->container->register('router', \get_class($router))->setPublic(true);
68+
$this->container->register('router', Router::class)->setPublic(true);
7069
$this->container->register('twig', 'Twig\Environment')->setPublic(true);
7170
$this->container->register('twig_loader', 'Twig\Loader\ArrayLoader')->addArgument([])->setPublic(true);
7271
$this->container->register('twig', 'Twig\Environment')->addArgument(new Reference('twig_loader'))->setPublic(true);
@@ -78,9 +77,9 @@ protected function setUp(): void
7877
$this->container->setParameter('kernel.charset', 'UTF-8');
7978
$this->container->setParameter('debug.file_link_format', null);
8079
$this->container->setParameter('profiler.class', ['Symfony\\Component\\HttpKernel\\Profiler\\Profiler']);
81-
$this->container->register('profiler', \get_class($profiler))
80+
$this->container->register('profiler', Profiler::class)
8281
->setPublic(true)
83-
->addArgument(new Definition(\get_class($profilerStorage)));
82+
->addArgument(new Definition(NullProfilerStorage::class));
8483
$this->container->setParameter('data_collector.templates', []);
8584
$this->container->set('kernel', $this->kernel);
8685
$< 8000 /span>this->container->addCompilerPass(new RegisterListenersPass());
@@ -212,3 +211,54 @@ private function getCompiledContainer()
212211
return $this->container;
213212
}
214213
}
214+
215+
class Router implements RouterInterface
216+
{
217+
private $context;
218+
219+
public function setContext(RequestContext $context): void
220+
{
221+
$this->context = $context;
222+
}
223+
224+
public function getContext(): RequestContext
225+
{
226+
return $this->context;
227+
}
228+
229+
public function getRouteCollection(): RouteCollection
230+
{
231+
return new RouteCollection();
232+
}
233+
234+
public function generate(string $name, array $parameters = [], int $referenceType = self::ABSOLUTE_PATH): string
235+
{
236+
}
237+
238+
public function match(string $pathinfo): array
239+
{
240+
return [];
241+
}
242+
}
243+
244+
class NullProfilerStorage implements ProfilerStorageInterface
245+
{
246+
public function find(?string $ip, ?string $url, ?int $limit, ?string $method, ?int $start = null, ?int $end = null): array
247+
{
248+
return [];
249+
}
250+
251+
public function read(string $token): ?Profile
252+
{
253+
706F return null;
254+
}
255+
256+
public function write(Profile $profile): bool
257+
{
258+
return true;
259+
}
260+
261+
public function purge()
262+
{
263+
}
264+
}

0 commit comments

Comments
 (0)
0