Skip to content

Commit c61c855

Browse files
author
Scott Pringle
committed
Added tests to the Native mapper
1 parent 7638eaa commit c61c855

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

lib/PHPExif/Mapper/Native.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPExif\Exif;
1515
use DateTime;
16+
use Exception;
1617

1718
/**
1819
* PHP Exif Native Mapper
@@ -142,7 +143,11 @@ public function mapRawData(array $data)
142143
// manipulate the value if necessary
143144
switch ($field) {
144145
case self::DATETIMEORIGINAL:
145-
$value = DateTime::createFromFormat('Y:m:d H:i:s', $value);
146+
try {
147+
$value = new DateTime($value);
148+
} catch (Exception $exception) {
149+
continue 2;
150+
}
146151
break;
147152
case self::EXPOSURETIME:
148153
// normalize ExposureTime

tests/PHPExif/Mapper/NativeMapperTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,4 +187,39 @@ public function testMapRawDataCorrectlyFormatsGPSData()
187187
$expected = '40.333452380952,-20.167314814815';
188188
$this->assertEquals($expected, reset($result));
189189
}
190+
191+
public function testMapRawDataCorrectlyFormatsDifferentDateTimeString()
192+
{
193+
$rawData = array(
194+
\PHPExif\Mapper\Native::DATETIMEORIGINAL => '2014-12-15 00:12:00'
195+
);
196+
197+
$mapped = $this->mapper->mapRawData(
198+
$rawData
199+
);
200+
201+
$result = reset($mapped);
202+
$this->assertInstanceOf('\DateTime', $result);
203+
$this->assertEquals(
204+
reset($rawData),
205+
$result->format("Y-m-d H:i:s")
206+
);
207+
}
208+
209+
public function testMapRawDataCorrectlyIgnoresInvalidCreateDate()
210+
{
211+
$rawData = array(
212+
\PHPExif\Mapper\Native::DATETIMEORIGINAL => 'Invalid Date String'
213+
);
214+
215+
$result = $this->mapper->mapRawData(
216+
$rawData
217+
);
218+
219+
$this->assertCount(0, $result);
220+
$this->assertNotEquals(
221+
reset($rawData),
222+
$result
223+
);
224+
}
190225
}

0 commit comments

Comments
 (0)