7

Here is what phpunit says:

1) Asgard\Entity\Tests\EntityTest::testToArray
Failed asserting that two arrays are equal.
--- Expected
+++ Actual
@@ @@
 Array (
     'id' => null
     'title' => 'Test Title'
     'content' => 'Test Content'
     'published' => 2015-03-04T11:19:50+0000
     'comments' => Array (
         0 => Array (
             'id' => null
             'content' => 'foo'
             'published' => 2015-03-04T11:19:50+0000
             'another_property' => null
             'news' => null
         )
         1 => Array (
             'id' => null
             'content' => 'bar'
             'published' => 2015-03-04T11:19:50+0000
             'another_property' => null
             'news' => null
         )
         2 => Array (
             'id' => null
             'content' => 'baz'
             'published' => 2015-03-04T11:19:50+0000
             'another_property' => null
             'news' => null
         )
     )
     'another_property' => null
 )

https://travis-ci.org/asgardphp/asgard/jobs/53029084

There is no difference between "expected" and "actual". The tests usually pass, but sometimes would fail on this.

4
  • the test fail always with php 5.6 ? Commented Mar 5, 2015 at 8:42
  • So far yes, I have only seen it failing with php 5.6 Commented Mar 5, 2015 at 11:05
  • i haven't php 5.6 yet. With 5.5 and 5.3 i can't replicate your problem. Commented Mar 5, 2015 at 12:07
  • Actually just happened with 5.4: travis-ci.org/asgardphp/asgard/jobs/54541383 Commented Mar 16, 2015 at 9:44

3 Answers 3

5

Found the issue. Elements were not in the same order. A bit annoying though that the output doesn't show it.

I have replaced assertEquals with:

$this->assertTrue($this->similar_arrays($arr1, $arr2));

protected function similar_arrays($a, $b) {
    if(is_array($a) && is_array($b)) {
        if(count(array_diff(array_keys($a), array_keys($b))) > 0)
            return false;

        foreach($a as $k => $v) {
            if(!$this->similar_arrays($v, $b[$k]))
                return false;
        }

        return true;
    }
    else
        return $a === $b;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Since you're using DateTime in your tests - Are you sure you aren't getting test that start in one second, and finish in another so there is a second difference in the timestamps depending on which stage you look at them? - Using dynamic dates and time in tests is a pretty common reason for occasional failed tests.

2 Comments

The datetime object is generated only once and is used both by the actual and the expected results. And anyway, even if this was the problem, phpunit should be showing a difference betweent expected/actual.
Thanks a lot, I would have wasted hours without the hint that datetimes could cause the problem. My problem was a little bit different though, I used the default convertion from laravel (Carbon), it worked after using a different format (iso8601).
1

Check if you have EOL in your values.

If you have a value like so:

'x' => 'line1

line2'

change it to

'x' => "line1\nline2"

Note: this may not answer direct issue the OP had, but this is what worked for me.

Comments

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.