php 如何测试字符串是否包含 PHPUnit 中的另一个字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24015943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to test if string contains another string in PHPUnit?
提问by TheStoryCoder
I can't seem to find an assertion in PHPUnitthat simply tests if a string is contained somewhere in another string. Trying to do something like this:
我似乎无法在PHPUnit中找到一个断言来简单地测试一个字符串是否包含在另一个字符串中的某个地方。试图做这样的事情:
public function testRecipe() {
$plaintext = get_bread_recipe();
$this->assertStringContains('flour', $plaintext);
}
What real assertion would I put instead of assertStringContains
? I would prefer not having to worry about regex in this case because there is absolutely no need for it.
我会用什么真正的断言来代替assertStringContains
?在这种情况下,我宁愿不必担心正则表达式,因为绝对不需要它。
It's so simple that there must be something I have overlooked, but I just can't figure it out! Funny enough there is assertStringStartsWith()
and assertStringEndsWith()
!
太简单了,肯定有我忽略的地方,但我就是想不通!有趣的是有assertStringStartsWith()
和assertStringEndsWith()
!
Update:I know strpos() !== false
could be used but I'm looking for something cleaner. If I just use vanilla PHP functions anyway what's the whole point with all the assertions then...
更新:我知道strpos() !== false
可以使用,但我正在寻找更清洁的东西。如果我只是使用普通的 PHP 函数,那么所有断言的重点是什么......
采纳答案by Tomá? Votruba
2019 and PHPUnit 8.0 update
2019 和 PHPUnit 8.0 更新
assertContains()
is deprecated since PHPUnit 8.0, see issue #3425.
assertContains()
自 PHPUnit 8.0 起已弃用,请参阅问题 #3425。
Now the specific method is recommended (see issue #3422
):
现在推荐具体方法(参见issue #3422
):
$this->assertStringContainsString('flour', $plaintext);
TIP: Upgrade instantly with Rector and PHPUnit 8.0 set.
提示:使用Rector 和 PHPUnit 8.0 set立即升级。
回答by Schleis
As you could tell assertContains
is for checking that an array contains a value.
正如您所知assertContains
,用于检查数组是否包含值。
Looking to see if the string contains a substring, your simplest query would be to use assertRegexp()
查看字符串是否包含子字符串,您最简单的查询是使用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.
如果你真的想要一个assertStringContains
断言,你可以扩展PHPUnit_Framework_TestCase
和创建你自己的断言。
UPDATE
更新
In the latest version of PHPUnit, assertContains
will work on strings.
在最新版本的 PHPUnit 中,assertContains
将处理字符串。
回答by malte
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...:)
您总是可以在自定义断言方法中隐藏丑陋,所以基本上我会有一个 BaseTestCase 类继承自 phpunit 测试用例,您可以使用它来拥有自己的可重用断言库(请参阅http://xunitpatterns.com/Custom %20Assertion.html..(在 php 5.4 中你也可以使用特征,恕我直言,断言库是特征实际有用的情况之一)。我总是在我的项目中引入相当多的自定义断言,通常是特定于领域的。是的,有些也很丑:) 好吧,我想这就是封装的用途...其中包括...:)
UPDATE:I just checked and 'assertContains' and 'assertNotContains' actually also operate on strings as well as arrays (and anything that implements 'Traversable'):
更新:我刚刚检查过,“assertContains”和“assertNotContains”实际上也对字符串和数组(以及任何实现“Traversable”的东西)进行操作:
function test_Contains()
{
$this->assertContains("test", "this is a test string" );
$this->assertNotContains("tst", "this is a test string");
}
回答by Elliot Chance
Extending from @Schleis, if you have more complicated strings you will need to escape the regex:
从@Schleis 扩展,如果您有更复杂的字符串,则需要转义正则表达式:
$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);
}
回答by GordonM
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.
您可以对任何东西进行断言,包括普通的 PHP 函数。如果您断言您正在测试的字符串上感兴趣的子字符串的 strpos 不会返回 FALSE,它将满足您的需求。
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).
不过,请确保您正在检查 FALSE,而不是评估为 FALSE 的内容,例如 0(如果被测试的字符串以给定的子字符串开头,strpos 将返回)。
回答by Farid Movsumov
if (strpos($plaintext, 'flour') === false){
$this->fail("planintext should contain 'flour'");
}
回答by hermitcodemonkey
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.
难道你不能只使用可用的普通 PHP 函数来处理字符串中的字符串,然后 assertEquals 的输出吗?当然,它没有那么闪亮,但它有效。
Alternatively you can use assertThat with a regular expression.
或者,您可以将 assertThat 与正则表达式一起使用。
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.
在测试中,您应该能够控制您的输入,因此您应该知道整个预期的输出字符串。如果你的函数破坏了字符串的其他部分,那么这个断言不会告诉你它。这可能就是 assertStringContains 函数不存在的原因。