简体   繁体   中英

PHPUnit: How to assert that a class extends another class?

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?

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'
        );

is_subclass_of() (或者可能是is_a() )可能就是你要找的东西。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM