Search code examples
regexfilterphpunitpcre

PHPUnit: exclude two or more namespaces using the --filter option


I need to exclude several namespaces from my local unit tests execution. This is because I don't have some services in my local environment that are available in the CI pipeline. I can include a namespace by running something like:

phpunit --filter 'MyApp\\Providers\\Provider1'

I understand that the filter option accepts regular expressions, as described in https://phpunit.readthedocs.io/en/9.5/textui.html#command-line-options . However, I'm not being able to find a valid one to exclude two or more namespaces.

Tried different combinations like:

phpunit --filter '(?!(MyApp\\Providers\\Provider1)|(MyApp\\Services\\Service1)).*'

which is probably wrong.


Solution

  • You did not specified look ahead starting position. So it will consume whole line from 0 to length of line content.

    For example, if there's 3 lines with a1, a2, a3 and used (?!(a1|a2)).*, result will be 1, 2, a3.

    Because you only skip 0 position of two lines (before a1 and b1).

    If you specify starting position like ^(?!(a1|a2)).*, result will be only a3.

    Because look ahead apply from only start of line(^) and .* will consume whole line.

    https://regex101.com/r/bBEUOD/1