ofbeaton/console-tester

Symfony Console Question Helper Tester

This package's canonical repository appears to be gone and the package has been frozen as a result.

1.1.0 2015-05-22 20:27 UTC

This package is not auto-updated.

Last update: 2019-02-18 17:39:19 UTC


README

Latest Stable Version Build Status Dependency Status Total Downloads License

of Beaton's Console Tester is a PHP 5.4+ trait that provides functions to help you test Symfony Console Command that use the QuestionHelper in a PHPUnit test. You can pass an anonymous function that easily provides the input for various questions a command may ask.

The advantage to doing this over pre-populating the input stream is that your questions can (if you so choose) change in order, and that an unhandled question will produce a failure in phpunit, instead of breaking the run completely with an unhandled input request.

Installing via Composer

The recommended way to install Console Tester is through Composer.

# Install Composer
curl -sS https://getcomposer.org/installer | php

Next, run the Composer command to install the latest stable version:

composer.phar require ofbeaton/console-tester

After installing, you need to require Composer's autoloader:

require 'vendor/autoload.php';

Usage

As it makes use of the PHPUnit getMock function, you will need to use the trait inside a PHPUnit PHPUnit_Framework_TestCase class (or similar).

// MyCommandTest.php

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Console\Question\Question;
use Ofbeaton\Console\Tester\QuestionTester;
use Ofbeaton\Console\Tester\UnhandledQuestionException;

class MyCommandTest extends \PHPUnit_Framework_TestCase
{
  // make sure you include the trait!
  use QuestionTester;
  
  // ...
  public function testExecute()
  {
    $app = new Application();
    
    // replace MyCommand with your command
    $app->add(new MyCommand());
    $cmd = $app->find('mycommand');

    // return the input you want to answer the question with
    $this->mockQuestionHelper($cmd, function($text, $order, Question $question) {
        // you can check against $text to see if this is the question you want to handle
        // and you can check against $order (starts at 0) for the order the questions come in

        // handle a question
        if (strpos($text, 'overwrite') !== false) {
            return true;

        // handle another question
        } elseif (strpos($text, 'api_key') !== false) {
            return 'api-test-key';
        }

        throw new UnhandledQuestionException();
    });

    $cmdTester = new CommandTester($cmd);
    $cmdTester->execute([
       'command' => $cmd->getName(),
       '--no-ansi' => true,
    ]);

    // $cmdTester->getDisplay() can be used to see the output and input of the interaction
    // you could also assert against other side effects like files your application writes
    // $this->assertRegExp('/.../', (string)$cmdTester->getDisplay());
}

Support Me

Hi, I'm Finlay Beaton (@ofbeaton). This software is only made possible by donations of fellow users like you, encouraging me to toil the midnight hours away and sweat into the code and documentation. Everyone's time should be valuable, please seriously consider donating.

Paypal Donate Form