phpunit icon indicating copy to clipboard operation
phpunit copied to clipboard

"getMock() on a non-object"-Call by update to PHPUnit 4.0.* with dbunit

Open andygrunwald opened this issue 12 years ago • 7 comments

I use PHPUnit with DBUnit to make some database tests. Every test works fine with PHPUnit 3.7.. After a upgrade to PHPUnit 4.0. the error occurs:

PHPUnit 4.0.7 by Sebastian Bergmann.

Configuration read from /Users/andygrunwald/Development/TYPO3-Analytics-Vagrant/Application/phpunit.xml.dist

..................................PHP Fatal error:  Call to a member function getMock() on a non-object in /Users/andygrunwald/Development/TYPO3-Analytics-Vagrant/Application/vendor/phpunit/phpunit/src/Framework/TestCase.php on line 1301
PHP Stack trace:
PHP   1. {main}() /usr/local/bin/phpunit:0
PHP   2. PHPUnit_TextUI_Command::main() /usr/local/bin/phpunit:615
PHP   3. PHPUnit_TextUI_Command->run() phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php:129
PHP   4. PHPUnit_TextUI_TestRunner->doRun() phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php:176
PHP   5. PHPUnit_Framework_TestSuite->run() /Users/andygrunwald/Development/TYPO3-Analytics-Vagrant/Application/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:419
PHP   6. PHPUnit_Framework_TestSuite->run() /Users/andygrunwald/Development/TYPO3-Analytics-Vagrant/Application/vendor/phpunit/phpunit/src/Framework/TestSuite.php:722
PHP   7. PHPUnit_Framework_TestCase->run() /Users/andygrunwald/Development/TYPO3-Analytics-Vagrant/Application/vendor/phpunit/phpunit/src/Framework/TestSuite.php:722
PHP   8. PHPUnit_Framework_TestResult->run() /Users/andygrunwald/Development/TYPO3-Analytics-Vagrant/Application/vendor/phpunit/phpunit/src/Framework/TestCase.php:764
PHP   9. PHPUnit_Framework_TestCase->runBare() /Users/andygrunwald/Development/TYPO3-Analytics-Vagrant/Application/vendor/phpunit/phpunit/src/Framework/TestResult.php:692
PHP  10. PHPUnit_Framework_TestCase->runTest() /Users/andygrunwald/Development/TYPO3-Analytics-Vagrant/Application/vendor/phpunit/phpunit/src/Framework/TestCase.php:824
PHP  11. ReflectionMethod->invokeArgs() /Users/andygrunwald/Development/TYPO3-Analytics-Vagrant/Application/vendor/phpunit/phpunit/src/Framework/TestCase.php:956
PHP  12. TYPO3Analysis\Tests\Helper\DatabaseTest->testInsertWithSuccess() /Users/andygrunwald/Development/TYPO3-Analytics-Vagrant/Application/vendor/phpunit/phpunit/src/Framework/TestCase.php:956
PHP  13. TYPO3Analysis\Tests\Helper\DatabaseTest->getDatabaseObject() /Users/andygrunwald/Development/TYPO3-Analytics-Vagrant/Application/src/TYPO3Analysis/Tests/Helper/DatabaseTest.php:190
PHP  14. PHPUnit_Framework_TestCase->getMock() /Users/andygrunwald/Development/TYPO3-Analytics-Vagrant/Application/src/TYPO3Analysis/Tests/Helper/DatabaseTest.php:95

Here is a TravisCI build where everything is fine: https://travis-ci.org/andygrunwald/TYPO3-Analytics/builds/20572239 After a PHPUnit update with composer (see https://github.com/andygrunwald/TYPO3-Analytics/compare/c526f6e36c82...37e33ce9ef0f) the TravisCI Build failes (and of course my local testing, see error message above): https://travis-ci.org/andygrunwald/TYPO3-Analytics/jobs/20637054

How to reproduce?

$ git clone --depth=50 --branch=master git://github.com/andygrunwald/TYPO3-Analytics.git andygrunwald/TYPO3-Analytics
$ cd andygrunwald/TYPO3-Analytics
$ composer install --dev
$ phpunit

With PHPUnit 3.7.* everything works fine. If you need more information please let me know.

andygrunwald avatar Mar 12 '14 20:03 andygrunwald

@andygrunwald , I may have the same issue as you on #1177 Try removing the constructor and move the logic inside the function setUp() instead

franz-deleon avatar Mar 12 '14 20:03 franz-deleon

Never, ever, implement a constructor in your test case class. And if you do (for whatever reason): call the parent constructor and pass along all arguments. Otherwise bad things are bound to happen.

sebastianbergmann avatar Mar 12 '14 20:03 sebastianbergmann

Yes! This was it. Thanks @franz-deleon and @sebastianbergmann. A simply parent::__construct();fixed the code.

@franz-deleon / @sebastianbergmann: I need a construtor to setup the database table for dbunit testcase. To move this into a setUp function does not work. @sebastianbergmann BankAccount-Example for DBUnit makes the same (define a construcor without caling its parent): https://github.com/sebastianbergmann/dbunit/blob/master/Samples/BankAccountDB/BankAccountDBTest.php

andygrunwald avatar Mar 12 '14 20:03 andygrunwald

@elazar Matthew, I think that example should be removed.

@andygrunwald The real documentation is at http://phpunit.de/manual/current/en/database.html and does not show the usage of a constructor.

sebastianbergmann avatar Mar 12 '14 21:03 sebastianbergmann

@sebastianbergmann The documentation does not show full working example (e.g. with table creation, or did i miss something?). This is the reason why i used the way which was used in the example.

I would agree with you to remove the example if the documentation is more clear with an full working example, because in my case i dont know an alternative solution how to setup the database table in https://github.com/andygrunwald/TYPO3-Analytics/blob/master/src/TYPO3Analysis/Tests/Helper/DatabaseTest.php

andygrunwald avatar Mar 12 '14 21:03 andygrunwald

I've updated the example in sebastianbergmann/dbunit@3c38aea68075a005d389b6ec0165b928ec260d66 to account for the suggestions here.

elazar avatar Mar 22 '14 19:03 elazar

@andygrunwald Per @sebastianbergmann's suggestion and the updated example code, you need to make your __construct() declaration and call to parent::__construct() here consistent with the signature from PHPUnit_Framework_TestCase.

elazar avatar Mar 22 '14 19:03 elazar