{"action":"created","issue":{"url":"https://api.github.com/repos/octobercms/october/issues/3156","repository_url":"https://api.github.com/repos/octobercms/october","labels_url":"https://api.github.com/repos/octobercms/october/issues/3156/labels{/name}","comments_url":"https://api.github.com/repos/octobercms/october/issues/3156/comments","events_url":"https://api.github.com/repos/octobercms/october/issues/3156/events","html_url":"https://github.com/octobercms/october/pull/3156","id":263630207,"number":3156,"title":"Added PluginManager::unregisterAll()","user":{"login":"harmenjanssen","id":803176,"avatar_url":"https://avatars0.githubusercontent.com/u/803176?v=4","gravatar_id":"","url":"https://api.github.com/users/harmenjanssen","html_url":"https://github.com/harmenjanssen","followers_url":"https://api.github.com/users/harmenjanssen/followers","following_url":"https://api.github.com/users/harmenjanssen/following{/other_user}","gists_url":"https://api.github.com/users/harmenjanssen/gists{/gist_id}","starred_url":"https://api.github.com/users/harmenjanssen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/harmenjanssen/subscriptions","organizations_url":"https://api.github.com/users/harmenjanssen/orgs","repos_url":"https://api.github.com/users/harmenjanssen/repos","events_url":"https://api.github.com/users/harmenjanssen/events{/privacy}","received_events_url":"https://api.github.com/users/harmenjanssen/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":3,"created_at":"2017-10-07T09:49:28Z","updated_at":"2017-10-08T07:04:27Z","closed_at":null,"author_association":"NONE","pull_request":{"url":"https://api.github.com/repos/octobercms/october/pulls/3156","html_url":"https://github.com/octobercms/october/pull/3156","diff_url":"https://github.com/octobercms/october/pull/3156.diff","patch_url":"https://github.com/octobercms/october/pull/3156.patch"},"body":"### Preface\r\nThis PR follows up on issue #2592. \r\nTo recap: in the testing environment, if you're testing plugins working together, you need to do \r\n\r\n```php\r\nPluginManager::instance()->registerAll(true);\r\nPluginManager::instance()->bootAll(true);\r\n```\r\n\r\nto make sure everything is setup in the same way your frontend would be using them.\r\n\r\n### One last issue\r\nWe noticed however, that the `app` instance bound to `$this->app` in a plugin gets corrupted, sort of.\r\n\r\nFor instance, usually `$app['translator']` is set, and this is true for the first test. Every subsequent test however, throws an exception from deep within Laravel's ServiceProvider stating `ReflectionException: Class translator does not exist`. \r\n\r\nAdding the following to my plugin's `boot()` method shows `true` for the first test, and `false` for every subsequent test:\r\n\r\n```php\r\npublic function boot()\r\n{\r\n    dump(isset($this->app['translator']));\r\n}\r\n```\r\n\r\n### Solution\r\nThis lead me to think something got destroyed in `tearDown` but not reset in `setUp` again. Eventually I ended up in `PluginTestCase::runPluginRefreshCommand()`.   \r\nIf I removed the `if` statement below the comment\r\n\r\n```php\r\n        /*\r\n         * First time seeing this plugin, load it up\r\n         */\r\n```\r\n\r\nmy tests would run correctly, because it would run `loadPlugin()` regardless of wether it was the first time or not.\r\n\r\nThis is why I offer an `unregisterAll()` method that can be called from within my `tearDown()` function, making sure plugins are registered and booted from scratch for every test.\r\n\r\nThis would look something like this:\r\n\r\n```php\r\n    public function setUp()\r\n    {\r\n        parent::setUp();\r\n\r\n        $pluginManager = PluginManager::instance();\r\n        $pluginManager->registerAll(true);\r\n        $pluginManager->bootAll(true);\r\n    }\r\n\r\n    public function tearDown()\r\n    {\r\n        parent::tearDown();\r\n\r\n        $pluginManager = PluginManager::instance();\r\n        $pluginManager->unregisterAll();\r\n    }\r\n```\r\n\r\nIt feels right for `tearDown` to do the exact opposite of `setUp` and clean up after it.\r\n\r\nThis fixes my problem, but I can imagine I might miss some intricacies regarding the PluginManager, so let me know if I should elaborate either my implementation or the accompanying unit test.\r\n\r\nI can create a repo to reproduce the problem if you want."},"comment":{"url":"https://api.github.com/repos/octobercms/october/issues/comments/334987666","html_url":"https://github.com/octobercms/october/pull/3156#issuecomment-334987666","issue_url":"https://api.github.com/repos/octobercms/october/issues/3156","id":334987666,"user":{"login":"harmenjanssen","id":803176,"avatar_url":"https://avatars0.githubusercontent.com/u/803176?v=4","gravatar_id":"","url":"https://api.github.com/users/harmenjanssen","html_url":"https://github.com/harmenjanssen","followers_url":"https://api.github.com/users/harmenjanssen/followers","following_url":"https://api.github.com/users/harmenjanssen/following{/other_user}","gists_url":"https://api.github.com/users/harmenjanssen/gists{/gist_id}","starred_url":"https://api.github.com/users/harmenjanssen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/harmenjanssen/subscriptions","organizations_url":"https://api.github.com/users/harmenjanssen/orgs","repos_url":"https://api.github.com/users/harmenjanssen/repos","events_url":"https://api.github.com/users/harmenjanssen/events{/privacy}","received_events_url":"https://api.github.com/users/harmenjanssen/received_events","type":"User","site_admin":false},"created_at":"2017-10-08T07:04:27Z","updated_at":"2017-10-08T07:04:27Z","author_association":"NONE","body":"The only case I can think of is, again, between unit tests, to completely break down an application and return to a fresh state for the next test.\r\n\r\nBut maybe, in the spirit of [YAGNI](http://www.agilenutshell.com/yagni), we should just leave it at this and create it whenever the actual need pops up."}}
{"actor":{"display_login":"harmenjanssen"}}