Friday, September 5, 2025
HomeLanguagesPHPUnit assertStringMatchesFormat() Function

PHPUnit assertStringMatchesFormat() Function

The assertStringMatchesFormat() function is a builtin function in PHPUnit and is used to assert whether the given string is matched with the format  or not. This assertion will return true in the case if the given string matched with the type of format else returns false. In case of true the asserted test case got passed else test case got failed.

Syntax:

assertStringMatchesFormat(string $format, string $string[, string $message = ''])

Parameters: This function accepts three parameters as mentioned above and described below:

  •   $format: This parameter is represented preestablish layout for data.
  •   $string: This parameter is of any type which represents the string.
  •   $message: This parameter takes a string value. When the test case got failed this string message got displayed as an error message.

Below examples illustrate the assertStringMatchesFormat() function in PHPUnit:

Example 1:

PHP




<?php
use PHPUnit\Framework\TestCase;
   
class GeeksPhpunitTestCase extends TestCase
{
    public function testNegativeTestcaseForassertStringMatchesFormat()
    {
        $format "%d";
        $String  "hh";
       
        // Assert function to test whether given
        // string is matched with type of format or not
        $this->assertStringMatchesFormat(
            $format,
            $String,
            " given string is matched with type
              of format or not"
        );
    }
}
   
?>


Output:

PHPUnit 8.5.8 by Sebastian Bergmann and contributors.

F                                                            1 / 1 (100%)

Time: 88 ms, Memory: 10.00 MB

There was 1 failure:

1) GeeksPhpunitTestCase::testNegativeTestcaseForassertStringMatchesFormat
type of format is matched to given string
Failed asserting that string matches format description.
--- Expected
+++ Actual
@@ @@
-%d
+hh

/home/lovely/Documents/php/test.php:16

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

Example 2:

PHP




<?php
use PHPUnit\Framework\TestCase;
   
class GeeksPhpunitTestCase extends TestCase
{
    public function testPositiveTestcaseForassertStringMatchesFormat()
    {
        $format "%d";
        $String  "24";
       
        // Assert function to test whether given
        // string is matched with type of format or not
        $this->assertStringMatchesFormat(
            $format,
            $String,
            " given string is matched with type
              of format or not"
        );
    }
}
   
?>


Output:

PHPUnit 8.5.8 by Sebastian Bergmann and contributors.

.                                                  1 / 1 (100%)

Time: 89 ms, Memory: 10.00 MB

OK (1 test, 1 assertion)

Reference:https://phpunit.readthedocs.io/en/9.2/assertions.html#assertstringmatchesformat
 

RELATED ARTICLES

Most Popular

Dominic
32267 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6635 POSTS0 COMMENTS
Nicole Veronica
11801 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11865 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7026 POSTS0 COMMENTS
Thapelo Manthata
6703 POSTS0 COMMENTS
Umr Jansen
6720 POSTS0 COMMENTS