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:
//AppBundle/Tests/Controller/PagesAvailableTest.php
<?php
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"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="bootstrap.php.cache"
>
<testsuites>
<testsuite name="Project Test Suite">
<directory>../src/*/*Bundle/Tests</directory>
<directory>../src/*/Bundle/*Bundle/Tests</directory>
<directory>../src/*Bundle/Tests</directory>
</testsuite>
</testsuites>
<!--<php>-->
<!--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="." />-->
<!--</php>-->
<filter>
<whitelist>
<directory>../src</directory>
<exclude>
<directory>../src/*Bundle/Resources</directory>
<directory>../src/*Bundle/Tests</directory>
<directory>../src/*/*Bundle/Resources</directory>
<directory>../src/*/*Bundle/Tests</directory>
<directory>../src/*/Bundle/*Bundle/Resources</directory>
<directory>../src/*/Bundle/*Bundle/Tests</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
How can I load these classes when using PHPUnit?
phpunit -c app/
. The-c
option takes a file, not a directory. Perhaps you should be runningphpunit -c app/phpunit.xml.dist
. – bishopphpunit -c app/phpunit.xml.dist
doesn't seem to change anything. I am runningphpunit -c app/
because it was the suggested command in the docs I refer to. – HPierce-c
takes a directory. Try runningcomposer run-script post-update-cmd
to update the bootstrap cache file? – bishop$ composer run-script post-update-cmd
ran successfully - but I still get the same error. – HPierce