{"action":"created","issue":{"url":"https://api.github.com/repos/laravel/framework/issues/9178","labels_url":"https://api.github.com/repos/laravel/framework/issues/9178/labels{/name}","comments_url":"https://api.github.com/repos/laravel/framework/issues/9178/comments","events_url":"https://api.github.com/repos/laravel/framework/issues/9178/events","html_url":"https://github.com/laravel/framework/issues/9178","id":86880044,"number":9178,"title":"Defining package validators & problems concerning unit testing","user":{"login":"Propaganistas","id":6680176,"avatar_url":"https://avatars.githubusercontent.com/u/6680176?v=3","gravatar_id":"","url":"https://api.github.com/users/Propaganistas","html_url":"https://github.com/Propaganistas","followers_url":"https://api.github.com/users/Propaganistas/followers","following_url":"https://api.github.com/users/Propaganistas/following{/other_user}","gists_url":"https://api.github.com/users/Propaganistas/gists{/gist_id}","starred_url":"https://api.github.com/users/Propaganistas/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Propaganistas/subscriptions","organizations_url":"https://api.github.com/users/Propaganistas/orgs","repos_url":"https://api.github.com/users/Propaganistas/repos","events_url":"https://api.github.com/users/Propaganistas/events{/privacy}","received_events_url":"https://api.github.com/users/Propaganistas/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-06-10T07:56:07Z","updated_at":"2015-06-10T10:03:57Z","closed_at":null,"body":"Duplicate of [this post](http://stackoverflow.com/questions/30750303/best-practice-for-defining-a-validator-in-a-package) on Stackoverflow, but I think that immediate contact with some high-level devs is justified.\r\n\r\n----\r\n\r\nCurrently Laravel has three ways to add additional validator methods:\r\n\r\n 1. Extend the validator using a closure:\r\n\r\n    ```php\r\n    Validator::extend('foo', function($attribute, $value, $parameters) {\r\n        return $value == 'foo';\r\n    });\r\n    ```\r\n\r\n\r\n 2. Extend the validator using a separate class:\r\n\r\n    ```php\r\n    Validator::extend('foo', 'FooValidator@validate');\r\n    ```\r\n\r\n    ```php\r\n    class FooValidator {\r\n        public function validate($attribute, $value, $parameters, $validator) {\r\n            return $value == 'foo';\r\n        }\r\n    }\r\n    ```\r\n\r\n 3. Define a custom validator resolver:\r\n\r\n    ```php\r\n    Validator::resolver(function($translator,$data,$rules,$messages) {\r\n        return new CustomValidator($translator,$data,$rules,$messages);\r\n    });\r\n    ```\r\n\r\n    ```php\r\n    class CustomValidator extends \\Illuminate\\Validation\\Validator {\r\n        public function validateFoo($attribute, $value, $parameters) {\r\n            return $value == 'foo';\r\n        }\r\n    }\r\n    ```\r\n\r\nThinking about defining a validator in a package, here's the catch:\r\n\r\n - option 1 (extend using a closure): if you need complex validation handling which most certainly should be split into several sub-methods, this options seems rather unusable to me.\r\n\r\n\r\n - option 2 (extend using a class): at first sight this seems to be the way to go. Note that the class should **not** extend `Illuminate\\Validation\\Validator` as it will throw errors concerning construction of the base `Validator` class. However this also throws a problem when implementing unit testing: for what I have found, the only way to test custom validator packages is by extension of the original `Validator` class with custom methods added to it.\r\n\r\n\r\n - option 3 (defining a resolver): this shouldn't be used in my opinion as it *replaces* the validator instead of *extending* it. Two packages defining their own resolver will simply override eachother rendering one of them unusable.\r\n\r\n\r\nI'm the author of the [Laravel-Phone](https://packagist.org/packages/propaganistas/laravel-phone) package, and for now I went with option 2. As a workaround for the unit testing problem, I have defined all validation logic in a trait and created stub classes for production use and unit testing. It works perfectly, but it's rather unclean.\r\n\r\nHence my question: Am I overlooking something? What's the way to go to define validators in packages while still allowing other validator packages to work properly? Is there a cleaner way to solve the unit testing problem in option 2?\r\n"},"comment":{"url":"https://api.github.com/repos/laravel/framework/issues/comments/110679118","html_url":"https://github.com/laravel/framework/issues/9178#issuecomment-110679118","issue_url":"https://api.github.com/repos/laravel/framework/issues/9178","id":110679118,"user":{"login":"Sladewill","id":6969435,"avatar_url":"https://avatars.githubusercontent.com/u/6969435?v=3","gravatar_id":"","url":"https://api.github.com/users/Sladewill","html_url":"https://github.com/Sladewill","followers_url":"https://api.github.com/users/Sladewill/followers","following_url":"https://api.github.com/users/Sladewill/following{/other_user}","gists_url":"https://api.github.com/users/Sladewill/gists{/gist_id}","starred_url":"https://api.github.com/users/Sladewill/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Sladewill/subscriptions","organizations_url":"https://api.github.com/users/Sladewill/orgs","repos_url":"https://api.github.com/users/Sladewill/repos","events_url":"https://api.github.com/users/Sladewill/events{/privacy}","received_events_url":"https://api.github.com/users/Sladewill/received_events","type":"User","site_admin":false},"created_at":"2015-06-10T10:03:57Z","updated_at":"2015-06-10T10:03:57Z","body":"This is a question more suited for the laravel forums http://laravel.io/forum. But as a heads up unit testing packages like this may require the whole framework to install things such as the translations, validation, and the symphony components. If you do use the whole framework you have the ability to use Facade faking http://laravel.com/docs/5.1/testing#mocking-facades so you will only need to validate your own code and not have the tests relating to anything else."}}