4

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?

8
  • Your OP states you run phpunit -c app/. The -c option takes a file, not a directory. Perhaps you should be running phpunit -c app/phpunit.xml.dist. Commented Sep 2, 2015 at 20:39
  • I appreciate the tip - but running phpunit -c app/phpunit.xml.dist doesn't seem to change anything. I am running phpunit -c app/ because it was the suggested command in the docs I refer to. Commented Sep 2, 2015 at 20:45
  • Curious on those docs, I didn't know (and am dubious) that -c takes a directory. Try running composer run-script post-update-cmd to update the bootstrap cache file? Commented Sep 2, 2015 at 20:52
  • Nope, I edited my comment. Try again with composer. Commented Sep 2, 2015 at 20:55
  • $ composer run-script post-update-cmd ran successfully - but I still get the same error. Commented Sep 2, 2015 at 21:02

1 Answer 1

6

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/

Sign up to request clarification or add additional context in comments.

Comments

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.