In my PHPUnit test, I would like to assert that the class that I am testing extends another class. How can I do this with PHPUnit?
If you want to know whether or not a Class extends another, use Class#isAssignableFrom(Class) Class#isAssignableFrom(Class) also returns true if both classes are same. To find if an object is instance of a class use instanceof operator.
The assertSame() function is a builtin function in PHPUnit and is used to assert whether the actually obtained value is the same as the expected value or not. This assertion will return true in the case if the expected value is the same as the actual value else returns false.
Use assertInstanceOf()
instead of PHP's built in instanceof
operator or functions so that you get a meaningful failure message.
function testInstanceOf() {
$obj = new Foo;
self::assertInstanceOf('Bar', $obj);
}
...
Failed asserting that <Foo> is an instance of class "Bar".
Or also you should use this assert like this:
$this->assertSame(
'Symfony\Component\Form\AbstractType',
get_parent_class('AppBundle\Form\CarType'),
'The form does not extend the AbstractType class'
);
What about using instanceof?
-> http://php.net/manual/en/internals2.opcodes.instanceof.php
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With