为返回数组的函数编写PHPUnit Test

Awa*_*rni 3 php phpunit unit-testing

现在我正在玩PHPUnit.我已经完成了它的文档,但我无法理解它.让我解释一下我的情况.

我在类中有一个函数,它有三个参数1 array, 2 some string, 3 a class object.此函数通过将第二个参数作为数组的索引并将结果作为该索引的对象来返回数组.我的功能如下

 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;
}
Run Code Online (Sandbox Code Playgroud)

如果我把这个函数称为

    $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);
Run Code Online (Sandbox Code Playgroud)

该函数返回数组

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

    )
)
Run Code Online (Sandbox Code Playgroud)

现在我想编写一个PHPUnit Test来检查对象的属性是否isRepairable, level and infoText存在而不是空的.我已经明白assertNotEmpty并且assertAttributeEmpty可以做一些事情但是我无法理解如何去做.

我的测试看起来像

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);
}
Run Code Online (Sandbox Code Playgroud)

任何人都可以指导:-)

Fab*_*ler 5

最后一行应assertContains代替assertNotContains.测试的后续步骤是:

$this->assertContains('Database', $result['databaseschema']);
$this->assertAttributeNotEmpty('isRepairable', $result['databaseschema']['Database']);
$this->assertAttributeNotEmpty('level', $result['databaseschema']['Database']);
$this->assertAttributeNotEmpty('infoText', $result['databaseschema']['Database']);
Run Code Online (Sandbox Code Playgroud)

assertAttributeNotEmpty将属性名称和对象作为参数,就像assertContains获取数组键和数组一样.