3 namespace Consolidation\SiteAlias;
5 use PHPUnit\Framework\TestCase;
6 use Symfony\Component\Console\Output\BufferedOutput;
8 class ExampleCommandsTest extends TestCase
11 protected $commandClasses;
17 protected $appVersion;
20 const STATUS_ERROR = 1;
23 * Instantiate a new runner
25 public function setUp()
27 // Store the command classes we are going to test
28 $this->commandClasses = [ \Consolidation\SiteAlias\Cli\SiteAliasCommands::class ];
30 // Define our invariants for our test
31 $this->appName = 'TestFixtureApp';
32 $this->appVersion = '1.0.1';
36 * Data provider for testExample.
38 * Return an array of arrays, each of which contains the parameter
39 * values to be used in one invocation of the testExample test function.
41 public function exampleTestCommandParameters()
46 'Add search location: /fixtures/sitealiases/sites', self::STATUS_ERROR,
47 'site:list', '/fixtures/sitealiases/sites',
51 'List available site aliases', self::STATUS_OK,
59 * Test our example class. Each time this function is called, it will
60 * be passed data from the data provider function idendified by the
61 * dataProvider annotation.
63 * @dataProvider exampleTestCommandParameters
65 public function testExampleCommands($expectedOutput, $expectedStatus, $variable_args)
67 // Create our argv array and run the command
68 $argv = $this->argv(func_get_args());
69 list($actualOutput, $statusCode) = $this->execute($argv);
71 // Confirm that our output and status code match expectations
72 $this->assertContains($expectedOutput, $actualOutput);
73 $this->assertEquals($expectedStatus, $statusCode);
77 * Prepare our $argv array; put the app name in $argv[0] followed by
78 * the command name and all command arguments and options.
80 protected function argv($functionParameters)
82 $argv = $functionParameters;
85 array_unshift($argv, $this->appName);
87 // TODO: replace paths beginning with '/fixtures' with actual path to fixture data
93 * Simulated front controller
95 protected function execute($argv)
97 // Define a global output object to capture the test results
98 $output = new BufferedOutput();
100 // We can only call `Runner::execute()` once; then we need to tear down.
101 $runner = new \Robo\Runner($this->commandClasses);
102 $statusCode = $runner->execute($argv, $this->appName, $this->appVersion, $output);
103 \Robo\Robo::unsetContainer();
105 // Return the output and status code.
106 $actualOutput = trim($output->fetch());
107 return [$actualOutput, $statusCode];