Changed StreamFilterTrait and CITestStreamFilter.

Fixed tests that use streams.
This commit is contained in:
Andrey Pyzhikov 2022-08-29 16:33:35 +08:00
parent 43cca387a9
commit defbedacbf
No known key found for this signature in database
GPG Key ID: BA61560FB9B7B817
5 changed files with 72 additions and 90 deletions

View File

@ -28,6 +28,16 @@ class CITestStreamFilter extends php_user_filter
protected static bool $registered = false;
/**
* @var resource|null
*/
private static $err;
/**
* @var resource|null
*/
private static $out;
/**
* This method is called whenever data is read from or written to the
* attached stream (such as with fread() or fwrite()).
@ -56,4 +66,37 @@ class CITestStreamFilter extends php_user_filter
static::$buffer = '';
}
public static function addErrorFilter(): void
{
self::removeFilter(self::$err);
self::$err = stream_filter_append(STDERR, 'CITestStreamFilter');
}
public static function addOutputFilter(): void
{
self::removeFilter(self::$out);
self::$out = stream_filter_append(STDOUT, 'CITestStreamFilter');
}
public static function removeErrorFilter(): void
{
self::removeFilter(self::$err);
}
public static function removeOutputFilter(): void
{
self::removeFilter(self::$out);
}
/**
* @param resource $stream
*/
protected static function removeFilter(&$stream): void
{
if (is_resource($stream)) {
stream_filter_remove($stream);
$stream = null;
}
}
}

View File

@ -15,86 +15,17 @@ use CodeIgniter\Test\Filters\CITestStreamFilter;
trait StreamFilterTrait
{
/**
* @var resource|null
*/
private $outputStreamFilterResource;
/**
* @var resource|null
*/
private $errorStreamFilterResource;
protected function setUpStreamFilterTrait(): void
{
$this->registerStreamFilterClass()
->appendOutputStreamFilter()
->appendErrorStreamFilter();
CITestStreamFilter::registration();
CITestStreamFilter::addOutputFilter();
CITestStreamFilter::addErrorFilter();
}
protected function tearDownStreamFilterTrait(): void
{
$this->removeOutputStreamFilter()->removeErrorStreamFilter();
}
/**
* @return $this
*/
protected function appendOutputStreamFilter()
{
$this->removeOutputStreamFilter();
$this->outputStreamFilterResource = stream_filter_append(STDOUT, 'CITestStreamFilter');
return $this;
}
/**
* @return $this
*/
protected function appendErrorStreamFilter()
{
$this->removeErrorStreamFilter();
$this->errorStreamFilterResource = stream_filter_append(STDERR, 'CITestStreamFilter');
return $this;
}
/**
* @return $this
*/
protected function removeOutputStreamFilter()
{
if (is_resource($this->outputStreamFilterResource)) {
stream_filter_remove($this->outputStreamFilterResource);
$this->outputStreamFilterResource = null;
}
return $this;
}
/**
* @return $this
*/
protected function removeErrorStreamFilter()
{
if (is_resource($this->errorStreamFilterResource)) {
stream_filter_remove($this->errorStreamFilterResource);
$this->errorStreamFilterResource = null;
}
return $this;
}
/**
* @return $this
*/
protected function registerStreamFilterClass()
{
CITestStreamFilter::registration();
return $this;
CITestStreamFilter::removeOutputFilter();
CITestStreamFilter::removeErrorFilter();
}
protected function getStreamFilterBuffer(): string

View File

@ -569,9 +569,9 @@ final class CodeIgniterTest extends CIUnitTestCase
public function testPageCacheSendSecureHeaders()
{
// Suppress command() output
CITestStreamFilter::$buffer = '';
$outputStreamFilter = stream_filter_append(STDOUT, 'CITestStreamFilter');
$errorStreamFilter = stream_filter_append(STDERR, 'CITestStreamFilter');
CITestStreamFilter::registration();
CITestStreamFilter::addErrorFilter();
CITestStreamFilter::addOutputFilter();
// Clear Page cache
command('cache:clear');
@ -618,8 +618,8 @@ final class CodeIgniterTest extends CIUnitTestCase
// Clear Page cache
command('cache:clear');
// Remove stream fliters
stream_filter_remove($outputStreamFilter);
stream_filter_remove($errorStreamFilter);
// Remove stream filters
CITestStreamFilter::removeErrorFilter();
CITestStreamFilter::removeOutputFilter();
}
}

View File

@ -12,37 +12,30 @@
namespace CodeIgniter\Commands\Utilities;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\Filters\CITestStreamFilter;
use CodeIgniter\Test\StreamFilterTrait;
/**
* @internal
*/
final class NamespacesTest extends CIUnitTestCase
{
private $streamFilter;
use StreamFilterTrait;
protected function setUp(): void
{
$this->resetServices();
parent::setUp();
CITestStreamFilter::$buffer = '';
$this->streamFilter = stream_filter_append(STDOUT, 'CITestStreamFilter');
$this->streamFilter = stream_filter_append(STDERR, 'CITestStreamFilter');
}
protected function tearDown(): void
{
stream_filter_remove($this->streamFilter);
$this->resetServices();
}
protected function getBuffer()
{
return CITestStreamFilter::$buffer;
return $this->getStreamFilterBuffer();
}
public function testNamespacesCommandCodeIgniterOnly()

View File

@ -277,6 +277,21 @@ See :ref:`Testing Traits <testing-overview-traits>`.
If you override the ``setUp()`` or ``tearDown()`` methods in your test, then you must call the ``parent::setUp()`` and
``parent::tearDown()`` methods respectively to configure the ``StreamFilterTrait``.
**CITestStreamFilter** for manual/single use.
If you need to capture streams in only one test, then instead of using the StreamFilterTrait trait, you can manually
add a filter to streams.
**Overview of methods**
- ``CITestStreamFilter::registration()`` Filter registration.
- ``CITestStreamFilter::addOutputFilter()`` Adding a filter to the output stream.
- ``CITestStreamFilter::addErrorFilter()`` Adding a filter to the error stream.
- ``CITestStreamFilter::removeOutputFilter()`` Removing a filter from the output stream.
- ``CITestStreamFilter::removeErrorFilter()`` Removing a filter from the error stream.
.. literalinclude:: overview/020.php
.. _testing-cli-input:
Testing CLI Input