{"action":"created","issue":{"url":"https://api.github.com/repos/symfony/symfony/issues/18411","repository_url":"https://api.github.com/repos/symfony/symfony","labels_url":"https://api.github.com/repos/symfony/symfony/issues/18411/labels{/name}","comments_url":"https://api.github.com/repos/symfony/symfony/issues/18411/comments","events_url":"https://api.github.com/repos/symfony/symfony/issues/18411/events","html_url":"https://github.com/symfony/symfony/issues/18411","id":145280831,"number":18411,"title":"[Tests] Feature request - using a DataCollector to collect assertions","user":{"login":"HeahDude","id":10107633,"avatar_url":"https://avatars.githubusercontent.com/u/10107633?v=3","gravatar_id":"","url":"https://api.github.com/users/HeahDude","html_url":"https://github.com/HeahDude","followers_url":"https://api.github.com/users/HeahDude/followers","following_url":"https://api.github.com/users/HeahDude/following{/other_user}","gists_url":"https://api.github.com/users/HeahDude/gists{/gist_id}","starred_url":"https://api.github.com/users/HeahDude/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/HeahDude/subscriptions","organizations_url":"https://api.github.com/users/HeahDude/orgs","repos_url":"https://api.github.com/users/HeahDude/repos","events_url":"https://api.github.com/users/HeahDude/events{/privacy}","received_events_url":"https://api.github.com/users/HeahDude/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":4,"created_at":"2016-04-01T20:41:56Z","updated_at":"2016-04-02T13:13:20Z","closed_at":null,"body":"What about collecting assertions in controllers, services, entities ... ?\r\n\r\nWe can already `dump()` objects but I think this could be useful to have results of assertions directly shown in the profiler and the web debug toolbar.\r\n\r\nIt could be based on `webmozart/assert` (ping @webmozart :)\r\n\r\nWe could use an `assert` service:\r\n```php\r\nclass SomeRequestService\r\n{\r\n    private $assert;\r\n    private $request;\r\n\r\n    public function __construct(Request $request, AssertInterface $assert)\r\n    {\r\n        $this->request = $request;\r\n        $this->assert = $assert;\r\n    }\r\n\r\n    public function someMethod()\r\n    {\r\n        $session = $this->request;\r\n\r\n        $this->assert->true($session->has('some_attribute'), 'The session should have a \"some_attribute\"', $session());\r\n        $this->assert->same('test', $session->get('some_attribute'), 'The session attribute \"some_attribute\" should be \"test\" but got %s', $this->request);\r\n        // or\r\n        $this->assert->regex('/^test/', $session->get('some_attribute'), 'The session attribute \"some_attribute\" should start with \"test\" but got %s', $this->request);\r\n        // or even better\r\n        $this->assert->uuid($session->get('some_uuid'), 'The session attribute \"some_uuid\" is invalid\" got %s', $this->request);\r\n```\r\n\r\nA controller could get it by `$this->container->get('assert')` and use a trait to hold it as protected parameter and be able to do `$this->assert->*`.\r\n\r\n`$assert->*`would be a wrapper for `\\Webmozart\\Assert::` taking  the \"dumped\" context as extra last argument, using `dump` function of the VarDumper component to handsome display in the profiler.\r\n\r\nIt would be nice if the `assert` service wrapper get passed the Stopwatch to check time performances:\r\n```php\r\n// from anywhere using SomeService instance\r\n$assert->stopwatch('some_service_event', '<', 1000, $someService);\r\n```\r\n\r\nThe `assert` service could handle different channels like `Logger`, to sort them in the profiler.\r\n\r\nWhat do you think ?"},"comment":{"url":"https://api.github.com/repos/symfony/symfony/issues/comments/204714872","html_url":"https://github.com/symfony/symfony/issues/18411#issuecomment-204714872","issue_url":"https://api.github.com/repos/symfony/symfony/issues/18411","id":204714872,"user":{"login":"HeahDude","id":10107633,"avatar_url":"https://avatars.githubusercontent.com/u/10107633?v=3","gravatar_id":"","url":"https://api.github.com/users/HeahDude","html_url":"https://github.com/HeahDude","followers_url":"https://api.github.com/users/HeahDude/followers","following_url":"https://api.github.com/users/HeahDude/following{/other_user}","gists_url":"https://api.github.com/users/HeahDude/gists{/gist_id}","starred_url":"https://api.github.com/users/HeahDude/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/HeahDude/subscriptions","organizations_url":"https://api.github.com/users/HeahDude/orgs","repos_url":"https://api.github.com/users/HeahDude/repos","events_url":"https://api.github.com/users/HeahDude/events{/privacy}","received_events_url":"https://api.github.com/users/HeahDude/received_events","type":"User","site_admin":false},"created_at":"2016-04-02T13:13:20Z","updated_at":"2016-04-02T13:13:20Z","body":"I was adding a `strategy` field to my factory with constants:\r\n```php\r\ninterface Closer\r\n{\r\n    const LOG_ASSERTION = 0;\r\n    const DUMP_ASSERTION = 1;\r\n    const THROW_ASSERTION = 2;\r\n\r\n   // ...\r\n}\r\n```\r\n\r\nBut thanks to @nicolas-grekas and #18414 I guess I can add:\r\n```\r\nconst STREAM_ASSERTION;\r\n```\r\n\r\nBeautiful functional assertions from the console :)"}}