tags:

views:

20

answers:

2

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.

A: 

here according to me use as you say 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
            )
    )

i hope it will work

Ricky Dang
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.
sidhartha
@sidharth foreach loop is used to get the value of arrays...
Ricky Dang
@sidhartha: yes, a foreach can be used in phpunit.
prodigitalson
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.
sidhartha
A: 
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.

sidhartha