Awais Qarni
Awais Qarni

Reputation: 18006

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 :-)

Upvotes: 4

Views: 14040

Answers (2)

Alejandro Moreno
Alejandro Moreno

Reputation: 5718

I'd use assertArrayHasKey ie:

$this->assertArrayHasKey($key, $array);

Because you need to test a complex structure, what I'd do is going to each one of the expected elements and assert that they are there, and they are not empty with assertNotEmpty()

I found this a good example, similar to your problem:

http://opensourceame.com/asserting-that-an-array-contains-another-array-in-phpunit/

Upvotes: 4

Fabian Schmengler
Fabian Schmengler

Reputation: 24551

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.

Upvotes: 4

Related Questions