Open In App

PHPUnit | assertNotContainsOnly() Function

Last Updated : 26 Jul, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

The assertNotContainsOnly() function is a builtin function in PHPUnit and is used to assert an array not to contain all its values as the given data type. This assertion will return true in the case if the array contains values apart from given data type else return false. In case of true the asserted test case got passed else test case got failed.

Syntax:

assertNotContainsOnly( string $dataType, array $array,
                 boolean $isNativeType = null, string $message = '' )

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

  • $dataType: This parameter is a string which consists of the name of datatype.
  • $array: This parameter is array for which the assert function will check whether it contains only values of given type or not.
  • $isNativeType: This parameters tell whether the datatype is PHP native data type or not.
  • $message: This parameter takes string value. When the testcase got failed this string message got displayed as error message.

Below programs illustrate the assertNotContainsOnly() function in PHPUnit:

Program 1:




<?php
use PHPUnit\Framework\TestCase;
  
class GeeksPhpunitTestCase extends TestCase
{
    public function testNegativeTestcaseForAssertNotContainsOnly()
    {
        $testArray = array("geeksforgeek", "true");
        $dataType = "string"
  
        // Assert function to test whether testArray contains
        // only string values or not
        $this->assertNotContainsOnly($dataType, $testArray, null,
                 "testArray contains only string") ;
    }
}
  
?>


Output:

PHPUnit 8.2.5 by Sebastian Bergmann and contributors.

F                                                                   1 / 1 (100%)

Time: 265 ms, Memory: 10.00 MB

There was 1 failure:

1) GeeksPhpunitTestCase::testNegativeTestcaseForAssertNotContainsOnly
testArray contains only string
Failed asserting that Array &0 (
    0 => 'geeksforgeek'
    1 => 'true'
) does not contain only values of type "string".

/home/shivam/Documents/geeks/phpunit/abc.php:13

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

Program 2:




<?php
use PHPUnit\Framework\TestCase;
  
class GeeksPhpunitTestCase extends TestCase
{
    public function testPositiveTestcaseForAssertNotContainsOnly()
    {
        $testArray = array("geeksforgeek", 4);
        $dataType = "string"
  
        // Assert function to test whether testArray contains
        // only string values
        $this->assertNotContainsOnly($dataType, $testArray, null,
               "testArray  contains only string") ;
    }
}
  
?>


Output:

PHPUnit 8.2.5 by Sebastian Bergmann and contributors.

.                                                                   1 / 1 (100%)

Time: 67 ms, Memory: 10.00 MB

OK (1 test, 1 assertion)

Note: To run testcases with phpunit follow steps from here. Also, assertNotContainsOnly() is supported by phpunit 7 and above.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads