Skip to content

Commit

Permalink
Added input mocking to cli integration test case
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyharris committed Jun 23, 2017
1 parent 8998635 commit 8be905e
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/TestSuite/ConsoleIntegrationTestCase.php
Expand Up @@ -62,11 +62,21 @@ class ConsoleIntegrationTestCase extends TestCase
* Runs cli integration test
*
* @param string $command Command to run
* @param array $input Input values to pass to an interactive shell
* @return void
*/
public function cli($command)
public function cli($command, array $input = [])
{
$dispatcher = $this->_makeDispatcher("bin/cake $command");

$i = 0;
foreach ($input as $in) {
$this->_in
->expects($this->at($i++))
->method('read')
->will($this->returnValue($in));
}

$this->_exitCode = $dispatcher->dispatch();
}

Expand Down Expand Up @@ -121,6 +131,18 @@ public function assertOutputContains($expected)
$this->assertContains($expected, $output);
}

/**
* Asserts `stderr` contains expected output
*
* @param string $expected Expected output
* @return void
*/
public function assertErrorContains($expected)
{
$output = implode(PHP_EOL, $this->_err->messages());
$this->assertContains($expected, $output);
}

/**
* Builds the appropriate command dispatcher
*
Expand Down
39 changes: 39 additions & 0 deletions tests/TestCase/TestSuite/ConsoleIntegrationTestCaseTest.php
Expand Up @@ -2,11 +2,24 @@
namespace Cake\Test\TestCase\TestSuite;

use Cake\Console\Shell;
use Cake\Core\Configure;
use Cake\TestSuite\ConsoleIntegrationTestCase;

class ConsoleIntegrationTestCaseTest extends ConsoleIntegrationTestCase
{

/**
* setUp
*
* @return void
*/
public function setUp()
{
parent::setUp();

Configure::write('App.namespace', 'TestApp');
}

/**
* tests cli
*
Expand All @@ -33,6 +46,32 @@ public function testCliCoreCommand()
$this->assertExitCode(Shell::CODE_SUCCESS);
}

/**
* tests cli with input
*
* @return void
*/
public function testCliWithInput()
{
$this->cli('sample bridge', ['javascript']);

$this->assertErrorContains('No!');
$this->assertExitCode(Shell::CODE_ERROR);
}

/**
* tests cli with multiple inputs
*
* @return void
*/
public function testCliWithMultipleInput()
{
$this->cli('sample bridge', ['cake', 'blue']);

$this->assertOutputContains('You may pass');
$this->assertExitCode(Shell::CODE_SUCCESS);
}

/**
* tests _commandStringToArgs
*
Expand Down
24 changes: 24 additions & 0 deletions tests/test_app/TestApp/Shell/SampleShell.php
Expand Up @@ -56,4 +56,28 @@ public function returnValue()
{
return 99;
}

/**
* Bridge of Death question
*
* @return void
*/
public function bridge()
{
$name = $this->in('What is your name');

if ($name !== 'cake') {
$this->err('No!');
$this->_stop(Shell::CODE_ERROR);
}

$color = $this->in('What is your favorite color?');

if ($color !== 'blue') {
$this->err('Wrong! <blink>Aaaahh</blink>');
$this->_stop(Shell::CODE_ERROR);
}

$this->out('You may pass.');
}
}

0 comments on commit 8be905e

Please sign in to comment.