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

Add ShouldHaveAttribute assertion #244

Merged
merged 2 commits into from
Nov 13, 2023

Conversation

gertjuhh
Copy link
Contributor

@gertjuhh gertjuhh commented Nov 6, 2023

This adds a shouldHaveAttribute assertion.

<?php declare(strict_types=1);

namespace App;

use Doctrine\ORM\Mapping\Entity;
use PHPat\Selector\Selector;
use PHPat\Test\Builder\Rule;
use PHPat\Test\PHPat;
use Symfony\Component\HttpKernel\Attribute\AsController;

class ArchitectureTest
{
    public function test_controllers_have_as_controller_attribute(): Rule
    {
        return PHPat::rule()
            ->classes(Selector::inNamespace('App\Controller'))
            ->shouldHaveAttribute()
            ->classes(
                Selector::classname(AsController::class),
            );
    }

    public function test_entities_have_entity_attribute(): Rule
    {
        return PHPat::rule()
            ->classes(Selector::inNamespace('App\Entity'))
            ->shouldHaveAttribute()
            ->classes(
                Selector::classname(Entity::class),
            );
    }
}

@carlosas
Copy link
Owner

carlosas commented Nov 10, 2023

Hi @gertjuhh, thanks for the contribution.

The implementation and the tests look great 🙌
I realized I didn't use the proper term for Attributes in the hasAttribute Selector. I will change that selector to appliesAttribute. Could you also use the term to "apply" instead of "have"? then the error message should be %s should apply the attribute %s

I'm going to do some manual testing of the assertion in the meantime :)

@gertjuhh
Copy link
Contributor Author

Hi @carlosas, thanks for the review.
I just pushed the requested changes.

@carlosas carlosas merged commit 4a46fc8 into carlosas:master Nov 13, 2023
12 checks passed
@gertjuhh gertjuhh deleted the should-have-attribute branch November 13, 2023 19:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants