{"action":"opened","issue":{"url":"https://api.github.com/repos/sebastianbergmann/phpunit/issues/3143","repository_url":"https://api.github.com/repos/sebastianbergmann/phpunit","labels_url":"https://api.github.com/repos/sebastianbergmann/phpunit/issues/3143/labels{/name}","comments_url":"https://api.github.com/repos/sebastianbergmann/phpunit/issues/3143/comments","events_url":"https://api.github.com/repos/sebastianbergmann/phpunit/issues/3143/events","html_url":"https://github.com/sebastianbergmann/phpunit/issues/3143","id":326723832,"number":3143,"title":"Risky global state manipulation","user":{"login":"Stadly","id":7263579,"avatar_url":"https://avatars2.githubusercontent.com/u/7263579?v=4","gravatar_id":"","url":"https://api.github.com/users/Stadly","html_url":"https://github.com/Stadly","followers_url":"https://api.github.com/users/Stadly/followers","following_url":"https://api.github.com/users/Stadly/following{/other_user}","gists_url":"https://api.github.com/users/Stadly/gists{/gist_id}","starred_url":"https://api.github.com/users/Stadly/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Stadly/subscriptions","organizations_url":"https://api.github.com/users/Stadly/orgs","repos_url":"https://api.github.com/users/Stadly/repos","events_url":"https://api.github.com/users/Stadly/events{/privacy}","received_events_url":"https://api.github.com/users/Stadly/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-05-26T09:18:00Z","updated_at":"2018-05-26T09:18:00Z","closed_at":null,"author_association":"NONE","body":"| Q                   | A\r\n| --------------------| ---------------\r\n| PHPUnit version     | 7.2-gdf5774cf7\r\n| PHP version         | 7.1.11\r\n| Installation Method | Composer\r\n\r\nAccording to the [documentation](http://phpunit.readthedocs.io/en/7.1/risky-tests.html#global-state-manipulation), running PHPUnit using `--strict-global-state`, means that PHPUnit is strict about tests that manipulate global state.\r\n\r\nConsider the tests below.\r\n\r\n```\r\nclass Test extends \\PHPUnit\\Framework\\TestCase {\r\n\r\n    /**\r\n     * @backupGlobals disabled\r\n     */\r\n    public function test1() {\r\n        self::assertFalse(isset($_SERVER['foo']));\r\n        $_SERVER['foo'] = 1;\r\n    }\r\n\r\n    /**\r\n     * @backupGlobals enabled\r\n     */\r\n    public function test2() {\r\n        self::assertSame(1, $_SERVER['foo']);\r\n        $_SERVER['foo'] = 2;\r\n    }\r\n\r\n    /**\r\n     * @backupGlobals disabled\r\n     * @runInSeparateProcess\r\n     */\r\n    public function test3() {\r\n        self::assertSame(1, $_SERVER['foo']);\r\n        $_SERVER['foo'] = 5;\r\n    }\r\n\r\n    /**\r\n     * @backupGlobals enabled\r\n     * @runInSeparateProcess\r\n     */\r\n    public function test4() {\r\n        self::assertSame(1, $_SERVER['foo']);\r\n        $_SERVER['foo'] = 4;\r\n    }\r\n}\r\n```\r\n\r\nWhen running these tests using `--strict-global-state`, `test2` is the only test marked as risky. It seems that tests run using `@runInSeparateProcess` or `@backupGlobals disabled` are never considered risky. From my understanding, the opposite would make more sense - only tests run using `@backupGlobals enabled` should never be considered risky.\r\n\r\nA best practice is that running a test should not affect any other test. This implies that the global state should be the same before and after any test.\r\n\r\n- When using `@backupGlobals disabled`, the global state is not backed up and restored, hence it is risky if the global state is manipulated by such a test.\r\n- When using `@backupGlobals enabled`, the global state is backed up and restored. This indicates that any modification of the global sate is intentional, and hence it is not risky.\r\n- When using `@runInSeparateProcess`, the test will never affect any other test, but any modification of the global state could still be unintentional, and hence it is risky. To indicate that any modification of the global sate is intentional, the test could be run using `@backupGlobals enabled` in addition.\r\n\r\nExpected result from the tests above: `test1` and `test3` are risky.\r\nActual result from the tests above: `test2` is risky."}}
{"actor":{"display_login":"Stadly"}}