php 如何断言结果是 PHPUnit 中的整数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21654368/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:19:56  来源:igfitidea点击:

How do I assert the result is an integer in PHPUnit?

phpunit-testingphpunit

提问by bnp887

I would like to be able test that a result is an integer (1,2,3...) where the function could return any number, e.g.:

我希望能够测试结果是一个整数(1,2,3 ...),其中函数可以返回任何数字,例如:

$new_id = generate_id();

I had thought it would be something like:

我原以为它会是这样的:

$this->assertInstanceOf('int', $new_id);

But I get this error:

但我收到此错误:

Argument #1 of PHPUnit_Framework_Assert::assertInstanceOf() must be a class or interface name

PHPUnit_Framework_Assert::assertInstanceOf() 的参数 #1 必须是类或接口名称

回答by Farid Movsumov

$this->assertInternalType("int", $id);

Edit: As of PHPUnit 8, the answer is:

编辑:从PHPUnit 8 开始,答案是:

$this->assertIsInt($id);

回答by Francesco Casula

I prefer using the official PHPUnit class constants.

我更喜欢使用官方的 PHPUnit 类常量。

PHPUnit v5.2:

PHPUnit v5.2

use PHPUnit_Framework_Constraint_IsType as PHPUnit_IsType;

// ...

$this->assertInternalType(PHPUnit_IsType::TYPE_INT, $new_id);

Or latest which at the moment of writing is v7.0:

或者在撰写本文时最新的是v7.0

use PHPUnit\Framework\Constraint\IsType;

// ...

$this->assertInternalType(IsType::TYPE_INT, $new_id);

回答by Mike Brant

Original answer is given below for posterity, but I would strongly recommend using assertInternalType()as suggested in other answers.

下面给出了原始答案供后代使用,但我强烈建议assertInternalType()按照其他答案中的建议使用。



Original answer:

原答案:

Simply use assertTrue with is_int().

只需将 assertTrue 与 is_int() 一起使用。

$this->assertTrue(is_int($new_id));

回答by Kudashev Serge

I think better use this construction:

我认为最好使用这种结构:

$this->assertThat($new_id, $this->logicalAnd(
    $this->isType('int'), 
    $this->greaterThan(0)
));

because it will check not only the type of $new_id variable, but will check if this variable is greater than 0 (assume id cannot be negative or zero) and this is more strict and secure.

因为它不仅会检查 $new_id 变量的类型,还会检查这个变量是否大于 0(假设 id 不能为负数或零),这更加严格和安全。

回答by ShadowBeast

As of PHPUnit 8, The other approaches are now deprecated, assertInternalType()will now throw this error and fail:

从 PHPUnit 8 开始,其他方法现已弃用,assertInternalType()现在将抛出此错误并失败:

assertInternalType() is deprecated and will be removed in PHPUnit 9. Refactor your test to use assertIsArray(), assertIsBool(), assertIsFloat(), assertIsInt(), assertIsNumeric(), assertIsObject(), assertIsResource(), assertIsString(), assertIsScalar(), assertIsCallable(), or assertIsIterable() instead.

assertInternalType() 已弃用,将在 PHPUnit 9 中移除。重构您的测试以使用 assertIsArray()、assertIsBool()、assertIsFloat()、assertIsInt()、assertIsNumeric()、assertIsObject()、assertIsResource()、assertIsString()、改为 assertIsScalar()、assertIsCallable() 或 assertIsIterable()。

It is now recommended to use assertIsNumeric()or assertIsInt()for this purpose.

现在建议使用assertIsNumeric()assertIsInt()用于此目的。

回答by iateadonut

This answer is for people wondering how to make sure that the result is EITHER an integer or a string of integers. (The issue appears in some comments):

这个答案适用于想知道如何确保结果是整数或整数字符串的人们。(该问题出现在一些评论中):

$this->assertEquals( 1 , preg_match( '/^[0-9]+$/', $new_id ),
    $new_id . ' is not a set of digits' );

Here we make sure preg_match returns a '1', where preg_match is testing that all string characters are digits.

在这里,我们确保 preg_match 返回“1”,其中 preg_match 测试所有字符串字符都是数字。