CakePHP:在shell上为表编写测试

CakePHP:在shell上为表编写测试,cakephp,phpunit,cakephp-3.0,Cakephp,Phpunit,Cakephp 3.0,我正在为CakePHP编写一个导入/导出数据库的插件。此插件有一个shell,其index()方法列出了已导出的数据库: public function index() { //Gets alla files $files = BackupManager::index(); $this->out(__d('mysql_backup', 'Backup files found: {0}', count($files))); if (!empty($files)

我正在为CakePHP编写一个导入/导出数据库的插件。此插件有一个shell,其
index()
方法列出了已导出的数据库:

public function index()
{
    //Gets alla files
    $files = BackupManager::index();
    $this->out(__d('mysql_backup', 'Backup files found: {0}', count($files)));
    if (!empty($files)) {
        //Parses files
        $files = array_map(function ($file) {
            if (isset($file->compression) && !$file->compression) {
                $file->compression = __d('mysql_backup', 'none');
            }
            $file->size = Number::toReadableSize($file->size);
            return array_values((array)$file);
        }, $files);
        //Table headers
        $headers = [
            __d('mysql_backup', 'Filename'),
            __d('mysql_backup', 'Extension'),
            __d('mysql_backup', 'Compression'),
            __d('mysql_backup', 'Size'),
            __d('mysql_backup', 'Datetime'),
        ];
        $this->helper('table')->output(array_merge([$headers], $files));
    }
}
没有数据库的输出:

$ bin/cake backup

Welcome to CakePHP v3.3.8 Console
---------------------------------------------------------------
App : src
Path: /home/mirko/Server/mirkopagliai/src/
PHP : 7.0.12-1
---------------------------------------------------------------
Backup files found: 0
使用某些数据库进行输出:

$ bin/cake backup

Welcome to CakePHP v3.3.8 Console
---------------------------------------------------------------
App : src
Path: /home/mirko/Server/mirkopagliai/src/
PHP : 7.0.12-1
---------------------------------------------------------------
Backup files found: 2
+-------------------------------------------+-----------+-------------+-----------+-----------------+
| Filename                                  | Extension | Compression | Size      | Datetime        |
+-------------------------------------------+-----------+-------------+-----------+-----------------+
| backup_mirkopagliai_20161113110419.sql.gz | sql.gz    | gzip        | 51,05 KB  | 13/11/16, 11:04 |
| backup_mirkopagliai_20161113110414.sql    | sql       | none        | 150,93 KB | 13/11/16, 11:04 |
+-------------------------------------------+-----------+-------------+-----------+-----------------+
现在我需要编写一些测试。
我编写了没有数据库代码的测试:

public function testIndexNoBackups()
{
    $this->io->expects($this->once())
        ->method('out')
        ->with('Backup files found: 0', 1);
    $this->BackupShell->index();
}
我的问题是编写一个表是否输出的测试(第二个示例)

目前,我只写了以下内容:

public function testIndex()
{
    //Creates a database
    (new BackupExport())->export();

    $this->io->expects($this->once())
        ->method('out')
        ->with('Backup files found: 1', 1);

    $this->BackupShell->index();
}
输出:

$ phpunit tests/TestCase/Shell/BackupShellTest.php --filter testIndex
PHPUnit 5.4.6 by Sebastian Bergmann and contributors.

E.                                                                  2 / 2 (100%)

Time: 114 ms, Memory: 6.00MB

There was 1 error:

1) MysqlBackup\Test\TestCase\Shell\BackupShellTest::testIndex
Error: Call to a member function output() on null

/home/mirko/Libs/Plugins/cakephp-mysql-backup/src/Shell/BackupShell.php:110
/home/mirko/Libs/Plugins/cakephp-mysql-backup/tests/TestCase/Shell/BackupShellTest.php:191

ERRORS!
Tests: 2, Assertions: 1, Errors: 1.

那么,如何编写测试来检查表输出呢?我在哪里可以找到一个例子?

这取决于你想做的清洁程度。 就我个人而言,主要是为了速度,而不是使用对输出的期望

相反,我只是在一个“伪模拟”类型的容器中收集输出:

php
...
使用Tools\TestSuite\ConsoleOutput;
...
$this->out=new ConsoleOutput();
$this->err=new ConsoleOutput();
$io=新控制台io($this->out,$this->err);
$this->Shell=$this->getMockBuilder(infictShell::class)
->setMethods(['in','u stop']))
->setConstructorArgs([$io])
->getMock();
这个特殊的控制台输出将不会写入stdout或stderr,而是在内部收集所有内容,供您稍后获取

在运行命令之后,您可以执行以下操作

php
$output=$this->out->output();
$expected='foo bar';
$this->assertContains($expected,$output);


有关详细信息,请查看。该链接还为您提供了上述示例所需的工具。

感谢@mark。我的解决方案:

...
use Cake\TestSuite\Stub\ConsoleOutput;
use Cake\Console\ConsoleIo;
...

class BackupShellTest extends TestCase
{    
    public function setUp()
    {
        parent::setUp();

        $this->out = new ConsoleOutput();        

        $this->Shell = $this->getMockBuilder('MysqlBackup\Shell\BackupShell')
            ->setMethods(['in', 'err', '_stop', 'clear'])
            ->setConstructorArgs([new ConsoleIo($this->out)])
            ->getMock();
    }

    public function testIndex()
    {
        $this->Shell->index();
        $output = $this->out->messages();

        //Some tests on $output HERE
    }
}
$output
debug()
示例:

########## DEBUG ##########
[
        (int) 0 => 'Backup files found: 3',
        (int) 1 => '+----------------+-----------+-------------+-----------+--------------------+',
        (int) 2 => '| <info>Filename</info>       | <info>Extension</info> | <info>Compression</info> | <info>Size</info>      | <info>Datetime</info>           |',
        (int) 3 => '+----------------+-----------+-------------+-----------+--------------------+',
        (int) 4 => '| backup.sql.gz  | sql.gz    | gzip        | 443 Bytes | 11/15/16, 11:45 AM |',
        (int) 5 => '| backup.sql.bz2 | sql.bz2   | bzip2       | 523 Bytes | 11/15/16, 11:45 AM |',
        (int) 6 => '| backup.sql     | sql       | none        | 1.23 KB   | 11/15/16, 11:45 AM |',
        (int) 7 => '+----------------+-----------+-------------+-----------+--------------------+'
]
###########################
调试##########
[
(int)0=>“找到备份文件:3”,
(int)1=>'+-------------+-------------+--------------+--------------+--------------+-------------+',,
(int)2=>“|文件名|扩展名|压缩|大小|日期时间|”,
(int)3=>'+-------------+-------------+--------------+--------------+--------------+-------------+',,
(int)4=>“|backup.sql.gz | sql.gz | gzip | 443字节| 11/15/16,上午11:45 |”,
(int)5=>“| backup.sql.bz2 | sql.bz2 | bzip2 | 523字节| 11/15/16,上午11:45 |”,
(int)6=>“|backup.sql | sql | none | 1.23kb | 11/15/16am 11:45 |”,
(int)7=>'+-------------+-------------+--------------+--------------+--------------+-------------+'
]
###########################

请不要链接到您的代码,始终在问题中包含所有相关代码(仅4小时后,您的链接已过时)!谢谢@ndm first post FixedTanks@mark。你帮了大忙。我没有使用你的插件,但是你提出的逻辑在我看来是正确的。我看到,
Cake\TestSuite\Stub\ConsoleOutput
的工作原理基本相同(对吗?)。如果这是足够的,现在我会满足于这一点,很快我会尝试你的插件