Wednesday, November 20, 2024
Google search engine
HomeLanguagesPHPUnit assertStringNotMatchesFormat() Function

PHPUnit assertStringNotMatchesFormat() Function

The assertStringNotMatchesFormat() function is a builtin function in PHPUnit and is used to assert whether the difference between the given string and given format. This assertion will return true in the case if the given string not 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:

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

Parameters: This function accepts three parameters as shown in the above syntax. The parameters are described below:

  •  $format: This parameter is represented as a pre-established 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 assertStringNotMatchesFormat() function in PHPUnit:

Example 1:

PHP




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


Output:

PHPUnit 8.5.8 by Sebastian Bergmann and contributors.

F                                                               1 / 1 (100%)

Time: 91 ms, Memory: 10.00 MB

There was 1 failure:

1) GeeksPhpunitTestCase::testNegativeTestcaseForassertStringNotMatchesFormat
given string is not matched with type of format
Failed asserting that string does not match format description.

/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 testPositiveTestcaseForassertStringNotMatchesFormat()
    {
        $format "%d";
        $String  "hh";
       
        // Assert function to test whether difference
        //between given string and type of format
        $this->assertStringNotMatchesFormat(
            $format,
            $String,
            " given string is not matched with
              type of format"
        );
    }
}
   
?>


Output:

PHPUnit 8.5.8 by Sebastian Bergmann and contributors.

.                                                  1 / 1 (100%)

Time: 87 ms, Memory: 10.00 MB

OK (1 test, 1 assertion)

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments