【问题标题】:How to use Symfony Table Helper inside phpunit?如何在 phpunit 中使用 Symfony Table Helper?
【发布时间】:2020-03-29 10:39:49
【问题描述】:

本文展示了 laravel 的 artisan 命令支持 $this->table($headers, $data),它在后台使用“Symfony Table Helper”来显示来自标量数组的 cli 表。

https://mattstauffer.com/blog/advanced-input-output-with-artisan-commands-tables-and-progress-bars-in-laravel-5.1/

http://symfony.com/doc/current/components/console/helpers/tablehelper.html

我想在 phpunit 中使用这个功能进行调试。

$ php artisan tinker
Psy Shell v0.9.9 (PHP 7.2.24-0ubuntu0.18.04.1 — cli) by Justin Hileman
>>> new Symfony\Component\Console\Helper\Table()
TypeError: Too few arguments to function Symfony/Component/Console/Helper/Table::__construct(), 0 passed in Psy Shell code on line 1 and exactly 1 expected
>>> app(\Symfony\Component\Console\Output\OutputInterface::class)
Illuminate/Contracts/Container/BindingResolutionException with message 'Target [Symfony/Component/Console/Output/OutputInterface] is not instantiable.'
>>> app()->make(\Symfony\Component\Console\Output\OutputInterface::class)
Illuminate/Contracts/Container/BindingResolutionException with message 'Target [Symfony/Component/Console/Output/OutputInterface] is not instantiable.'
>>> app()->make(\Symfony\Component\Console\Helper\Table::class)
Illuminate/Contracts/Container/BindingResolutionException with message 'Target [Symfony/Component/Console/Output/OutputInterface] is not instantiable while building [Symfony/Component/Console/Helper/Table].'
>>>

现在我不知道如何实例化辅助对象。我想做一个dump_table 函数,可以在tinker 和phpunit 中使用,类似于现有的dump 助手。

【问题讨论】:

    标签: php laravel symfony laravel-5 phpunit


    【解决方案1】:

    我整理了一个解决方案:

    <?php
    
    namespace App\Console;
    
    use Symfony\Component\Console\Helper\Table as SymfonyTable;
    use Symfony\Component\Console\Output\StreamOutput;
    
    class Table
    {
        static function dump ($headers, $rows = null) {
            if ($rows === null) {
                $rows = $headers;
                $headers = null;
            }
            if (!$rows || count($rows) === 0) {
                dump('no table data');
            }
    
            $output = new StreamOutput(fopen('php://stdout', 'w'));
            $table = new SymfonyTable($output);
    
            if ($headers === true) {
                $table->setHeaders(array_keys($rows[0]));
            } else if (is_array($headers) && count($headers) > 0) {
                $table->setHeaders($headers);
            }
    
            $table->setRows($rows);
            // writes to output
            $table->render();
        }
    }
    

    php artisan tinker可以这样使用:

    >>> App\Console\Table::dump([[1,2],[3,4]]);
    +---+---+
    | 1 | 2 |
    | 3 | 4 |
    +---+---+
    >>> App\Console\Table::dump(true, [[1,2],[3,4]]);
    +---+---+
    | 0 | 1 |
    +---+---+
    | 1 | 2 |
    | 3 | 4 |
    +---+---+
    => null
    >>> App\Console\Table::dump(['a', 'b'], [[1,2],[3,4]]);
    +---+---+
    | a | b |
    +---+---+
    | 1 | 2 |
    | 3 | 4 |
    +---+---+
    => null
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-11
      • 2019-07-08
      • 1970-01-01
      • 2021-07-25
      • 2021-09-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多