Skip to content

Commit 1bac251

Browse files
[Tests] Replace setMethods() by onlyMethods() and addMethods()
1 parent 4f82d8d commit 1bac251

File tree

10 files changed

+29
-23
lines changed

10 files changed

+29
-23
lines changed

Tests/DataCollector/LoggerDataCollectorTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function testCollectWithUnexpectedFormat()
2626
{
2727
$logger = $this
2828
->getMockBuilder(DebugLoggerInterface::class)
29-
->setMethods(['countErrors', 'getLogs', 'clear'])
29+
->onlyMethods(['countErrors', 'getLogs', 'clear'])
3030
->getMock();
3131
$logger->expects($this->once())->method('countErrors')->willReturn(123);
3232
$logger->expects($this->exactly(2))->method('getLogs')->willReturn([]);
@@ -66,7 +66,7 @@ public function testCollectFromDeprecationsLog()
6666

6767
$logger = $this
6868
->getMockBuilder(DebugLoggerInterface::class)
69-
->setMethods(['countErrors', 'getLogs', 'clear'])
69+
->onlyMethods(['countErrors', 'getLogs', 'clear'])
7070
->getMock();
7171

7272
$logger->expects($this->once())->method('countErrors')->willReturn(0);
@@ -100,7 +100,7 @@ public function testWithMainRequest()
100100

101101
$logger = $this
102102
->getMockBuilder(DebugLoggerInterface::class)
103-
->setMethods(['countErrors', 'getLogs', 'clear'])
103+
->onlyMethods(['countErrors', 'getLogs', 'clear'])
104104
->getMock();
105105
$logger->expects($this->once())->method('countErrors')->with(null);
106106
$logger->expects($this->exactly(2))->method('getLogs')->with(null)->willReturn([]);
@@ -121,7 +121,7 @@ public function testWithSubRequest()
121121

122122
$logger = $this
123123
->getMockBuilder(DebugLoggerInterface::class)
124-
->setMethods(['countErrors', 'getLogs', 'clear'])
124+
->onlyMethods(['countErrors', 'getLogs', 'clear'])
125125
->getMock();
126126
$logger->expects($this->once())->method('countErrors')->with($subRequest);
127127
$logger->expects($this->exactly(2))->method('getLogs')->with($subRequest)->willReturn([]);
@@ -139,7 +139,7 @@ public function testCollect($nb, $logs, $expectedLogs, $expectedDeprecationCount
139139
{
140140
$logger = $this
141141
->getMockBuilder(DebugLoggerInterface::class)
142-
->setMethods(['countErrors', 'getLogs', 'clear'])
142+
->onlyMethods(['countErrors', 'getLogs', 'clear'])
143143
->getMock();
144144
$logger->expects($this->once())->method('countErrors')->willReturn($nb);
145145
$logger->expects($this->exactly(2))->method('getLogs')->willReturn($logs);
@@ -171,7 +171,7 @@ public function testReset()
171171
{
172172
$logger = $this
173173
->getMockBuilder(DebugLoggerInterface::class)
174-
->setMethods(['countErrors', 'getLogs', 'clear'])
174+
->onlyMethods(['countErrors', 'getLogs', 'clear'])
175175
->getMock();
176176
$logger->expects($this->once())->method('clear');
177177

Tests/Debug/TraceableEventDispatcherTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function testStopwatchSections()
4848
public function testStopwatchCheckControllerOnRequestEvent()
4949
{
5050
$stopwatch = $this->getMockBuilder(Stopwatch::class)
51-
->setMethods(['isStarted'])
51+
->onlyMethods(['isStarted'])
5252
->getMock();
5353
$stopwatch->expects($this->once())
5454
->method('isStarted')
@@ -64,7 +64,7 @@ public function testStopwatchCheckControllerOnRequestEvent()
6464
public function testStopwatchStopControllerOnRequestEvent()
6565
{
6666
$stopwatch = $this->getMockBuilder(Stopwatch::class)
67-
->setMethods(['isStarted', 'stop'])
67+
->onlyMethods(['isStarted', 'stop'])
6868
->getMock();
6969
$stopwatch->expects($this->once())
7070
->method('isStarted')

Tests/EventListener/LocaleListenerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function testLocaleSetForRoutingContext()
7676
$context = $this->createMock(RequestContext::class);
7777
$context->expects($this->once())->method('setParameter')->with('_locale', 'es');
7878

79-
$router = $this->getMockBuilder(Router::class)->setMethods(['getContext'])->disableOriginalConstructor()->getMock();
79+
$router = $this->getMockBuilder(Router::class)->onlyMethods(['getContext'])->disableOriginalConstructor()->getMock();
8080
$router->expects($this->once())->method('getContext')->willReturn($context);
8181

8282
$request = Request::create('/');
@@ -92,7 +92,7 @@ public function testRouterResetWithParentRequestOnKernelFinishRequest()
9292
$context = $this->createMock(RequestContext::class);
9393
$context->expects($this->once())->method('setParameter')->with('_locale', 'es');
9494

95-
$router = $this->getMockBuilder(Router::class)->setMethods(['getContext'])->disableOriginalConstructor()->getMock();
95+
$router = $this->getMockBuilder(Router::class)->onlyMethods(['getContext'])->disableOriginalConstructor()->getMock();
9696
$router->expects($this->once())->method('getContext')->willReturn($context);
9797

9898
$parentRequest = Request::create('/');

Tests/HttpCache/EsiTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ public function testHandleWhenResponseIsNotModified()
232232

233233
protected function getCache($request, $response)
234234
{
235-
$cache = $this->getMockBuilder(HttpCache::class)->setMethods(['getRequest', 'handle'])->disableOriginalConstructor()->getMock();
235+
$cache = $this->getMockBuilder(HttpCache::class)->onlyMethods(['getRequest', 'handle'])->disableOriginalConstructor()->getMock();
236236
$cache->expects($this->any())
237237
->method('getRequest')
238238
->willReturn($request)

Tests/HttpCache/HttpCacheTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function testTerminateDelegatesTerminationOnlyForTerminableInterface()
4141
// implements TerminableInterface
4242
$kernelMock = $this->getMockBuilder(Kernel::class)
4343
->disableOriginalConstructor()
44-
->setMethods(['terminate', 'registerBundles', 'registerContainerConfiguration'])
44+
->onlyMethods(['terminate', 'registerBundles', 'registerContainerConfiguration'])
4545
->getMock();
4646

4747
$kernelMock->expects($this->once())

Tests/HttpCache/SsiTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ public function testHandleWhenResponseIsNot200AndAltIsPresent()
190190

191191
protected function getCache($request, $response)
192192
{
193-
$cache = $this->getMockBuilder(HttpCache::class)->setMethods(['getRequest', 'handle'])->disableOriginalConstructor()->getMock();
193+
$cache = $this->getMockBuilder(HttpCache::class)->onlyMethods(['getRequest', 'handle'])->disableOriginalConstructor()->getMock();
194194
$cache->expects($this->any())
195195
->method('getRequest')
196196
->willReturn($request)

Tests/HttpKernelBrowserTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ public function testUploadedFileWhenSizeExceedsUploadMaxFileSize()
155155
$file = $this
156156
->getMockBuilder(UploadedFile::class)
157157
->setConstructorArgs([$source, 'original', 'mime/original', \UPLOAD_ERR_OK, true])
158-
->setMethods(['getSize', 'getClientSize'])
158+
->onlyMethods(['getSize'])
159+
->addMethods(['getClientSize'])
159160
->getMock()
160161
;
161162
/* should be modified when the getClientSize will be removed */

Tests/HttpKernelTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ public function testVerifyRequestStackPushPopDuringHandle()
361361
{
362362
$request = new Request();
363363

364-
$stack = $this->getMockBuilder(RequestStack::class)->setMethods(['push', 'pop'])->getMock();
364+
$stack = $this->getMockBuilder(RequestStack::class)->onlyMethods(['push', 'pop'])->getMock();
365365
$stack->expects($this->once())->method('push')->with($this->equalTo($request));
366366
$stack->expects($this->once())->method('pop');
367367

Tests/KernelTest.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public function testBootSetsTheBootedFlagToTrue()
148148

149149
public function testClassCacheIsNotLoadedByDefault()
150150
{
151-
$kernel = $this->getKernel(['initializeBundles', 'doLoadClassCache']);
151+
$kernel = $this->getKernel(['initializeBundles'], [], false, ['doLoadClassCache']);
152152
$kernel->expects($this->never())
153153
->method('doLoadClassCache');
154154

@@ -449,7 +449,7 @@ public function testTerminateDelegatesTerminationOnlyForTerminableInterface()
449449
// implements TerminableInterface
450450
$httpKernelMock = $this->getMockBuilder(HttpKernel::class)
451451
->disableOriginalConstructor()
452-
->setMethods(['terminate'])
452+
->onlyMethods(['terminate'])
453453
->getMock();
454454

455455
$httpKernelMock
@@ -630,7 +630,7 @@ protected function getBundle($dir = null, $parent = null, $className = null, $bu
630630
{
631631
$bundle = $this
632632
->getMockBuilder(BundleInterface::class)
633-
->setMethods(['getPath', 'getName'])
633+
->onlyMethods(['getPath', 'getName'])
634634
->disableOriginalConstructor()
635635
;
636636

@@ -661,16 +661,21 @@ protected function getBundle($dir = null, $parent = null, $className = null, $bu
661661
* @param array $methods Additional methods to mock (besides the abstract ones)
662662
* @param array $bundles Bundles to register
663663
*/
664-
protected function getKernel(array $methods = [], array $bundles = [], bool $debug = false): Kernel
664+
protected function getKernel(array $methods = [], array $bundles = [], bool $debug = false, array $methodsToAdd = []): Kernel
665665
{
666666
$methods[] = 'registerBundles';
667667

668-
$kernel = $this
668+
$kernelMockBuilder = $this
669669
->getMockBuilder(KernelForTest::class)
670-
->setMethods($methods)
670+
->onlyMethods($methods)
671671
->setConstructorArgs(['test', $debug])
672-
->getMock()
673672
;
673+
674+
if (0 !== \count($methodsToAdd)) {
675+
$kernelMockBuilder->addMethods($methodsToAdd);
676+
}
677+
678+
$kernel = $kernelMockBuilder->getMock();
674679
$kernel->expects($this->any())
675680
->method('registerBundles')
676681
->willReturn($bundles)

Tests/Profiler/ProfilerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function testCollect()
4545
public function testReset()
4646
{
4747
$collector = $this->getMockBuilder(DataCollectorInterface::class)
48-
->setMethods(['collect', 'getName', 'reset'])
48+
->onlyMethods(['collect', 'getName', 'reset'])
4949
->getMock();
5050
$collector->expects($this->any())->method('getName')->willReturn('mock');
5151
$collector->expects($this->once())->method('reset');

0 commit comments

Comments
 (0)