{"action":"created","issue":{"url":"https://api.github.com/repos/laravel/framework/issues/24160","repository_url":"https://api.github.com/repos/laravel/framework","labels_url":"https://api.github.com/repos/laravel/framework/issues/24160/labels{/name}","comments_url":"https://api.github.com/repos/laravel/framework/issues/24160/comments","events_url":"https://api.github.com/repos/laravel/framework/issues/24160/events","html_url":"https://github.com/laravel/framework/issues/24160","id":321607029,"number":24160,"title":"RefreshDatabase Trait and Expected Exceptions","user":{"login":"hettiger","id":4583871,"avatar_url":"https://avatars1.githubusercontent.com/u/4583871?v=4","gravatar_id":"","url":"https://api.github.com/users/hettiger","html_url":"https://github.com/hettiger","followers_url":"https://api.github.com/users/hettiger/followers","following_url":"https://api.github.com/users/hettiger/following{/other_user}","gists_url":"https://api.github.com/users/hettiger/gists{/gist_id}","starred_url":"https://api.github.com/users/hettiger/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hettiger/subscriptions","organizations_url":"https://api.github.com/users/hettiger/orgs","repos_url":"https://api.github.com/users/hettiger/repos","events_url":"https://api.github.com/users/hettiger/events{/privacy}","received_events_url":"https://api.github.com/users/hettiger/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":3,"created_at":"2018-05-09T15:00:57Z","updated_at":"2018-05-10T05:39:02Z","closed_at":"2018-05-10T02:59:53Z","author_association":"CONTRIBUTOR","body":"<!--\r\nPlease use this issue tracker only for reporting Laravel bugs.\r\n\r\nIf you need support, please use the forums:\r\n\r\n- https://laracasts.com/discuss \r\n- https://laravel.io/forum\r\n\r\nAlternatively, you may use Slack (https://larachat.co/) or Stack Overflow (http://stackoverflow.com/questions/tagged/laravel).\r\n\r\nIf you would like to propose new Laravel features, please make a pull request, or open an issue at https://github.com/laravel/ideas/issues.\r\n-->\r\n\r\n- Laravel Version: v5.6.20\r\n- PHP Version: 7.1.7\r\n- Database Driver & Version: MySQL 5.7.15\r\n\r\n### Description:\r\n\r\n`RefreshDatabase` Trait fails to refresh the database on a test that expects an exception to be thrown.\r\nConsequent tests fail due to this.\r\n\r\n### Steps To Reproduce:\r\n\r\nHere's the initial version of the method:\r\n\r\n```php\r\npublic function testThrowsIntegrityMismatchException()\r\n{\r\n    $this->expectException(IntegrityMismatchException::class);\r\n\r\n    $alterStatement = <<<SQL\r\nALTER TABLE `replication`\r\nMODIFY `title` VARCHAR(200);\r\nSQL;\r\n\r\n    DB::statement($alterStatement);\r\n    Artisan::call('example:check-integrity');\r\n}\r\n```\r\n\r\nThe `Artisan::call('example:check-integrity');` call will throw a `IntegrityMismatchException` because of the modified column.\r\n\r\nUnfortunately the `RefreshDatabase` Trait silently fails to refresh the database after this test and consequent tests fail.\r\n\r\nNow I've found a workaround:\r\n\r\n```php\r\npublic function testThrowsIntegrityMismatchException()\r\n{\r\n    $this->expectException(IntegrityMismatchException::class);\r\n\r\n    $alterStatement = <<<SQL\r\nALTER TABLE `replication`\r\nMODIFY `title` VARCHAR(200);\r\nSQL;\r\n\r\n    DB::statement($alterStatement);\r\n\r\n    try {\r\n        Artisan::call(example:check-integrity');\r\n    } catch (Exception $exception) {\r\n        Artisan::call('migrate:fresh');\r\n\r\n        /** @noinspection PhpUnhandledExceptionInspection */\r\n        throw $exception;\r\n    }\r\n}\r\n```\r\n\r\nAnyways I'm not too happy about it because now I'm no longer benefitting from the optimized Trait. Also I think this `try`& `catch` stuff should not be necessary.\r\n\r\nI also tried to use `$this->refreshDatabase()` instead of `Artisan::call('migrate:fresh');` but it did throw the following error:\r\n\r\n`Doctrine\\DBAL\\Driver\\PDOException : SQLSTATE[42000]: Syntax error or access violation: 1305 SAVEPOINT trans2 does not exist`\r\n\r\nI think that's the reason why the `RefreshDatabase` Trait is unable to refresh correctly behind the scenes. Also this should not fail silently?"},"comment":{"url":"https://api.github.com/repos/laravel/framework/issues/comments/387957358","html_url":"https://github.com/laravel/framework/issues/24160#issuecomment-387957358","issue_url":"https://api.github.com/repos/laravel/framework/issues/24160","id":387957358,"user":{"login":"hettiger","id":4583871,"avatar_url":"https://avatars1.githubusercontent.com/u/4583871?v=4","gravatar_id":"","url":"https://api.github.com/users/hettiger","html_url":"https://github.com/hettiger","followers_url":"https://api.github.com/users/hettiger/followers","following_url":"https://api.github.com/users/hettiger/following{/other_user}","gists_url":"https://api.github.com/users/hettiger/gists{/gist_id}","starred_url":"https://api.github.com/users/hettiger/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hettiger/subscriptions","organizations_url":"https://api.github.com/users/hettiger/orgs","repos_url":"https://api.github.com/users/hettiger/repos","events_url":"https://api.github.com/users/hettiger/events{/privacy}","received_events_url":"https://api.github.com/users/hettiger/received_events","type":"User","site_admin":false},"created_at":"2018-05-10T05:39:02Z","updated_at":"2018-05-10T05:39:02Z","author_association":"CONTRIBUTOR","body":"@laurencei\r\n\r\n> That's not necceasrily correct - you are catching the generic Exception in your example.\r\n\r\nYes but i rethrow it and assert that it is indeed the expected exception:\r\n\r\n```php\r\n$this->expectException(IntegrityMismatchException::class);\r\n```\r\n\r\nThe reason that I catch the generic Exception is that I want the `migrate:fresh` command bein called no matter what.\r\n\r\nThe workaround test that is using `try` & `catch` is passing.\r\n\r\nI will try to reproduce this in a simplified example running on a clean install and let you know.\r\n\r\nThanks for the quick response."}}
{"actor":{"display_login":"hettiger"}}