expectOutputString()
またはを使用して、PHPUnitライブラリでphp出力をテストする方法を知っていますexpectOutputString()
。次に、出力に特定の文字列が含まれていないことを確認する必要があります。出力バッファリングと内部の文字列の検索を使用してこれを行うことができますが、おそらくより良い方法はexpectOutputString()
適切な式で使用することです。
この式はどのように作成する必要がありますか?
expectOutputString()
またはを使用して、PHPUnitライブラリでphp出力をテストする方法を知っていますexpectOutputString()
。次に、出力に特定の文字列が含まれていないことを確認する必要があります。出力バッファリングと内部の文字列の検索を使用してこれを行うことができますが、おそらくより良い方法はexpectOutputString()
適切な式で使用することです。
この式はどのように作成する必要がありますか?
正規表現を使用したいのですが、否定的な一致を行うには、先読みアサーション構文を使用する必要があります。たとえば、出力に「hello」が含まれていないことをテストするには:
class OutputRegexTest extends PHPUnit_Framework_TestCase
{
private $regex='/^((?!Hello).)*$/s';
public function testExpectNoHelloAtFrontFails()
{
$this->expectOutputRegex($this->regex);
echo "Hello World!\nAnother sentence\nAnd more!";
}
public function testExpectNoHelloInMiddleFails()
{
$this->expectOutputRegex($this->regex);
echo "This is Hello World!\nAnother sentence\nAnd more!";
}
public function testExpectNoHelloAtEndFails()
{
$this->expectOutputRegex($this->regex);
echo "A final Hello";
}
public function testExpectNoHello()
{
$this->expectOutputRegex($this->regex);
echo "What a strange world!\nAnother sentence\nAnd more!";
}
}
次の出力が得られます。
$ phpunit testOutputRegex.php
PHPUnit 3.6.12 by Sebastian Bergmann.
FFF.
Time: 0 seconds, Memory: 4.25Mb
There were 3 failures:
1) OutputRegexTest::testExpectNoHelloAtFrontFails
Failed asserting that 'Hello World!
Another sentence
And more!' matches PCRE pattern "/^((?!Hello).)*$/s".
2) OutputRegexTest::testExpectNoHelloInMiddleFails
Failed asserting that 'This is Hello World!
Another sentence
And more!' matches PCRE pattern "/^((?!Hello).)*$/s".
3) OutputRegexTest::testExpectNoHelloAtEndFails
Failed asserting that 'A final Hello' matches PCRE pattern "/^((?!Hello).)*$/s".
FAILURES!
Tests: 4, Assertions: 4, Failures: 3.