Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Unit Testing problems


Go to End


7 Posts   5832 Views

Avatar
geekdenz

Community Member, 37 Posts

2 September 2014 at 5:06pm

Our team is attempting to setup PHPUnit testing for an existing Silverstripe project. We have encountered issues with running tests.

Problem

The bootstrap is not finding the Silverstripe PHPUnit specific classes

Details

OS: Ubuntu 12.04
Silverstripe was installed not using composer
Installed PHPUnit through phar method
Run phpunit from the browser (fails with no phpunit is found)
Run phpunit from the command line (fails with the following error)

PHP Fatal error:  Class 'SapphireTest' not found in /var/www/aep-ss/mysite/tests/FastTest.php on line 26

Summary

Since its an existing project, we don't want to use composer to install phpunit (it shouldn't be necessary). We think there is a problem with the boostrap not loading the necessary php files as when adding *SapphireTest* to *vendor/composer/autoload_classmap.ph* it resolved that error. However, it introduced another error (missing the next php class).
We have also tried installation of phpunit which resulted in the same errors via
apt-get
pear

Questions

Is there a possible problem with the boostrap?
Are there any solutions not using composer to setting up PHPUnit testing?

Avatar
kinglozzer

Community Member, 187 Posts

2 September 2014 at 10:32pm

Have you got a phpunit.xml in your document root? I think I’ve had that happen before when it’s missing: https://github.com/silverstripe/silverstripe-installer/blob/3.1/phpunit.xml.dist

Avatar
geekdenz

Community Member, 37 Posts

3 September 2014 at 9:35am

Thanks, but the phpunit.xml file is there. It has problems loading the classes. I'm writing my own autoload function, but posted this because it seems like there must be a better/easier way.

Avatar
geekdenz

Community Member, 37 Posts

3 September 2014 at 1:38pm

I wrote a CustomAutoloader class and added it to the queue. Now it loads the classes successfully. However, my tests don't run because DB::getConn()->currentDatabase() gives this error:

PHP Fatal error: Call to a member function currentDatabase() on a non-object in /var/www/aep-ss/mysite/tests/FastTest.php on line 103
PHP Stack trace:
PHP 1. {main}() /usr/local/bin/phpunit:0
PHP 2. PHPUnit_TextUI_Command::main() /usr/local/bin/phpunit:599
PHP 3. PHPUnit_TextUI_Command->run() phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php:138
PHP 4. PHPUnit_TextUI_TestRunner->doRun() phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php:186
PHP 5. PHPUnit_Framework_TestSuite->run() phar:///usr/local/bin/phpunit/phpunit/TextUI/TestRunner.php:423
PHP 6. PHPUnit_Framework_TestCase->run() phar:///usr/local/bin/phpunit/phpunit/Framework/TestSuite.php:703
PHP 7. PHPUnit_Framework_TestResult->run() phar:///usr/local/bin/phpunit/phpunit/Framework/TestCase.php:771
PHP 8. PHPUnit_Framework_TestCase->runBare() phar:///usr/local/bin/phpunit/phpunit/Framework/TestResult.php:643
PHP 9. FastTest->setUp() phar:///usr/local/bin/phpunit/phpunit/Framework/TestCase.php:831

FastTest is a class that I wrote to cache the test DB, so it won't have to initialise the DB every time. I will try to find the bootstrap for the DB.

Question

Is there something I can just include in the bootstrap file that initialises the DB connection?

Avatar
geekdenz

Community Member, 37 Posts

4 September 2014 at 9:36am

OK, fixed the DB error. Now the next problem:

PHP Fatal error: Call to a member function getItemPath() on a non-object in framework/dev/SapphireTest.php on line 451
PHP Stack trace:
PHP 1. {main}() /usr/local/bin/phpunit:0
PHP 2. PHPUnit_TextUI_Command::main() /usr/local/bin/phpunit:599
PHP 3. PHPUnit_TextUI_Command->run() phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php:138
PHP 4. PHPUnit_TextUI_TestRunner->doRun() phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php:186
PHP 5. PHPUnit_Framework_TestSuite->run() phar:///usr/local/bin/phpunit/phpunit/TextUI/TestRunner.php:423
PHP 6. PHPUnit_Framework_TestCase->run() phar:///usr/local/bin/phpunit/phpunit/Framework/TestSuite.php:703
PHP 7. PHPUnit_Framework_TestResult->run() phar:///usr/local/bin/phpunit/phpunit/Framework/TestCase.php:771
PHP 8. PHPUnit_Framework_TestCase->runBare() phar:///usr/local/bin/phpunit/phpunit/Framework/TestResult.php:643
PHP 9. FastTest->setUp() phar:///usr/local/bin/phpunit/phpunit/Framework/TestCase.php:831
PHP 10. SapphireTest->getCurrentAbsolutePath() /var/www/aep-ss/mysite/tests/FastTest.php:123

Avatar
kinglozzer

Community Member, 187 Posts

4 September 2014 at 10:58pm

You definitely shouldn’t need to write your own autoloader.

Does your phpunit.xml point to the right bootstrap file (framework/tests/bootstrap.php)? Judging by that error, the class manifest isn’t being built (would also explain the error about SapphireTest not being found), looks like bootstrap.php is responsible for triggering that.

Also, which version of PHPUnit are you using?

Avatar
geekdenz

Community Member, 37 Posts

5 September 2014 at 9:21am

That's what I thought.

BUT

PHPUnit 4.2.4 by Sebastian Bergmann.

My own auto loader now works and unit tests work. The problem, I think, is that we're not using Composer. I looked in the source code of framework/tests/bootstrap.php and it seems like it is generated by composer, but since we're not using that (we had major problems with it) it doesn't find the classes any more. And yes, we were using the bootstrap.php, but it didn't work. PHPUnit is not recommended to be installed via Pear any longer by the PHPUnit team.

So, I think the documentation and code for unit testing needs to be updated.