Skip to content

When backupStaticAttributes is enable, static data from a first test case leaks to the subsequent tests cases #1

@ghost

Description

Basically, when using autoloading, the framework cannot have access to a certain class until it's autoloaded. Then, if the first test case stored something in the static scope, it will be available to all the subsequent tests cases and, if someone access the static scope, it can breaks a test with an unexpected behavior. It's an edge case.

I created some example files to show this: http://github.com/eriksencosta/phpunit-bsa-edge-case

Just run AllTests and see that the assertEquals calls fails. Then, uncommenting the 14th line from OneTest will cause the tests passes (or just checkouts the "pass" branch).

One solution I thought was to replace the registered autoloaders with rewrited copies that would call GlobalState::backupStaticAttributes() when the class was first loaded (something like what MockObject do when create the mock with a class skeleton).

For this, backupStaticAttributes could be refactored and a specific method would backup the static attributes of a newly loaded class. All the subsequent calls would behave as expected because as the class name would be returned in the get_declared_classes() call of GlobalState::backupStaticAttributes().

Maybe with the backupStaticAttributes = FALSE recommendation, this could be an unnecessary effort. But while the option exists, some people will eventually have problems with this, maybe thinking is misusing the option.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions