Skip to content

Instantly share code, notes, and snippets.

@gnugat
Last active August 29, 2015 14:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gnugat/400729d3f97fd2ed7fc9 to your computer and use it in GitHub Desktop.
Save gnugat/400729d3f97fd2ed7fc9 to your computer and use it in GitHub Desktop.
gnugat/ripozi - CommandTestApplication
<?php
namespace Gnugat\Ripozi\Test;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
/**
* Assumes that the application kernel is named AppKernel and has been loaded
*/
class CommandTestApplication
{
const EXIT_SUCCESS = 0;
/**
* @var Application
*/
private $application;
/**
* @var \Symfony\Component\DependencyInjection\ContainerInterface
*/
private $container;
/**
* @var \Symfony\Component\HttpKernel\KernelInterface
*/
private $kernel;
public function __construct()
{
$this->kernel = new \AppKernel('test', true);
$this->kernel->boot();
$this->container = $this->kernel->getContainer();
$this->application = new Application($this->kernel);
$this->application->setAutoExit(false);
}
/**
* @param array $rawInput
*
* @return int
*/
public function run(array $rawInput)
{
$input = new ArrayInput($rawInput);
$output = new NullOutput();
return $this->application->run($input, $output);
}
}
@gnugat
Copy link
Author

gnugat commented Oct 29, 2014

Usage:

<?php

namespace Acme\DemoBundle\Tests;

use Gnugat\Ripozi\Test\CommandTestApplication;

class HelloWorldCommandTest extends \PHPUnit_Framework_TestCase
{
    protected function setUp()
    {
        $this->app = new CommandTestApplication();
    }

    public function testItRunsSuccessfully()
    {
        $exitCode = $this->app->run(array(
            'command' => 'acme:demo',
        ));

        $this->assertSame(CommandTestApplication::EXIT_SUCCESS, $exitCode);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment