I am using symfony 3.4 for some of our projects. According to the docs, we should write functional tests for ours controllers. However, it seems that we cannot collect code coverage for functional test because symfony will run main code in another PHP processs.

  • In phpunit process: var_dump(xdebug_code_coverage_started()) => true
  • In controller code: var_dump(xdebug_code_coverage_started()) => false.

UPDATE: It seems that the problem only occurs when I put tests in separate folder to src folder. It works when I put tests in the same bundle folder (src/MyBubndle/Tests)

PHP Unit config

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.0/phpunit.xsd"
         backupGlobals="false"
         colors="true"
         processIsolation = "false"
         bootstrap="vendor/autoload.php"
>
    <php>
        <ini name="error_reporting" value="-1" />
        <server name="KERNEL_DIR" value="app/" />
        <env name="SYMFONY_DEPRECATIONS_HELPER" value="weak"/>
        <!--<env name="KERNEL_CLASS" value="App\Kernel" />-->
        <env name="APP_ENV" value="test"/>
        <env name="APP_DEBUG" value="1"/>
        <env name="SYMFONY_ENV" value="test"/>
        <env name="MYSQL_SERVER" value="127.0.0.1"/>
        <env name="MYSQL_DATABASE" value="db"/>
        <env name="MYSQL_USER" value="root"/>
        <env name="MYSQL_PASSWORD" value="root"/>
        <env name="MYSQL_PORT" value="3306"/>
        <env name="JWT_PUBLIC_KEY_PATH" value="jwt/public.pem"/>
        <env name="SYMFONY_PHPUNIT_VERSION" value="6.5.5"/>
    </php>

    <testsuites>
        <testsuite name="Project Test Suite">
            <directory suffix="Test.php" >.</directory>
            <exclude>vendor</exclude>
        </testsuite>
    </testsuites>

    <filter>
        <whitelist>
            <directory suffix=".php">.</directory>
            <exclude>
                <directory>src/*Bundle/Resources</directory>
                <directory>src/*/*Bundle/Resources</directory>
                <directory>src/*/Bundle/*Bundle/Resources</directory>
                <directory>app</directory>
                <directory>bin</directory>
                <directory>docker</directory>
                <directory>jwt</directory>
                <directory>var</directory>
                <directory>vendor</directory>
                <directory>web</directory>
                <directory>tests</directory>
            </exclude>
        </whitelist>
    </filter>
    <logging>
        <log type="coverage-html" target="./var/logs/codeCoverage" charset="UTF-8"
             yui="true" highlight="true"
             lowUpperBound="50" highLowerBound="80"/>
        <log type="coverage-text" target="php://stdout" showOnlySummary = "true" showUncoveredFiles="false"/>
        <log type="testdox-html" target="./var/logs/testdox.html" />
        <log type="junit" target="./var/logs/testreport.xml" logIncompleteSkipped="false"/>
        <log type="coverage-clover" target="./var/logs/coverage.xml"/>
    </logging>
    <!--<listeners>-->
        <!--<listener class="Symfony\Bridge\PhpUnit\CoverageListener" />-->
    <!--</listeners>-->
</phpunit>

Also tried:

<?xml version="1.0" encoding="UTF-8"?>

<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.0/phpunit.xsd"
         backupGlobals="false"
         colors="true"
         processIsolation = "false"
         bootstrap="vendor/autoload.php"
>
    <php>
        <ini name="error_reporting" value="-1" />
        <server name="KERNEL_DIR" value="app/" />
        <env name="SYMFONY_DEPRECATIONS_HELPER" value="weak"/>
        <!--<env name="KERNEL_CLASS" value="App\Kernel" />-->
        <env name="APP_ENV" value="test"/>
        <env name="APP_DEBUG" value="1"/>
        <env name="SYMFONY_ENV" value="test"/>
        <env name="MYSQL_SERVER" value="127.0.0.1"/>
        <env name="MYSQL_DATABASE" value="db"/>
        <env name="MYSQL_USER" value="root"/>
        <env name="MYSQL_PASSWORD" value="root"/>
        <env name="MYSQL_PORT" value="3306"/>
        <env name="JWT_PUBLIC_KEY_PATH" value="jwt/public.pem"/>
        <env name="SYMFONY_PHPUNIT_VERSION" value="6.5.5"/>
    </php>

    <testsuites>
        <testsuite name="Project Test Suite">
            <directory suffix="Test.php" >./tests</directory>
        </testsuite>
    </testsuites>

    <filter>
        <whitelist>
            <directory suffix=".php">./src</directory>
            <exclude>
                <directory>src/*Bundle/Resources</directory>
                <directory>src/*/*Bundle/Resources</directory>
                <directory>src/*/Bundle/*Bundle/Resources</directory>
               </exclude>
        </whitelist>
    </filter>
    <logging>
        <log type="coverage-html" target="./var/logs/codeCoverage" charset="UTF-8"
             yui="true" highlight="true"
             lowUpperBound="50" highLowerBound="80"/>
        <log type="coverage-text" target="php://stdout" showOnlySummary = "true" showUncoveredFiles="false"/>
        <log type="testdox-html" target="./var/logs/testdox.html" />
        <log type="junit" target="./var/logs/testreport.xml" logIncompleteSkipped="false"/>
        <log type="coverage-clover" target="./var/logs/coverage.xml"/>
    </logging>
    <!--<listeners>-->
        <!--<listener class="Symfony\Bridge\PhpUnit\CoverageListener" />-->
    <!--</listeners>-->
</phpunit>

Is there any workaround for this without using other testing framework?

All helps are highly appreciated!

upvote
  flag
Please edit your question to include the contents of phpunit.xml. – geoB
upvote
  flag
@geoB I updated my phpunit config – Pha
upvote
  flag
try <directory suffix=".php">./src/*</directory> or <directory suffix=".php">./*</directory> on phpunit.xml – Marçal Berga
upvote
  flag
tried ./*, it still not woking – Pha

0 Answers 11

Not the answer you're looking for? Browse other questions tagged or ask your own question.