PHPUnit: assertInstanceOf() not working PHPUnit: assertInstanceOf() not working php php

PHPUnit: assertInstanceOf() not working


It's always a good idea to use ::class wherever you can. If you get used to this standard, you don't have to use FQCNs (fully qualified classnames), or escape backslashes. Also, IDEs provide better functionality if they know that User here is not just a string, but rather a class.

$this->assertInstanceOf(User::class, $user);


Or You can use something like:

$this->assertInstanceOf(get_class($expectedObject), $user);

I usually use this when I'm checking i.e. if setter method is returning reference to self.

$testedObj = new ObjectToTest();$this->assertInstanceOf(    get_class($testedObj),     $testedObj->setSomething('someValue'),    'Setter is not returning $this reference');