4

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

1
  • It looks like your problem might just be with the asserts. These happen when they fail. Therefore, an assertTrue(Condition) will actually only through the assertion (error) when the Condition is not true. The assert happens only on failures, so it is as if they are missing the 'only when results is not' from the name. Commented Feb 26, 2013 at 15:41

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks dude. Well explained
4

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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.