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

Protect against Runkit (re-)definitions happening before fixtures run #14

Closed
stevegrunwell opened this issue Oct 21, 2020 · 0 comments · Fixed by #16
Closed

Protect against Runkit (re-)definitions happening before fixtures run #14

stevegrunwell opened this issue Oct 21, 2020 · 0 comments · Fixed by #16
Labels
bug Something isn't working enhancement New feature or request
Milestone

Comments

@stevegrunwell
Copy link
Member

Ran into this on project that implements this library:

Imagine we have a test class that looks like this:

<?php

namespace Tests;

use AssertWell\PHPUnitGlobalState\Constants;
use PHPUnit\Framework\TestCase;

class MyTest extends TestCase
{
    use Constants;

    /**
     * @before
     */
    protected function defineInitialConstants(): void
    {
        $this->setConstant('SOME_CONSTANT', 'some value');
    }
}

In some versions of PHPUnit (in my experience, 5.x for PHP 5.6), MyTest::defineInitialConstants() will be executed before the Constants::resetConstants() fixture, meaning the following steps happen in order:

  1. The SOME_CONSTANT constant gets defined, and its name tracked in $_constants (implicitly creating the array structure)
  2. Constants::resetConstants() executes, overwriting the $_constants property with a properly-formatted array
  3. The test method executes, with SOME_CONSTANT defined as expected
  4. The Constants::restoreConstants() fixture runs, but sees no constants in $_constants to restore and thus does nothing

If a fixture attempts to define a constant (or, once #11 and #12 are completed, a function/method), an error should be thrown if this occurs before the @before fixture has run.

@stevegrunwell stevegrunwell added bug Something isn't working enhancement New feature or request labels Oct 21, 2020
@stevegrunwell stevegrunwell added this to the Version 0.2.0 milestone Oct 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant