Skip to content

StringMatchesFormatDescription Does Not Handle Escaped % Properly #2907

@mkasberg

Description

@mkasberg
Q A
PHPUnit version 6.5.3
PHP version 7.0.22-0ubuntu0.17.04.1
Installation Method Git

I would expect the following test case to pass because the format string '%%' should produce a literal '%' (sprintf). But it fails in PHPUnit 6.5.3.

StringMatchesFormatDescriptionTest.php

<?php

use PHPUnit\Framework\TestCase;

class StringMatchesFormatDescriptionTest extends TestCase {

    public function testEscapedPercent() {
        $this->assertStringMatchesFormat('This %%d is escaped.', 'This %d is escaped.');
    }

}

Sample Output:

$ ./phpunit StringMatchesFormatDescriptionTest.php 
PHPUnit 6.5.3 by Sebastian Bergmann and contributors.

Runtime:       PHP 7.0.22-0ubuntu0.17.04.1 with Xdebug 2.5.0
Configuration: /home/mkasberg/repositories/phpunit/phpunit.xml

F                                                                   1 / 1 (100%)

Time: 436 ms, Memory: 4.00MB

There was 1 failure:

1) StringMatchesFormatDescriptionTest::testEscapedPercent
Failed asserting that string matches format description.
--- Expected
+++ Actual
@@ @@
-This %%d is escaped.
+This %d is escaped.

/home/mkasberg/repositories/phpunit/src/Framework/Constraint/Constraint.php:117
/home/mkasberg/repositories/phpunit/src/Framework/Constraint/Constraint.php:62
/home/mkasberg/repositories/phpunit/src/Framework/Assert.php:2116
/home/mkasberg/repositories/phpunit/src/Framework/Assert.php:1768
/home/mkasberg/repositories/phpunit/StringMatchesFormatDescriptionTest.php:8
/home/mkasberg/repositories/phpunit/src/Framework/TestCase.php:1071
/home/mkasberg/repositories/phpunit/src/Framework/TestCase.php:939
/home/mkasberg/repositories/phpunit/src/Framework/TestResult.php:698
/home/mkasberg/repositories/phpunit/src/Framework/TestCase.php:894
/home/mkasberg/repositories/phpunit/src/Framework/TestSuite.php:755
/home/mkasberg/repositories/phpunit/src/TextUI/TestRunner.php:546
/home/mkasberg/repositories/phpunit/src/TextUI/Command.php:195
/home/mkasberg/repositories/phpunit/src/TextUI/Command.php:148

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.

I skimmed the source code for StringMatchesFormatDescription.php, and it is perhaps worth noting that we also seem to be missing a few other descriptors which are supported by PHP. For example, %b, %E, and %g. (Should we expect PHPUnit to support all of these?)

None of these are major bugs since it's certainly possible to work around them, but it might be nice to have better support for format string matching.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions