doctrine-test-bundle icon indicating copy to clipboard operation
doctrine-test-bundle copied to clipboard

Not working with PHPUnit process isolation

Open Growiel opened this issue 7 years ago • 8 comments
trafficstars

Hello,

I configured the bundle as per the documentation (add the bundle AppKernel, add the listener in phpunit.xml.dist...) but it doesn't work.

From my own debugging of the bundle, the issue is that when StaticDriver::connect() is called, self::$keepStaticConnections is false, which makes this bundle do absolutely nothing.

I did find the place where the keepStaticConnections is supposed to be set to true in PHPUnitListener, but for some reason the change doesn't stay.

I dumped all the relevant places, the variables goes to true in the Listener but it's back to false when connect() is called.

Changing line 23 of StaticDriver from private static $keepStaticConnections = false; to private static $keepStaticConnections = true; works.

I'm using PHPUnit 6.5 and PHP 7.2. Symfony is 3.4.

What am I missing ? Why is the static value change not sticking ?

Thanks.

Growiel avatar Oct 10 '18 16:10 Growiel

Hmm that is interesting :confused:

How exactly are you running your tests? Are you doing some parallel execution in multiple php processes?

If you could somehow give me a minimal reproducer I could also have a look myself.

dmaicher avatar Oct 11 '18 06:10 dmaicher

Hello, thanks for the quick response!

I actually just found out what's happening: my test was running with @runIngSeparateProcess, and it looks like when this happens, the keepStaticConnections variable is not set to true for the separate process.

Removing the @runIngSeparateProcess makes it work. I tweaked my original code so I do not require @runIngSeparateProcess, but if you somehow can make it work with it, it would be neat for the people who do need to use it.

thanks.

Growiel avatar Oct 11 '18 06:10 Growiel

Ok I see :wink:

One idea could be to put this call

https://github.com/dmaicher/doctrine-test-bundle/blob/master/src/DAMA/DoctrineTestBundle/PHPUnit/PHPUnitListener.php#L23

here

https://github.com/dmaicher/doctrine-test-bundle/blob/master/src/DAMA/DoctrineTestBundle/PHPUnit/PHPUnitListener.php#L13

dmaicher avatar Oct 11 '18 14:10 dmaicher

I had to create ulgy class which override base test client.

<?php

namespace App\Test;

use Symfony\Bundle\FrameworkBundle\Client;

class TestClient extends Client
{
    public function getHandleScript()
    {
        // due to: https://github.com/dmaicher/doctrine-test-bundle/issues/66
        // we have to set keep static connections for scripts which are called in separated process
        return
            '\DAMA\DoctrineTestBundle\Doctrine\DBAL\StaticDriver::setKeepStaticConnections(true);' .
            parent::getHandleScript();
    }
}

Now everything works fine.

piszczek avatar Jun 04 '19 10:06 piszczek

Issue still exists


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

<!-- https://phpunit.readthedocs.io/en/latest/configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
         colors="true"
         bootstrap="tests/bootstrap.php"
         beStrictAboutCoversAnnotation="true"
         beStrictAboutOutputDuringTests="true"
         verbose="true"
         beStrictAboutChangesToGlobalState="true"
         beStrictAboutTodoAnnotatedTests="true"
>
    <!--         processIsolation="true"-->
    <php>
        <ini name="error_reporting" value="-1" />
        <server name="APP_ENV" value="test" force="true" />
        <server name="SHELL_VERBOSITY" value="-1" />
    </php>

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

    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">src</directory>
        </whitelist>
    </filter>

    <!-- Add this for PHPUnit 7.5 or higher -->
    <extensions>
        <extension class="DAMA\DoctrineTestBundle\PHPUnit\PHPUnitExtension"/>
    </extensions>
</phpunit>


{
    "type": "project",
    "license": "proprietary",
    "require": {
        "php": "^7.1.3",
        "ext-ctype": "*",
        "ext-iconv": "*",
        "ext-json": "*",
        "drenso/phan-extensions": "^2.5",
        "friendsofsymfony/rest-bundle": "^2.7",
        "nelmio/api-doc-bundle": "^3.5",
        "phan/phan": "^2.5",
        "ramsey/uuid": "^3.9",
        "sensio/framework-extra-bundle": "^5.5",
        "symfony/browser-kit": "4.4.*",
        "symfony/console": "4.4.*",
        "symfony/dotenv": "4.4.*",
        "symfony/flex": "^1.3.1",
        "symfony/framework-bundle": "4.4.*",
        "symfony/monolog-bundle": "^3.5",
        "symfony/options-resolver": "4.4.*",
        "symfony/orm-pack": "^1.0",
        "symfony/security-bundle": "4.4.*",
        "symfony/serializer": "4.4.*",
        "symfony/serializer-pack": "^1.0",
        "symfony/yaml": "4.4.*"
    },
    "require-dev": {
        "ext-json": "*",
        "dama/doctrine-test-bundle": "^6.3",
        "doctrine/doctrine-fixtures-bundle": "^3.3",
        "phpdocumentor/reflection-docblock": "^4.0",
        "phpunit/phpunit": "^8.5",
        "symfony/maker-bundle": "^1.14"
    },
    "config": {
        "preferred-install": {
            "*": "dist"
        },
        "sort-packages": true
    },
    "autoload": {
        "psr-4": {
            "App\\": "src/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "App\\Tests\\": "tests/"
        }
    },
    "replace": {
        "paragonie/random_compat": "2.*",
        "symfony/polyfill-ctype": "*",
        "symfony/polyfill-iconv": "*",
        "symfony/polyfill-php71": "*",
        "symfony/polyfill-php70": "*",
        "symfony/polyfill-php56": "*"
    },
    "scripts": {
        "auto-scripts": {
            "cache:clear": "symfony-cmd",
            "assets:install %PUBLIC_DIR%": "symfony-cmd"
        },
        "post-install-cmd": [
            "@auto-scripts"
        ],
        "post-update-cmd": [
            "@auto-scripts"
        ]
    },
    "conflict": {
        "symfony/symfony": "*"
    },
    "extra": {
        "symfony": {
            "allow-contrib": false,
            "require": "4.4.*"
        }
    }
}

P.S. I saw "feature" label, I think this I faced this long ago and mentioned already. At least please mention that in docs that it DOES NOT WORK WITH PROCESS ISOLATION.

BonBonSlick avatar Mar 02 '20 07:03 BonBonSlick

@BonBonSlick I'm not personally using process isolation in any of my projects. If you are interested in supporting it then feel free to dig into it and create a PR. Also feel free to suggest any Readme update for this issue with a PR :wink:

dmaicher avatar Mar 04 '20 10:03 dmaicher

@dmaicher Hello, I'm having a similar problem. I already have a solution (in my code) for my use case but I wonder if you would consider the following idea.

Since it is sometimes painful to set StaticDriver::setKeepStaticConnections(true) before a connection is already opened, maybe there could be an option in the configuration to make true the default value so that it is set by the bundle itself. Something like that: https://github.com/jeanlucc/doctrine-test-bundle/commit/9d47c36b61cd8e43925dad1e19113aff33942700

My solution involves using a custom index.php to perform StaticDriver::setKeepStaticConnections(true)

jeanlucc avatar Jun 10 '20 18:06 jeanlucc

I've just added your bundle to an older Symfony 4.4 project to add some first tests.

I went strictly by the official Symfony documentation and it simply does not work because of this issue :(

I already had overwritten WebTestCase::createClient and could add a StaticDriver::setKeepStaticConnections(true); call in there, but that's not really a good solution.

Why is that boolean in there at all? Are there connections to be expected outside of the tests?


dama/[email protected] [email protected]

uncaught avatar Jul 17 '22 16:07 uncaught