Navigation Menu

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

Test environment and DI refactor #7931

Closed
wants to merge 39 commits into from
Closed

Test environment and DI refactor #7931

wants to merge 39 commits into from

Conversation

diosmosis
Copy link
Member

This PR includes several improvements to test environment setup, and includes several DI related improvements.

List of changes:

  1. Moved the following objects to DI: Piwik\Config, Piwik\EventDispatcher, Piwik\Access, Piwik_TestingEnvironment (renamed to Piwik\Tests\Framework\TestingEnvironment)

2. Allow event observers to be defined before the DI container is created by creating a new observers.global DI entry.

3. Removed use of the GlobalSettingsProvider singleton, it's no longer needed.

4. Introduced the concept of EnvironmentManipulators (or possibly EnvironmentInterceptors). These are hooks that are only used for testing setup, and should not be used by plugins, plugin tests or core tests, only test environment setup.

  1. Test environment setup is done mostly by the test.php DI config now. The ContainerFactory will apply test.php DI config files (core + plugins) if PIWIK_TEST_MODE is defined, so it should not be needed to explicitly use the 'test' environment name (ie, new Environment('test')).
  2. I had to re-enable logging in tests. This is only for ArchiveWebTest which tests log output of the archive.php script. Maybe it can be enabled conditionally via DI\decorate.

7. Added the ability for Integration tests and system tests to override DI config for tests (not just for child processes as is done currently). This is done by overriding provideContainerConfig for integration tests and provideContainerConfigBeforeClass for system tests.

  1. Moved Piwik\Auth creation to DI only (instead of it being set in Login controller event). This required some tweaks to the Login auth class in order to fix local tracking. Since the Auth instance will end up being re-used when local tracking is called, we have to be able to clear all state before re-authenticating. So I made it possible to set the password/password hash to null. And added these sets to the Tracker.

There are still some TODOs left, but they are just about removing unused code + filling out docs.

diosmosis added 30 commits May 18, 2015 07:29
…ndler in TestingEnvironment to TestConfig object.
…te kernel globals. Instead of adding configFile... static members in Environment, use a single static array of EnvironmentManipulators.
…ry because EventDispatcher is now stored in DI).
…t.php and get one system + one UI test to pass.
…nInstance issues after having put certain classes in DI.
…ss::setSingletonInstance. Added ability for IntegrationTestCases to provide DI config overrides.
…tion test setup, and login as superuser in Fixture setUP.
…ce, make Fixture::loginAsSuperUser public and fix LocalTracker issue that occurs if Auth is created by DI (old auth info is not overwritten, causing auth errors).
…tly, instead use 'test' environment name). And Fix ManagerTest (order of plugins in one test changes because they are sorted by TestConfig, and loadTrackerPlugins doesn't remove all plugins and re-add, it just removes plugins from internal array).
@@ -64,6 +64,7 @@ public function test_constructor_shouldEstablishADatabaseConnection_AsSoonAsWeSe
*/
public function test_setSettingValue_shouldThrowException_IfAnonymousIsTryingToSetASettingWhichNeedsUserPermission()
{
$this->setAnoymous();
Copy link
Contributor

Choose a reason for hiding this comment

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

Anoymous? Shouldn't it be Anonymous?

@mattab
Copy link
Member

mattab commented May 20, 2015

HI @fabiocarneiro

it's nice that you try to help, but actually please take a look our previous Pull requests and our existing style of code reviews. In general, we try not to review trivial points such as most of the ones you point out. During the review we focus on bugs, security, whether the code works, whether it makes usable interfaces, whether it's documented in user and developer guides, wheter hacks are avoided, etc.

generally we don't care about minor details like missing comment blocks or missing line breaks etc.
Feel free to review the PR if you find real bugs or problems or hacks etc. Cheers!

@kaz231
Copy link

kaz231 commented May 20, 2015

But it will be really good to check the code compliance with PSR-2 before the commit.

@fabiocarneiro
Copy link
Contributor

@mattab I usually avoid commenting in things that are already in the code. I see there are many "missing empty line in the end of file" and "missing docblocks" and I didn't pointed them out. But if a new code is being introduced or modified (lines with diffs), imho it makes sense to get it following PSR-2 at least.

Since these are really trivial points, the author will probably take few minutes to fix all of them in one single commit.

Feel free to delete the ones you think are inappropriate. 👍

A good way to force good code style and avoid flooding the PR is by running an automatic tool in Travis to check PSR-2 compliance, so everybody gets forced to follow the same pattern and it helps the team to improve the readability of the code, which also improve productivity. I'd suggest https://github.com/FriendsOfPHP/PHP-CS-Fixer or https://github.com/squizlabs/PHP_CodeSniffer.

Cheers!

@@ -115,7 +120,10 @@ private function createContainer()
protected function getGlobalSettingsCached()
{
if ($this->globalSettingsProvider === null) {
$this->globalSettingsProvider = $this->getGlobalSettings();
$result = $this->invokeFirstManipulator(
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd suggest this approach:

    protected function getGlobalSettingsCached()
    {
        if (null !== $this->globalSettingsProvider) {
            return $this->globalSettingsProvider;
        }

        $this->globalSettingsProvider = $this->invokeFirstManipulator(
            'makeKernelObject',
            array(
                'Piwik\Application\Kernel\GlobalSettingsProvider',
                $this->getKernelObjectsArray()
            )
        );

        if (! $this->globalSettingsProvider instanceof GlobalSettingsProvider) {
            $this->globalSettingsProvider = $this->getGlobalSettings();
        }

        return $this->globalSettingsProvider;
    }

This way, you're checking if globalSettingsProvider is always a instance of GlobalSettingsProvider, since invokeFirstManipulator could return anything. I also made some code cleanup by removing the ternary operator and by adding an early return.

@diosmosis
Copy link
Member Author

@fabiocarneiro You can stop reviewing this PR, I am taking it in a different direction.

You should also note, you can avoid flooding PRs with your comments by leaving just one.

@mattab
Copy link
Member

mattab commented May 20, 2015

@kaz231 @fabiocarneiro it would be good to have PSR-2 in Piwik and fix those little trivial matters. However we won't make the code compliant with human code reviews as it is not high value work for us to manually review those things. Rather as you point out we need to automate it with PHP CS fixer. This is discussed in this issue: #7186 Fix warnings + coding standards with PHP-CS-Fixer

@kaz231
Copy link

kaz231 commented May 22, 2015

At the same beginning, this will be enough - https://confluence.jetbrains.com/display/PhpStorm/PHP+Code+Sniffer+in+PhpStorm .

@diosmosis diosmosis self-assigned this May 23, 2015
@mnapoli mnapoli removed the Needs Review PRs that need a code review label May 28, 2015
@diosmosis
Copy link
Member Author

Everything in this PR has been done in another PR (2 waiting for merge, 1 new one with changes that aren't in this one). Closing and removing from current milestone.

@diosmosis diosmosis closed this Jun 1, 2015
@diosmosis diosmosis deleted the test_env_di branch June 1, 2015 22:32
@diosmosis diosmosis removed this from the 2.14.0 milestone Jun 1, 2015
@diosmosis diosmosis added not-in-changelog For issues or pull requests that should not be included in our release changelog on matomo.org. and removed Pull Request WIP Indicates the current pull request is still work in progress and not ready yet for a review. labels Jun 1, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
not-in-changelog For issues or pull requests that should not be included in our release changelog on matomo.org.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants