Skip to content
Permalink
Browse files

Remove the `$io` parameter from a factory method.

  • Loading branch information...
robertpustulka committed Mar 5, 2018
1 parent ac64ea3 commit 40a4bdcd56932424bb98b7e821b423d4f421814d
@@ -26,15 +26,10 @@ class CommandFactory implements CommandFactoryInterface
/**
* {@inheritDoc}
*/
public function create($className, ConsoleIo $io)
public function create($className)
{
if (is_subclass_of($className, Shell::class)) {
return new $className($io);
}
// Command class
$command = new $className();
if (!$command instanceof Command) {
if (!($command instanceof Command) && !($command instanceof Shell)) {
$valid = implode('` or `', [Shell::class, Command::class]);
$message = sprintf('Class `%s` must be an instance of `%s`.', $className, $valid);
throw new InvalidArgumentException($message);
@@ -21,8 +21,7 @@ interface CommandFactoryInterface
* The factory method for creating Command and Shell instances.
*
* @param string $className Command/Shell class name.
* @param \Cake\Console\ConsoleIo $io The IO wrapper for the created shell class.
* @return \Cake\Console\Shell|\Cake\Console\Command
*/
public function create($className, ConsoleIo $io);
public function create($className);
}
@@ -285,6 +285,11 @@ protected function runShell(Shell $shell, array $argv)
*/
protected function createShell($className, ConsoleIo $io)
{
return $this->factory->create($className, $io);
$shell = $this->factory->create($className);
if ($shell instanceof Shell) {
$shell->setIo($io);
}
return $shell;
}
}
@@ -25,19 +25,16 @@ public function testCreateCommand()
{
$factory = new CommandFactory();
$command = $factory->create(DemoCommand::class, new ConsoleIo());
$command = $factory->create(DemoCommand::class);
$this->assertInstanceOf(DemoCommand::class, $command);
}
public function testCreateShell()
{
$factory = new CommandFactory();
$io = new ConsoleIo();
$shell = $factory->create(SampleShell::class, $io);
$shell = $factory->create(SampleShell::class);
$this->assertInstanceOf(SampleShell::class, $shell);
$this->assertSame($io, $shell->getIo());
}
public function testInvalid()
@@ -47,6 +44,6 @@ public function testInvalid()
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Class `Cake\Test\TestCase\Console\CommandFactoryTest` must be an instance of `Cake\Console\Shell` or `Cake\Console\Command`.');
$factory->create(static::class, new ConsoleIo());
$factory->create(static::class);
}
}
@@ -332,7 +332,7 @@ public function testRunWithCustomFactory()
$factory = $this->createMock(CommandFactoryInterface::class);
$factory->expects($this->once())
->method('create')
->with(DemoCommand::class, $io)
->with(DemoCommand::class)
->willReturn(new DemoCommand());
$app = $this->makeAppWithCommands(['ex' => DemoCommand::class]);

0 comments on commit 40a4bdc

Please sign in to comment.
You can’t perform that action at this time.