Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not working with PHPUnit process isolation #66

Open
Growiel opened this issue Oct 10, 2018 · 8 comments
Open

Not working with PHPUnit process isolation #66

Growiel opened this issue Oct 10, 2018 · 8 comments

Comments

@Growiel
Copy link

Growiel commented Oct 10, 2018

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.

@dmaicher
Copy link
Owner

Hmm that is interesting 😕

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.

@Growiel
Copy link
Author

Growiel commented Oct 11, 2018

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.

@dmaicher dmaicher changed the title Can't get this to work. Not working with @runInSeparateProcess Oct 11, 2018
@dmaicher dmaicher changed the title Not working with @runInSeparateProcess Not working with PHPUnit process isolation Nov 22, 2018
@piszczek
Copy link

piszczek commented Jun 4, 2019

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.

@BonBonSlick
Copy link

BonBonSlick commented Mar 2, 2020

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.

@dmaicher
Copy link
Owner

dmaicher commented Mar 4, 2020

@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 😉

@jeanlucc
Copy link

@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)

@uncaught
Copy link

uncaught commented Jul 17, 2022

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/doctrine-test-bundle@6.7.5
PHPUnit@9.5.21

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants