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

Improve compatibility with framework based applications #440

Merged
merged 2 commits into from
Aug 18, 2018

Conversation

patrickfunke
Copy link
Contributor

@patrickfunke patrickfunke commented Aug 10, 2018

When using a custom bootstrap/autoloader, this file is directly required by the InfectionCommand. Applications to be tested may also use the variable names $config and $container for their own configuration and dependency injection. This will cause either the application or Infection to use the wrong config/container. With a unique name, this collision is avoided.

This PR:

  • Renames config and container to avoid naming collisions.

Copy link
Member

@theofidry theofidry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for reporting & fixing the issue!

I however doubt the current change will be enough: it does reduce the likelihood of a conflict but it's easy to miss & forget about it and introduce back the issue. I added a comment about another possible approach which I hope is simpler

@@ -170,29 +170,29 @@ protected function configure(): void

protected function execute(InputInterface $input, OutputInterface $output)
{
$container = $this->getContainer();
$infectionContainer = $this->getContainer();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the renaming is necessary here

{
$bootstrap = $config->getBootstrap();
$bootstrap = $infectionConfig->getBootstrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe instead of changing names here the bootstrap file should be included in its own scope:

$bootstrap = $config->getBootstrap();

(function ($_file) {
    // Include the file in a separate scope to avoid variable shadowing
    require_once $_file;
})($bootstrap);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or you can unset($config) before the inclusion.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a closure will be safer to use: you don't have any change to do if you add a variable or parameter; but yeah ultimately both works so let's pick one and be done with it :)

* Rename config variable to avoid naming collisions
* Rename container variable to avoid naming collisions
@maks-rafalko
Copy link
Member

I've reverted (hope @patrickfunke you don't mind) all variables renaming and added anonymous function as was suggested.

Also updated a test to proof there was an issue.

@infection/core please review again

@maks-rafalko maks-rafalko added this to the 0.11.0 milestone Aug 18, 2018
@maks-rafalko
Copy link
Member

thank you @patrickfunke

@maks-rafalko maks-rafalko merged commit 44b661c into infection:master Aug 18, 2018
sanmai pushed a commit to sanmai/infection that referenced this pull request Aug 29, 2018
* Improve compatibility with framework based applications

* Rename config variable to avoid naming collisions
* Rename container variable to avoid naming collisions

* Require bootstrap file in the separe scope to avoid variables conflicts
@patrickfunke
Copy link
Contributor Author

Please excuse that I didn't react - I was on vacation. I definitely like the solution, thanks for fixing the issue so quickly!

@maks-rafalko
Copy link
Member

No worries. I'm so glad you didn't check github notifications during vacation and didn't do any programming. This is something I should also try when I have one :)

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

Successfully merging this pull request may close these issues.

5 participants