64

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

bnp887
  • 3,610
  • 1
  • 23
  • 27
  • 1
    Since generally ids shouldn't be zero, you'll probably also want to check that: 0 < $new_id in cases like this. – Kzqai Jul 17 '14 at 17:21

6 Answers6

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

Edit: As of PHPUnit 8, the answer is:

$this->assertIsInt($id);
bnp887
  • 3,610
  • 1
  • 23
  • 27
Farid Movsumov
  • 10,745
  • 8
  • 65
  • 92
28

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);
Francesco Casula
  • 22,369
  • 11
  • 121
  • 128
17

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));
Mike Brant
  • 66,858
  • 9
  • 86
  • 97
  • 3
    When creating tests, it's advised to go with the most specific test case. This will create better more descriptive errors. – Logan Bailey Aug 17 '16 at 19:27
5

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.

4

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

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.

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

Robson
  • 766
  • 5
  • 20
  • 36
ShadowBeast
  • 91
  • 1
  • 5
  • 1
    Thank you for this answer! I included it in the top answer as well, so it's seen straight away. – bnp887 Apr 07 '19 at 16:13
0

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.

iateadonut
  • 1,241
  • 11
  • 22