Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[RFC] [Tests] use assertions anywhere in the code and profile them #18411

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
HeahDude opened this issue Apr 1, 2016 · 6 comments
Closed

[RFC] [Tests] use assertions anywhere in the code and profile them #18411

HeahDude opened this issue Apr 1, 2016 · 6 comments
Labels

Comments

@HeahDude
Copy link
Contributor

HeahDude commented Apr 1, 2016

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 ?

@HeahDude
Copy link
Contributor Author

HeahDude commented Apr 2, 2016

FYI, I'm working on a micro bundle already. If it does not have its place in core, it may help in community projects willing to make functional tests.

@Simperfit
Copy link
Contributor

Thanks @HeahDude !

@HeahDude
Copy link
Contributor Author

HeahDude commented Apr 2, 2016

Working on an implementation and the dependencies always makes things clear, I will update the API proposed in the op shortly :)

@HeahDude HeahDude changed the title [Tests] Feature request - using a DataCollector to collect assertions [RFC] [Tests] use assertions anywhere in the code and profile them Apr 3, 2016
@stof
Copy link
Member

stof commented Apr 5, 2016

I don't think this needs to be in core.

@HeahDude
Copy link
Contributor Author

HeahDude commented Apr 5, 2016

I agree its not needed as part of the core but that would be cool to bring webmozart/assert in a bundle that uses most basic symfony features. what about FOSAssert ?

@javiereguiluz
Copy link
Member

I'm not strongly against this ... but I agree with @stof reasons. So this would be a 👎 for me.

@fabpot fabpot closed this as completed Jun 30, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants