HPierce HPierce - 1 year ago 465
PHP Question

Class not found only when using PHPUnit

I'm working on testing with a Symfony2 (2.7.3) app and a page controller is failing to load a class only when a request is sent from PHPUnit (4.8.6).

The test looks like this:



namespace AppBundle\Tests\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class PagesAvailableTest extends WebTestCase
public function testpages()
$client = static::createClient();
$client->request('GET', '/contact'); // Throws the error

and throws this error when run with
$ phpunit -c app/

PHP Fatal error: Class 'AppBundle\Entity\ContactMessage' not found in SymfonyRoot/src/AppBundle/Controller/ContactController.php on line 28

When called through the browser, the page loads as expected. When called using
$ curl -I http://localhost/contact
, the page's status is
HTTP/1.1 200 OK

I've looked at other similar questions that discuss problems with autoloading - but the Symfony docs suggest that classes should be autoloading without problems:

Just like in your real application - autoloading is automatically enabled via the bootstrap.php.cache file (as configured by default in the app/phpunit.xml.dist file).

<!-- app/phpunit.xml.dist -->
<?xml version="1.0" encoding="UTF-8"?>

<!-- http://phpunit.de/manual/4.1/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<testsuite name="Project Test Suite">

<!--In my experimentation, I uncommented this and tinkered with
a few values. After having no luck, I commented it out again. -->
<!--<server name="KERNEL_DIR" value="." />-->


How can I load these classes when using PHPUnit?

Answer Source

I embarrassingly overlooked a critical detail: My application is running on a different PHP interpreter than my PATH environment variable points to.

When I ran $ phpunit -c app/, it used my OS's default PHP (5.5.20) - my site is running on PHP 5.6.10 as provided by MAMP.

I was able to get around this by providing my own path to the correct PHP interpreter when invoking PHPUnit:

$ /path/to/correct/php phpunit -c app/

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download