phpunit - CakePHP: write test for table on shell -


i'm writing plugin cakephp import/export databases. plugin has shell , index() method lists databases exported:

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));     } } 

output no database:

$ bin/cake backup  welcome cakephp v3.3.8 console --------------------------------------------------------------- app : src path: /home/mirko/server/mirkopagliai/src/ php : 7.0.12-1 --------------------------------------------------------------- backup files found: 0 

output databases:

$ bin/cake backup  welcome 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 | +-------------------------------------------+-----------+-------------+-----------+-----------------+ 

now need write tests.
wrote test no database code:

public function testindexnobackups() {     $this->io->expects($this->once())         ->method('out')         ->with('backup files found: 0', 1);     $this->backupshell->index(); } 

my problem write test of whether table output (second example).

for wrote this:

public function testindex() {     //creates database     (new backupexport())->export();      $this->io->expects($this->once())         ->method('out')         ->with('backup files found: 1', 1);      $this->backupshell->index(); } 

output:

$ phpunit tests/testcase/shell/backupshelltest.php --filter testindex phpunit 5.4.6 sebastian bergmann , contributors.  e.                                                                  2 / 2 (100%)  time: 114 ms, memory: 6.00mb  there 1 error:  1) mysqlbackup\test\testcase\shell\backupshelltest::testindex error: call 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. 

so, how write test check table output? can find example?

it depends on how clean want it. personally, speed mainly, not use expectations output.

instead collect output in "pseudo-mock" kind of container:

php ... use tools\testsuite\consoleoutput; ... $this->out = new consoleoutput(); $this->err = new consoleoutput(); $io = new consoleio($this->out, $this->err); $this->shell = $this->getmockbuilder(inflectshell::class) ->setmethods(['in', '_stop']) ->setconstructorargs([$io]) ->getmock(); special consoleoutput not write stdout or stderr, instead internally collect fetch afterwards.

after running command can do

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

check out test cases in tools plugin details. link provides tooling need above example.


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -