thom
thom

Reputation: 43

PHPUnit: dataProvider issue

What's wrong with the following test:

<?php

class TestSomething extends PHPUnit_Framework_TestCase
{
    /**
     * @dataProvider provider
     */
    public function testSomething($array, $expectedResult)
    {
        $this->assertEquals($array, $expectedResult);
    }

    public function provider()
    {
        return array(
            array(array(), array()),
        );
    }
}

?>

Error message:

$phpunit index.php
PHP Warning:  Missing argument 1 for TestSomething::testSomething() in /var/www/tests/something-test/index.php on line 8
PHP Warning:  Missing argument 2 for TestSomething::testSomething() in /var/www/tests/something-test/index.php on line 8
PHP Notice:  Undefined variable: array in /var/www/tests/something-test/index.php on line 11
PHP Notice:  Undefined variable: expectedResult in /var/www/tests/something-test/index.php on line 11
PHP Fatal error:  Uncaught exception 'PHPUnit_Framework_ExpectationFailedException' with message 'Failed asserting that 
Array
(
    [0] => Array
        (
        )

    [1] => Array
        (
        )

)
 is equal to <string:testSomething>.' in /usr/share/php/PHPUnit/Framework/Constraint/IsEqual.php:164
Stack trace:
#0 /usr/share/php/PHPUnit/Framework/Assert.php(2087): PHPUnit_Framework_Constraint_IsEqual->fail(Array, '')
#1 /usr/share/php/PHPUnit/Framework/Assert.php(343): PHPUnit_Framework_Assert::assertThat(Array, Object(PHPUnit_Framework_Constraint_IsEqual), '')
#2 /var/www/tests/something-test/index.php(11): PHPUnit_Framework_Assert::assertEquals('testSomething', Array)
#3 /usr/share/php/PHPUnit/Framework/TestSuite.php(537): TestSomething->testSomething('testSomething', Array, 0)
#4 /usr/share/php/PHPUnit/Framework/TestSuite.php(816): PHPUnit_Framework_TestSuite::createTest(Object(ReflectionClass), 'testSomething')
#5 /usr/share/php/PHPUnit/Framework/TestSuite.php(224): PHPUnit_Framework_TestSuite->addTestMethod(Object(ReflectionClass), Object(Reflectio in /usr/share/php/PHPUnit/Framework/Constraint/IsEqual.php on line 164

Thanks.

Upvotes: 4

Views: 5538

Answers (4)

Shadi
Shadi

Reputation: 10335

For future-comers arriving here because phpunit ran the data provider function as a test and showed a "risky test" flag with This test did not perform any assertions, it seems that since phpunit 6 (maybe 6.3?) phpunit no longer ignores the "test" prefix in the data provider functions, e.g. testAdditionProvider. Renaming it to additionProvider as in the current docs works. I'm not 100% sure though.

Upvotes: 7

Alister Bulman
Alister Bulman

Reputation: 35139

I've also just gotten bit by much the same thing, I was using the __construct() method to setup internal variables.

What I needed to do instead was have a function setUp() {} where that would happen.


I've just fallen over this problem again - but this time the problem was the comment - I had used:

/*
 * @dataProvider ....
 */

But the comment has to start with /** to be recognised.

Upvotes: 13

them_nl
them_nl

Reputation: 1

If you're using PHP pre-5.3.3:

Are you giving the variables on establishing the class? Because your class and function name (TestSomething/testSomething) are the same (case-insensitive). So it sees the function testSomething as as constructor.

Upvotes: 0

netcoder
netcoder

Reputation: 67695

It's because your test is also being executed as the constructor:

class TestSomething extends PHPUnit_Framework_TestCase
{
    /**
     * @dataProvider provider
     */
    public function testSomething($array, $expectedResult)
    {
        $this->assertEquals($array, $expectedResult);
    }

    // ...

}

For PHP4 compatibility, you can use the class name as the method name to declare a constructor. It is also done in a case insensitive manner (i.e.: testSomething() is considered a constructor to TestSomething). Usually, you will append the Test keyword to your class name to prevent that from happening (instead of prepending):

class SomethingTest extends PHPUnit_Framework_TestCase
{
    // ...
}

Upvotes: 11

Related Questions