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.
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.
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
{
// ...
}
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