Skip to content

Commit

Permalink
Merge pull request codeigniter4#1039 from jim-parry/testing/cli-fix
Browse files Browse the repository at this point in the history
Refactor to use CITestStreamFilter
  • Loading branch information
jim-parry authored May 21, 2018
2 parents 56e5b51 + 6b9630f commit 38fc7a9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 49 deletions.
34 changes: 8 additions & 26 deletions tests/system/CLI/CommandRunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Config\MockCLIConfig;
use CodeIgniter\HTTP\UserAgent;
use CodeIgniter\Test\Filters\CITestStreamFilter;

class CommandRunnerTest extends \CIUnitTestCase
{
Expand All @@ -10,8 +11,8 @@ class CommandRunnerTest extends \CIUnitTestCase

public function setUp()
{
CLICommandStreamFilter::$buffer = '';
$this->stream_filter = stream_filter_append(STDOUT, 'CLICommandStreamFilter');
CITestStreamFilter::$buffer = '';
$this->stream_filter = stream_filter_append(STDOUT, 'CITestStreamFilter');

$this->env = new \CodeIgniter\Config\DotEnv(ROOTPATH);
$this->env->load();
Expand Down Expand Up @@ -40,7 +41,7 @@ public function tearDown()
public function testGoodCommand()
{
$this->runner->index(['list']);
$result = CLICommandStreamFilter::$buffer;
$result = CITestStreamFilter::$buffer;

// make sure the result looks like a command list
$this->assertContains('Lists the available commands.', $result);
Expand All @@ -50,7 +51,7 @@ public function testGoodCommand()
public function testDefaultCommand()
{
$this->runner->index([]);
$result = CLICommandStreamFilter::$buffer;
$result = CITestStreamFilter::$buffer;

// make sure the result looks like basic help
$this->assertContains('Displays basic usage information.', $result);
Expand All @@ -60,40 +61,21 @@ public function testDefaultCommand()
public function testEmptyCommand()
{
$this->runner->index([null,'list']);
$result = CLICommandStreamFilter::$buffer;
$result = CITestStreamFilter::$buffer;

// make sure the result looks like a command list
$this->assertContains('Lists the available commands.', $result);
}

public function testBadCommand()
{
$this->error_filter = stream_filter_append(STDERR, 'CLICommandStreamFilter');
$this->error_filter = stream_filter_append(STDERR, 'CITestStreamFilter');
$this->runner->index(['bogus']);
$result = CLICommandStreamFilter::$buffer;
$result = CITestStreamFilter::$buffer;
stream_filter_remove($this->error_filter);

// make sure the result looks like a command list
$this->assertContains("Command 'bogus' not found", $result);
}

}

class CLICommandStreamFilter extends \php_user_filter
{

public static $buffer = '';

public function filter($in, $out, &$consumed, $closing)
{
while ($bucket = stream_bucket_make_writeable($in))
{
self::$buffer .= $bucket->data;
$consumed += $bucket->datalen;
}
return PSFS_PASS_ON;
}

}

stream_filter_register('CLICommandStreamFilter', 'CodeIgniter\CLI\CLICommandStreamFilter');
28 changes: 5 additions & 23 deletions tests/system/CLI/ConsoleTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace CodeIgniter\CLI;

use Config\MockCLIConfig;
use CodeIgniter\Test\Filters\CITestStreamFilter;

class ConsoleTest extends \CIUnitTestCase
{
Expand All @@ -9,8 +10,8 @@ class ConsoleTest extends \CIUnitTestCase

public function setUp()
{
CLICTestStreamFilter::$buffer = '';
$this->stream_filter = stream_filter_append(STDOUT, 'CLICTestStreamFilter');
CITestStreamFilter::$buffer = '';
$this->stream_filter = stream_filter_append(STDOUT, 'CITestStreamFilter');

$this->env = new \CodeIgniter\Config\DotEnv(ROOTPATH);
$this->env->load();
Expand Down Expand Up @@ -44,38 +45,19 @@ public function testHeader()
{
$console = new \CodeIgniter\CLI\Console($this->app);
$console->showHeader();
$result = CLICTestStreamFilter::$buffer;
$result = CITestStreamFilter::$buffer;
$this->assertTrue(strpos($result, 'CodeIgniter CLI Tool') > 0);
}

public function testRun()
{
$console = new \CodeIgniter\CLI\Console($this->app);
$console->run();
$result = CLICTestStreamFilter::$buffer;
$result = CITestStreamFilter::$buffer;

// make sure the result looks like a command list
$this->assertContains('Lists the available commands.', $result);
$this->assertContains('Displays basic usage information.', $result);
}

}

class CLICTestStreamFilter extends \php_user_filter
{

public static $buffer = '';

public function filter($in, $out, &$consumed, $closing)
{
while ($bucket = stream_bucket_make_writeable($in))
{
self::$buffer .= $bucket->data;
$consumed += $bucket->datalen;
}
return PSFS_PASS_ON;
}

}

stream_filter_register('CLICTestStreamFilter', 'CodeIgniter\CLI\CLICTestStreamFilter');

0 comments on commit 38fc7a9

Please sign in to comment.