Open In App

PHPunit | assertContainsOnlyInstancesOf() Function

Improve
Improve
Like Article
Like
Save
Share
Report

The assertContainsOnlyInstancesOf() function is a builtin function in PHPUnit and is used to assert an array to contain all its values as instance of the given class. This assertion will return true in the case if the array contains the only instances of given class else returns false. In case of true the asserted test case got passed else test case got failed.
Syntax: 
 

assertContainsOnlyInstancesOf( string $classname, array $array, string $message = '' )

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

  • $classname: This parameter is a string which is the name of class.
  • $array: This parameter is array for which the assert function will check whether it contains only instances of given class 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 assertContainsOnlyInstancesOf() function in PHPUnit:
Program 1: 
 

php




<?php
 
use PHPUnit\Framework\TestCase;
 
class Foo {
    public function dummyFunction(){return true;}
}
 
class Bar {
    public function dummyFunction(){return true;}
}
 
class GeeksPhpunitTestCase extends TestCase
{
    public function testNegativeTestcaseForAssertContainsOnlyInstancesOf()
    {
     
        // Assert function to test whether testArray contains
        // only instance of Foo or not
        $this->assertContainsOnlyInstancesOf(
            Foo::class,
            [new Foo, new Bar, new Foo],
            "testArray doesn't contains only instance of Foo"
        );
    }
}
 
?>


Output: 
 

PHPUnit 8.2.5 by Sebastian Bergmann and contributors.

F                                                                   1 / 1 (100%)

Time: 81 ms, Memory: 10.00 MB

There was 1 failure:

1) GeeksPhpunitTestCase::testNegativeTestcaseForAssertContainsOnlyInstancesOf
testArray doesn't contains only instance of For
Failed asserting that Array &0 (
    0 => Foo Object &00000000051a5c33000000005d98efba ()
    1 => Bar Object &00000000051a5d77000000005d98efba ()
    2 => Foo Object &00000000051a5c32000000005d98efba ()
) contains only values of type "Foo".

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

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

Program 2: 
 

php




<?php
use PHPUnit\Framework\TestCase;
 
class Foo {
    public function dummyFunction(){return true;}
}
 
class Bar {
    public function dummyFunction(){return true;}
}
 
class GeeksPhpunitTestCase extends TestCase
{
    public function testNegativeTestcaseForAssertContainsOnlyInstancesOf()
    {
        // Assert function to test whether testArray contains
        // only instance of Foo or not
        $this->assertContainsOnlyInstancesOf(
            Foo::class,
            [new Foo, new Foo],
            "testArray contains only instance of Foo"
        );
    }
}
 
?>


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, assertContainsOnlyInstancesOf() is supported by phpunit 7 and above.
 



Last Updated : 03 Feb, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads