You want to use a regex, and to perform a negative match, you must use the lookahead statement syntax. For instance. to verify that the output does not contain hi:
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!"; } }
Gives this conclusion:
$ 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.
source share