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
$this->assertInternalType("int", $id);
Edit: As of PHPUnit 8, the answer is:
$this->assertIsInt($id);
I prefer using the official PHPUnit class constants.
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:
use PHPUnit\Framework\Constraint\IsType;
// ...
$this->assertInternalType(IsType::TYPE_INT, $new_id);
Original answer is given below for posterity, but I would strongly recommend using assertInternalType()
as suggested in other answers.
Original answer:
Simply use assertTrue with is_int().
$this->assertTrue(is_int($new_id));