PHPUnit assertInstanceOf() fails or throws a TypeError
Failed 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.
Fix it fast
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));2Match what you see, then jump to its fix
- 1
An object variable is the first argument of assertInstanceOf()An object was passed where the class name belongsFix - 2
The failure message names the classes the other way roundThe arguments are in the wrong orderFix - 3
Both names look right but the assertion failsThe class name resolves to another namespaceFix
- 1
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); // wrong2Fix
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 testNameConfirmed by docs.phpunit.de
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 testName2Fix
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 testNameConfirmed by docs.phpunit.de
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
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
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack 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 fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fix