forked from consolidation/robo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Runner function for retrieving an application to be used with tes…
…ts (consolidation#968) * Robo-947: Add function for retrieving an application to be used with tests. * Fix nits. Drupal does not dictate all code styles. * Fix the nit. * Don't invoke getRoboFileCommands unnecessarily. * Nitpicks. * Test try:exec to validate status codes. * Nitpicks. * Add references to usage of test app getter. * Don't use a trait in Runner from the tests dir. * Try disabling interactivity. * Revert "Try disabling interactivity." This reverts commit 46a727b. * Use null coalescence. * Remove incorrect commentary. * Mark test as skipped for lowest dependency check for symfony4.
- Loading branch information
Showing
3 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
namespace Robo; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Robo\Traits\CommandTesterTrait; | ||
use RoboExample\Robo\Plugin\Commands\ExampleCommands; | ||
|
||
class CommandTestertTest extends TestCase | ||
{ | ||
use CommandTesterTrait; | ||
|
||
public function setUp() | ||
{ | ||
$this->setupCommandTester(ExampleCommands::class); | ||
} | ||
|
||
public function testInputApis() | ||
{ | ||
if (getenv('SCENARIO') == 'symfony4' && getenv('DEPENDENCIES') == 'lowest') { | ||
$this->markTestSkipped('There is a bug with a lower dependency of symfony4 in how it handles tty.'); | ||
} | ||
list($tryInputOutput, $statusCode) = $this->executeCommand('try:input', ["I'm great!", "yes", "PHP", "1234"]); | ||
$this->assertEquals(0, $statusCode); | ||
$this->assertContains("I'm great!", $tryInputOutput); | ||
$this->assertContains("PHP", $tryInputOutput); | ||
$this->assertContains("1234", $tryInputOutput); | ||
} | ||
|
||
public function testTesterWithOptions() | ||
{ | ||
list($execOutput, $statusCode) = $this->executeCommand('try:exec', []); | ||
$this->assertEquals(0, $statusCode); | ||
list($execOutput, $statusCode) = $this->executeCommand('try:exec', [], ['--error']); | ||
$this->assertNotEquals(0, $statusCode); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace Robo\Traits; | ||
|
||
use Consolidation\AnnotatedCommand\CommandFileDiscovery; | ||
use Robo\Robo; | ||
use Robo\Runner; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
|
||
trait CommandTesterTrait | ||
{ | ||
/** @var string */ | ||
protected $appName; | ||
|
||
/** @var string */ | ||
protected $appVersion; | ||
|
||
/** @var string|array|null */ | ||
protected $commandClasses = null; | ||
|
||
/** @var Runner */ | ||
protected $runner; | ||
|
||
/** | ||
* Setup the tester. | ||
* | ||
* @param string|array|null $commandClasses | ||
*/ | ||
public function setupCommandTester($commandClasses = null) | ||
{ | ||
// Define our invariants for our test. | ||
$this->runner = new Runner(); | ||
if (!is_null($commandClasses)) { | ||
$this->commandClasses = $commandClasses; | ||
} | ||
} | ||
|
||
/** | ||
* @param $command_string | ||
* @param array $inputs | ||
* @param array $command_extra | ||
* @param string|array|null $commandClasses | ||
* @return array | ||
*/ | ||
protected function executeCommand($command_string, $inputs = [], $command_extra = [], $commandClasses = null) | ||
{ | ||
$commandClasses = $commandClasses ?? $this->commandClasses; | ||
$app = $this->runner->getAppForTesting($this->appName, $this->appVersion, $commandClasses); | ||
$command = $app->get($command_string); | ||
$tester = new CommandTester($command); | ||
$tester->setInputs($inputs); | ||
$status_code = $tester->execute(array_merge(['command' => $command_string], $command_extra)); | ||
Robo::unsetContainer(); | ||
return [trim($tester->getDisplay()), $status_code]; | ||
} | ||
} |