Friday, October 24, 2025
HomeLanguagesPHPUnit assertFileEquals() Function

PHPUnit assertFileEquals() Function

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

Syntax:

assertFileEquals(string $expected, 
      string $actual[, string $message = ''])

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

  • $expected: This parameter is a string that represents the expected file path.
  • $actual: This parameter is a string that represents the actual file path.
  • $message: This parameter takes a string value. When the test case got failed this string message got displayed as an error message.

Below programs illustrate the assertFileEquals() function in PHPUnit:

Example 1:

PHP




<?php 
use PHPUnit\Framework\TestCase; 
    
class GeeksPhpunitTestCase extends TestCase 
{ 
    public function testNegativeTestcaseForassertFileEquals() 
    { 
        $expected = "/home/bittu/php"; 
        $actual = "/home/bittu/Documents"; 
    
        // Assert function to test whether expected 
        // file content is equal to actual file content or not 
        $this->assertFileEquals(
            $expected, 
            $actual, 
"actual file content is not equals to expected file content"
        ); 
    } 
} 
    
?> 


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::testNegativeTestcaseForassertFileEquals
actual file content is not equals to expected file content
Failed asserting that file "/home/bittu/php" exists.

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

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

Example 2:

PHP




<?php 
use PHPUnit\Framework\TestCase; 
    
class GeeksPhpunitTestCase extends TestCase 
{ 
    public function testPositiveTestcaseForassertFileEquals() 
    { 
        $expected = "/home/bittu/Documents"; 
        $actual = "/home/bittu/Documents"; 
    
        // Assert file equal function to test whether expected 
        // file content is equal to actual file content or not 
        $this->assertFileEquals(
            $expected, 
            $actual, 
"actual file content is not equals to expected file content"
        ); 
    } 
} 
    
?> 


Output:

PHPUnit 8.5.8 by Sebastian Bergmann and contributors.

.                                             1 / 1 (100%)

Time: 89 ms, Memory: 10.00 MB

OK (1 test, 3 assertions)

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

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS