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

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


$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));