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

Injection Inception, Change #4: Using DI as primary mechanism in setting up Test environment #8008

Merged
merged 19 commits into from Jun 3, 2015

Conversation

diosmosis
Copy link
Member

Includes various changes to how the test environment is setup, focusing on using DI as the primary overriding mechanism.

Changes include:

  • Moved all static DI config overrides from TestingEnvironment.php to config/environment/test.php.
  • Slightly cleaned up the test event observers (now defined in config/environment/test.php) by merging some together.
  • Unified test environment logic for unit/integration/system tests & UI tests. TestingEnvironment.php was created initially for UI tests only, so there were clashes when doing this. For example, UI tests requires translations to be loaded, but they should not be loaded (by default) for other tests. To solve these conflicts, I added new TestingEnvironment properties that let the system default to unit/integration/system test behaviour, and let UI tests override the behaviour. The UI test overrides are added in test-environment.js.
  • Modified StaticContainer::addDefinitions() to stack. Instead of holding one array of definitions, it now holds an array of definition arrays. In ContainerFactory, we loop through the outer array and add each definition array on one by one. This allows us to override observers.global in individual tests.
  • Moved the TestingEnvironment class to the Piwik\Tests\Framework namespace.
  • The test.php environment file is now applied automatically if PIWIK_TEST_MODE is defined. This means we don't have to inject the 'test' environment string into normal Piwik entry points AND means we can combine the test environment w/ other environments (like the tracker one).
  • Fixed an issue w/ provideContainerConfigBeforeClass not being recognized by TestingEnvironment.php. The testCaseClass TestingEnvironment property was not set in Fixture.php, so it didn't get propagated to other child processes.
  • Added the method FakeAccess::clearAccess() so it could be explicitly cleared.
  • Added TestingEnvironment to DI (TestConfig now gets it from the constructor instead of creating an instance).

Everything else are fixes for tests. Individual commit messages should explain what the changes were and why they were added.

@diosmosis diosmosis added c: Platform For Matomo platform changes that aren't impacting any of our APIs but improve the core itself. Pull Request WIP Indicates the current pull request is still work in progress and not ready yet for a review. labels May 27, 2015
@diosmosis diosmosis self-assigned this May 27, 2015
@diosmosis diosmosis added this to the 2.14.0 milestone May 27, 2015
@diosmosis diosmosis force-pushed the test_env_di_5 branch 3 times, most recently from 4f7b4b8 to da404e3 Compare May 31, 2015 02:43
@diosmosis diosmosis added the c: Tests & QA For issues related to automated tests or making it easier to QA & test issues. label May 31, 2015
@diosmosis diosmosis added Needs Review PRs that need a code review and removed Pull Request WIP Indicates the current pull request is still work in progress and not ready yet for a review. labels May 31, 2015
@diosmosis
Copy link
Member Author

Ready for review and (possibly) merge.

'ini.log.log_level' => $level,
'ini.log.string_message_format' => self::STRING_MESSAGE_FORMAT,
'ini.log.logger_file_path' => self::getLogFileLocation(),
'Psr\Log\LoggerInterface' => \DI\get('Monolog\Logger')
Copy link
Contributor

Choose a reason for hiding this comment

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

Could these 3 lines be moved to provideContainerConfig() or does the new environment completely ignores the current one? If that's the case, then why do we bind LoggerInterface in provideContainerConfig()? (isn't that useless?)

Copy link
Member Author

Choose a reason for hiding this comment

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

provideContainerConfig() is called when the test case creates a new Environment instance, so we'd have to call it here if we put the other lines in that method. But you're right, since we create a new Environment in order to inject the Logger into Log, we don't need the provideContainerConfig(), just forgot to remove it.

Note: Will remove in a new commit since PRs depend on this one.

@mnapoli
Copy link
Contributor

mnapoli commented Jun 1, 2015

Overall it looks good, moving some test config to config/test.php is really good as it makes the whole thing less magic (at least to me). On remark on that though: the observers.global entry now contains closures with lots of code - could we keep the longest closure (Environment.bootstrapped) inside a class somewhere and call it from the DI config? I want to avoid cluttering the config files too much.

For the file tests/PHPUnit/Framework/TestingEnvironment.php it now contains a cleanly namespaced Piwik\Tests\Framework\TestingEnvironment but also another Piwik\Tests\Framework\Piwik_MockAccess class => could this class be moved to its own file? (and maybe it would also make sense to rename it to MockAccess?)

Except that 👍 System tests are failing but it seems to be a random failure since AllTests is green.

diosmosis added 15 commits June 1, 2015 15:05
…ass to Piwik\Tests\Framework namespace. Remove manual require statements.
…y entry points will apply overridden DI config.
…logger), we must enable it in a test class override method, since this test checks the log output.

Fixing LogTest now that NullLogger is used for test environment setup.

Make sure translations are loaded in EnvironmentValidationTest.php.

Make sure the logger is enabled for ArchiveCronTest.
…me reason cron archiver will fail when archiving segments if translations aren't available. Segment archiving requests return the token General_DateRangeFromTo w/o anything else (only some of them).
Fix Fixture class reference in MultiSites' ControllerTest.

Fix Fixture class reference in ExamplePlugin system test.
… not to my knowledge been useful in debugging mysterious test failures.
diosmosis added 4 commits June 1, 2015 15:05
…o calls to StaticContainer::addDefinitions will stack. This allows us to override global.observers in tests.
…ing authentication hack (which no longer works since test environment setup is unified), add an observer to Tracker.newHandler which throws an exception and returns the HTTP status code 500.
@diosmosis
Copy link
Member Author

@mnapoli The two items in your last comment will be taken care of here: #8027

mnapoli added a commit that referenced this pull request Jun 3, 2015
Injection Inception, Change #4: Using DI as primary mechanism in setting up Test environment
@mnapoli mnapoli merged commit 9ae767c into master Jun 3, 2015
@mnapoli mnapoli deleted the test_env_di_5 branch June 3, 2015 09:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: Platform For Matomo platform changes that aren't impacting any of our APIs but improve the core itself. c: Tests & QA For issues related to automated tests or making it easier to QA & test issues. Needs Review PRs that need a code review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants