Skip to content

Instantly share code, notes, and snippets.

@wouterj
Last active December 20, 2015 12:18
Show Gist options
  • Select an option

  • Save wouterj/6129648 to your computer and use it in GitHub Desktop.

Select an option

Save wouterj/6129648 to your computer and use it in GitHub Desktop.
{
"require": {
"symfony/console": "2.3.*"
}
}
<?php
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class MyCommand extends Command
{
public function configure()
{
$this
->setName('acme:foo')
->setDescription('Foobar')
->addOption('bar', null, InputOption::VALUE_NONE);
}
public function execute(InputInterface $i, OutputInterface $o)
{
if ($i->getOption('bar')) {
$o->writeln('BAR');
} else {
$o->writeln('FOO');
}
}
}
<?php
require_once __DIR__.'/vendor/autoload.php';
require_once __DIR__.'/MyCommand.php';
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
class MyCommandTest extends \PHPUnit_Framework_TestCase
{
public function testExecute()
{
$application = new Application();
$application->add(new MyCommand());
$command = $application->find('acme:foo');
$commandTester = new CommandTester($command);
$commandTester->execute(
array(
'--bar' => true,
// 'command' => 'acme:foo',
)
);
$this->assertRegExp('{BAR}', $commandTester->getDisplay());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment