Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test inner arrays with phpunit

Tags:

phpunit

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.

like image 316
sidhartha Avatar asked Sep 12 '10 07:09

sidhartha


People also ask

What is PHPUnit testing?

PHPUnit is a unit testing framework for the PHP programming language. It is an instance of the xUnit design for unit testing systems that began with SUnit and became popular with JUnit. Even a small software development project usually takes hours of hard work.

What is assertion in PHPUnit?

The assertSame() function is a builtin function in PHPUnit and is used to assert whether the actually obtained value is the same as the expected value or not. This assertion will return true in the case if the expected value is the same as the actual value else returns false.

Is PHPUnit a framework?

PHPUnit is a programmer-oriented testing framework for PHP. It is an instance of the xUnit architecture for unit testing frameworks. PHPUnit 9 is the current stable version. PHPUnit 10 is currently in development.


1 Answers

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.

like image 154
sidhartha Avatar answered Sep 21 '22 00:09

sidhartha