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

ErrorException : sprintf(): Too few arguments #15

Closed
dmason30 opened this issue May 9, 2020 · 2 comments · Fixed by #16
Closed

ErrorException : sprintf(): Too few arguments #15

dmason30 opened this issue May 9, 2020 · 2 comments · Fixed by #16
Assignees

Comments

@dmason30
Copy link

dmason30 commented May 9, 2020

ErrorException : sprintf(): Too few arguments
.../vendor/nunomaduro/laravel-mojito/src/ViewAssertion.php:168
.../vendor/nunomaduro/laravel-mojito/src/ViewAssertion.php:99

I encountered this error when doing a contains where the expected text has a percentage (%) symbol and does not exist in the html provided. See below test that replicates this issue and currently fails but will pass when the issue has been fixed.

public function testPercentageEscaped()
{
    try {
        (new ViewAssertion('<div>This is a test</div>'))
            ->contains('This text has a percentage 43% symbol');

        $this->assertTrue(false, 'Expected assertion error was not thrown.');
    } catch (AssertionFailedError $e) {
        $this->assertSame(
            "Failed asserting that the text `This text has a percentage 43% symbol` exists within `<div>This is a test</div>`.",
            $e->getMessage()
        );
    }
}

One solution would be to add this str_replace line to the contains method which will escape any percentage symbols. Although it might be better to stop using sprintf in ViewAssertion::assert:

public function contains(string $text): ViewAssertion
{
    $escaped = str_replace('%', '%%', $text);
    self::assert(function () use ($text) {
        Assert::assertStringContainsString((string) $text, $this->html);
    }, "Failed asserting that the text `$escaped` exists within %s.");

    return $this;
}
@SimoTod
Copy link
Collaborator

SimoTod commented May 11, 2020

Hi @dmason30, it's a good shout. We are looking into fixing it just now, thanks.
I'll let you know when the new version is released.

@dmason30
Copy link
Author

@SimoTod Much appreciated thanks!

SimoTod added a commit to SimoTod/laravel-mojito that referenced this issue May 11, 2020
SimoTod added a commit that referenced this issue Jun 1, 2020
Bug (#15) sprintf breaks tests when a matching strings contains '%'
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 a pull request may close this issue.

2 participants