Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.:

$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

like image 271
bnp887 Avatar asked Feb 09 '14 02:02

bnp887


People also ask

What is assert in PHPUnit?

The assertEquals() function is a builtin function in PHPUnit and is used to assert whether the actual obtained value is equals to expected value or not. This assertion will return true in the case if the expected value is the same as the actual value else returns false.

How do I assert exceptions in PHPUnit?

phpunit Assertions Assert an Exception is ThrownexpectException($exception) expectExceptionMessage($message) expectExceptionCode($code)

How do I run a PHPUnit test?

How to Run Tests in PHPUnit. You can run all the tests in a directory using the PHPUnit binary installed in your vendor folder. You can also run a single test by providing the path to the test file. You use the --verbose flag to get more information on the test status.

What is mock PHPUnit?

Likewise, PHPUnit mock object is a simulated object that performs the behavior of a part of the application that is required in the unit test. The developers control the mock object by defining the pre-computed results on the actions.


1 Answers

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

Edit: As of PHPUnit 8, the answer is:

$this->assertIsInt($id); 
like image 92
Farid Movsumov Avatar answered Sep 18 '22 21:09

Farid Movsumov