Skip to content

Commit d7cf233

Browse files
committed
Merge pull request #103 from phpcr/date_tests
refactored date comparisons to not care about timezone
2 parents 2868668 + 944de12 commit d7cf233

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

inc/BaseCase.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use PHPCR\SessionInterface;
55
use PHPCR\NodeInterface;
6+
use \DateTime;
67

78
// PHPUnit 3.4 compat
89
if (method_exists('PHPUnit_Util_Filter', 'addDirectoryToFilter')) {
@@ -117,7 +118,7 @@ protected function setUp()
117118
$case = $parts[$case_n];
118119
$chapter = '';
119120

120-
for($i = 2; $i < $case_n; $i++) {
121+
for ($i = 2; $i < $case_n; $i++) {
121122
$chapter .= $parts[$i] . '\\';
122123
}
123124

@@ -241,4 +242,14 @@ protected function checkNodeProperty(NodeInterface $node, $property, $value)
241242
$this->assertTrue($node->hasProperty($property));
242243
$this->assertEquals($value, $node->getPropertyValue($property));
243244
}
245+
246+
protected function assertEqualDateString($date1, $date2)
247+
{
248+
$this->assertEquals(strtotime($date1), strtotime($date2));
249+
}
250+
251+
protected function assertEqualDateTime(DateTime $date1, DateTime $date2)
252+
{
253+
$this->assertEquals($date1->getTimestamp(), $date2->getTimestamp());
254+
}
244255
}

tests/05_Reading/PropertyReadMethodsTest.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,11 @@ public function testGetValueMulti()
145145
*/
146146
public function testGetString()
147147
{
148-
$expectedStr = '2011-04-21T14:34:20'; //date precision might not be down to milliseconds
148+
$expectedStr = '2011-04-21T14:34:20+01:00'; //date precision might not be down to milliseconds
149149
$str = $this->dateProperty->getString();
150150
$this->assertInternalType('string', $str);
151-
$this->assertStringStartsWith($expectedStr, $str);
152151

152+
$this->assertEqualDateString($expectedStr, $str);
153153
$str = $this->valProperty->getString();
154154
$this->assertInternalType('string', $str);
155155
$this->assertEquals('bar', $str);
@@ -298,11 +298,13 @@ public function testGetDateMulti()
298298
$this->assertInstanceOf('DateTime', $v);
299299
}
300300
//check correct values and sort order
301-
$expected = array(
301+
$expectedArray = array(
302302
new \DateTime('2011-04-22T14:34:20+01:00'),
303303
new \DateTime('2011-10-23T14:34:20+01:00'),
304304
new \DateTime('2010-10-23T14:34:20+01:00'));
305-
$this->assertEquals($expected, $arr);
305+
foreach ($expectedArray as $key => $expected) {
306+
$this->assertEqualDateTime($expected, $arr[$key]);
307+
}
306308
}
307309
/**
308310
* arbitrary string can not be converted to date

tests/07_Export/ExportRepositoryContentTest.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ private function buildPath(DOMNode $n)
5454
return "/$ret";
5555
}
5656

57+
private function isDate($date)
58+
{
59+
return preg_match('/^\d{4}-\d{2}-\d{2}/', $date);
60+
}
61+
5762
/**
5863
* compare two system view documents.
5964
*
@@ -81,7 +86,11 @@ private function assertEquivalentSystem(DOMElement $expected, DOMElement $output
8186
$o = $output->childNodes->item($index);
8287
$this->assertInstanceOf('DOMElement', $o, "No child element at $index in ".$this->buildPath($child));
8388
$this->assertEquals('sv:value', $o->tagName, 'Unexpected tag name at '.$this->buildPath($expected)."sv:value[$index]");
84-
$this->assertEquals($child->textContent, $o->textContent, 'Not the same text at '.$this->buildPath($output)."sv:value[$index]");
89+
if ($this->isDate($child->textContent) && $this->isDate($o->textContent)) {
90+
$this->assertEqualDateString($child->textContent, $o->textContent, 'Not the same date at '.$this->buildPath($output)."sv:value[$index]");
91+
} else {
92+
$this->assertEquals($child->textContent, $o->textContent, 'Not the same text at '.$this->buildPath($output)."sv:value[$index]");
93+
}
8594
}
8695
}
8796
} elseif ($expected->tagName == 'sv:node') {
@@ -130,7 +139,11 @@ private function assertEquivalentDocument(DOMElement $expected, DOMElement $outp
130139
} else {
131140
$oattr = $output->attributes->getNamedItem($attr->name);
132141
$this->assertNotNull($oattr, 'missing attribute '.$attr->name.' at '.$this->buildPath($expected));
133-
$this->assertEquals($attr->value, $oattr->value, 'wrong attribute value at '.$this->buildPath($expected).'/'.$attr->name);
142+
if ($this->isDate($attr->value) && $this->isDate($oattr->value)) {
143+
$this->assertEqualDateString($attr->value, $oattr->value, 'wrong attribute value at '.$this->buildPath($expected).'/'.$attr->name);
144+
} else {
145+
$this->assertEquals($attr->value, $oattr->value, 'wrong attribute value at '.$this->buildPath($expected).'/'.$attr->name);
146+
}
134147
}
135148
}
136149

0 commit comments

Comments
 (0)