Skip to content

Commit

Permalink
Use createMock() and use import instead of FQCN
Browse files Browse the repository at this point in the history
  • Loading branch information
OskarStark authored and nicolas-grekas committed Jan 27, 2021
1 parent df632a4 commit f75654c
Show file tree
Hide file tree
Showing 46 changed files with 203 additions and 185 deletions.
3 changes: 2 additions & 1 deletion Tests/CacheClearer/ChainCacheClearerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\HttpKernel\Tests\CacheClearer;

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpKernel\CacheClearer\CacheClearerInterface;
use Symfony\Component\HttpKernel\CacheClearer\ChainCacheClearer;

class ChainCacheClearerTest extends TestCase
Expand Down Expand Up @@ -41,6 +42,6 @@ public function testInjectClearersInConstructor()

protected function getMockClearer()
{
return $this->getMockBuilder(\Symfony\Component\HttpKernel\CacheClearer\CacheClearerInterface::class)->getMock();
return $this->createMock(CacheClearerInterface::class);
}
}
4 changes: 2 additions & 2 deletions Tests/CacheClearer/Psr6CacheClearerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Psr6CacheClearerTest extends TestCase
{
public function testClearPoolsInjectedInConstructor()
{
$pool = $this->getMockBuilder(CacheItemPoolInterface::class)->getMock();
$pool = $this->createMock(CacheItemPoolInterface::class);
$pool
->expects($this->once())
->method('clear');
Expand All @@ -29,7 +29,7 @@ public function testClearPoolsInjectedInConstructor()

public function testClearPool()
{
$pool = $this->getMockBuilder(CacheItemPoolInterface::class)->getMock();
$pool = $this->createMock(CacheItemPoolInterface::class);
$pool
->expects($this->once())
->method('clear');
Expand Down
16 changes: 4 additions & 12 deletions Tests/CacheWarmer/CacheWarmerAggregateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerAggregate;
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;

class CacheWarmerAggregateTest extends TestCase
{
Expand All @@ -30,7 +31,7 @@ public static function tearDownAfterClass(): void

public function testInjectWarmersUsingConstructor()
{
$warmer = $this->getCacheWarmerMock();
$warmer = $this->createMock(CacheWarmerInterface::class);
$warmer
->expects($this->once())
->method('warmUp');
Expand All @@ -40,7 +41,7 @@ public function testInjectWarmersUsingConstructor()

public function testWarmupDoesCallWarmupOnOptionalWarmersWhenEnableOptionalWarmersIsEnabled()
{
$warmer = $this->getCacheWarmerMock();
$warmer = $this->createMock(CacheWarmerInterface::class);
$warmer
->expects($this->never())
->method('isOptional');
Expand All @@ -55,7 +56,7 @@ public function testWarmupDoesCallWarmupOnOptionalWarmersWhenEnableOptionalWarme

public function testWarmupDoesNotCallWarmupOnOptionalWarmersWhenEnableOptionalWarmersIsNotEnabled()
{
$warmer = $this->getCacheWarmerMock();
$warmer = $this->createMock(CacheWarmerInterface::class);
$warmer
->expects($this->once())
->method('isOptional')
Expand All @@ -67,13 +68,4 @@ public function testWarmupDoesNotCallWarmupOnOptionalWarmersWhenEnableOptionalWa
$aggregate = new CacheWarmerAggregate([$warmer]);
$aggregate->warmUp(self::$cacheDir);
}

protected function getCacheWarmerMock()
{
$warmer = $this->getMockBuilder(\Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface::class)
->disableOriginalConstructor()
->getMock();

return $warmer;
}
}
5 changes: 3 additions & 2 deletions Tests/Config/FileLocatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpKernel\Config\FileLocator;
use Symfony\Component\HttpKernel\KernelInterface;

class FileLocatorTest extends TestCase
{
public function testLocate()
{
$kernel = $this->getMockBuilder(\Symfony\Component\HttpKernel\KernelInterface::class)->getMock();
$kernel = $this->createMock(KernelInterface::class);
$kernel
->expects($this->atLeastOnce())
->method('locateResource')
Expand All @@ -39,7 +40,7 @@ public function testLocate()
*/
public function testLocateWithGlobalResourcePath()
{
$kernel = $this->getMockBuilder(\Symfony\Component\HttpKernel\KernelInterface::class)->getMock();
$kernel = $this->createMock(KernelInterface::class);
$kernel
->expects($this->atLeastOnce())
->method('locateResource')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\HttpKernel\Tests\Controller\ArgumentResolver;

use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\ServiceLocator;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\NotTaggedControllerValueResolver;
Expand Down Expand Up @@ -55,7 +56,7 @@ public function testDoNotSupportEmptyController()

public function testController()
{
$this->expectException(\Symfony\Component\DependencyInjection\Exception\RuntimeException::class);
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Could not resolve argument $dummy of "App\Controller\Mine::method()", maybe you forgot to register the controller as a service or missed tagging it with the "controller.service_arguments"?');
$resolver = new NotTaggedControllerValueResolver(new ServiceLocator([]));
$argument = new ArgumentMetadata('dummy', \stdClass::class, false, false, null);
Expand All @@ -66,7 +67,7 @@ public function testController()

public function testControllerWithATrailingBackSlash()
{
$this->expectException(\Symfony\Component\DependencyInjection\Exception\RuntimeException::class);
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Could not resolve argument $dummy of "App\Controller\Mine::method()", maybe you forgot to register the controller as a service or missed tagging it with the "controller.service_arguments"?');
$resolver = new NotTaggedControllerValueResolver(new ServiceLocator([]));
$argument = new ArgumentMetadata('dummy', \stdClass::class, false, false, null);
Expand All @@ -77,7 +78,7 @@ public function testControllerWithATrailingBackSlash()

public function testControllerWithMethodNameStartUppercase()
{
$this->expectException(\Symfony\Component\DependencyInjection\Exception\RuntimeException::class);
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Could not resolve argument $dummy of "App\Controller\Mine::method()", maybe you forgot to register the controller as a service or missed tagging it with the "controller.service_arguments"?');
$resolver = new NotTaggedControllerValueResolver(new ServiceLocator([]));
$argument = new ArgumentMetadata('dummy', \stdClass::class, false, false, null);
Expand All @@ -88,7 +89,7 @@ public function testControllerWithMethodNameStartUppercase()

public function testControllerNameIsAnArray()
{
$this->expectException(\Symfony\Component\DependencyInjection\Exception\RuntimeException::class);
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Could not resolve argument $dummy of "App\Controller\Mine::method()", maybe you forgot to register the controller as a service or missed tagging it with the "controller.service_arguments"?');
$resolver = new NotTaggedControllerValueResolver(new ServiceLocator([]));
$argument = new ArgumentMetadata('dummy', \stdClass::class, false, false, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\ServiceLocator;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\ServiceValueResolver;
Expand Down Expand Up @@ -107,7 +108,7 @@ public function testControllerNameIsAnArray()

public function testErrorIsTruncated()
{
$this->expectException(\Symfony\Component\DependencyInjection\Exception\RuntimeException::class);
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Cannot autowire argument $dummy of "Symfony\Component\HttpKernel\Tests\Controller\ArgumentResolver\DummyController::index()": it references class "Symfony\Component\HttpKernel\Tests\Controller\ArgumentResolver\DummyService" but no such service exists.');
$container = new ContainerBuilder();
$container->addCompilerPass(new RegisterControllerArgumentLocatorsPass());
Expand Down
6 changes: 3 additions & 3 deletions Tests/Controller/ArgumentResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public function testGetArgumentWithoutArray()
{
$this->expectException(\InvalidArgumentException::class);
$factory = new ArgumentMetadataFactory();
$valueResolver = $this->getMockBuilder(ArgumentValueResolverInterface::class)->getMock();
$valueResolver = $this->createMock(ArgumentValueResolverInterface::class);
$resolver = new ArgumentResolver($factory, [$valueResolver]);

$valueResolver->expects($this->any())->method('supports')->willReturn(true);
Expand Down Expand Up @@ -241,7 +241,7 @@ public function testGetSessionArgumentsWithExtendedSession()

public function testGetSessionArgumentsWithInterface()
{
$session = $this->getMockBuilder(SessionInterface::class)->getMock();
$session = $this->createMock(SessionInterface::class);
$request = Request::create('/');
$request->setSession($session);
$controller = [$this, 'controllerWithSessionInterface'];
Expand All @@ -252,7 +252,7 @@ public function testGetSessionArgumentsWithInterface()
public function testGetSessionMissMatchWithInterface()
{
$this->expectException(\RuntimeException::class);
$session = $this->getMockBuilder(SessionInterface::class)->getMock();
$session = $this->createMock(SessionInterface::class);
$request = Request::create('/');
$request->setSession($session);
$controller = [$this, 'controllerWithExtendingSession'];
Expand Down
6 changes: 3 additions & 3 deletions Tests/Controller/ContainerControllerResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public function testExceptionWhenUsingRemovedControllerServiceWithClassNameAsNam
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Controller "Symfony\Component\HttpKernel\Tests\Controller\ControllerTestService" cannot be fetched from the container because it is private. Did you forget to tag the service with "controller.service_arguments"?');
$container = $this->getMockBuilder(Container::class)->getMock();
$container = $this->createMock(Container::class);
$container->expects($this->once())
->method('has')
->with(ControllerTestService::class)
Expand All @@ -177,7 +177,7 @@ public function testExceptionWhenUsingRemovedControllerService()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Controller "app.my_controller" cannot be fetched from the container because it is private. Did you forget to tag the service with "controller.service_arguments"?');
$container = $this->getMockBuilder(Container::class)->getMock();
$container = $this->createMock(Container::class);
$container->expects($this->once())
->method('has')
->with('app.my_controller')
Expand Down Expand Up @@ -232,7 +232,7 @@ protected function createControllerResolver(LoggerInterface $logger = null, Cont

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

Expand Down
2 changes: 1 addition & 1 deletion Tests/Controller/ControllerResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ControllerResolverTest extends TestCase
{
public function testGetControllerWithoutControllerParameter()
{
$logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
$logger = $this->createMock(LoggerInterface::class);
$logger->expects($this->once())->method('warning')->with('Unable to look for the controller as the "_controller" parameter is missing.');
$resolver = $this->createControllerResolver($logger);

Expand Down
4 changes: 2 additions & 2 deletions Tests/Controller/ErrorControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ErrorControllerTest extends TestCase
*/
public function testInvokeController(Request $request, \Exception $exception, int $statusCode, string $content)
{
$kernel = $this->getMockBuilder(HttpKernelInterface::class)->getMock();
$kernel = $this->createMock(HttpKernelInterface::class);
$errorRenderer = new HtmlErrorRenderer();
$controller = new ErrorController($kernel, null, $errorRenderer);
$response = $controller($exception);
Expand Down Expand Up @@ -67,7 +67,7 @@ public function testPreviewController()
$_controller = 'error_controller';
$code = 404;

$kernel = $this->getMockBuilder(HttpKernelInterface::class)->getMock();
$kernel = $this->createMock(HttpKernelInterface::class);
$kernel
->expects($this->once())
->method('handle')
Expand Down
2 changes: 1 addition & 1 deletion Tests/DataCollector/DumpDataCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function testDumpWithServerConnection()
$data = new Data([[123]]);

// Server is up, server dumper is used
$serverDumper = $this->getMockBuilder(Connection::class)->disableOriginalConstructor()->getMock();
$serverDumper = $this->createMock(Connection::class);
$serverDumper->expects($this->once())->method('write')->willReturn(true);

$collector = new DumpDataCollector(null, null, null, null, $serverDumper);
Expand Down
7 changes: 4 additions & 3 deletions Tests/DataCollector/RequestDataCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface;
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
use Symfony\Component\HttpKernel\DataCollector\RequestDataCollector;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
Expand Down Expand Up @@ -203,7 +204,7 @@ public function testItAddsRedirectedAttributesWhenRequestContainsSpecificCookie(
'sf_redirect' => '{}',
]);

$kernel = $this->getMockBuilder(HttpKernelInterface::class)->getMock();
$kernel = $this->createMock(HttpKernelInterface::class);

$c = new RequestDataCollector();
$c->onKernelResponse(new ResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST, $this->createResponse()));
Expand Down Expand Up @@ -288,8 +289,8 @@ protected function createResponse()
*/
protected function injectController($collector, $controller, $request)
{
$resolver = $this->getMockBuilder(\Symfony\Component\HttpKernel\Controller\ControllerResolverInterface::class)->getMock();
$httpKernel = new HttpKernel(new EventDispatcher(), $resolver, null, $this->getMockBuilder(ArgumentResolverInterface::class)->getMock());
$resolver = $this->createMock(ControllerResolverInterface::class);
$httpKernel = new HttpKernel(new EventDispatcher(), $resolver, null, $this->createMock(ArgumentResolverInterface::class));
$event = new ControllerEvent($httpKernel, $controller, $request, HttpKernelInterface::MASTER_REQUEST);
$collector->onKernelController($event);
}
Expand Down
3 changes: 2 additions & 1 deletion Tests/DataCollector/TimeDataCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\TimeDataCollector;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Stopwatch\Stopwatch;

/**
Expand Down Expand Up @@ -43,7 +44,7 @@ public function testCollect()
$c->collect($request, new Response());
$this->assertEquals(0, $c->getStartTime());

$kernel = $this->getMockBuilder(\Symfony\Component\HttpKernel\KernelInterface::class)->getMock();
$kernel = $this->createMock(KernelInterface::class);
$kernel->expects($this->once())->method('getStartTime')->willReturn(123456.0);

$c = new TimeDataCollector($kernel);
Expand Down
6 changes: 4 additions & 2 deletions Tests/Debug/TraceableEventDispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface;
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
use Symfony\Component\HttpKernel\Debug\TraceableEventDispatcher;
use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\Stopwatch\Stopwatch;
Expand Down Expand Up @@ -110,9 +112,9 @@ public function testListenerCanRemoveItselfWhenExecuted()

protected function getHttpKernel($dispatcher, $controller)
{
$controllerResolver = $this->getMockBuilder(\Symfony\Component\HttpKernel\Controller\ControllerResolverInterface::class)->getMock();
$controllerResolver = $this->createMock(ControllerResolverInterface::class);
$controllerResolver->expects($this->once())->method('getController')->willReturn($controller);
$argumentResolver = $this->getMockBuilder(\Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface::class)->getMock();
$argumentResolver = $this->createMock(ArgumentResolverInterface::class);
$argumentResolver->expects($this->once())->method('getArguments')->willReturn([]);

return new HttpKernel($dispatcher, $controllerResolver, new RequestStack(), $argumentResolver);
Expand Down
9 changes: 6 additions & 3 deletions Tests/DependencyInjection/LazyLoadingFragmentHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,25 @@
namespace Symfony\Component\HttpKernel\Tests\DependencyInjection;

use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DependencyInjection\LazyLoadingFragmentHandler;
use Symfony\Component\HttpKernel\Fragment\FragmentRendererInterface;

class LazyLoadingFragmentHandlerTest extends TestCase
{
public function testRender()
{
$renderer = $this->getMockBuilder(\Symfony\Component\HttpKernel\Fragment\FragmentRendererInterface::class)->getMock();
$renderer = $this->createMock(FragmentRendererInterface::class);
$renderer->expects($this->once())->method('getName')->willReturn('foo');
$renderer->expects($this->any())->method('render')->willReturn(new Response());

$requestStack = $this->getMockBuilder(\Symfony\Component\HttpFoundation\RequestStack::class)->getMock();
$requestStack = $this->createMock(RequestStack::class);
$requestStack->expects($this->any())->method('getCurrentRequest')->willReturn(Request::create('/'));

$container = $this->getMockBuilder(\Psr\Container\ContainerInterface::class)->getMock();
$container = $this->createMock(ContainerInterface::class);
$container->expects($this->once())->method('has')->with('foo')->willReturn(true);
$container->expects($this->once())->method('get')->willReturn($renderer);

Expand Down
Loading

0 comments on commit f75654c

Please sign in to comment.