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

Running with --process-isolation breaks semantics of setUpBeforeClass() #8

Closed
jakubkulhan opened this issue Aug 19, 2010 · 2 comments
Closed

Comments

@jakubkulhan
Copy link

With --process-isolation setUpBeforeClass() is not run before tests, but instead for each test.

<?php
require 'PHPUnit/Framework/TestCase.php';

class FooTest extends PHPUnit_Framework_TestCase
{
    static function setUpBeforeClass()
    {
        echo "setUpBeforeClass, pid=" . getmypid() . "\n";
    }

    function testA()
    {
        echo "testA, pid=" . getmypid() . "\n";
        $this->assertTrue(TRUE);
    }

    function testB()
    {
        echo "testB, pid=" . getmypid() . "\n";
        $this->assertTrue(TRUE);
    }
}

Expected output:

PHPUnit 3.5.0RC1 by Sebastian Bergmann.

setUpBeforeClass, pid=23871
testA, pid=23872
.testB, pid=23873
.

Time: 0 seconds, Memory: 3.50Mb

OK (2 tests, 2 assertions)

Actual output:

PHPUnit 3.5.0RC1 by Sebastian Bergmann.

setUpBeforeClass, pid=23882
setUpBeforeClass, pid=23883
testA, pid=23883
.setUpBeforeClass, pid=23884
testB, pid=23884
.

Time: 1 second, Memory: 3.50Mb

OK (2 tests, 2 assertions)
@sebastianbergmann
Copy link
Owner

That is the expected behavior for process isolation.

@jakubkulhan
Copy link
Author

OK, I see.

How do you suggest to test cache that can make item depedent on presence/content of a constant? If setUpBeforeClass() was called only once per testcase, it would be simple:

<?php
require 'PHPUnit/Framework/TestCase.php';

class CacheTest extends PHPUnit_Framework_TestCase
{
    static $cache;

    static function setUpBeforeClass()
    {
        self::$cache = new Cache(TEMP_DIR);
        self::$cache->clean(); // removes all data from cache (and that is the problem,
                               // because it will be called before
                               // testChangeOfConstantWillRemoveItemFromCache, so 
                               // the test will say everything is OK even if it may 
                               // not be)
    }

    /**
     * @runInSeparateProcess
     */
    function testSaveItemThatDependsOnConstant()
    {
        define('FOO', 'bar');

        self::$cache->save(
            'key', 'value',
            self::$cache->constantDependency('FOO') // makes item saved under
                                                    // key 'key' dependent
                                                    // on constant FOO
        );

        $this->assertTrue(self::$cache->isCached('key'));
        $this->assertEquals(self::$cache->read('key'), 'value');
    }

    /**
     * @depends testSaveItemThatDependsOnConstant
     * @runInSeparateProcess
     */
    function testChangeOfConstantWillRemoveItemFromCache()
    {
        define('FOO', 'not bar');

        $this->assertFalse(
            self::$cache->isCached('key') // because content of FOO has changed,
                                          // item should not be in cache anymore
        );
    }
}

glensc pushed a commit to glensc/php-phpunit that referenced this issue Oct 4, 2022
* replace travis with github actions

Additionally:

* php 8.x compat - trigger_error

* php 8.x compat - JSON_ERROR_NONE

* php 8.x compat - unsupported phpunit-token-stream lib

Codeception/Codeception#6074
sebastianbergmann/php-token-stream@a853a0e

* github actions - enable coverage for php 7.4 only
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants