This is very simple. Test that string does not contain other string:
Edit
In 2020 for PHPUnit 9.x up there is another concise approach:
$this->assertStringNotContainsString(needle, haystack);
See PHPUnit assertion doc. Not all available assertions are documented in the docs.
The good way to find these is to dd($this)
for Laravel (or var_dump($this)
in pure PHP) from within your PHPUnit test class and scroll through the output's methods
section. There you see the available assertions including the undocumented ones.
The Older Answer
More verbose, more flexible with different assertions not only for strings.
$string='just some string';
$this->assertThat($string, $this->logicalNot($this->stringContains('script')));
// Assertion is passed.
Based on the great example of multiple assertions in the same test from http://www.kreamer.org/phpunit-cookbook/1.0/assertions/use-multiple-assertions-in-one-test
I use it to check the form fields sanitization went OK.
Within PHPUnit test I establish a test array having <script>
tag in it to be sanitized. Pass it to the sanitization method under test. Serialize the sanitization result (to avoid messing with asserting an array, plus easier on my eyes when var_dump
'ing serialized result).
And then apply the stringContains
method within assertThat
assertion as seen above and enjoy :)