Skip to content

Commit

Permalink
Merge branch '4.4' into 5.1
Browse files Browse the repository at this point in the history
* 4.4:
  Use createMock() and use import instead of FQCN
  • Loading branch information
nicolas-grekas committed Jan 27, 2021
2 parents d6ecfe3 + 24026c4 commit d9a267b
Show file tree
Hide file tree
Showing 16 changed files with 51 additions and 37 deletions.
14 changes: 8 additions & 6 deletions Tests/ApplicationTest.php
Expand Up @@ -14,6 +14,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Command\HelpCommand;
use Symfony\Component\Console\CommandLoader\FactoryCommandLoader;
use Symfony\Component\Console\DependencyInjection\AddConsoleCommandPass;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
Expand All @@ -29,6 +30,7 @@
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\Output;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -134,7 +136,7 @@ public function testAll()
{
$application = new Application();
$commands = $application->all();
$this->assertInstanceOf(\Symfony\Component\Console\Command\HelpCommand::class, $commands['help'], '->all() returns the registered commands');
$this->assertInstanceOf(HelpCommand::class, $commands['help'], '->all() returns the registered commands');

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

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

public function testHasGetWithCommandLoader()
Expand Down Expand Up @@ -351,7 +353,7 @@ public function testFind()
$application->add(new \FooCommand());

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

$this->assertInstanceOf(\FooCommand::class, $application->find('foo:bar'), '->find() returns a command if its name exists');
$this->assertInstanceOf(\Symfony\Component\Console\Command\HelpCommand::class, $application->find('h'), '->find() returns a command if its name exists');
$this->assertInstanceOf(HelpCommand::class, $application->find('h'), '->find() returns a command if its name exists');
$this->assertInstanceOf(\FooCommand::class, $application->find('f:bar'), '->find() returns a command if the abbreviation for the namespace exists');
$this->assertInstanceOf(\FooCommand::class, $application->find('f:b'), '->find() returns a command if the abbreviation for the namespace and the command name exist');
$this->assertInstanceOf(\FooCommand::class, $application->find('a'), '->find() returns a command if the abbreviation exists for an alias');
Expand Down Expand Up @@ -937,7 +939,7 @@ public function testRun()
ob_end_clean();

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

$application = new Application();
$application->setAutoExit(false);
Expand Down
3 changes: 2 additions & 1 deletion Tests/Command/CommandTest.php
Expand Up @@ -14,6 +14,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\InvalidOptionException;
use Symfony\Component\Console\Helper\FormatterHelper;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputDefinition;
Expand Down Expand Up @@ -286,7 +287,7 @@ public function testExecuteMethodNeedsToBeOverridden()

public function testRunWithInvalidOption()
{
$this->expectException(\Symfony\Component\Console\Exception\InvalidOptionException::class);
$this->expectException(InvalidOptionException::class);
$this->expectExceptionMessage('The "--bar" option does not exist.');
$command = new \TestCommand();
$tester = new CommandTester($command);
Expand Down
3 changes: 2 additions & 1 deletion Tests/CommandLoader/ContainerCommandLoaderTest.php
Expand Up @@ -14,6 +14,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\CommandLoader\ContainerCommandLoader;
use Symfony\Component\Console\Exception\CommandNotFoundException;
use Symfony\Component\DependencyInjection\ServiceLocator;

class ContainerCommandLoaderTest extends TestCase
Expand Down Expand Up @@ -43,7 +44,7 @@ public function testGet()

public function testGetUnknownCommandThrows()
{
$this->expectException(\Symfony\Component\Console\Exception\CommandNotFoundException::class);
$this->expectException(CommandNotFoundException::class);
(new ContainerCommandLoader(new ServiceLocator([]), []))->get('unknown');
}

Expand Down
3 changes: 2 additions & 1 deletion Tests/CommandLoader/FactoryCommandLoaderTest.php
Expand Up @@ -14,6 +14,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\CommandLoader\FactoryCommandLoader;
use Symfony\Component\Console\Exception\CommandNotFoundException;

class FactoryCommandLoaderTest extends TestCase
{
Expand Down Expand Up @@ -42,7 +43,7 @@ public function testGet()

public function testGetUnknownCommandThrows()
{
$this->expectException(\Symfony\Component\Console\Exception\CommandNotFoundException::class);
$this->expectException(CommandNotFoundException::class);
(new FactoryCommandLoader([]))->get('unknown');
}

Expand Down
4 changes: 2 additions & 2 deletions Tests/EventListener/ErrorListenerTest.php
Expand Up @@ -117,7 +117,7 @@ public function testCommandNameIsDisplayedForNonStringableInput()
;

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

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

private function getOutput()
{
return $this->getMockBuilder(OutputInterface::class)->getMock();
return $this->createMock(OutputInterface::class);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/Helper/AbstractQuestionHelperTest.php
Expand Up @@ -18,7 +18,7 @@ abstract class AbstractQuestionHelperTest extends TestCase
{
protected function createStreamableInputInterfaceMock($stream = null, $interactive = true)
{
$mock = $this->getMockBuilder(StreamableInputInterface::class)->getMock();
$mock = $this->createMock(StreamableInputInterface::class);
$mock->expects($this->any())
->method('isInteractive')
->willReturn($interactive);
Expand Down
2 changes: 1 addition & 1 deletion Tests/Helper/DumperNativeFallbackTest.php
Expand Up @@ -37,7 +37,7 @@ public static function tearDownAfterClass(): void
*/
public function testInvoke($variable, $primitiveString)
{
$dumper = new Dumper($this->getMockBuilder(OutputInterface::class)->getMock());
$dumper = new Dumper($this->createMock(OutputInterface::class));

$this->assertSame($primitiveString, $dumper($variable));
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/Helper/DumperTest.php
Expand Up @@ -37,7 +37,7 @@ public static function tearDownAfterClass(): void
*/
public function testInvoke($variable)
{
$output = $this->getMockBuilder(OutputInterface::class)->getMock();
$output = $this->createMock(OutputInterface::class);
$output->method('isDecorated')->willReturn(false);

$dumper = new Dumper($output);
Expand Down
5 changes: 3 additions & 2 deletions Tests/Helper/HelperSetTest.php
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\ExceptionInterface;
use Symfony\Component\Console\Helper\HelperInterface;
use Symfony\Component\Console\Helper\HelperSet;

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

private function getGenericMockHelper($name, HelperSet $helperset = null)
{
$mock_helper = $this->getMockBuilder(HelperInterface::class)->getMock();
$mock_helper = $this->createMock(HelperInterface::class);
$mock_helper->expects($this->any())
->method('getName')
->willReturn($name);
Expand Down
12 changes: 7 additions & 5 deletions Tests/Helper/QuestionHelperTest.php
Expand Up @@ -13,10 +13,12 @@

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Exception\MissingInputException;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Helper\FormatterHelper;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\StreamOutput;
use Symfony\Component\Console\Question\ChoiceQuestion;
Expand Down Expand Up @@ -685,7 +687,7 @@ public function testChoiceOutputFormattingQuestionForUtf8Keys()
' [<info>żółw </info>] bar',
' [<info>łabądź</info>] baz',
];
$output = $this->getMockBuilder(OutputInterface::class)->getMock();
$output = $this->createMock(OutputInterface::class);
$output->method('getFormatter')->willReturn(new OutputFormatter());

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

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

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

public function testAskThrowsExceptionOnMissingInputWithValidator()
{
$this->expectException(\Symfony\Component\Console\Exception\MissingInputException::class);
$this->expectException(MissingInputException::class);
$this->expectExceptionMessage('Aborted.');
$dialog = new QuestionHelper();

Expand Down Expand Up @@ -897,7 +899,7 @@ protected function createOutputInterface()

protected function createInputInterfaceMock($interactive = true)
{
$mock = $this->getMockBuilder(\Symfony\Component\Console\Input\InputInterface::class)->getMock();
$mock = $this->createMock(InputInterface::class);
$mock->expects($this->any())
->method('isInteractive')
->willReturn($interactive);
Expand Down
10 changes: 6 additions & 4 deletions Tests/Helper/SymfonyQuestionHelperTest.php
Expand Up @@ -2,9 +2,11 @@

namespace Symfony\Component\Console\Tests\Helper;

use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Helper\FormatterHelper;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Helper\SymfonyQuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\StreamOutput;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Question\Question;
Expand Down Expand Up @@ -124,7 +126,7 @@ public function testLabelTrailingBackslash()

public function testAskThrowsExceptionOnMissingInput()
{
$this->expectException(\Symfony\Component\Console\Exception\RuntimeException::class);
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Aborted.');
$dialog = new SymfonyQuestionHelper();
$dialog->ask($this->createStreamableInputInterfaceMock($this->getInputStream('')), $this->createOutputInterface(), new Question('What\'s your name?'));
Expand All @@ -149,7 +151,7 @@ public function testChoiceQuestionPadding()
[foo ] foo
[żółw ] bar
[łabądź] baz
>
>
EOT
, $output, true);
}
Expand All @@ -168,7 +170,7 @@ public function testChoiceQuestionCustomPrompt()
$this->assertOutputContains(<<<EOT
qqq:
[0] foo
>ccc>
>ccc>
EOT
, $output, true);
}
Expand All @@ -192,7 +194,7 @@ protected function createOutputInterface()

protected function createInputInterfaceMock($interactive = true)
{
$mock = $this->getMockBuilder(\Symfony\Component\Console\Input\InputInterface::class)->getMock();
$mock = $this->createMock(InputInterface::class);
$mock->expects($this->any())
->method('isInteractive')
->willReturn($interactive);
Expand Down
10 changes: 6 additions & 4 deletions Tests/Helper/TableTest.php
Expand Up @@ -12,6 +12,8 @@
namespace Symfony\Component\Console\Tests\Helper;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Helper\TableCell;
Expand Down Expand Up @@ -808,7 +810,7 @@ public function testColumnStyle()

public function testThrowsWhenTheCellInAnArray()
{
$this->expectException(\Symfony\Component\Console\Exception\InvalidArgumentException::class);
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('A cell must be a TableCell, a scalar or an object implementing "__toString()", "array" given.');
$table = new Table($output = $this->getOutputStream());
$table
Expand Down Expand Up @@ -983,7 +985,7 @@ public function testSectionOutputWithoutDecoration()

public function testAppendRowWithoutSectionOutput()
{
$this->expectException(\Symfony\Component\Console\Exception\RuntimeException::class);
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Output should be an instance of "Symfony\Component\Console\Output\ConsoleSectionOutput" when calling "Symfony\Component\Console\Helper\Table::appendRow".');
$table = new Table($this->getOutputStream());

Expand Down Expand Up @@ -1024,15 +1026,15 @@ public function testSectionOutputHandlesZeroRowsAfterRender()

public function testIsNotDefinedStyleException()
{
$this->expectException(\Symfony\Component\Console\Exception\InvalidArgumentException::class);
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Style "absent" is not defined.');
$table = new Table($this->getOutputStream());
$table->setStyle('absent');
}

public function testGetStyleDefinition()
{
$this->expectException(\Symfony\Component\Console\Exception\InvalidArgumentException::class);
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Style "absent" is not defined.');
Table::getStyleDefinition('absent');
}
Expand Down
3 changes: 2 additions & 1 deletion Tests/Input/StringInputTest.php
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Console\Tests\Input;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\StringInput;
Expand All @@ -24,7 +25,7 @@ class StringInputTest extends TestCase
public function testTokenize($input, $tokens, $message)
{
$input = new StringInput($input);
$r = new \ReflectionClass(\Symfony\Component\Console\Input\ArgvInput::class);
$r = new \ReflectionClass(ArgvInput::class);
$p = $r->getProperty('tokens');
$p->setAccessible(true);
$this->assertEquals($tokens, $p->getValue($input), $message);
Expand Down
3 changes: 2 additions & 1 deletion Tests/Logger/ConsoleLoggerTest.php
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Console\Tests\Logger;

use PHPUnit\Framework\TestCase;
use Psr\Log\InvalidArgumentException;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Symfony\Component\Console\Logger\ConsoleLogger;
Expand Down Expand Up @@ -138,7 +139,7 @@ public function provideLevelsAndMessages()

public function testThrowsOnInvalidLevel()
{
$this->expectException(\Psr\Log\InvalidArgumentException::class);
$this->expectException(InvalidArgumentException::class);
$logger = $this->getLogger();
$logger->log('invalid level', 'Foo');
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/Output/ConsoleSectionOutputTest.php
Expand Up @@ -147,7 +147,7 @@ public function testClearSectionContainingQuestion()
fwrite($inputStream, "Batman & Robin\n");
rewind($inputStream);

$input = $this->getMockBuilder(StreamableInputInterface::class)->getMock();
$input = $this->createMock(StreamableInputInterface::class);
$input->expects($this->once())->method('isInteractive')->willReturn(true);
$input->expects($this->once())->method('getStream')->willReturn($inputStream);

Expand Down

0 comments on commit d9a267b

Please sign in to comment.