Skip to content

Commit 24026c4

Browse files
OskarStarknicolas-grekas
authored andcommitted
Use createMock() and use import instead of FQCN
1 parent a6d92f8 commit 24026c4

16 files changed

+51
-37
lines changed

Tests/ApplicationTest.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Console\Application;
1616
use Symfony\Component\Console\Command\Command;
17+
use Symfony\Component\Console\Command\HelpCommand;
1718
use Symfony\Component\Console\CommandLoader\FactoryCommandLoader;
1819
use Symfony\Component\Console\DependencyInjection\AddConsoleCommandPass;
1920
use Symfony\Component\Console\Event\ConsoleCommandEvent;
@@ -29,6 +30,7 @@
2930
use Symfony\Component\Console\Input\InputDefinition;
3031
use Symfony\Component\Console\Input\InputInterface;
3132
use Symfony\Component\Console\Input\InputOption;
33+
use Symfony\Component\Console\Output\ConsoleOutput;
3234
use Symfony\Component\Console\Output\NullOutput;
3335
use Symfony\Component\Console\Output\Output;
3436
use Symfony\Component\Console\Output\OutputInterface;
@@ -134,7 +136,7 @@ public function testAll()
134136
{
135137
$application = new Application();
136138
$commands = $application->all();
137-
$this->assertInstanceOf(\Symfony\Component\Console\Command\HelpCommand::class, $commands['help'], '->all() returns the registered commands');
139+
$this->assertInstanceOf(HelpCommand::class, $commands['help'], '->all() returns the registered commands');
138140

139141
$application->add(new \FooCommand());
140142
$commands = $application->all('foo');
@@ -145,7 +147,7 @@ public function testAllWithCommandLoader()
145147
{
146148
$application = new Application();
147149
$commands = $application->all();
148-
$this->assertInstanceOf(\Symfony\Component\Console\Command\HelpCommand::class, $commands['help'], '->all() returns the registered commands');
150+
$this->assertInstanceOf(HelpCommand::class, $commands['help'], '->all() returns the registered commands');
149151

150152
$application->add(new \FooCommand());
151153
$commands = $application->all('foo');
@@ -229,7 +231,7 @@ public function testHasGet()
229231
$p->setAccessible(true);
230232
$p->setValue($application, true);
231233
$command = $application->get('foo:bar');
232-
$this->assertInstanceOf(\Symfony\Component\Console\Command\HelpCommand::class, $command, '->get() returns the help command if --help is provided as the input');
234+
$this->assertInstanceOf(HelpCommand::class, $command, '->get() returns the help command if --help is provided as the input');
233235
}
234236

235237
public function testHasGetWithCommandLoader()
@@ -351,7 +353,7 @@ public function testFind()
351353
$application->add(new \FooCommand());
352354

353355
$this->assertInstanceOf(\FooCommand::class, $application->find('foo:bar'), '->find() returns a command if its name exists');
354-
$this->assertInstanceOf(\Symfony\Component\Console\Command\HelpCommand::class, $application->find('h'), '->find() returns a command if its name exists');
356+
$this->assertInstanceOf(HelpCommand::class, $application->find('h'), '->find() returns a command if its name exists');
355357
$this->assertInstanceOf(\FooCommand::class, $application->find('f:bar'), '->find() returns a command if the abbreviation for the namespace exists');
356358
$this->assertInstanceOf(\FooCommand::class, $application->find('f:b'), '->find() returns a command if the abbreviation for the namespace and the command name exist');
357359
$this->assertInstanceOf(\FooCommand::class, $application->find('a'), '->find() returns a command if the abbreviation exists for an alias');
@@ -398,7 +400,7 @@ public function testFindWithCommandLoader()
398400
]));
399401

400402
$this->assertInstanceOf(\FooCommand::class, $application->find('foo:bar'), '->find() returns a command if its name exists');
401-
$this->assertInstanceOf(\Symfony\Component\Console\Command\HelpCommand::class, $application->find('h'), '->find() returns a command if its name exists');
403+
$this->assertInstanceOf(HelpCommand::class, $application->find('h'), '->find() returns a command if its name exists');
402404
$this->assertInstanceOf(\FooCommand::class, $application->find('f:bar'), '->find() returns a command if the abbreviation for the namespace exists');
403405
$this->assertInstanceOf(\FooCommand::class, $application->find('f:b'), '->find() returns a command if the abbreviation for the namespace and the command name exist');
404406
$this->assertInstanceOf(\FooCommand::class, $application->find('a'), '->find() returns a command if the abbreviation exists for an alias');
@@ -951,7 +953,7 @@ public function testRun()
951953
ob_end_clean();
952954

953955
$this->assertInstanceOf(ArgvInput::class, $command->input, '->run() creates an ArgvInput by default if none is given');
954-
$this->assertInstanceOf(\Symfony\Component\Console\Output\ConsoleOutput::class, $command->output, '->run() creates a ConsoleOutput by default if none is given');
956+
$this->assertInstanceOf(ConsoleOutput::class, $command->output, '->run() creates a ConsoleOutput by default if none is given');
955957

956958
$application = new Application();
957959
$application->setAutoExit(false);

Tests/Command/CommandTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Console\Application;
1616
use Symfony\Component\Console\Command\Command;
17+
use Symfony\Component\Console\Exception\InvalidOptionException;
1718
use Symfony\Component\Console\Helper\FormatterHelper;
1819
use Symfony\Component\Console\Input\InputArgument;
1920
use Symfony\Component\Console\Input\InputDefinition;
@@ -293,7 +294,7 @@ public function testExecuteMethodNeedsToBeOverridden()
293294

294295
public function testRunWithInvalidOption()
295296
{
296-
$this->expectException(\Symfony\Component\Console\Exception\InvalidOptionException::class);
297+
$this->expectException(InvalidOptionException::class);
297298
$this->expectExceptionMessage('The "--bar" option does not exist.');
298299
$command = new \TestCommand();
299300
$tester = new CommandTester($command);

Tests/CommandLoader/ContainerCommandLoaderTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Console\Command\Command;
1616
use Symfony\Component\Console\CommandLoader\ContainerCommandLoader;
17+
use Symfony\Component\Console\Exception\CommandNotFoundException;
1718
use Symfony\Component\DependencyInjection\ServiceLocator;
1819

1920
class ContainerCommandLoaderTest extends TestCase
@@ -43,7 +44,7 @@ public function testGet()
4344

4445
public function testGetUnknownCommandThrows()
4546
{
46-
$this->expectException(\Symfony\Component\Console\Exception\CommandNotFoundException::class);
47+
$this->expectException(CommandNotFoundException::class);
4748
(new ContainerCommandLoader(new ServiceLocator([]), []))->get('unknown');
4849
}
4950

Tests/CommandLoader/FactoryCommandLoaderTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Console\Command\Command;
1616
use Symfony\Component\Console\CommandLoader\FactoryCommandLoader;
17+
use Symfony\Component\Console\Exception\CommandNotFoundException;
1718

1819
class FactoryCommandLoaderTest extends TestCase
1920
{
@@ -42,7 +43,7 @@ public function testGet()
4243

4344
public function testGetUnknownCommandThrows()
4445
{
45-
$this->expectException(\Symfony\Component\Console\Exception\CommandNotFoundException::class);
46+
$this->expectException(CommandNotFoundException::class);
4647
(new FactoryCommandLoader([]))->get('unknown');
4748
}
4849

Tests/EventListener/ErrorListenerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public function testCommandNameIsDisplayedForNonStringableInput()
117117
;
118118

119119
$listener = new ErrorListener($logger);
120-
$listener->onConsoleTerminate($this->getConsoleTerminateEvent($this->getMockBuilder(InputInterface::class)->getMock(), 255));
120+
$listener->onConsoleTerminate($this->getConsoleTerminateEvent($this->createMock(InputInterface::class), 255));
121121
}
122122

123123
private function getLogger()
@@ -132,7 +132,7 @@ private function getConsoleTerminateEvent(InputInterface $input, $exitCode)
132132

133133
private function getOutput()
134134
{
135-
return $this->getMockBuilder(OutputInterface::class)->getMock();
135+
return $this->createMock(OutputInterface::class);
136136
}
137137
}
138138

Tests/Helper/AbstractQuestionHelperTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ abstract class AbstractQuestionHelperTest extends TestCase
1818
{
1919
protected function createStreamableInputInterfaceMock($stream = null, $interactive = true)
2020
{
21-
$mock = $this->getMockBuilder(StreamableInputInterface::class)->getMock();
21+
$mock = $this->createMock(StreamableInputInterface::class);
2222
$mock->expects($this->any())
2323
->method('isInteractive')
2424
->willReturn($interactive);

Tests/Helper/DumperNativeFallbackTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static function tearDownAfterClass(): void
3737
*/
3838
public function testInvoke($variable, $primitiveString)
3939
{
40-
$dumper = new Dumper($this->getMockBuilder(OutputInterface::class)->getMock());
40+
$dumper = new Dumper($this->createMock(OutputInterface::class));
4141

4242
$this->assertSame($primitiveString, $dumper($variable));
4343
}

Tests/Helper/DumperTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static function tearDownAfterClass(): void
3737
*/
3838
public function testInvoke($variable)
3939
{
40-
$output = $this->getMockBuilder(OutputInterface::class)->getMock();
40+
$output = $this->createMock(OutputInterface::class);
4141
$output->method('isDecorated')->willReturn(false);
4242

4343
$dumper = new Dumper($output);

Tests/Helper/HelperSetTest.php

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

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Console\Command\Command;
16+
use Symfony\Component\Console\Exception\ExceptionInterface;
1617
use Symfony\Component\Console\Helper\HelperInterface;
1718
use Symfony\Component\Console\Helper\HelperSet;
1819

@@ -68,7 +69,7 @@ public function testGet()
6869
$this->fail('->get() throws InvalidArgumentException when helper not found');
6970
} catch (\Exception $e) {
7071
$this->assertInstanceOf(\InvalidArgumentException::class, $e, '->get() throws InvalidArgumentException when helper not found');
71-
$this->assertInstanceOf(\Symfony\Component\Console\Exception\ExceptionInterface::class, $e, '->get() throws domain specific exception when helper not found');
72+
$this->assertInstanceOf(ExceptionInterface::class, $e, '->get() throws domain specific exception when helper not found');
7273
$this->assertStringContainsString('The helper "foo" is not defined.', $e->getMessage(), '->get() throws InvalidArgumentException when helper not found');
7374
}
7475
}
@@ -112,7 +113,7 @@ public function testIteration()
112113

113114
private function getGenericMockHelper($name, HelperSet $helperset = null)
114115
{
115-
$mock_helper = $this->getMockBuilder(HelperInterface::class)->getMock();
116+
$mock_helper = $this->createMock(HelperInterface::class);
116117
$mock_helper->expects($this->any())
117118
->method('getName')
118119
->willReturn($name);

Tests/Helper/QuestionHelperTest.php

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

1414
use Symfony\Component\Console\Application;
1515
use Symfony\Component\Console\Exception\InvalidArgumentException;
16+
use Symfony\Component\Console\Exception\MissingInputException;
1617
use Symfony\Component\Console\Formatter\OutputFormatter;
1718
use Symfony\Component\Console\Helper\FormatterHelper;
1819
use Symfony\Component\Console\Helper\HelperSet;
1920
use Symfony\Component\Console\Helper\QuestionHelper;
21+
use Symfony\Component\Console\Input\InputInterface;
2022
use Symfony\Component\Console\Output\OutputInterface;
2123
use Symfony\Component\Console\Output\StreamOutput;
2224
use Symfony\Component\Console\Question\ChoiceQuestion;
@@ -685,7 +687,7 @@ public function testChoiceOutputFormattingQuestionForUtf8Keys()
685687
' [<info>żółw </info>] bar',
686688
' [<info>łabądź</info>] baz',
687689
];
688-
$output = $this->getMockBuilder(OutputInterface::class)->getMock();
690+
$output = $this->createMock(OutputInterface::class);
689691
$output->method('getFormatter')->willReturn(new OutputFormatter());
690692

691693
$dialog = new QuestionHelper();
@@ -700,23 +702,23 @@ public function testChoiceOutputFormattingQuestionForUtf8Keys()
700702

701703
public function testAskThrowsExceptionOnMissingInput()
702704
{
703-
$this->expectException(\Symfony\Component\Console\Exception\MissingInputException::class);
705+
$this->expectException(MissingInputException::class);
704706
$this->expectExceptionMessage('Aborted.');
705707
$dialog = new QuestionHelper();
706708
$dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream('')), $this->createOutputInterface(), new Question('What\'s your name?'));
707709
}
708710

709711
public function testAskThrowsExceptionOnMissingInputForChoiceQuestion()
710712
{
711-
$this->expectException(\Symfony\Component\Console\Exception\MissingInputException::class);
713+
$this->expectException(MissingInputException::class);
712714
$this->expectExceptionMessage('Aborted.');
713715
$dialog = new QuestionHelper();
714716
$dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream('')), $this->createOutputInterface(), new ChoiceQuestion('Choice', ['a', 'b']));
715717
}
716718

717719
public function testAskThrowsExceptionOnMissingInputWithValidator()
718720
{
719-
$this->expectException(\Symfony\Component\Console\Exception\MissingInputException::class);
721+
$this->expectException(MissingInputException::class);
720722
$this->expectExceptionMessage('Aborted.');
721723
$dialog = new QuestionHelper();
722724

@@ -878,7 +880,7 @@ protected function createOutputInterface()
878880

879881
protected function createInputInterfaceMock($interactive = true)
880882
{
881-
$mock = $this->getMockBuilder(\Symfony\Component\Console\Input\InputInterface::class)->getMock();
883+
$mock = $this->createMock(InputInterface::class);
882884
$mock->expects($this->any())
883885
->method('isInteractive')
884886
->willReturn($interactive);

0 commit comments

Comments
 (0)