5 use Webmozart\PathUtil\Path;
10 class AnnotatedCommandCase extends CommandUnishTestCase
12 use TestModuleHelperTrait;
14 public function testGlobal()
16 $globalIncludes = $targetDir = Path::join(__DIR__, 'resources/global-includes');
20 // We modified the set of available Drush commands; we need to clear the Drush command cache
21 $this->drush('cc', ['drush'], $options);
24 $options['include'] = "$globalIncludes";
25 $this->drush('foobar', [], $options);
26 $output = $this->getOutput();
27 $this->assertEquals('baz', $output);
30 $this->drush('foobaz', [], $options);
31 $output = $this->getOutput();
32 $this->assertEquals('bar', $output);
36 'include' => $globalIncludes,
37 'directory' => self::getSandbox(),
40 $original = getenv('SHELL_INTERACTIVE');
41 $this->setEnv(['SHELL_INTERACTIVE' => 1]);
42 $this->drush('generate', ['foo-example'], $options);
43 $this->setEnv(['SHELL_INTERACTIVE' => $original]);
45 $target = Path::join($this->getSandbox(), 'foo.php');
46 $actual = trim(file_get_contents($target));
47 $this->assertEquals('Foo.', $actual);
51 public function testExecute()
53 $this->setUpDrupal(1, true);
55 // Copy the 'woot' module over to the Drupal site we just set up.
56 $this->setupModulesForTests(['woot'], Path::join(__DIR__, 'resources/modules/d8'));
58 // Enable our module. This will also clear the commandfile cache.
59 $this->drush('pm-enable', ['woot']);
61 // In theory this is not necessary, but this test keeps failing.
62 // $this->drush('cc', array('drush'), $options);
64 // Make sure that modules can supply DCG Generators and they work.
65 $optionsExample['answers'] = json_encode([
67 'machine_name' => 'bar',
69 $optionsExample['directory'] = self::getSandbox();
70 $optionsExample['yes'] = null;
71 $original = getenv('SHELL_INTERACTIVE');
72 $this->setEnv(['SHELL_INTERACTIVE' => 1]);
73 $this->drush('generate', ['woot-example'], $optionsExample);
74 $this->setEnv(['SHELL_INTERACTIVE' => $original]);
75 $target = Path::join(self::getSandbox(), '/src/Commands/ExampleBarCommands.php');
76 $actual = trim(file_get_contents($target));
77 $this->assertEquals('ExampleBarCommands says Woot mightily.', $actual);
81 $output = $this->getOutput();
82 $this->assertEquals('Woot!', $output);
84 // drush my-cat bet alpha --flip
85 $this->drush('my-cat', ['bet', 'alpha'], ['flip' => null]);
86 $output = $this->getOutput();
87 $this->assertEquals('alphabet', $output);
89 // drush my-cat bet alpha --flip
90 $this->drush('my-cat', ['bet', 'alpha'], ['flip' => null, 'ignored-modules' => 'woot'], null, null, self::EXIT_ERROR);
92 $this->drush('try-formatters');
93 $output = $this->getOutput();
102 ------ ------ -------
104 $this->assertEquals(trim(preg_replace('#[ \n]+#', ' ', $expected)), trim(preg_replace('#[ \n]+#', ' ', $output)));
106 $this->drush('try-formatters --format=yaml --fields=III,II', [], [], null, null, self::EXIT_SUCCESS);
107 $output = $this->getOutput();
108 // TODO: If there are different versions of symfony/yaml in Drush and Drupal,
109 // then we can get indentation errors. Ignore that in these tests; this is not
110 // a problem with site-local Drush.
111 $output = str_replace(' ', ' ', $output);
126 $this->assertEquals($expected, $output);
128 $this->drush('try-formatters', [], ['format' => 'json']);
129 $data = $this->getOutput();
154 $this->assertEquals($expected, $data);
157 $this->drush('help', ['my-cat']);
158 $output = $this->getOutput();
159 $this->assertContains('bet alpha --flip Concatinate "alpha" and "bet".', $output);
160 $this->assertContains('Aliases: c', $output);
163 $this->drush('help', ['woot']);
164 $output = $this->getOutput();
165 $this->assertContains('Woot mightily.', $output);
167 // TODO: support console.command commands
168 $this->drush('annotated:greet symfony');
169 $output = $this->getOutput();
170 $this->assertEquals('Hello symfony', $output);
172 $this->drush('demo:greet symfony');
173 $output = $this->getOutput();
174 $this->assertEquals('Hello symfony', $output);
176 $this->markTestSkipped('--help not working yet.');
178 // drush my-cat --help
179 $this->drush('my-cat', [], ['help' => null]);
180 $output = $this->getOutput();
181 $this->assertContains('my-cat bet alpha --flip', $output);
182 $this->assertContains('The first parameter', $output);
183 $this->assertContains('The other parameter', $output);
184 $this->assertContains('Whether or not the second parameter', $output);
187 $this->drush('woot', [], ['help' => null]);
188 $output = $this->getOutput();
189 $this->assertContains('Usage:', $output);
190 $this->assertContains('woot [options]', $output);
191 $this->assertContains('Woot mightily.', $output);
193 // drush try-formatters --help
194 $this->drush('try-formatters', [], ['help' => null]);
195 $output = $this->getOutput();
196 $this->assertContains('Demonstrate formatters', $output);
197 $this->assertContains('try:formatters --fields=first,third', $output);
198 $this->assertContains('try:formatters --fields=III,II', $output);
199 // $this->assertContains('--fields=<first, second, third>', $output);
200 $this->assertContains('Available fields:', $output);
201 $this->assertContains('[default: "table"]', $output);
203 $this->markTestSkipped('--ignored-modules not supported yet');
205 // TODO: Support --ignored-modules
206 // drush woot --help with the 'woot' module ignored
207 $this->drush('woot', [], ['help' => null, 'ignored-modules' => 'woot'], null, null, self::EXIT_ERROR);
210 public function setupGlobalExtensionsForTests()
212 $globalExtension = __DIR__ . '/resources/global-includes';
213 $targetDir = Path::join(self::getSandbox(), 'global-includes');
214 $this->mkdir($targetDir);
215 $this->recursiveCopy($globalExtension, $targetDir);