carlosas / phpat

PHP Architecture Tester - Easy architecture testing for PHP :heavy_check_mark:
https://phpat.dev
MIT License
1.09k stars 44 forks source link

Namespace naming assertions #282

Open rela589n opened 4 months ago

rela589n commented 4 months ago

Enhancement description

It would really make sense to have the ability to enforce not only the actual class naming like this:

        return PHPat::rule()
            ->classes(Selector::extends(ApiTestCase::class))
            ->shouldBeNamed('/ApiTest$/', true);

But to apply rules for namespace:

        return PHPat::rule()
            ->classes(Selector::extends(ApiTestCase::class))
            ->shouldBeNamed('/\\\\Tests\\\\Api\\\\.+ApiTest$/', true);

In this example, phpat would check if api tests are placed in Tests\Api directory and named with ApiTest suffix.

carlosas commented 4 months ago

To achieve this we would need to add the ability to combine assertions (#242) and a new assertion shouldBeInNamespace, being the first one the most challenging feature.

rela589n commented 4 months ago

I wonder why couldn't we allow shouldBeNamed to work with full classname (including namespace) rather than class basename?

Currently shouldBeNamed applied to Foo\Bar class checks only Bar part (rather than Foo\Bar).

From my side, while I was trying to make it work, it has taken me quite a while until I understood that shouldBeNamed works only with the last part of the namespace.

Please, let me know your thoughts on this regard

carlosas commented 1 day ago

TBH I have no hard feelings on this, but the classname selector works with FQCN so this could be an opportunity to standarize selector and assertion. There is a PR open but since this is a breaking change, it will need to be released with v0.11 (which I plan to finish by the end of the month)