Skip to content

Commit cb45edd

Browse files
minor #39941 Use createMock() and use import instead of FQCN (OskarStark)
This PR was squashed before being merged into the 4.4 branch. Discussion ---------- Use createMock() and use import instead of FQCN | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | --- | License | MIT | Doc PR | --- Commits ------- e7e61ee551 Use createMock() and use import instead of FQCN
2 parents ba084ae + f75654c commit cb45edd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+203
-185
lines changed

Tests/CacheClearer/ChainCacheClearerTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\HttpKernel\Tests\CacheClearer;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\HttpKernel\CacheClearer\CacheClearerInterface;
1516
use Symfony\Component\HttpKernel\CacheClearer\ChainCacheClearer;
1617

1718
class ChainCacheClearerTest extends TestCase
@@ -41,6 +42,6 @@ public function testInjectClearersInConstructor()
4142

4243
protected function getMockClearer()
4344
{
44-
return $this->getMockBuilder(\Symfony\Component\HttpKernel\CacheClearer\CacheClearerInterface::class)->getMock();
45+
return $this->createMock(CacheClearerInterface::class);
4546
}
4647
}

Tests/CacheClearer/Psr6CacheClearerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Psr6CacheClearerTest extends TestCase
1919
{
2020
public function testClearPoolsInjectedInConstructor()
2121
{
22-
$pool = $this->getMockBuilder(CacheItemPoolInterface::class)->getMock();
22+
$pool = $this->createMock(CacheItemPoolInterface::class);
2323
$pool
2424
->expects($this->once())
2525
->method('clear');
@@ -29,7 +29,7 @@ public function testClearPoolsInjectedInConstructor()
2929

3030
public function testClearPool()
3131
{
32-
$pool = $this->getMockBuilder(CacheItemPoolInterface::class)->getMock();
32+
$pool = $this->createMock(CacheItemPoolInterface::class);
3333
$pool
3434
->expects($this->once())
3535
->method('clear');

Tests/CacheWarmer/CacheWarmerAggregateTest.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerAggregate;
16+
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
1617

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

3132
public function testInjectWarmersUsingConstructor()
3233
{
33-
$warmer = $this->getCacheWarmerMock();
34+
$warmer = $this->createMock(CacheWarmerInterface::class);
3435
$warmer
3536
->expects($this->once())
3637
->method('warmUp');
@@ -40,7 +41,7 @@ public function testInjectWarmersUsingConstructor()
4041

4142
public function testWarmupDoesCallWarmupOnOptionalWarmersWhenEnableOptionalWarmersIsEnabled()
4243
{
43-
$warmer = $this->getCacheWarmerMock();
44+
$warmer = $this->createMock(CacheWarmerInterface::class);
4445
$warmer
4546
->expects($this->never())
4647
->method('isOptional');
@@ -55,7 +56,7 @@ public function testWarmupDoesCallWarmupOnOptionalWarmersWhenEnableOptionalWarme
5556

5657
public function testWarmupDoesNotCallWarmupOnOptionalWarmersWhenEnableOptionalWarmersIsNotEnabled()
5758
{
58-
$warmer = $this->getCacheWarmerMock();
59+
$warmer = $this->createMock(CacheWarmerInterface::class);
5960
$warmer
6061
->expects($this->once())
6162
->method('isOptional')
@@ -67,13 +68,4 @@ public function testWarmupDoesNotCallWarmupOnOptionalWarmersWhenEnableOptionalWa
6768
$aggregate = new CacheWarmerAggregate([$warmer]);
6869
$aggregate->warmUp(self::$cacheDir);
6970
}
70-
71-
protected function getCacheWarmerMock()
72-
{
73-
$warmer = $this->getMockBuilder(\Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface::class)
74-
->disableOriginalConstructor()
75-
->getMock();
76-
77-
return $warmer;
78-
}
7971
}

Tests/Config/FileLocatorTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\HttpKernel\Config\FileLocator;
16+
use Symfony\Component\HttpKernel\KernelInterface;
1617

1718
class FileLocatorTest extends TestCase
1819
{
1920
public function testLocate()
2021
{
21-
$kernel = $this->getMockBuilder(\Symfony\Component\HttpKernel\KernelInterface::class)->getMock();
22+
$kernel = $this->createMock(KernelInterface::class);
2223
$kernel
2324
->expects($this->atLeastOnce())
2425
->method('locateResource')
@@ -39,7 +40,7 @@ public function testLocate()
3940
*/
4041
public function testLocateWithGlobalResourcePath()
4142
{
42-
$kernel = $this->getMockBuilder(\Symfony\Component\HttpKernel\KernelInterface::class)->getMock();
43+
$kernel = $this->createMock(KernelInterface::class);
4344
$kernel
4445
->expects($this->atLeastOnce())
4546
->method('locateResource')

Tests/Controller/ArgumentResolver/NotTaggedControllerValueResolverTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\HttpKernel\Tests\Controller\ArgumentResolver;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
1516
use Symfony\Component\DependencyInjection\ServiceLocator;
1617
use Symfony\Component\HttpFoundation\Request;
1718
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\NotTaggedControllerValueResolver;
@@ -55,7 +56,7 @@ public function testDoNotSupportEmptyController()
5556

5657
public function testController()
5758
{
58-
$this->expectException(\Symfony\Component\DependencyInjection\Exception\RuntimeException::class);
59+
$this->expectException(RuntimeException::class);
5960
$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"?');
6061
$resolver = new NotTaggedControllerValueResolver(new ServiceLocator([]));
6162
$argument = new ArgumentMetadata('dummy', \stdClass::class, false, false, null);
@@ -66,7 +67,7 @@ public function testController()
6667

6768
public function testControllerWithATrailingBackSlash()
6869
{
69-
$this->expectException(\Symfony\Component\DependencyInjection\Exception\RuntimeException::class);
70+
$this->expectException(RuntimeException::class);
7071
$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"?');
7172
$resolver = new NotTaggedControllerValueResolver(new ServiceLocator([]));
7273
$argument = new ArgumentMetadata('dummy', \stdClass::class, false, false, null);
@@ -77,7 +78,7 @@ public function testControllerWithATrailingBackSlash()
7778

7879
public function testControllerWithMethodNameStartUppercase()
7980
{
80-
$this->expectException(\Symfony\Component\DependencyInjection\Exception\RuntimeException::class);
81+
$this->expectException(RuntimeException::class);
8182
$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"?');
8283
$resolver = new NotTaggedControllerValueResolver(new ServiceLocator([]));
8384
$argument = new ArgumentMetadata('dummy', \stdClass::class, false, false, null);
@@ -88,7 +89,7 @@ public function testControllerWithMethodNameStartUppercase()
8889

8990
public function testControllerNameIsAnArray()
9091
{
91-
$this->expectException(\Symfony\Component\DependencyInjection\Exception\RuntimeException::class);
92+
$this->expectException(RuntimeException::class);
9293
$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"?');
9394
$resolver = new NotTaggedControllerValueResolver(new ServiceLocator([]));
9495
$argument = new ArgumentMetadata('dummy', \stdClass::class, false, false, null);

Tests/Controller/ArgumentResolver/ServiceValueResolverTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
1617
use Symfony\Component\DependencyInjection\ServiceLocator;
1718
use Symfony\Component\HttpFoundation\Request;
1819
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\ServiceValueResolver;
@@ -107,7 +108,7 @@ public function testControllerNameIsAnArray()
107108

108109
public function testErrorIsTruncated()
109110
{
110-
$this->expectException(\Symfony\Component\DependencyInjection\Exception\RuntimeException::class);
111+
$this->expectException(RuntimeException::class);
111112
$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.');
112113
$container = new ContainerBuilder();
113114
$container->addCompilerPass(new RegisterControllerArgumentLocatorsPass());

Tests/Controller/ArgumentResolverTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public function testGetArgumentWithoutArray()
177177
{
178178
$this->expectException(\InvalidArgumentException::class);
179179
$factory = new ArgumentMetadataFactory();
180-
$valueResolver = $this->getMockBuilder(ArgumentValueResolverInterface::class)->getMock();
180+
$valueResolver = $this->createMock(ArgumentValueResolverInterface::class);
181181
$resolver = new ArgumentResolver($factory, [$valueResolver]);
182182

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

242242
public function testGetSessionArgumentsWithInterface()
243243
{
244-
$session = $this->getMockBuilder(SessionInterface::class)->getMock();
244+
$session = $this->createMock(SessionInterface::class);
245245
$request = Request::create('/');
246246
$request->setSession($session);
247247
$controller = [$this, 'controllerWithSessionInterface'];
@@ -252,7 +252,7 @@ public function testGetSessionArgumentsWithInterface()
252252
public function testGetSessionMissMatchWithInterface()
253253
{
254254
$this->expectException(\RuntimeException::class);
255-
$session = $this->getMockBuilder(SessionInterface::class)->getMock();
255+
$session = $this->createMock(SessionInterface::class);
256256
$request = Request::create('/');
257257
$request->setSession($session);
258258
$controller = [$this, 'controllerWithExtendingSession'];

Tests/Controller/ContainerControllerResolverTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public function testExceptionWhenUsingRemovedControllerServiceWithClassNameAsNam
153153
{
154154
$this->expectException(\InvalidArgumentException::class);
155155
$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"?');
156-
$container = $this->getMockBuilder(Container::class)->getMock();
156+
$container = $this->createMock(Container::class);
157157
$container->expects($this->once())
158158
->method('has')
159159
->with(ControllerTestService::class)
@@ -177,7 +177,7 @@ public function testExceptionWhenUsingRemovedControllerService()
177177
{
178178
$this->expectException(\InvalidArgumentException::class);
179179
$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"?');
180-
$container = $this->getMockBuilder(Container::class)->getMock();
180+
$container = $this->createMock(Container::class);
181181
$container->expects($this->once())
182182
->method('has')
183183
->with('app.my_controller')
@@ -232,7 +232,7 @@ protected function createControllerResolver(LoggerInterface $logger = null, Cont
232232

233233
protected function createMockContainer()
234234
{
235-
return $this->getMockBuilder(ContainerInterface::class)->getMock();
235+
return $this->createMock(ContainerInterface::class);
236236
}
237237
}
238238

Tests/Controller/ControllerResolverTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class ControllerResolverTest extends TestCase
2020
{
2121
public function testGetControllerWithoutControllerParameter()
2222
{
23-
$logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
23+
$logger = $this->createMock(LoggerInterface::class);
2424
$logger->expects($this->once())->method('warning')->with('Unable to look for the controller as the "_controller" parameter is missing.');
2525
$resolver = $this->createControllerResolver($logger);
2626

Tests/Controller/ErrorControllerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class ErrorControllerTest extends TestCase
2727
*/
2828
public function testInvokeController(Request $request, \Exception $exception, int $statusCode, string $content)
2929
{
30-
$kernel = $this->getMockBuilder(HttpKernelInterface::class)->getMock();
30+
$kernel = $this->createMock(HttpKernelInterface::class);
3131
$errorRenderer = new HtmlErrorRenderer();
3232
$controller = new ErrorController($kernel, null, $errorRenderer);
3333
$response = $controller($exception);
@@ -67,7 +67,7 @@ public function testPreviewController()
6767
$_controller = 'error_controller';
6868
$code = 404;
6969

70-
$kernel = $this->getMockBuilder(HttpKernelInterface::class)->getMock();
70+
$kernel = $this->createMock(HttpKernelInterface::class);
7171
$kernel
7272
->expects($this->once())
7373
->method('handle')

Tests/DataCollector/DumpDataCollectorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function testDumpWithServerConnection()
6262
$data = new Data([[123]]);
6363

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

6868
$collector = new DumpDataCollector(null, null, null, null, $serverDumper);

Tests/DataCollector/RequestDataCollectorTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Symfony\Component\HttpFoundation\Session\Session;
2222
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
2323
use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface;
24+
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
2425
use Symfony\Component\HttpKernel\DataCollector\RequestDataCollector;
2526
use Symfony\Component\HttpKernel\Event\ControllerEvent;
2627
use Symfony\Component\HttpKernel\Event\ResponseEvent;
@@ -203,7 +204,7 @@ public function testItAddsRedirectedAttributesWhenRequestContainsSpecificCookie(
203204
'sf_redirect' => '{}',
204205
]);
205206

206-
$kernel = $this->getMockBuilder(HttpKernelInterface::class)->getMock();
207+
$kernel = $this->createMock(HttpKernelInterface::class);
207208

208209
$c = new RequestDataCollector();
209210
$c->onKernelResponse(new ResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST, $this->createResponse()));
@@ -288,8 +289,8 @@ protected function createResponse()
288289
*/
289290
protected function injectController($collector, $controller, $request)
290291
{
291-
$resolver = $this->getMockBuilder(\Symfony\Component\HttpKernel\Controller\ControllerResolverInterface::class)->getMock();
292-
$httpKernel = new HttpKernel(new EventDispatcher(), $resolver, null, $this->getMockBuilder(ArgumentResolverInterface::class)->getMock());
292+
$resolver = $this->createMock(ControllerResolverInterface::class);
293+
$httpKernel = new HttpKernel(new EventDispatcher(), $resolver, null, $this->createMock(ArgumentResolverInterface::class));
293294
$event = new ControllerEvent($httpKernel, $controller, $request, HttpKernelInterface::MASTER_REQUEST);
294295
$collector->onKernelController($event);
295296
}

Tests/DataCollector/TimeDataCollectorTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\HttpFoundation\Request;
1616
use Symfony\Component\HttpFoundation\Response;
1717
use Symfony\Component\HttpKernel\DataCollector\TimeDataCollector;
18+
use Symfony\Component\HttpKernel\KernelInterface;
1819
use Symfony\Component\Stopwatch\Stopwatch;
1920

2021
/**
@@ -43,7 +44,7 @@ public function testCollect()
4344
$c->collect($request, new Response());
4445
$this->assertEquals(0, $c->getStartTime());
4546

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

4950
$c = new TimeDataCollector($kernel);

Tests/Debug/TraceableEventDispatcherTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
use Symfony\Component\HttpFoundation\Request;
1818
use Symfony\Component\HttpFoundation\RequestStack;
1919
use Symfony\Component\HttpFoundation\Response;
20+
use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface;
21+
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
2022
use Symfony\Component\HttpKernel\Debug\TraceableEventDispatcher;
2123
use Symfony\Component\HttpKernel\HttpKernel;
2224
use Symfony\Component\Stopwatch\Stopwatch;
@@ -110,9 +112,9 @@ public function testListenerCanRemoveItselfWhenExecuted()
110112

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

118120
return new HttpKernel($dispatcher, $controllerResolver, new RequestStack(), $argumentResolver);

Tests/DependencyInjection/LazyLoadingFragmentHandlerTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,25 @@
1212
namespace Symfony\Component\HttpKernel\Tests\DependencyInjection;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Psr\Container\ContainerInterface;
1516
use Symfony\Component\HttpFoundation\Request;
17+
use Symfony\Component\HttpFoundation\RequestStack;
1618
use Symfony\Component\HttpFoundation\Response;
1719
use Symfony\Component\HttpKernel\DependencyInjection\LazyLoadingFragmentHandler;
20+
use Symfony\Component\HttpKernel\Fragment\FragmentRendererInterface;
1821

1922
class LazyLoadingFragmentHandlerTest extends TestCase
2023
{
2124
public function testRender()
2225
{
23-
$renderer = $this->getMockBuilder(\Symfony\Component\HttpKernel\Fragment\FragmentRendererInterface::class)->getMock();
26+
$renderer = $this->createMock(FragmentRendererInterface::class);
2427
$renderer->expects($this->once())->method('getName')->willReturn('foo');
2528
$renderer->expects($this->any())->method('render')->willReturn(new Response());
2629

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

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

0 commit comments

Comments
 (0)