Since: 1.0
Author: Bernhard Schussek (bschussek@gmail.com)
Inheritance: extends Webmozart\Console\Api\IO\IO
Example #1
0
    public function testRenderWithIndentation()
    {
        $layout = new BlockLayout();
        $layout->add(new Paragraph('HEADING 1'))->add(new Paragraph(self::LOREM_IPSUM))->add(new EmptyLine())->add(new LabeledParagraph('Not Aligned', self::LOREM_IPSUM, 1, false))->add(new EmptyLine())->add(new Paragraph('HEADING 2'))->beginBlock()->add(new LabeledParagraph('Label 1', self::LOREM_IPSUM))->add(new LabeledParagraph('Label 2', self::LOREM_IPSUM))->endBlock()->add(new Paragraph('HEADING 3'))->beginBlock()->add(new LabeledParagraph('Longer Label', self::LOREM_IPSUM))->endBlock();
        $layout->render($this->io, 4);
        $expected = <<<'EOF'
    HEADING 1
    Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
    eirmod tempor invidunt

    Not Aligned Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed
                diam nonumy eirmod tempor invidunt

    HEADING 2
      Label 1       Lorem ipsum dolor sit amet, consetetur sadipscing elitr,
                    sed diam nonumy eirmod tempor invidunt
      Label 2       Lorem ipsum dolor sit amet, consetetur sadipscing elitr,
                    sed diam nonumy eirmod tempor invidunt
    HEADING 3
      Longer Label  Lorem ipsum dolor sit amet, consetetur sadipscing elitr,
                    sed diam nonumy eirmod tempor invidunt

EOF;
        $this->assertSame($expected, $this->io->fetchOutput());
    }
 public function testRenderApplicationJson()
 {
     $args = new Args($this->helpCommand->getArgsFormat());
     $status = $this->handler->handle($args, $this->io, $this->command);
     $this->assertStringStartsWith('{"commands":[{"name":"help",', $this->io->fetchOutput());
     $this->assertSame(0, $status);
 }
Example #3
0
 public function testClearErrors()
 {
     $io = new BufferedIO();
     $io->error('Lorem');
     $io->clearErrors();
     $io->error('ipsum');
     $this->assertSame('ipsum', $io->fetchErrors());
 }
Example #4
0
    /**
     * testTableRenderer.
     */
    public function testListRenderer()
    {
        $renderer = RendererFactory::create('list', $this->io);
        $renderer->render($this->createResultCollection());
        $output = <<<LIST
[OK][200] Example - 0.42

LIST;
        $this->assertInstanceOf(RendererInterface::class, $renderer);
        $this->assertSame($output, $this->io->fetchOutput());
    }
Example #5
0
    public function testRenderApplicationXml()
    {
        $args = new Args($this->helpCommand->getArgsFormat());
        $status = $this->handler->handle($args, $this->io, $this->command);
        $expected = <<<'EOF'
<?xml version="1.0" encoding="UTF-8"?>
<symfony name="The Application" version="1.2.3">
EOF;
        $this->assertStringStartsWith($expected, $this->io->fetchOutput());
        $this->assertSame(0, $status);
    }
Example #6
0
    /**
     * testCsvRenderer.
     */
    public function testCsvRenderer()
    {
        $renderer = RendererFactory::create('csv', $this->io);
        $renderer->render($this->createResultCollection());
        $output = <<<CSV
"Global Status","Status Code",Name,"Reponse Time","Http Error Log","Validator Error Log"
OK,200,Example,0.42,,

CSV;
        $this->assertInstanceOf(RendererInterface::class, $renderer);
        $this->assertSame($output, $this->io->fetchOutput());
    }
    public function testRenderApplicationText()
    {
        $args = new Args($this->helpCommand->getArgsFormat());
        $status = $this->handler->handle($args, $this->io, $this->command);
        $expected = <<<'EOF'
The Application version 1.2.3

USAGE
  console
EOF;
        $this->assertStringStartsWith($expected, $this->io->fetchOutput());
        $this->assertSame(0, $status);
    }
Example #8
0
    public function testSkipIndentationForEmptyLines()
    {
        $para = new Paragraph(self::LOREM_IPSUM . "\n\n" . self::LOREM_IPSUM);
        $para->render($this->io, 6);
        $expected = <<<'EOF'
      Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
      eirmod tempor invidunt

      Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
      eirmod tempor invidunt

EOF;
        $this->assertSame($expected, $this->io->fetchOutput());
    }
Example #9
0
    /**
     * testTableRenderer.
     */
    public function testTableRenderer()
    {
        $renderer = RendererFactory::create('table', $this->io);
        $renderer->render($this->createResultCollection());
        $output = <<<TABLE
+--------+--------+---------+----------+----------------+---------------------+
| Global | Status | Name    | Response | Http Error Log | Validator Error Log |
| Status | Code   |         | Time     |                |                     |
+--------+--------+---------+----------+----------------+---------------------+
| OK     | 200    | Example | 0.42     |                |                     |
+--------+--------+---------+----------+----------------+---------------------+

TABLE;
        $this->assertInstanceOf(RendererInterface::class, $renderer);
        $this->assertSame($output, $this->io->fetchOutput());
    }
 public function testHandlePrintsToOutputIfProcessLauncherNotSupported()
 {
     $this->processLauncher->expects($this->once())->method('isSupported')->will($this->returnValue(false));
     $this->processLauncher->expects($this->never())->method('launchProcess');
     $status = $this->handler->handle($this->args, $this->io, $this->command);
     $this->assertSame("Contents of the-command.txt\n", $this->io->fetchOutput());
     $this->assertSame(0, $status);
 }
Example #11
0
    public function testRenderWithCauseIfVeryVerbose()
    {
        $this->io->setVerbosity(IO::VERY_VERBOSE);
        $cause = new RuntimeException('The message of the cause.');
        $exception = NoSuchCommandException::forCommandName('foobar', 0, $cause);
        $trace = new ExceptionTrace($exception);
        $trace->render($this->io);
        // Prevent trimming of trailing spaces in the box
        $box1 = '                                                          ' . "\n" . '  [Webmozart\\Console\\Api\\Command\\NoSuchCommandException]  ' . "\n" . '  The command "foobar" does not exist.                    ' . "\n" . '                                                          ';
        $expected1 = <<<EOF


{$box1}


Exception trace:
  ()
    src/Api/Command/NoSuchCommandException.php:??
  Webmozart\\Console\\Api\\Command\\NoSuchCommandException::forCommandName()
    tests/UI/Component/ExceptionTraceTest.php
EOF;
        $box2 = '                             ' . "\n" . '  [RuntimeException]         ' . "\n" . '  The message of the cause.  ' . "\n" . '                             ';
        $expected2 = <<<EOF


Caused by:


{$box2}


Exception trace:
  ()
    tests/UI/Component/ExceptionTraceTest.php
EOF;
        $actual = $this->io->fetchErrors();
        // Normalize line numbers across PHP and HHVM
        $actual = preg_replace('~(NoSuchCommandException.php:)\\d+~', '$1??', $actual);
        // Normalize slashes across OS
        $expected1 = str_replace(array("\n", '/'), array(PHP_EOL, DIRECTORY_SEPARATOR), $expected1);
        $expected2 = str_replace(array("\n", '/'), array(PHP_EOL, DIRECTORY_SEPARATOR), $expected2);
        $this->assertStringStartsWith($expected1, $actual);
        $this->assertContains($expected2, $actual);
        $this->assertStringEndsWith(PHP_EOL . PHP_EOL, $actual);
    }
    public function testRenderWithLabelDistanceWrapsText()
    {
        $para = new LabeledParagraph('Label', self::LOREM_IPSUM, 6);
        $para->render($this->io);
        $expected = <<<'EOF'
Label      Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam
           nonumy eirmod tempor invidunt

EOF;
        $this->assertSame($expected, $this->io->fetchOutput());
    }
Example #13
0
    public function testRenderAllCellsInOneLine()
    {
        $grid = new Grid(GridStyle::asciiBorder());
        $grid->addCells(array('99921-58-10-7', 'Divine Comedy', 'Dante Alighieri'));
        $grid->render($this->io);
        $expected = <<<'EOF'
+---------------+---------------+-----------------+
| 99921-58-10-7 | Divine Comedy | Dante Alighieri |
+---------------+---------------+-----------------+

EOF;
        $this->assertSame($expected, $this->io->fetchOutput());
    }
Example #14
0
    public function testHelpPrintsTextIfAsciiDocNotFound()
    {
        $args = $this->helpCommand->parseArgs(new StringArgs('--help'));
        $this->processLauncher->expects($this->any())->method('isSupported')->will($this->returnValue(true));
        $this->executableFinder->expects($this->never())->method('find');
        $this->processLauncher->expects($this->never())->method('launchProcess');
        $this->handler->setApplicationPage('foobar');
        $status = $this->handler->handle($args, $this->io, $this->command);
        $expected = <<<'EOF'
The Application version 1.2.3

USAGE
  the-app
EOF;
        $this->assertStringStartsWith($expected, $this->io->fetchOutput());
        $this->assertSame(0, $status);
    }
Example #15
0
    public function testRenderOptionCommandWithPreferredLongName()
    {
        $config = ApplicationConfig::create()->setName('test-bin')->beginCommand('command')->beginOptionCommand('add', 'a')->setDescription('Description of "add"')->setPreferLongName()->end()->end();
        $application = new ConsoleApplication($config);
        $help = new CommandHelp($application->getCommand('command'));
        $help->render($this->io);
        $expected = <<<'EOF'
USAGE
      test-bin command
  or: test-bin command --add

COMMANDS
  --add (-a)
    Description of "add"


EOF;
        $this->assertSame($expected, $this->io->fetchOutput());
    }
Example #16
0
    public function testRenderFormattedCells()
    {
        $table = new Table(TableStyle::asciiBorder());
        $table->setHeaderRow(array('ISBN', 'Title', 'Author'));
        $table->addRows(array(array('<b>99921-58-10-7</b>', 'Divine Comedy', 'Dante Alighieri'), array('<b>9971-5-0210-0</b>', 'A Tale of Two Cities', 'Charles Dickens'), array('<b>960-425-059-0</b>', 'The Lord of the Rings', 'J. R. R. Tolkien'), array('<b>80-902734-1-6</b>', 'And Then There Were None', 'Agatha Christie')));
        $table->render($this->io);
        $expected = <<<'EOF'
+---------------+--------------------------+------------------+
| ISBN          | Title                    | Author           |
+---------------+--------------------------+------------------+
| 99921-58-10-7 | Divine Comedy            | Dante Alighieri  |
| 9971-5-0210-0 | A Tale of Two Cities     | Charles Dickens  |
| 960-425-059-0 | The Lord of the Rings    | J. R. R. Tolkien |
| 80-902734-1-6 | And Then There Were None | Agatha Christie  |
+---------------+--------------------------+------------------+

EOF;
        $this->assertSame($expected, $this->io->fetchOutput());
    }
Example #17
0
 public function testHandleCommand()
 {
     $args = new Args(new ArgsFormat());
     $io = new BufferedIO("line1\nline2");
     $command = new Command(new CommandConfig('command'));
     $handler = new CallbackHandler(function (Args $passedArgs, IO $io, Command $passedCommand) use($args, $command) {
         PHPUnit_Framework_Assert::assertSame($args, $passedArgs);
         PHPUnit_Framework_Assert::assertSame($command, $passedCommand);
         $io->write($io->readLine());
         $io->error($io->readLine());
         return 123;
     });
     $this->assertSame(123, $handler->handle($args, $io, $command));
     $this->assertSame("line1\n", $io->fetchOutput());
     $this->assertSame('line2', $io->fetchErrors());
 }
Example #18
0
    public function testRenderDescription()
    {
        $config = ApplicationConfig::create()->setHelp('The help');
        $application = new ConsoleApplication($config);
        $help = new ApplicationHelp($application);
        $help->render($this->io);
        $expected = <<<'EOF'
Console Tool

USAGE
  console <command> [<arg1>] ... [<argN>]

ARGUMENTS
  <command>  The command to execute
  <arg>      The arguments of the command

DESCRIPTION
  The help


EOF;
        $this->assertSame($expected, $this->io->fetchOutput());
    }
Example #19
0
 public function testRenderIgnoresIndentation()
 {
     $line = new EmptyLine();
     $line->render($this->io, 10);
     $this->assertSame("\n", $this->io->fetchOutput());
 }