mercredi 14 septembre 2016

How to test exceptions using a data provider in PHPUnit?

PHPUnit has a very useful feature @dataProvider, that allows to test multiple cases of a method. It also has another cool annotation -- @expectedException to ensure the application throws a correct Exception at a defined place.

I'm currently testing a method against multiple edge cases and would like to combine these two features like this (not working code):

class TestMyClass
{
    /**
     * @dataProvider provideDataForFoo
     */
    public function testFoo($paramBar, $paramBuz, $expected)
    {
        $this->assertEquals($expected, $paramBar, $paramBuz);
    }
    public function provideDataForFoo()
    {
        return [
            ['expected lorem', 'bar lorem', 'buz lorem'],
            ['expected ipsum', 'bar ipsum', 'buz ipsum'],
            ['expected exception', 'bar invalid argument', 'buz invalid argument'],
        ];
    }
}

Is possible / How to use @expectedException as one of the cases, when using @dataProvider?

Aucun commentaire:

Enregistrer un commentaire