Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing PHPUnit Test for a function returning an array

now a days I am playing with PHPUnit. I have gone through its documentation but I am unable to understand it well. Let me explain my case.

I have a function in a class which takes three parameters 1 array, 2 some string, 3 a class object. This function returns the array by putting second parameter as an index of the array and result as object of that index. My function is as below

 public function construct($testArray, $test,$analysisResult) {
    $postedTest = explode('_', $test);
    $testName = end($postedTest);
    $postedTest = implode("_", array_slice($postedTest, 0, -1));
    if (in_array($postedTest, array_keys($testArray))) {
        $testArray[$postedTest][$testName] = $analysisResult;
    } else {
        $testArray[$postedTest] = array($testName => $analysisResult);
    }
    return $testArray;
}

If I call this function like

    $constructObj = new Application_Model_ConstructTree();
    $test=$this->getMockForAbstractClass('Abstract_Result');
    $test->level = "Error";
    $test->infoText = "Not Immplemented";
    $testArray = Array('databaseschema' => Array('Database' => $test));

    $result = $constructObj->construct($testArray,"Database",$test);

The function returns the array like

Array
(
 [databaseschema] => Array
    (
        [Database] => AnalysisResult Object
            (
                [isRepairable] => 1
                [level] => Error
                [infoText] => Not Implemented
            )

    )
)

Now I want to write a PHPUnit Test to check that the attributes of object like isRepairable, level and infoText exists and not empty. I have gone an idea that assertNotEmpty and assertAttributeEmpty can do some thing But I am unable to understand how to do it.

My test looks like

public function testcontruct() {
    $constructObj = new Application_Model_ConstructTree();
    $test=$this->getMockForAbstractClass('Abstract_Result');
    $test->level = "Error";
    $test->infoText = "Not Immplemented";
    $testArray = Array('databaseschema' => Array('Database' => $test));

    $result = $constructObj->construct($testArray,"Database",$test);

    $this->assertNotCount(0, $result);
    $this->assertNotContains('databaseschema', $result);
}

Can anyone please guide :-)

like image 838
Awais Qarni Avatar asked Feb 18 '23 09:02

Awais Qarni


1 Answers

The last line should be assertContains instead assertNotContains. The next steps in your test would be:

$this->assertContains('Database', $result['databaseschema']);
$this->assertAttributeNotEmpty('isRepairable', $result['databaseschema']['Database']);
$this->assertAttributeNotEmpty('level', $result['databaseschema']['Database']);
$this->assertAttributeNotEmpty('infoText', $result['databaseschema']['Database']);

assertAttributeNotEmpty takes the attribute name and the object as parameters, just as assertContains takes the array key and the array.

like image 89
Fabian Schmengler Avatar answered Mar 05 '23 16:03

Fabian Schmengler