Friday, July 24, 2026
HomeLanguagesPHPUnit assertStringEqualsFile() Function

PHPUnit assertStringEqualsFile() Function

The assertStringEqualsFile() function is a builtin function in PHPUnit and is used to assert whether the actual string content obtained is equaled to the expected file or not. This assertion will return true in the case if the expected file content is the same as the actual string content else returns false. In case of true the asserted test case got passed else test case got failed.

Syntax:

assertStringEqualsFile(string $expectedFile, 
string $actualString[, string $message = ''])

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

  • $expectedfile: This parameter is of any type which represents the expected file.
  • $actualstring: This parameter is of any type which represents the actual string content.
  • $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 assertStringEqualsFile() function in PHPUnit:

Example 1:

PHP




<?php 
use PHPUnit\Framework\TestCase; 
    
class GeeksPhpunitTestCase extends TestCase 
{ 
    public function testNegativeTestcaseForassertStringEqualsFile()
    { 
        $expectedfile = "/home/lovely/Documents/php/abc";
        $actualstring = "File has neveropen"; 
    
        // Assert function to test whether 
        // expected file content is equal to 
        // actual string content or not 
        $this->assertStringEqualsFile(
            $expectedfile,
            $actualstring, 
            "actual string content is equals to
             expected file content or not"
        ); 
    } 
} 
    
?>


Output:

PHPUnit 8.5.8 by Sebastian Bergmann and contributors.

F                                                         1 / 1 (100%)

Time: 89 ms, Memory: 10.00 MB

There was 1 failure:

1) GeeksPhpunitTestCase::testNegativeTestcaseForassertStringEqualsFile
actual string content is not equals to expected file content
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'File has not neveropen'
+'File has neveropen'

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

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

Example 2:

PHP




<?php 
use PHPUnit\Framework\TestCase; 
    
class GeeksPhpunitTestCase extends TestCase 
{ 
    public function testPositiveTestcaseForassertStringEqualsFile()
    { 
        $expectedfile = "/home/lovely/Documents/php/abc";
        $actualstring = "File has neveropen"; 
    
        // Assert function to test whether 
        // expected file content is equal 
        // to actual string content or not 
        $this->assertStringEqualsFile(
            $expectedfile,
            $actualstring, 
            "actual string content is equals 
             to expected file content or not"
        ); 
    } 
} 
    
?>


Output:

PHPUnit 8.5.8 by Sebastian Bergmann and contributors.

.                                                  1 / 1 (100%)

Time: 87 ms, Memory: 10.00 MB

OK (1 test, 2 assertions)

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

RELATED ARTICLES

4 COMMENTS

Most Popular

Dominic
32520 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6902 POSTS0 COMMENTS
Nicole Veronica
12017 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12115 POSTS0 COMMENTS
Shaida Kate Naidoo
7023 POSTS0 COMMENTS
Ted Musemwa
7265 POSTS0 COMMENTS
Thapelo Manthata
6980 POSTS0 COMMENTS
Umr Jansen
6971 POSTS0 COMMENTS