如何测试字符串是否包含 PHPUnit 中的另一个字符串?

PHPUnit 中似乎找不到仅仅测试字符串是否包含在另一个字符串中的断言。试图做这样的事情:

public function testRecipe() {


$plaintext = get_bread_recipe();


$this->assertStringContains('flour', $plaintext);


}

我应该用什么真正的断言来代替 assertStringContains?在这种情况下,我希望不必担心正则表达式,因为绝对不需要它。

这么简单,一定有什么我忽略了,但我就是想不出来!有趣的是 assertStringStartsWith()assertStringEndsWith()

更新: 我知道 strpos() !== false可以使用,但我正在寻找更干净的东西。如果我只是使用普通的 PHP 函数,那么这些断言的意义何在呢。

53024 次浏览

Can't you just do the string in string using the plain PHP functions available, and then assertEquals the output of that? Granted, it's not as shiny, but it works.

Alternatively you can use assertThat with a regular expression.

It sounds like something is inherently wrong with the design of the code / test though, from the limited perspective of the example. Why would a function return a complex string of unknown length, with the expected bit 'somewhere' in the string?

In the test you should be able to control your input, so you should know your entire expected output string. If your function ruins other parts of the string just this assertion wouldn't tell you about it. Which is probably why the assertStringContains function doesn't exist.

You can do asserts against anything, including vanilla PHP functions. If you assert that strpos for the substring you're interested in on the string you're testing doesn't return FALSE it will meet your needs.

Make sure you're checking against FALSE though, and not something that evaluates to FALSE such as 0 (which is what strpos will return if the string being tested starts with the given substring).

As you could tell assertContains is for checking that an array contains a value.

Looking to see if the string contains a substring, your simplest query would be to use assertRegexp()

$this->assertRegexp('/flour/', $plaintext);

You would just need to add the delimiters.

If you really want to have an assertStringContains assertion, you can extend PHPUnit_Framework_TestCase and create your own.

UPDATE

In the pre-9 versions of PHPUnit, assertContains will work on strings.

From 9+ we need to use assertStringContainsString or assertStringContainsStringIgnoringCase.

You can always hide the ugliness inside a custom assertion method, so basically I would have a BaseTestCase class which inherits from the phpunit test case which you could use to have your own library of reusable assertions (see http://xunitpatterns.com/Custom%20Assertion.html).. (In php 5.4 you can use traits as well, imho assertion libraries are one of the cases where traits actually are useful)..I always introduce quite a few custom assertions in my projects, often domain specific. And yes, some are ugly too:) well, I guess that's what encapsulation is there for... Amongst things...:)

UPDATE: I just checked and 'assertContains' and 'assertNotContains' actually also operate on strings as well as arrays (and anything that implements 'Traversable'):

function test_Contains()
{
$this->assertContains("test", "this is a test string" );
$this->assertNotContains("tst", "this is a test string");
}

Extending from @Schleis, if you have more complicated strings you will need to escape the regex:

$this->assertRegexp('/' . preg_quote('[some $stuff]') . '/', $plaintext);

Or wrap it up in a neater method:

public function assertStringContains($needle, $haystack)
{
$this->assertRegexp('/' . preg_quote($needle) . '/', $haystack);
}

2019 and PHPUnit 8.0 update

assertContains() is deprecated since PHPUnit 8.0, see issue #3425.


Now the specific method is recommended (see issue #3422):

$plaintext = 'I fell on the flour';
$this->assertStringContainsString('flour', $plaintext);

TIP: Upgrade instantly with Rector and PHPUnit 8.0 set.