Skip to content

Commit d5ae08a

Browse files
committed
Creation date Field Mapper & unit tests
Signed-off-by: Tom Van Herreweghe <[email protected]>
1 parent dc0f460 commit d5ae08a

File tree

2 files changed

+183
-0
lines changed

2 files changed

+183
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
/**
3+
* Mapper for mapping data between raw input and a datetime object
4+
*
5+
* @category PHPExif
6+
* @copyright Copyright (c) 2016 Tom Van Herreweghe <[email protected]>
7+
* @license http://github.com/PHPExif/php-exif-exiftool/blob/master/LICENSE MIT License
8+
* @link http://github.com/PHPExif/php-exif-exiftool for the canonical source repository
9+
* @package Exiftool
10+
*/
11+
12+
namespace PHPExif\Adapter\Exiftool\Reader\Mapper\Exif;
13+
14+
use PHPExif\Common\Data\ExifInterface;
15+
use PHPExif\Common\Mapper\FieldMapper;
16+
use PHPExif\Common\Mapper\GuardInvalidArgumentsForExifTrait;
17+
use \DateTimeImmutable;
18+
19+
/**
20+
* Mapper
21+
*
22+
* @category PHPExif
23+
* @package Common
24+
*/
25+
class DateTimeFieldMapper implements FieldMapper
26+
{
27+
use GuardInvalidArgumentsForExifTrait;
28+
29+
/**
30+
* {@inheritDoc}
31+
*/
32+
public function getSupportedFields()
33+
{
34+
return array(
35+
DateTimeImmutable::class,
36+
);
37+
}
38+
39+
/**
40+
* {@inheritDoc}
41+
*/
42+
public function mapField($field, array $input, &$output)
43+
{
44+
$this->guardInvalidArguments($field, $input, $output);
45+
46+
$validKeys = [
47+
'system:filemodifydate',
48+
'composite:subsecdatetimeoriginal',
49+
'composite:subsecmodifydate',
50+
'exififd:datetimeoriginal',
51+
'exififd:createdate',
52+
'ifd0:modifydate',
53+
];
54+
55+
while ($key = array_shift($validKeys)) {
56+
if (!array_key_exists($key, $input)) {
57+
continue;
58+
}
59+
60+
$datetimeOriginal = new DateTimeImmutable($input[$key]);
61+
$output = $output->withCreationDate($datetimeOriginal);
62+
break;
63+
}
64+
}
65+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
3+
namespace Tests\PHPExif\Adapter\Exiftool\Reader\Mapper\Exif;
4+
5+
use Mockery as m;
6+
use PHPExif\Adapter\Exiftool\Reader\Mapper\Exif\DateTimeFieldMapper;
7+
use PHPExif\Common\Data\Exif;
8+
use \DateTimeImmutable;
9+
10+
/**
11+
* Class: DateTimeFieldMapperTest
12+
*
13+
* @see \PHPUnit_Framework_TestCase
14+
* @coversDefaultClass \PHPExif\Adapter\Exiftool\Reader\Mapper\Exif\DateTimeFieldMapper
15+
* @covers ::<!public>
16+
*/
17+
class DateTimeFieldMapperTest extends BaseFieldMapperTest
18+
{
19+
/**
20+
* FQCN of the fieldmapper being tested
21+
*
22+
* @var mixed
23+
*/
24+
protected $fieldMapperClass = DateTimeFieldMapper::class;
25+
26+
/**
27+
* List of supported fields
28+
*
29+
* @var array
30+
*/
31+
protected $supportedFields = [
32+
DateTimeImmutable::class,
33+
];
34+
35+
/**
36+
* Valid input data
37+
*
38+
* @var array
39+
*/
40+
protected $validInput = [
41+
'system:filemodifydate' => '2016-11-17 20:00:00',
42+
'composite:subsecdatetimeoriginal' => '2016-11-17 20:01:00',
43+
'composite:subsecmodifydate' => '2016-11-17 20:02:00',
44+
'exififd:datetimeoriginal' => '2016-11-17 20:03:00',
45+
'exififd:createdate' => '2016-11-17 20:04:00',
46+
'ifd0:modifydate' => '2016-11-17 20:05:00',
47+
];
48+
49+
/**
50+
* @covers ::mapField
51+
* @group mapper
52+
*
53+
* @return void
54+
*/
55+
public function testMapFieldHasDataInOutput()
56+
{
57+
$field = reset($this->supportedFields);
58+
$output = new Exif;
59+
$mapper = new $this->fieldMapperClass();
60+
61+
$originalData = $output->getCreationDate();
62+
$mapper->mapField($field, $this->validInput, $output);
63+
$newData = $output->getCreationDate();
64+
65+
$this->assertNotSame($originalData, $newData);
66+
67+
$this->assertInstanceOf(
68+
DateTimeImmutable::class,
69+
$newData
70+
);
71+
72+
$this->assertEquals(
73+
$this->validInput['system:filemodifydate'],
74+
$newData->format('Y-m-d H:i:s')
75+
);
76+
}
77+
78+
/**
79+
* @covers ::mapField
80+
* @group mapper
81+
*
82+
* @return void
83+
*/
84+
public function testMapFieldTraversesSetOfKeys()
85+
{
86+
$field = reset($this->supportedFields);
87+
$output = new Exif;
88+
$mapper = new $this->fieldMapperClass();
89+
90+
$keys = array_keys($this->validInput);
91+
foreach ($this->validInput as $key => $value) {
92+
unset($this->validInput[$key]);
93+
array_shift($keys);
94+
95+
if (count($keys) === 0) {
96+
break;
97+
}
98+
99+
$newKey = $keys[0];
100+
101+
$originalData = $output->getCreationDate();
102+
$mapper->mapField($field, $this->validInput, $output);
103+
$newData = $output->getCreationDate();
104+
105+
$this->assertNotSame($originalData, $newData);
106+
107+
$this->assertInstanceOf(
108+
DateTimeImmutable::class,
109+
$newData
110+
);
111+
112+
$this->assertEquals(
113+
$this->validInput[$newKey],
114+
$newData->format('Y-m-d H:i:s')
115+
);
116+
}
117+
}
118+
}

0 commit comments

Comments
 (0)