9

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.

1
  • 1
    If it never makes it to the loop then there is a problem elsewhere and we cant tell only with the code youve posted. Your usage is correct as is. Commented Sep 12, 2010 at 7:55

5 Answers 5

5
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.

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

Comments

1

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);
    }
}

Comments

0

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

1 Comment

this function has note of this @deprecated github.com/sebastianbergmann/phpunit/issues/3494
0

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]);

Comments

-1

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

2 Comments

in real case whole is not there. i just kept it here to make it more descriptive. any way i will edit that. can u please suggest whether foreach() be used in phpunit.
thanks everybody for answering. i found the mistake. it is no where related to foreach.rather it was in passing args to function. now foreach() works fine. thank u again to all.

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.