0

I am trying to use PHPUnit testing with Laravel 5.2.10. I have PHPUnit 5.1.4 installed locally within my project.

When I run:

phpunit

PHPUnit runs all tests in my test directory perfectly.

However, when I run:

phpunit tests/unit/testThatFails.php

I get:

PHP Fatal error:  Class 'PHPUNIT_Framework_TestCase' not found in ...

The strange thing is that when I run phpunit it is succesfully running every test in the test directory, including this testThatFails.php just fine.

Why does it break when I try to run just one test?

Update:
Here is my phpunit.xml:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="bootstrap/autoload.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false">
    <testsuites>
        <testsuite name="Application Test Suite">
            <directory>./tests/</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist>
            <directory suffix=".php">app/</directory>
        </whitelist>
    </filter>
    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="sync"/>
    </php>
</phpunit>

Update:
For now, since I am using Laravel, I am circumventing this issue by extending TestCase rather than PHPUNIT_Framework_TestCase, and my tests are able to run as expected. However, this is just a circumvention and not a solution. (It is also strange to me that this works given the fact that TestCase itself does ultimately extend from PHPUNIT_Framework_TestCase.

2 Answers 2

9

The message says PHPUNIT_Framework_TestCase, but the class is actually called PHPUnit_Framework_TestCase. Now here is the tricky thing

  • Class names in PHP are case insensitive, that's why it works when you run the whole test suite and the class is already loaded. BUT
  • The autoloader is case sensitive, because your file system is case sensitive and it cannot guess the correct case.

So when you run the test separately, the class PHPUNIT_Framework_TestCase is not loaded before your test case file is included and the autoloader will look for a file PHPUNIT/Framework/TestCase.php instead of PHPUnit/Framework/TestCase.php. This file does not exist and you receive the "class not found" error.

Solution

Replace

class TestThatFails extends PHPUNIT_Framework_TestCase

with

class TestThatFails extends PHPUnit_Framework_TestCase
Sign up to request clarification or add additional context in comments.

1 Comment

ahhh.... excellent answer. I didn't even realize the difference in case until you pointed it out. Thanks for the clear explanation too. Everything is green now :)
2

PHPUnit loads the configuration file phpunit.xml (see here) that actually holds the info about the bootstrap file

...
bootstrap="tests/bootstrap.php"
...

It is the bootstrap.php that loads the autoloader thus making all of those classes available in your app.

require __DIR__.'/../vendor/autoload.php';

The autoload.php, probably, is not included in the testThatFails.php. The autoload php loads the PHPUNIT_Framework_TestCase along with other files/classes that may be relevant to this case (some other unit test classes, etc.). IMHO the best way, other than including it in the phpunit.xml, is to include the bootstrap file as an argument in command

phpunit --bootstrap bootstrap/autoload.php testThatFails.php

5 Comments

can you elaborate as to why the classes are being loaded when I run phpunit but not when I try to run a specific test?
The command you suggest is for running all tests. I can already successfully run all tests. For some reason it is when I try and single out a single test that the class is not able to be resolved. I have updated the question to include my phpunit.xml file, maybe there is an explanation hidden somewhere in there?
@tam you can specify the test you would like to run right after the bootstrap file. See my updated answer.
ah I see I thought that was probably an option, thanks. The trouble is, I still get the same error! :(
@tam what is the content of bootstrap/autoload.php ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.