Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 331
thom Avatar asked Nov 28 '22 09:11

thom


2 Answers

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.

like image 138
Alister Bulman Avatar answered Dec 06 '22 00:12

Alister Bulman


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
{
    // ...
}
like image 42
netcoder Avatar answered Dec 05 '22 23:12

netcoder