1<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Component\Console\Tests\Command;
13
14use PHPUnit\Framework\TestCase;
15use Symfony\Component\Console\Application;
16use Symfony\Component\Console\Tester\CommandTester;
17
18class ListCommandTest extends TestCase
19{
20    public function testExecuteListsCommands()
21    {
22        $application = new Application();
23        $commandTester = new CommandTester($command = $application->get('list'));
24        $commandTester->execute(['command' => $command->getName()], ['decorated' => false]);
25
26        $this->assertRegExp('/help\s{2,}Displays help for a command/', $commandTester->getDisplay(), '->execute() returns a list of available commands');
27    }
28
29    public function testExecuteListsCommandsWithXmlOption()
30    {
31        $application = new Application();
32        $commandTester = new CommandTester($command = $application->get('list'));
33        $commandTester->execute(['command' => $command->getName(), '--format' => 'xml']);
34        $this->assertRegExp('/<command id="list" name="list" hidden="0">/', $commandTester->getDisplay(), '->execute() returns a list of available commands in XML if --xml is passed');
35    }
36
37    public function testExecuteListsCommandsWithRawOption()
38    {
39        $application = new Application();
40        $commandTester = new CommandTester($command = $application->get('list'));
41        $commandTester->execute(['command' => $command->getName(), '--raw' => true]);
42        $output = <<<'EOF'
43help   Displays help for a command
44list   Lists commands
45
46EOF;
47
48        $this->assertEquals($output, $commandTester->getDisplay(true));
49    }
50
51    public function testExecuteListsCommandsWithNamespaceArgument()
52    {
53        require_once realpath(__DIR__.'/../Fixtures/FooCommand.php');
54        $application = new Application();
55        $application->add(new \FooCommand());
56        $commandTester = new CommandTester($command = $application->get('list'));
57        $commandTester->execute(['command' => $command->getName(), 'namespace' => 'foo', '--raw' => true]);
58        $output = <<<'EOF'
59foo:bar   The foo:bar command
60
61EOF;
62
63        $this->assertEquals($output, $commandTester->getDisplay(true));
64    }
65
66    public function testExecuteListsCommandsOrder()
67    {
68        require_once realpath(__DIR__.'/../Fixtures/Foo6Command.php');
69        $application = new Application();
70        $application->add(new \Foo6Command());
71        $commandTester = new CommandTester($command = $application->get('list'));
72        $commandTester->execute(['command' => $command->getName()], ['decorated' => false]);
73        $output = <<<'EOF'
74Console Tool
75
76Usage:
77  command [options] [arguments]
78
79Options:
80  -h, --help            Display this help message
81  -q, --quiet           Do not output any message
82  -V, --version         Display this application version
83      --ansi            Force ANSI output
84      --no-ansi         Disable ANSI output
85  -n, --no-interaction  Do not ask any interactive question
86  -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
87
88Available commands:
89  help      Displays help for a command
90  list      Lists commands
91 0foo
92  0foo:bar  0foo:bar command
93EOF;
94
95        $this->assertEquals($output, trim($commandTester->getDisplay(true)));
96    }
97
98    public function testExecuteListsCommandsOrderRaw()
99    {
100        require_once realpath(__DIR__.'/../Fixtures/Foo6Command.php');
101        $application = new Application();
102        $application->add(new \Foo6Command());
103        $commandTester = new CommandTester($command = $application->get('list'));
104        $commandTester->execute(['command' => $command->getName(), '--raw' => true]);
105        $output = <<<'EOF'
106help       Displays help for a command
107list       Lists commands
1080foo:bar   0foo:bar command
109EOF;
110
111        $this->assertEquals($output, trim($commandTester->getDisplay(true)));
112    }
113}
114