From 0bc2199c6c1f05276b05956f1ddc63f6d7eb5fc3 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Mon, 12 Jan 2026 14:59:48 +0100 Subject: [PATCH 1/2] do not use PHPUnit mock objects without configured expectations --- Tests/ApplicationTest.php | 14 +++++++------- Tests/EventListener/ErrorListenerTest.php | 8 ++++---- Tests/Helper/AbstractQuestionHelperTestCase.php | 6 +++--- Tests/Helper/DumperNativeFallbackTest.php | 4 ++-- Tests/Helper/DumperTest.php | 7 ++----- Tests/Helper/HelperSetTest.php | 6 +++--- Tests/Helper/QuestionHelperTest.php | 4 ++-- Tests/Helper/SymfonyQuestionHelperTest.php | 4 ++-- Tests/Logger/ConsoleLoggerTest.php | 10 ++-------- Tests/Style/SymfonyStyleTest.php | 16 ++++++++-------- 10 files changed, 35 insertions(+), 44 deletions(-) diff --git a/Tests/ApplicationTest.php b/Tests/ApplicationTest.php index 5482fa238..606bc5608 100644 --- a/Tests/ApplicationTest.php +++ b/Tests/ApplicationTest.php @@ -931,13 +931,13 @@ public function testRenderExceptionEscapesLines() public function testRenderExceptionLineBreaks() { - $application = $this->getMockBuilder(MockableAppliationWithTerminalWidth::class) - ->onlyMethods(['getTerminalWidth']) - ->getMock(); + $application = new class extends MockableAppliationWithTerminalWidth { + public function getTerminalWidth(): int + { + return 120; + } + }; $application->setAutoExit(false); - $application->expects($this->any()) - ->method('getTerminalWidth') - ->willReturn(120); $application->register('foo')->setCode(function () { throw new \InvalidArgumentException("\n\nline 1 with extra spaces \nline 2\n\nline 4\n"); }); @@ -1521,7 +1521,7 @@ public function testRunWithFindError() $application->setCatchExceptions(false); // Throws an exception when find fails - $commandLoader = $this->createMock(CommandLoaderInterface::class); + $commandLoader = $this->createStub(CommandLoaderInterface::class); $commandLoader->method('getNames')->willThrowException(new \Error('Find exception')); $application->setCommandLoader($commandLoader); diff --git a/Tests/EventListener/ErrorListenerTest.php b/Tests/EventListener/ErrorListenerTest.php index 40020baee..80cb17522 100644 --- a/Tests/EventListener/ErrorListenerTest.php +++ b/Tests/EventListener/ErrorListenerTest.php @@ -22,7 +22,7 @@ use Symfony\Component\Console\Input\Input; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\StringInput; -use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Output\NullOutput; class ErrorListenerTest extends TestCase { @@ -38,7 +38,7 @@ public function testOnConsoleError() ; $listener = new ErrorListener($logger); - $listener->onConsoleError(new ConsoleErrorEvent(new ArgvInput(['console.php', 'test:run', '--foo=baz', 'buzz']), $this->createMock(OutputInterface::class), $error, new Command('test:run'))); + $listener->onConsoleError(new ConsoleErrorEvent(new ArgvInput(['console.php', 'test:run', '--foo=baz', 'buzz']), new NullOutput(), $error, new Command('test:run'))); } public function testOnConsoleErrorWithNoCommandAndNoInputString() @@ -53,7 +53,7 @@ public function testOnConsoleErrorWithNoCommandAndNoInputString() ; $listener = new ErrorListener($logger); - $listener->onConsoleError(new ConsoleErrorEvent(new NonStringInput(), $this->createMock(OutputInterface::class), $error)); + $listener->onConsoleError(new ConsoleErrorEvent(new NonStringInput(), new NullOutput(), $error)); } public function testOnConsoleTerminateForNonZeroExitCodeWritesToLog() @@ -122,7 +122,7 @@ public function testCommandNameIsDisplayedForNonStringableInput() private function getConsoleTerminateEvent(InputInterface $input, $exitCode) { - return new ConsoleTerminateEvent(new Command('test:run'), $input, $this->createMock(OutputInterface::class), $exitCode); + return new ConsoleTerminateEvent(new Command('test:run'), $input, new NullOutput(), $exitCode); } } diff --git a/Tests/Helper/AbstractQuestionHelperTestCase.php b/Tests/Helper/AbstractQuestionHelperTestCase.php index 66f45c27f..ebaf5e661 100644 --- a/Tests/Helper/AbstractQuestionHelperTestCase.php +++ b/Tests/Helper/AbstractQuestionHelperTestCase.php @@ -18,13 +18,13 @@ abstract class AbstractQuestionHelperTestCase extends TestCase { protected function createStreamableInputInterfaceMock($stream = null, $interactive = true) { - $mock = $this->createMock(StreamableInputInterface::class); - $mock->expects($this->any()) + $mock = $this->createStub(StreamableInputInterface::class); + $mock ->method('isInteractive') ->willReturn($interactive); if ($stream) { - $mock->expects($this->any()) + $mock ->method('getStream') ->willReturn($stream); } diff --git a/Tests/Helper/DumperNativeFallbackTest.php b/Tests/Helper/DumperNativeFallbackTest.php index 1b37e4e93..362513ef9 100644 --- a/Tests/Helper/DumperNativeFallbackTest.php +++ b/Tests/Helper/DumperNativeFallbackTest.php @@ -14,7 +14,7 @@ use PHPUnit\Framework\TestCase; use Symfony\Bridge\PhpUnit\ClassExistsMock; use Symfony\Component\Console\Helper\Dumper; -use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Output\NullOutput; use Symfony\Component\VarDumper\Dumper\CliDumper; class DumperNativeFallbackTest extends TestCase @@ -37,7 +37,7 @@ public static function tearDownAfterClass(): void */ public function testInvoke($variable, $primitiveString) { - $dumper = new Dumper($this->createMock(OutputInterface::class)); + $dumper = new Dumper(new NullOutput()); $this->assertSame($primitiveString, $dumper($variable)); } diff --git a/Tests/Helper/DumperTest.php b/Tests/Helper/DumperTest.php index 0a30c6ec3..04e4bf498 100644 --- a/Tests/Helper/DumperTest.php +++ b/Tests/Helper/DumperTest.php @@ -13,7 +13,7 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\Console\Helper\Dumper; -use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Output\NullOutput; use Symfony\Component\VarDumper\Test\VarDumperTestTrait; class DumperTest extends TestCase @@ -37,10 +37,7 @@ public static function tearDownAfterClass(): void */ public function testInvoke($variable) { - $output = $this->createMock(OutputInterface::class); - $output->method('isDecorated')->willReturn(false); - - $dumper = new Dumper($output); + $dumper = new Dumper(new NullOutput()); $this->assertDumpMatchesFormat($dumper($variable), $variable); } diff --git a/Tests/Helper/HelperSetTest.php b/Tests/Helper/HelperSetTest.php index 389ee0ed3..014a745e8 100644 --- a/Tests/Helper/HelperSetTest.php +++ b/Tests/Helper/HelperSetTest.php @@ -89,13 +89,13 @@ public function testIteration() private function getGenericMockHelper($name, ?HelperSet $helperset = null) { - $mock_helper = $this->createMock(HelperInterface::class); - $mock_helper->expects($this->any()) + $mock_helper = $this->createStub(HelperInterface::class); + $mock_helper ->method('getName') ->willReturn($name); if ($helperset) { - $mock_helper->expects($this->any()) + $mock_helper ->method('setHelperSet') ->with($this->equalTo($helperset)); } diff --git a/Tests/Helper/QuestionHelperTest.php b/Tests/Helper/QuestionHelperTest.php index a152f596e..18ab1aa36 100644 --- a/Tests/Helper/QuestionHelperTest.php +++ b/Tests/Helper/QuestionHelperTest.php @@ -987,8 +987,8 @@ protected function createOutputInterface() protected function createInputInterfaceMock($interactive = true) { - $mock = $this->createMock(InputInterface::class); - $mock->expects($this->any()) + $mock = $this->createStub(InputInterface::class); + $mock ->method('isInteractive') ->willReturn($interactive); diff --git a/Tests/Helper/SymfonyQuestionHelperTest.php b/Tests/Helper/SymfonyQuestionHelperTest.php index 6cf79965b..c31a9106d 100644 --- a/Tests/Helper/SymfonyQuestionHelperTest.php +++ b/Tests/Helper/SymfonyQuestionHelperTest.php @@ -202,8 +202,8 @@ protected function createOutputInterface() protected function createInputInterfaceMock($interactive = true) { - $mock = $this->createMock(InputInterface::class); - $mock->expects($this->any()) + $mock = $this->createStub(InputInterface::class); + $mock ->method('isInteractive') ->willReturn($interactive); diff --git a/Tests/Logger/ConsoleLoggerTest.php b/Tests/Logger/ConsoleLoggerTest.php index 43d779631..115de14db 100644 --- a/Tests/Logger/ConsoleLoggerTest.php +++ b/Tests/Logger/ConsoleLoggerTest.php @@ -151,14 +151,7 @@ public function testContextReplacement() public function testObjectCastToString() { - if (method_exists($this, 'createPartialMock')) { - $dummy = $this->createPartialMock(DummyTest::class, ['__toString']); - } else { - $dummy = $this->createPartialMock(DummyTest::class, ['__toString']); - } - $dummy->method('__toString')->willReturn('DUMMY'); - - $this->getLogger()->warning($dummy); + $this->getLogger()->warning(new DummyTest()); $expected = ['warning DUMMY']; $this->assertEquals($expected, $this->getLogs()); @@ -201,5 +194,6 @@ class DummyTest { public function __toString(): string { + return 'DUMMY'; } } diff --git a/Tests/Style/SymfonyStyleTest.php b/Tests/Style/SymfonyStyleTest.php index 0b40c7c3f..52befcf44 100644 --- a/Tests/Style/SymfonyStyleTest.php +++ b/Tests/Style/SymfonyStyleTest.php @@ -98,7 +98,7 @@ public function testOutputProgressIterate() public function testGetErrorStyle() { - $input = $this->createMock(InputInterface::class); + $input = $this->createStub(InputInterface::class); $errorOutput = $this->createMock(OutputInterface::class); $errorOutput @@ -123,7 +123,7 @@ public function testGetErrorStyle() public function testCreateTableWithConsoleOutput() { - $input = $this->createMock(InputInterface::class); + $input = $this->createStub(InputInterface::class); $output = $this->createMock(ConsoleOutputInterface::class); $output ->method('getFormatter') @@ -131,7 +131,7 @@ public function testCreateTableWithConsoleOutput() $output ->expects($this->once()) ->method('section') - ->willReturn($this->createMock(ConsoleSectionOutput::class)); + ->willReturn($this->createStub(ConsoleSectionOutput::class)); $style = new SymfonyStyle($input, $output); @@ -140,8 +140,8 @@ public function testCreateTableWithConsoleOutput() public function testCreateTableWithoutConsoleOutput() { - $input = $this->createMock(InputInterface::class); - $output = $this->createMock(OutputInterface::class); + $input = $this->createStub(InputInterface::class); + $output = $this->createStub(OutputInterface::class); $output ->method('getFormatter') ->willReturn(new OutputFormatter()); @@ -156,12 +156,12 @@ public function testCreateTableWithoutConsoleOutput() public function testGetErrorStyleUsesTheCurrentOutputIfNoErrorOutputIsAvailable() { - $output = $this->createMock(OutputInterface::class); + $output = $this->createStub(OutputInterface::class); $output ->method('getFormatter') ->willReturn(new OutputFormatter()); - $style = new SymfonyStyle($this->createMock(InputInterface::class), $output); + $style = new SymfonyStyle($this->createStub(InputInterface::class), $output); $this->assertInstanceOf(SymfonyStyle::class, $style->getErrorStyle()); } @@ -186,7 +186,7 @@ public function testAskAndClearExpectFullSectionCleared() $inputStream = fopen('php://memory', 'r+'); fwrite($inputStream, $answer.\PHP_EOL); rewind($inputStream); - $input = $this->createMock(Input::class); + $input = $this->createStub(Input::class); $sections = []; $output = new ConsoleSectionOutput(fopen('php://memory', 'r+', false), $sections, StreamOutput::VERBOSITY_NORMAL, true, new OutputFormatter()); $input From a28c3e5b4df406e8fae8e9b18c40557b5dfc430c Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Mon, 12 Jan 2026 14:59:48 +0100 Subject: [PATCH 2/2] do not use PHPUnit mock objects without configured expectations --- Tests/Command/LockableTraitTest.php | 2 +- Tests/Style/SymfonyStyleTest.php | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Tests/Command/LockableTraitTest.php b/Tests/Command/LockableTraitTest.php index 3000906d7..c9746fceb 100644 --- a/Tests/Command/LockableTraitTest.php +++ b/Tests/Command/LockableTraitTest.php @@ -76,7 +76,7 @@ public function testCustomLockFactoryIsUsed() $tester = new CommandTester($command); - $lock = $this->createMock(SharedLockInterface::class); + $lock = $this->createStub(SharedLockInterface::class); $lock->method('acquire')->willReturn(false); $lockFactory->expects(static::once())->method('createLock')->willReturn($lock); diff --git a/Tests/Style/SymfonyStyleTest.php b/Tests/Style/SymfonyStyleTest.php index e66131248..1cada068d 100644 --- a/Tests/Style/SymfonyStyleTest.php +++ b/Tests/Style/SymfonyStyleTest.php @@ -158,12 +158,12 @@ public function testCreateTableWithoutConsoleOutput() public function testCreateTree() { - $output = $this->createMock(OutputInterface::class); + $output = $this->createStub(OutputInterface::class); $output ->method('getFormatter') ->willReturn(new OutputFormatter()); - $style = new SymfonyStyle($this->createMock(InputInterface::class), $output); + $style = new SymfonyStyle($this->createStub(InputInterface::class), $output); $tree = $style->createTree([]); $this->assertInstanceOf(TreeHelper::class, $tree); @@ -171,7 +171,7 @@ public function testCreateTree() public function testTree() { - $input = $this->createMock(InputInterface::class); + $input = $this->createStub(InputInterface::class); $output = new BufferedOutput(); $style = new SymfonyStyle($input, $output); @@ -192,7 +192,7 @@ public function testTree() public function testCreateTreeWithArray() { - $input = $this->createMock(InputInterface::class); + $input = $this->createStub(InputInterface::class); $output = new BufferedOutput(); $style = new SymfonyStyle($input, $output); @@ -213,7 +213,7 @@ public function testCreateTreeWithArray() public function testCreateTreeWithIterable() { - $input = $this->createMock(InputInterface::class); + $input = $this->createStub(InputInterface::class); $output = new BufferedOutput(); $style = new SymfonyStyle($input, $output); @@ -234,7 +234,7 @@ public function testCreateTreeWithIterable() public function testCreateTreeWithConsoleOutput() { - $input = $this->createMock(InputInterface::class); + $input = $this->createStub(InputInterface::class); $output = $this->createMock(ConsoleOutputInterface::class); $output ->method('getFormatter') @@ -242,7 +242,7 @@ public function testCreateTreeWithConsoleOutput() $output ->expects($this->once()) ->method('section') - ->willReturn($this->createMock(ConsoleSectionOutput::class)); + ->willReturn($this->createStub(ConsoleSectionOutput::class)); $style = new SymfonyStyle($input, $output);