-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set backend results and add tests for annotated commands.
- Loading branch information
1 parent
5ca898f
commit 4d110d2
Showing
12 changed files
with
363 additions
and
6 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,14 @@ | ||
<?php | ||
namespace Drush\Backend; | ||
|
||
use Consolidation\AnnotatedCommand\Hooks\ExtractOutputInterface; | ||
|
||
class BackendResultSetter implements ExtractOutputInterface | ||
{ | ||
public function extractOutput($structured_data) { | ||
$return = drush_backend_get_result(); | ||
if (empty($return)) { | ||
drush_backend_set_result($structured_data); | ||
} | ||
} | ||
} |
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,148 @@ | ||
<?php | ||
|
||
namespace Unish; | ||
|
||
/** | ||
* @group base | ||
*/ | ||
class annotatedCommandCase extends CommandUnishTestCase { | ||
public function testExecute() { | ||
$sites = $this->setUpDrupal(1, TRUE); | ||
$uri = key($sites); | ||
$root = $this->webroot(); | ||
$options = array( | ||
'root' => $root, | ||
'uri' => $uri, | ||
'yes' => NULL, | ||
); | ||
|
||
// Copy the 'woot' module over to the Drupal site we just set up. | ||
$this->setupModulesForTests($root); | ||
|
||
// Enable out module. This will also clear the commandfile cache. | ||
$this->drush('pm-enable', array('woot'), $options, NULL, NULL, self::EXIT_SUCCESS); | ||
|
||
// drush woot --help | ||
$this->drush('woot', array(), $options + ['help' => NULL], NULL, NULL, self::EXIT_SUCCESS); | ||
$output = $this->getOutput(); | ||
$this->assertContains('Woot mightily.', $output); | ||
// TODO: Symfony Console does not print alias info like this | ||
// $this->assertContains('Aliases: wt', $output); | ||
|
||
// drush help woot. TODO: drush help does not find annotated commands yet | ||
// $this->drush('help', array('woot'), $options, NULL, NULL, self::EXIT_SUCCESS); | ||
// $output = $this->getOutput(); | ||
// $this->assertContains('Woot mightily.', $output); | ||
|
||
// drush woot | ||
$this->drush('woot', array(), $options, NULL, NULL, self::EXIT_SUCCESS); | ||
$output = $this->getOutput(); | ||
$this->assertEquals('Woot!', $output); | ||
|
||
// drush my-cat --help | ||
$this->drush('my-cat', array(), $options + ['help' => NULL], NULL, NULL, self::EXIT_SUCCESS); | ||
$output = $this->getOutput(); | ||
// TODO: the command description does not appear in the help text yet | ||
//$this->assertContains('This is the my-cat command', $output); | ||
$this->assertContains('bet alpha --flip', $output); | ||
$this->assertContains('The first parameter', $output); | ||
$this->assertContains('The other parameter', $output); | ||
$this->assertContains('Whether or not the second parameter', $output); | ||
// TODO: Symfony Console does not print alias info like this | ||
// $this->assertContains('Aliases: c', $output); | ||
|
||
// drush help my-cat | ||
// TODO: help cannot find annotated commands yet | ||
//$this->drush('help', array('my-cat'), $options, NULL, NULL, self::EXIT_SUCCESS); | ||
//$output = $this->getOutput(); | ||
//$this->assertContains('This is the my-cat command', $output); | ||
|
||
// drush my-cat bet alpha --flip | ||
$this->drush('my-cat', array('bet', 'alpha'), $options + ['flip' => NULL], NULL, NULL, self::EXIT_SUCCESS); | ||
$output = $this->getOutput(); | ||
$this->assertEquals('alphabet', $output); | ||
|
||
// drush woot --help with the 'woot' module ignored | ||
$this->drush('woot', array(), $options + ['help' => NULL, 'ignored-modules' => 'woot'], NULL, NULL, self::EXIT_ERROR); | ||
|
||
// drush my-cat bet alpha --flip | ||
$this->drush('my-cat', array('bet', 'alpha'), $options + ['flip' => NULL, 'ignored-modules' => 'woot'], NULL, NULL, self::EXIT_ERROR); | ||
|
||
$this->drush('try-formatters', array(), $options, NULL, NULL, self::EXIT_SUCCESS); | ||
$output = $this->getOutput(); | ||
$expected = <<<EOT | ||
+------+------+-------+ | ||
| I | II | III | | ||
+------+------+-------+ | ||
| One | Two | Three | | ||
| Eins | Zwei | Drei | | ||
| Ichi | Ni | San | | ||
| Uno | Dos | Tres | | ||
+------+------+-------+ | ||
EOT; | ||
$this->assertEquals($expected, $output); | ||
|
||
$this->drush('try-formatters --format=yaml --fields=III,II', array(), $options, NULL, NULL, self::EXIT_SUCCESS); | ||
$output = $this->getOutput(); | ||
$expected = <<<EOT | ||
en: | ||
third: Three | ||
second: Two | ||
de: | ||
third: Drei | ||
second: Zwei | ||
jp: | ||
third: San | ||
second: Ni | ||
es: | ||
third: Tres | ||
second: Dos | ||
EOT; | ||
$this->assertEquals($expected, $output); | ||
|
||
$this->drush('try-formatters', array(), $options + ['backend' => NULL]); | ||
$parsed = $this->parse_backend_output($this->getOutput()); | ||
$data = $parsed['object']; | ||
$expected = <<<EOT | ||
{"en":{"first":"One","second":"Two","third":"Three"},"de":{"first":"Eins","second":"Zwei","third":"Drei"},"jp":{"first":"Ichi","second":"Ni","third":"San"},"es":{"first":"Uno","second":"Dos","third":"Tres"}} | ||
EOT; | ||
$this->assertEquals($expected, json_encode($data)); | ||
|
||
// drush try-formatters --help | ||
$this->drush('try-formatters', array(), $options + ['help' => NULL], NULL, NULL, self::EXIT_SUCCESS); | ||
$output = $this->getOutput(); | ||
// TODO: Command description does not appear in help text yet | ||
//$this->assertContains('Demonstrate formatters', $output); | ||
$this->assertContains('try:formatters --fields=first,third', $output); | ||
$this->assertContains('try:formatters --fields=III,II', $output); | ||
// TODO: Help for formats and fields not available yet | ||
//$this->assertContains('--fields=<first, second, third>', $output); | ||
//$this->assertContains('Fields to output. All available', $output); | ||
//$this->assertContains('--format=<table>', $output); | ||
//$this->assertContains('Select output format. Available:', $output); | ||
// TODO: Symfony Console does not print alias info like this | ||
// $this->assertContains('Aliases: try-formatters', $output); | ||
|
||
// Disable the woot module to avoid cross-contamination of the Drupal | ||
// test site's database. (not necessary?) | ||
if (UNISH_DRUPAL_MAJOR_VERSION == 8) { | ||
$this->drush('pm-uninstall', array('woot'), $options, NULL, NULL, self::EXIT_SUCCESS); | ||
} | ||
else { | ||
$this->drush('pm-disable', array('woot'), $options, NULL, NULL, self::EXIT_SUCCESS); | ||
} | ||
// Also kill the Drush cache so that our 'woot' command is not cached. | ||
$this->drush('cache-clear', array('drush'), $options, NULL, NULL, self::EXIT_SUCCESS); | ||
} | ||
|
||
public function setupModulesForTests($root) { | ||
$wootModule = __DIR__ . '/resources/modules/d' . UNISH_DRUPAL_MAJOR_VERSION . '/woot'; | ||
$modulesDir = "$root/sites/all/modules"; | ||
$this->mkdir($modulesDir); | ||
\symlink($wootModule, "$modulesDir/woot"); | ||
if ((UNISH_DRUPAL_MAJOR_VERSION < 8) && !file_exists("$wootModule/Command/WootCommands.php")) { | ||
$woot8Module = __DIR__ . '/resources/modules/d8/woot'; | ||
\symlink("$woot8Module/src/Command/WootCommands.php", "$wootModule/Command/WootCommands.php"); | ||
} | ||
} | ||
} |
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 @@ | ||
WootCommands.php |
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,4 @@ | ||
name = woot | ||
description = Woot Mightily | ||
core = 7.x | ||
files[] = woot.module |
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,22 @@ | ||
<?php | ||
|
||
/** | ||
* Implements hook_menu | ||
*/ | ||
function woot_menu() { | ||
$items = array(); | ||
|
||
$items['woot'] = array( | ||
'title' => 'Woot', | ||
'description' => 'Woot mightily.', | ||
'page callback' => 'woot_page', | ||
'access callback' => TRUE, | ||
'type' => MENU_CALLBACK, | ||
); | ||
|
||
return $items; | ||
} | ||
|
||
function woot_page() { | ||
return array('#markup' => 'Woot!'); | ||
} |
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,14 @@ | ||
{ | ||
"name": "drupal/woot", | ||
"type": "drupal-module", | ||
"description": "Woot Mightily.", | ||
"keywords": ["Drupal"], | ||
"license": "GPL-2.0+", | ||
"homepage": "http://drupal.org/project/woot", | ||
"minimum-stability": "dev", | ||
"support": { | ||
"issues": "http://drupal.org/project/issues/woot", | ||
"source": "http://cgit.drupalcode.org/woot" | ||
}, | ||
"require": { } | ||
} |
66 changes: 66 additions & 0 deletions
66
tests/resources/modules/d8/woot/src/Command/WootCommands.php
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,66 @@ | ||
<?php | ||
namespace Drupal\woot\Command; | ||
|
||
use Consolidation\OutputFormatters\StructuredData\RowsOfFields; | ||
|
||
/** | ||
* For commands that are parts of modules, Drush expects to find commandfiles in | ||
* __MODULE__/src/Command, and the namespace is Drupal/__MODULE__/Command. | ||
*/ | ||
class WootCommands | ||
{ | ||
/** | ||
* Woot mightily. | ||
* | ||
* @aliases wt | ||
*/ | ||
public function woot() | ||
{ | ||
return 'Woot!'; | ||
} | ||
|
||
/** | ||
* This is the my-cat command | ||
* | ||
* This command will concatinate two parameters. If the --flip flag | ||
* is provided, then the result is the concatination of two and one. | ||
* | ||
* @param string $one The first parameter. | ||
* @param string $two The other parameter. | ||
* @option boolean $flip Whether or not the second parameter should come first in the result. | ||
* @aliases c | ||
* @usage bet alpha --flip | ||
* Concatinate "alpha" and "bet". | ||
*/ | ||
public function myCat($one, $two = '', $options = ['flip' => false]) | ||
{ | ||
if ($options['flip']) { | ||
return "{$two}{$one}"; | ||
} | ||
return "{$one}{$two}"; | ||
} | ||
|
||
/** | ||
* Demonstrate formatters. Default format is 'table'. | ||
* | ||
* @field-labels | ||
* first: I | ||
* second: II | ||
* third: III | ||
* @usage try:formatters --format=yaml | ||
* @usage try:formatters --format=csv | ||
* @usage try:formatters --fields=first,third | ||
* @usage try:formatters --fields=III,II | ||
* @return Consolidation\OutputFormatters\StructuredData\RowsOfFields | ||
*/ | ||
public function tryFormatters($options = ['format' => 'table', 'fields' => '']) | ||
{ | ||
$outputData = [ | ||
'en' => [ 'first' => 'One', 'second' => 'Two', 'third' => 'Three' ], | ||
'de' => [ 'first' => 'Eins', 'second' => 'Zwei', 'third' => 'Drei' ], | ||
'jp' => [ 'first' => 'Ichi', 'second' => 'Ni', 'third' => 'San' ], | ||
'es' => [ 'first' => 'Uno', 'second' => 'Dos', 'third' => 'Tres' ], | ||
]; | ||
return new RowsOfFields($outputData); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
tests/resources/modules/d8/woot/src/Controller/WootController.php
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,31 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Contains Drupal\woot\Controller\WootController. | ||
*/ | ||
|
||
namespace Drupal\woot\Controller; | ||
|
||
use Drupal\Core\Controller\ControllerBase; | ||
|
||
/** | ||
* Class WootController. | ||
* | ||
* @package Drupal\woot\Controller | ||
*/ | ||
class WootController extends ControllerBase { | ||
/** | ||
* Woot. | ||
* | ||
* @return string | ||
* Return Hello string. | ||
*/ | ||
public function woot() { | ||
return [ | ||
'#type' => 'markup', | ||
'#markup' => $this->t('Woot!') | ||
]; | ||
} | ||
|
||
} |
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,5 @@ | ||
name: Woot | ||
type: module | ||
description: Woot Mightily. | ||
core: 8.x | ||
package: Other |
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,33 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Contains woot.module. | ||
*/ | ||
|
||
use Drupal\Core\Routing\RouteMatchInterface; | ||
|
||
/** | ||
* Implements hook_help(). | ||
*/ | ||
function woot_help($route_name, RouteMatchInterface $route_match) { | ||
switch ($route_name) { | ||
// Main module help for the woot module. | ||
case 'help.page.woot': | ||
$output = ''; | ||
$output .= '<h3>' . t('About') . '</h3>'; | ||
$output .= '<p>' . t('Woot Mightily.') . '</p>'; | ||
return $output; | ||
|
||
default: | ||
} | ||
} | ||
|
||
/** | ||
* Implements hook_theme(). | ||
*/ | ||
function woot_theme() { | ||
$theme = []; | ||
|
||
return $theme; | ||
} |
Oops, something went wrong.