PHPUnit 3.7 does not take $delta into account when comparing arrays and objects which include DateTime objects #1175

Closed
jeremykendall opened this Issue Mar 11, 2014 · 1 comment

Projects

None yet

2 participants

@jeremykendall

When comparing objects and arrays that include \DateTime objects, if the \DateTime objects are not equal and I add a $delta, the arrays or objects are still not equal. The $delta does not seem to be applied. The same tests will pass in PHPUnit 4.

Example test (tests with $delta fail in 3.7, pass in 4):

<?php

class DateTimeTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @dataProvider dateTimeDataProvider
     */
    public function testNotEquals($thingOne, $thingTwo)
    {
        $this->assertNotEquals($thingOne, $thingTwo);
    }

    /**
     * This test demonstrates how the $delta arg does not take effect
     * when applied to arrays and objects containing DateTime objects in PHPUnit 
     * versions < 4.0.
     * 
     * @dataProvider dateTimeDataProvider
     */
    public function testShouldBeEqual($thingOne, $thingTwo)
    {
        $this->assertEquals($thingOne, $thingTwo, null, 5);
    }

    public function dateTimeDataProvider()
    {
        // Dates
        $dateOne = new \DateTime();

        $dateTwo = clone $dateOne;
        $dateTwo->modify('-3 seconds');

        // Array
        $arrayOne = array('date' => $dateOne);
        $arrayTwo = array('date' => $dateTwo);

        // Multi-dimensional array
        $arrayThree = array('name' => 'Jeremy', 'data' => array('dob' => $dateOne));
        $arrayFour = array('name' => 'Jeremy', 'data' => array('dob' => $dateTwo));

        // Objects
        $userOne = new \stdClass();
        $userOne->name = 'Jeremy';
        $userOne->dob = $dateOne;
        $userTwo = clone $userOne;
        $userTwo->dob = $dateTwo;

        return array(
            array($dateOne, $dateTwo),
            array($arrayOne, $arrayTwo),
            array($arrayThree, $arrayFour),
            array($userOne, $userTwo),
        );
    }
}

Test results for version 3.7.32:

PHPUnit 3.7.32 by Sebastian Bergmann.

....FFFF

Time: 30 ms, Memory: 2.75Mb

There were 4 failures:

1) DateTimeTest::testShouldBeEqual with data set #0 (DateTime, DateTime)
Failed asserting that two objects are equal.
--- Expected
+++ Actual
@@ @@
 DateTime Object (
-    'date' => '2014-03-11 05:01:19'
+    'date' => '2014-03-11 05:01:16'
     'timezone_type' => 3
     'timezone' => 'UTC'
 )

/Users/jkendall/dev/phpunit-datetime/date-time-tests.php:22

2) DateTimeTest::testShouldBeEqual with data set #1 (array(DateTime), array(DateTime))
Failed asserting that two arrays are equal.
--- Expected
+++ Actual
@@ @@
 Array (
     'date' => DateTime Object (
-        'date' => '2014-03-11 05:01:19'
+        'date' => '2014-03-11 05:01:16'
         'timezone_type' => 3
         'timezone' => 'UTC'
     )
 )

/Users/jkendall/dev/phpunit-datetime/date-time-tests.php:22

3) DateTimeTest::testShouldBeEqual with data set #2 (array('Jeremy', array(DateTime)), array('Jeremy', array(DateTime)))
Failed asserting that two arrays are equal.
--- Expected
+++ Actual
@@ @@
 Array (
     'name' => 'Jeremy'
     'data' => Array (
         'dob' => DateTime Object (
-            'date' => '2014-03-11 05:01:19'
+            'date' => '2014-03-11 05:01:16'
             'timezone_type' => 3
             'timezone' => 'UTC'
         )
     )
 )

/Users/jkendall/dev/phpunit-datetime/date-time-tests.php:22

4) DateTimeTest::testShouldBeEqual with data set #3 (stdClass, stdClass)
Failed asserting that two objects are equal.
--- Expected
+++ Actual
@@ @@
 stdClass Object (
     'name' => 'Jeremy'
     'dob' => DateTime Object (
-        'date' => '2014-03-11 05:01:19'
+        'date' => '2014-03-11 05:01:16'
         'timezone_type' => 3
         'timezone' => 'UTC'
     )
 )

/Users/jkendall/dev/phpunit-datetime/date-time-tests.php:22

FAILURES!
Tests: 8, Assertions: 8, Failures: 4.

Test results for version 4.0.5:

PHPUnit 4.0.5 by Sebastian Bergmann.

The Xdebug extension is not loaded. No code coverage will be generated.

........

Time: 35 ms, Memory: 2.75Mb

OK (8 tests, 8 assertions)
@whatthejeff
Collaborator

Yeah, support for this wasn't added until 4.0. See:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment