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

Make possible to use assertMatchesPattern in static methods #352

Open
alexander-schranz opened this issue May 4, 2022 · 2 comments
Open

Comments

@alexander-schranz
Copy link
Contributor

Sometimes I get into cases where I want to use the assertMatchesPattern like other assertions in a static method.

Maybe the assertMatchesPattern could be changed from:

    protected function assertMatchesPattern($pattern, $value, string $message = '') : void
    {
        TestCase::assertThat($value, self::matchesPattern($pattern, $this->backtrace), $message);
    }
    protected function static assertMatchesPattern($pattern, $value, string $message = '', ?Backtrace $backtrace = null) : void
    {
        TestCase::assertThat($value, self::matchesPattern($pattern, ?Backtrace $backtrace = null), $message);
    }

As it would be a bc break it could only be done in a new major release.

@norberttech
Copy link
Member

hey, could you share some more context about those use cases? Maybe some simple code examples if possible? (even very simplified)

@alexander-schranz
Copy link
Contributor Author

alexander-schranz commented May 4, 2022

It was something like this:

public function testTest(): string
{
    $html = static::getAsyncMessage();
    
    HtmlJsonMatcher::assertHtml(<<<JSON
    
JSON, $html, 'list');
}
class HtmlJsonMatcher {
     use PHPMatcherAssertions;
    
     public static function assertHtml($expectedJsonPattern, string $html, string $variable): void
     {
        // some other asserts
     
        // extract javascript variable to match against the created component
        preg_match_all('/(<script>([\s\S^?]+?)var ' . $variable . ' =)([\s\S^?]+?)<\/script>/', $text, $matches);
        $content = trim(trim($matches[3][0]), ';');
        // convert javascript object to json via json5 library
        $contentJson = json_encode(json5_decode($content), \JSON_PRETTY_PRINT);

        // following fails while assertMatchesPattern is not able to be used in a static method
        self::assertMatchesPattern($expectedJson, $contentJson);
        
        // more asserts
     }
}

Sure it is possible to move things and not relay on static method here. But as all other assertions of PHPUnit are able to be used in static methods it would be nice to have possibility to use also assertMatchesPattern in static methods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants