Skip to content

Commit

Permalink
do not base services on PHPUnit mocks
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
xabbuh committed Sep 30, 2024
1 parent 75de6fa commit ac41164
Showing 1 changed file with 57 additions and 7 deletions.
64 changes: 57 additions & 7 deletions Tests/DependencyInjection/WebProfilerExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpKernel\DataCollector\DumpDataCollector;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\HttpKernel\Profiler\Profile;
use Symfony\Component\HttpKernel\Profiler\Profiler;
use Symfony\Component\HttpKernel\Profiler\ProfilerStorageInterface;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\RouterInterface;

class WebProfilerExtensionTest extends TestCase
Expand Down Expand Up @@ -58,15 +61,11 @@ protected function setUp(): void

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

$profiler = $this->createMock(Profiler::class);
$profilerStorage = $this->createMock(ProfilerStorageInterface::class);
$router = $this->createMock(RouterInterface::class);

$this->container = new ContainerBuilder();
$this->container->register('data_collector.dump', DumpDataCollector::class)->setPublic(true);
$this->container->register('error_handler.error_renderer.html', HtmlErrorRenderer::class)->setPublic(true);
$this->container->register('event_dispatcher', EventDispatcher::class)->setPublic(true);
$this->container->register('router', \get_class($router))->setPublic(true);
$this->container->register('router', Router::class)->setPublic(true);
$this->container->register('twig', 'Twig\Environment')->setPublic(true);
$this->container->register('twig_loader', 'Twig\Loader\ArrayLoader')->addArgument([])->setPublic(true);
$this->container->register('twig', 'Twig\Environment')->addArgument(new Reference('twig_loader'))->setPublic(true);
Expand All @@ -78,9 +77,9 @@ protected function setUp(): void
$this->container->setParameter('kernel.charset', 'UTF-8');
$this->container->setParameter('debug.file_link_format', null);
$this->container->setParameter('profiler.class', ['Symfony\\Component\\HttpKernel\\Profiler\\Profiler']);
$this->container->register('profiler', \get_class($profiler))
$this->container->register('profiler', Profiler::class)
->setPublic(true)
->addArgument(new Definition(\get_class($profilerStorage)));
->addArgument(new Definition(NullProfilerStorage::class));
$this->container->setParameter('data_collector.templates', []);
$this->container->set('kernel', $this->kernel);
$this->container->addCompilerPass(new RegisterListenersPass());
Expand Down Expand Up @@ -212,3 +211,54 @@ private function getCompiledContainer()
return $this->container;
}
}

class Router implements RouterInterface
{
private $context;

public function setContext(RequestContext $context): void
{
$this->context = $context;
}

public function getContext(): RequestContext
{
return $this->context;
}

public function getRouteCollection(): RouteCollection
{
return new RouteCollection();
}

public function generate(string $name, array $parameters = [], int $referenceType = self::ABSOLUTE_PATH): string
{
}

public function match(string $pathinfo): array
{
return [];
}
}

class NullProfilerStorage implements ProfilerStorageInterface
{
public function find(?string $ip, ?string $url, ?int $limit, ?string $method, ?int $start = null, ?int $end = null): array
{
return [];
}

public function read(string $token): ?Profile
{
return null;
}

public function write(Profile $profile): bool
{
return true;
}

public function purge()
{
}
}

0 comments on commit ac41164

Please sign in to comment.