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.

leyou
  • 806
  • 1
  • 13
  • 25

3 Answers3

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;
}
leyou
  • 806
  • 1
  • 13
  • 25
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.

OddEssay
  • 1,334
  • 11
  • 19
  • 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. – leyou Mar 18 '15 at 17:12
  • 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). – Maaaaa Dec 31 '17 at 00:04
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.

Dennis
  • 7,907
  • 11
  • 65
  • 115