Permalink
Browse files

Fixes #871.

Added a comparator for DateTime objects.
  • Loading branch information...
whatthejeff committed Mar 29, 2013
1 parent 9a12669 commit 41b75ee9eded115594aa210175a2c93a07c95a8a
View
@@ -6,6 +6,7 @@ This is the list of changes for the PHPUnit 3.8 release series.
PHPUnit 3.8.0
-------------
+* Implemented #871: Add Comparator for DateTime objects.
* Implemented #859: Added PHP label validation to attribute assertions.
* Implemented #838: Added a base test listener.
* Implemented #835: Printers that extend `PHPUnit_TextUI_ResultPrinter` should have similar construction
View
@@ -90,6 +90,7 @@ function ($class)
'phpunit_framework_basetestlistener' => '/Framework/BaseTestListener.php',
'phpunit_framework_comparator' => '/Framework/Comparator.php',
'phpunit_framework_comparator_array' => '/Framework/Comparator/Array.php',
+ 'phpunit_framework_comparator_datetime' => '/Framework/Comparator/DateTime.php',
'phpunit_framework_comparator_domdocument' => '/Framework/Comparator/DOMDocument.php',
'phpunit_framework_comparator_double' => '/Framework/Comparator/Double.php',
'phpunit_framework_comparator_exception' => '/Framework/Comparator/Exception.php',
@@ -0,0 +1,112 @@
+<?php
+/**
+ * PHPUnit
+ *
+ * Copyright (c) 2001-2013, Sebastian Bergmann <sebastian@phpunit.de>.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * * Neither the name of Sebastian Bergmann nor the names of his
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ * @package PHPUnit
+ * @subpackage Framework
+ * @author Jeff Welch <whatthejeff@gmail.com>
+ * @copyright 2001-2013 Sebastian Bergmann <sebastian@phpunit.de>
+ * @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
+ * @link http://www.phpunit.de/
+ * @since File available since Release 3.8.0
+ */
+
+/**
+ * Compares DateTime instances for equality.
+ *
+ * @package PHPUnit
+ * @subpackage Framework_Comparator
+ * @author Jeff Welch <whatthejeff@gmail.com>
+ * @copyright 2001-2013 Sebastian Bergmann <sebastian@phpunit.de>
+ * @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
+ * @link http://www.phpunit.de/
+ * @since Class available since Release 3.8.0
+ */
+class PHPUnit_Framework_Comparator_DateTime extends PHPUnit_Framework_Comparator_Object
+{
+ /**
+ * Returns whether the comparator can compare two values.
+ *
+ * @param mixed $expected The first value to compare
+ * @param mixed $actual The second value to compare
+ * @return boolean
+ */
+ public function accepts($expected, $actual)
+ {
+ return $expected instanceof DateTime && $actual instanceof DateTime;
+ }
+
+ /**
+ * Asserts that two values are equal.
+ *
+ * @param mixed $expected The first value to compare
+ * @param mixed $actual The second value to compare
+ * @param float $delta The allowed numerical distance between two values to
+ * consider them equal
+ * @param bool $canonicalize If set to TRUE, arrays are sorted before
+ * comparison
+ * @param bool $ignoreCase If set to TRUE, upper- and lowercasing is
+ * ignored when comparing string values
+ * @throws PHPUnit_Framework_ComparisonFailure Thrown when the comparison
+ * fails. Contains information about the
+ * specific errors that lead to the failure.
+ */
+ public function assertEquals($expected, $actual, $delta = 0, $canonicalize = FALSE, $ignoreCase = FALSE)
+ {
+ if ($expected != $actual) {
+ throw new PHPUnit_Framework_ComparisonFailure(
+ $expected,
+ $actual,
+ $this->dateTimeToString($expected),
+ $this->dateTimeToString($actual),
+ FALSE,
+ 'Failed asserting that two DateTimes are equal.'
+ );
+ }
+ }
+
+ /**
+ * Returns an ISO 8601 formatted string representation of a datetime or
+ * 'Invalid DateTime' if the provided DateTime was not properly initialized.
+ *
+ * @param DateTime $datetime The DateTime to get the string representation for
+ * @return string
+ */
+ protected function dateTimeToString(DateTime $datetime)
+ {
+ $string = $datetime->format(DateTime::ISO8601);
+ return $string ? $string : 'Invalid DateTime';
+ }
+}
@@ -82,6 +82,7 @@ public function __construct()
$this->register(new PHPUnit_Framework_Comparator_SplObjectStorage);
$this->register(new PHPUnit_Framework_Comparator_DOMDocument);
$this->register(new PHPUnit_Framework_Comparator_MockObject);
+ $this->register(new PHPUnit_Framework_Comparator_DateTime);
}
/**
@@ -707,6 +707,30 @@ protected function notEqualValues()
$this->createDOMDocument('<foo> bar </foo>'),
$this->createDOMDocument('<foo> bir </foo>'),
),
+ array(
+ new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/New_York')),
+ new DateTime('2013-03-29 03:13:35', new DateTimeZone('America/New_York')),
+ ),
+ array(
+ new DateTime('2013-03-29', new DateTimeZone('America/New_York')),
+ new DateTime('2013-03-30', new DateTimeZone('America/New_York')),
+ ),
+ array(
+ new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/New_York')),
+ new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/Chicago')),
+ ),
+ array(
+ new DateTime('2013-03-30', new DateTimeZone('America/New_York')),
+ new DateTime('2013-03-30', new DateTimeZone('America/Chicago')),
+ ),
+ array(
+ new DateTime('2013-03-29T05:13:35-0600'),
+ new DateTime('2013-03-29T04:13:35-0600'),
+ ),
+ array(
+ new DateTime('2013-03-29T05:13:35-0600'),
+ new DateTime('2013-03-29T05:13:35-0500'),
+ ),
// Exception
//array(new Exception('Exception 1'), new Exception('Exception 2')),
// different types
@@ -779,6 +803,30 @@ protected function equalValues()
$this->createDOMDocument("<root>\n <child/>\n</root>"),
$this->createDOMDocument('<root><child/></root>'),
),
+ array(
+ new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/New_York')),
+ new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/New_York')),
+ ),
+ array(
+ new DateTime('2013-03-29', new DateTimeZone('America/New_York')),
+ new DateTime('2013-03-29', new DateTimeZone('America/New_York')),
+ ),
+ array(
+ new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/New_York')),
+ new DateTime('2013-03-29 03:13:35', new DateTimeZone('America/Chicago')),
+ ),
+ array(
+ new DateTime('2013-03-30', new DateTimeZone('America/New_York')),
+ new DateTime('2013-03-29 23:00:00', new DateTimeZone('America/Chicago')),
+ ),
+ array(
+ new DateTime('@1364616000'),
+ new DateTime('2013-03-29 23:00:00', new DateTimeZone('America/Chicago')),
+ ),
+ array(
+ new DateTime('2013-03-29T05:13:35-0500'),
+ new DateTime('2013-03-29T04:13:35-0600'),
+ ),
// Exception
//array(new Exception('Exception 1'), new Exception('Exception 1')),
// mixed types
@@ -1746,7 +1794,7 @@ public function testReadAttribute5()
{
$this->readAttribute(NULL, 'foo');
}
-
+
/**
* @covers PHPUnit_Framework_Assert::readAttribute
* @covers PHPUnit_Framework_Assert::getStaticAttribute
@@ -2243,7 +2291,7 @@ public function testAssertClassHasAttributeThrowsException2()
{
$this->assertClassHasAttribute('foo', NULL);
}
-
+
/**
* @covers PHPUnit_Framework_Assert::assertClassHasAttribute
* @expectedException PHPUnit_Framework_Exception
@@ -2270,7 +2318,7 @@ public function testAssertClassNotHasAttributeThrowsException2()
{
$this->assertClassNotHasAttribute('foo', NULL);
}
-
+
/**
* @covers PHPUnit_Framework_Assert::assertClassNotHasAttribute
* @expectedException PHPUnit_Framework_Exception
@@ -2297,7 +2345,7 @@ public function testAssertClassHasStaticAttributeThrowsException2()
{
$this->assertClassHasStaticAttribute('foo', NULL);
}
-
+
/**
* @covers PHPUnit_Framework_Assert::assertClassHasStaticAttribute
* @expectedException PHPUnit_Framework_Exception
@@ -2324,7 +2372,7 @@ public function testAssertClassNotHasStaticAttributeThrowsException2()
{
$this->assertClassNotHasStaticAttribute('foo', NULL);
}
-
+
/**
* @covers PHPUnit_Framework_Assert::assertClassNotHasStaticAttribute
* @expectedException PHPUnit_Framework_Exception
@@ -2351,7 +2399,7 @@ public function testAssertObjectHasAttributeThrowsException2()
{
$this->assertObjectHasAttribute('foo', NULL);
}
-
+
/**
* @covers PHPUnit_Framework_Assert::assertObjectHasAttribute
* @expectedException PHPUnit_Framework_Exception
@@ -2378,7 +2426,7 @@ public function testAssertObjectNotHasAttributeThrowsException2()
{
$this->assertObjectNotHasAttribute('foo', NULL);
}
-
+
/**
* @covers PHPUnit_Framework_Assert::assertObjectNotHasAttribute
* @expectedException PHPUnit_Framework_Exception
@@ -85,6 +85,7 @@ public function instanceProvider()
array(array(1), array(1), 'PHPUnit_Framework_Comparator_Array'),
array($tmpfile, $tmpfile, 'PHPUnit_Framework_Comparator_Resource'),
array(new stdClass, new stdClass, 'PHPUnit_Framework_Comparator_Object'),
+ array(new DateTime, new DateTime, 'PHPUnit_Framework_Comparator_DateTime'),
array(new SplObjectStorage, new SplObjectStorage, 'PHPUnit_Framework_Comparator_SplObjectStorage'),
array(new Exception, new Exception, 'PHPUnit_Framework_Comparator_Exception'),
array(new DOMDocument, new DOMDocument, 'PHPUnit_Framework_Comparator_DOMDocument'),
@@ -921,6 +921,19 @@ public function isEqualProvider()
+ <foo/>
+</root>
+EOF
+ ),
+ array(
+ new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/New_York')),
+ new DateTime('2013-03-29 04:13:35', new DateTimeZone('America/Chicago')),
+ <<<EOF
+Failed asserting that two DateTimes are equal.
+--- Expected
++++ Actual
+@@ @@
+-2013-03-29T04:13:35-0400
++2013-03-29T04:13:35-0500
+
EOF
),
);
View
@@ -48,6 +48,7 @@
</dir>
<dir name="Comparator">
<file baseinstalldir="/" name="Array.php" role="php" />
+ <file baseinstalldir="/" name="DateTime.php" role="php" />
<file baseinstalldir="/" name="DOMDocument.php" role="php" />
<file baseinstalldir="/" name="Double.php" role="php" />
<file baseinstalldir="/" name="Exception.php" role="php" />

2 comments on commit 41b75ee

Awesome, thanks!

Contributor

whatthejeff replied Mar 29, 2013

No problemo :)

Please sign in to comment.