Open In App

PHPUnit assertLessThanOrEqual() Function

Improve
Improve
Like Article
Like
Save
Share
Report

The assertLessThanOrEqual() function is a builtin function in PHPUnit and is used to assert whether the actually obtained value is Less than or equal to the expected value or not. This assertion will return true in the case if the actual value is Less than or equal to the expected value else returns false. In case of true the asserted test case got passed else test case got failed.

Syntax:

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

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

  • $expected: This parameter is of any type of numeric value which represents the expected data.
  • $actual: This parameter is of any type of numeric value which represents the actual data.
  • $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 assertLessThanOrEqual() function in PHPUnit:

Example 1:  

PHP




<?php 
use PHPUnit\Framework\TestCase; 
    
class GeeksPhpunitTestCase extends TestCase 
    public function testNegativeForassertLessThanOrEqualWithEqualVal()
    {    $expected = 22; 
        $actual = 420;
        // Assert function to test whether expected 
        // value is less than actual or not 
        $this->assertLessThanOrEqual( 
            $expected
            $actual
            "actual value is Less than or equal to expected"
        );
          
    }
   
  
      
    public function testNegativeForassertLessThanOrEqualWithLessVal()
    {  
     $expected = 22; 
        $actual = 220;
        // Assert function to test whether expected 
        // value is less than actual or not 
          
        $this->assertLessThanOrEqual( 
            $expected
            $actual
            "actual value is Less than or equal to expected"
        ); 
    }  
 
?> 


Output:

PHPUnit 8.5.8 by Sebastian Bergmann and contributors.

FF                                                          2 / 2 (100%)

Time: 91 ms, Memory: 10.00 MB

There were 2 failures:

1) GeeksPhpunitTestCase::testNegativeForassertLessThanOrEqualWithEqualVal
actual value is Less than or equal to expected
Failed asserting that 420 is equal to 22 or is less than 22.

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

2) GeeksPhpunitTestCase::testNegativeForassertLessThanOrEqualWithLessVal
actual value is Less than or equal to expected
Failed asserting that 220 is equal to 22 or is less than 22.

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

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

Example 2:

PHP




<?php 
use PHPUnit\Framework\TestCase; 
    
class GeeksPhpunitTestCase extends TestCase 
    public function testPositiveForassertLessThanOrEqualWithEqualVal()
    {    $expected = 22; 
        $actual = 10;
        // Assert function to test whether expected 
        // value is less than actual or not 
        $this->assertLessThanOrEqual( 
            $expected
            $actual
            "actual value is greater than or equal to expected"
        );
          
    }
   
  
      
    public function testPositiveForassertLessThanOrEqualWithLessVal()
    {  
     $expected = 22; 
        $actual = 22;
        // Assert function to test whether expected 
        // value is less than actual or not 
          
        $this->assertLessThanOrEqual( 
            $expected
            $actual
            "actual value is greater than or equal to expected"
        ); 
    }  
 
?> 


Output:

PHPUnit 8.5.8 by Sebastian Bergmann and contributors.

..                                                 2 / 2 (100%)

Time: 87 ms, Memory: 10.00 MB

OK (2 tests, 4 assertions)
lovely@lovely:~/Documents/php$ ./phpunit test.php


PHPUnit 8.5.8 by Sebastian Bergmann and contributors.

..                                                 2 / 2 (100%)

Time: 87 ms, Memory: 10.00 MB

OK (2 tests, 4 assertions)

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



Last Updated : 07 Aug, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads