开发者

How to test inner arrays with phpunit

开发者 https://www.devze.com 2023-01-15 15:05 出处:网络
I have to test an array with inner arrays. my array looks like the following. $testdata=Array ( [0] => Array

I have to test an array with inner arrays.

my array looks like the following.

$testdata=Array
    (
        [0] => Array
            (
                [label] => 'Ammy'
                [idr] => 'user7'
                [rel] => 7
            )

        [1] => Array
            (
                [label] => 'sidh'
                [idr] => user8
                [rel] => 8
            )

        [2] => Array
            (
                [label] => 'Alan'
                [idr] => 'user9'
                [rel] => 9
            )
    )

in this case my requirement is to assert whether the keys for inner array present using assertArrayHasKey() assertion of phpunit. I tried to do it like this

开发者_如何学运维foreach ($testdata as $values) {
 //print_r($values);
  $this->assertArrayHasKey('idr', $values);
  $this->assertArrayHasKey('rel', $values);

}

but this is not working for me. even the control does not go inside the foreach() loop.

please suggest me some solution for this.


foreach ($testdata as $values) {
 //print_r($values);
  $this->assertArrayHasKey('idr', $values);
  $this->assertArrayHasKey('rel', $values);

}

this part in my question works fine. actually i was not getting the array itself in the test scenario. so it was not going inside the foreach(). now it is solved. i had a mistake in passing args to the function.


This is the example usage

/** Example One */
$testData = [
    [
        'label' => '',
        'idr'   => ''
    ], [
        'label' => '',
        'idr'   => ''
    ], [
        'label' => '',
        'idr'   => ''
    ]
];

$this->assertArrayStructure([
    ['label','idr']
], $testData);



/** Example Two */
$testData = [
    'result' => true,
    'data' => [
        'col_1' => '',
        'col_2' => ''
    ],
];

$this->assertArrayStructure([
    'result', 'data' => ['col_1', 'col_2']
], $testData);



/** Example Three */
$testData = [
    'result' => true,
    'data'   => [
        [
            'col_1' => '',
            'col_2' => ''
        ],
        [
            'col_1' => '',
            'col_2' => ''
        ]
    ],
];

$this->assertArrayStructure([
'result', 'data' => ['col_1', 'col_2']
], $testData, true);

Here is function

/**
 * Like as assertJsonStructure
 *
 * @param array $data
 * @param array $structures #e.g., [ key_1, key_2 => [child_key]]
 * @param bool $dataHasMultiArray #e.g., $data[0][key]
 */
protected function assertArrayStructure(array $structures, array $data, bool $dataHasMultiArray = false)
{
    $i = 0;
    foreach ($structures as $index => $key) {
        if (!is_numeric($index)) {
            $this->assertArrayHasKey($index, $data);
        }
        if (is_string($key)) {
            $this->assertArrayHasKey($key, $data);
        }
        if (is_array($key)) {
            $this->assertArrayHasKeys($key, $dataHasMultiArray ? $data[$index][$i] : $data[$index]);
            $i++;
        }
    }
}

/**
 * @param $structures
 * @param array $data
 */
protected function assertArrayHasKeys($structures, array $data)
{
    foreach ($structures as $key) {
        $this->assertArrayHasKey($key, $data);
    }
}


You can also use

assertArraySubset()

from: https://phpunit.de/manual/current/en/appendixes.assertions.html#appendixes.assertions.assertArraySubset

another solution is to compare arrays and then check if true:

$arrays_are_equal = ($array1 == $array2); // or === if you want identical
$this->assertTrue($arrays_are_equal);


Another option is to test only the first element of the multidimensional Array, like this:

 $this->assertArrayHasKey('idr', $testdata[0]);
 $this->assertArrayHasKey('rel', $testdata[0]);


I believe that that control is not going inside the foreach loop.

Remove whole :

$testdata= Array
    (
        [0] => Array
            (
                [label] => 'Ammy'
                [idr] => 'user7'
                [rel] => 7
            )

        [1] => Array
            (
                [label] => 'sidh'
                [idr] => user8
                [rel] => 8
            )

        [2] => Array
            (
                [label] => 'Alan'
                [idr] => 'user9'
                [rel] => 9
            )
    )
0

精彩评论

暂无评论...
验证码 换一张
取 消