Skip to content
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

Ability to combine assertions #242

Open
calebdw opened this issue Oct 26, 2023 · 3 comments
Open

Ability to combine assertions #242

calebdw opened this issue Oct 26, 2023 · 3 comments
Labels
enhancement New feature or request

Comments

@calebdw
Copy link
Contributor

calebdw commented Oct 26, 2023

Enhancement description

Hello!

I'm trying to write enforce that a domain object be both final and readonly but there does not seem to be a way to do this in the same test...

    public function testDomainObjectsAreFinalAndReadonly(): Rule
    {
        return PHPat::rule()
            ->classes(Selector::inNamespace('/.*\\\\DomainObjects/', regex: true))
            ->shouldBeFinal();
    }

Suggested approach or solution

I'm not sure what a good api would be...the ability to chain the assertions would be the easiest from the end user's perspective but maybe not so much the developer's haha.

    public function testDomainObjectsAreFinalAndReadonly(): Rule
    {
        return PHPat::rule()
            ->classes(Selector::inNamespace('/.*\\\\DomainObjects/', regex: true))
            ->shouldBeFinal()
            ->shouldBeReadonly();
    }

I've also seen ->and() used before (like in Pest)

    public function testDomainObjectsAreFinalAndReadonly(): Rule
    {
        return PHPat::rule()
            ->classes(Selector::inNamespace('/.*\\\\DomainObjects/', regex: true))
            ->shouldBeFinal()
            ->and()
            ->shouldBeReadonly();
    }

Thanks!

@calebdw calebdw added the enhancement New feature or request label Oct 26, 2023
@carlosas
Copy link
Owner

Sounds good. The API shoud be like that for declaration assertions (X should be final...), also should work with relation assertions (X should extend Y...) and it should allow to add as many assertions as needed.

Something like:

public function testDomainObjectsAreFinalAndReadonly(): Rule
{
    return PHPat::rule()
        ->classes(Selector::inNamespace('/.*\\\\DomainObjects/', regex: true))
        ->shouldNotDependOn()
        ->classes(Selector::inNamespace('/.*\\\\Whatever/', regex: true))
        ->excluding(Selector::implements(SomeInterface::class))
        ->and()
        ->shouldNotExtend()
        ->classes(Selector::classname(SomeAbstractClass::class))
        ->and()
        ->shouldBeReadonly();
}

I don't know how easy will be doing the trick with the builder steps, it's already tricky enough 😅

I will be offline until next weekend. I will think about it but I'm sure it's doable. Also open for a PR :)

@calebdw
Copy link
Contributor Author

calebdw commented Oct 26, 2023

That's a good idea, should there also be an or method (does that even make sense in this context)?

@carlosas
Copy link
Owner

That would over-complicate it, let's leave it in and() for now 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants