Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

PHPUnit assertInstanceOf() fails or throws a TypeError

ErrorFailed asserting that an instance of class Exception is an instance of class RuntimeException.

Short answer

assertInstanceOf() takes the expected class name as a string first and the object under test second. Passing an object, swapping the arguments, or writing a name that does not match the namespace fails. Use Foo::class, or $object::class on PHP 8, and check the use statement.

  • Applies to PHPUnit 11, PHP 8.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the exact class names that both sides resolve to. If they differ, the assertion is right and the test or code is wrong.

    var_dump(Foo::class, get_class($actual));
  2. 2Match what you see, then jump to its fix

    1. 1An object variable is the first argument of assertInstanceOf()An object was passed where the class name belongsFix
    2. 2The failure message names the classes the other way roundThe arguments are in the wrong orderFix
    3. 3Both names look right but the assertion failsThe class name resolves to another namespaceFix

What does the error look like?

  • Failed asserting that an instance of class Exception is an instance of class RuntimeException.
  • assertInstanceOf() passes in one test and fails in another although the object looks right

Cause 1 of 3

An object was passed where the class name belongs

1Check

The signature is assertInstanceOf(string $expected, mixed $actual). The expected value is the class name, the actual value is the object.

$this->assertInstanceOf($expectedObject, $actual);   // wrong

2Fix

Turn the object into its class name. ::class on an object works since PHP 8.0. For a known class, write Foo::class. ::class on a value that is not an object, a string variable included, throws a TypeError, so pass the string itself.

$this->assertInstanceOf($expectedObject::class, $actual);
$this->assertInstanceOf(Foo::class, $actual);

3Verify

The test runs and passes or fails on the real comparison.

vendor/bin/phpunit --filter testName

Confirmed by docs.phpunit.de

Did this fix it?

Cause 2 of 3

The arguments are in the wrong order

1Check

The failure text reads: Failed asserting that an instance of class A is an instance of class B, where A is the actual object and B the expected class. If A is the one you expected, the order is swapped.

vendor/bin/phpunit --filter testName

2Fix

Put the expected class first and the value under test second.

$this->assertInstanceOf(RuntimeException::class, $actual);

3Verify

The message now describes the real mismatch, or the test passes.

vendor/bin/phpunit --filter testName

Confirmed by docs.phpunit.de

Did this fix it?

Cause 3 of 3

The class name resolves to another namespace

1Check

Foo::class is resolved at compile time, by the namespace and use statements of the test file, and no autoloading happens. A missing use statement gives a name that points to a class that does not exist, and PHP issues no error.

var_dump(Foo::class);

2Fix

Add the correct use statement, or write the full name with a leading backslash.

use App\Service\Foo;
$this->assertInstanceOf(Foo::class, $actual);

3Verify

var_dump prints the fully qualified name you expect.

var_dump(Foo::class);

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Always use Foo::class, never a hand-typed string.
  • Keep the expected value first and the value under test second in every assertion.
  • Read the failure message: it prints both class names.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.