Description
What about collecting assertions in controllers, services, entities ... ?
We 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.
It could be based on webmozart/assert
(ping @webmozart :)
We could use an assert
service:
class SomeRequestService
{
private $assert;
private $request;
public function __construct(Request $request, AssertInterface $assert)
{
$this->request = $request;
$this->assert = $assert;
}
public function someMethod()
{
$session = $this->request;
$this->assert->true($session->has('some_attribute'), 'The session should have a "some_attribute"', $session());
$this->assert->same('test', $session->get('some_attribute'), 'The session attribute "some_attribute" should be "test" but got %s', $this->request);
// or
$this->assert->regex('/^test/', $session->get('some_attribute'), 'The session attribute "some_attribute" should start with "test" but got %s', $this->request);
// or even better
$this->assert->uuid($session->get('some_uuid'), 'The session attribute "some_uuid" is invalid" got %s', $this->request);
A 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->*
.
$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.
It would be nice if the assert
service wrapper get passed the Stopwatch to check time performances:
// from anywhere using SomeService instance
$assert->stopwatch('some_service_event', '<', 1000, $someService);
The assert
service could handle different channels like Logger
, to sort them in the profiler.
What do you think ?