Skip to content

Commit 48a7484

Browse files
committed
Filename Fieldmapper & unit tests
Signed-off-by: Tom Van Herreweghe <[email protected]>
1 parent 9824634 commit 48a7484

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-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 Filename VO
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\Data\ValueObject\Filename;
16+
use PHPExif\Common\Mapper\FieldMapper;
17+
use PHPExif\Common\Mapper\GuardInvalidArgumentsForExifTrait;
18+
19+
/**
20+
* Mapper
21+
*
22+
* @category PHPExif
23+
* @package Exiftool
24+
*/
25+
class FilenameFieldMapper implements FieldMapper
26+
{
27+
use GuardInvalidArgumentsForExifTrait;
28+
use ValidKeysTrait;
29+
30+
/**
31+
* @var array
32+
*/
33+
private $validKeys = [
34+
'sourcefile',
35+
'system:filename',
36+
];
37+
38+
/**
39+
* {@inheritDoc}
40+
*/
41+
public function getSupportedFields()
42+
{
43+
return array(
44+
Filename::class,
45+
);
46+
}
47+
48+
/**
49+
* {@inheritDoc}
50+
*/
51+
public function mapField($field, array $input, &$output)
52+
{
53+
$this->guardInvalidArguments($field, $input, $output);
54+
55+
foreach ($this->validKeys as $key) {
56+
if (!array_key_exists($key, $input)) {
57+
continue;
58+
}
59+
60+
$filename = new Filename($input[$key]);
61+
$output = $output->withFilename($filename);
62+
break;
63+
}
64+
}
65+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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\FilenameFieldMapper;
7+
use PHPExif\Common\Data\Exif;
8+
use PHPExif\Common\Data\ValueObject\Filename;
9+
10+
/**
11+
* Class: FilenameFieldMapperTest
12+
*
13+
* @see \PHPUnit_Framework_TestCase
14+
* @coversDefaultClass \PHPExif\Adapter\Exiftool\Reader\Mapper\Exif\FilenameFieldMapper
15+
* @covers ::<!public>
16+
*/
17+
class FilenameFieldMapperTest extends BaseFieldMapperTest
18+
{
19+
/**
20+
* {@inheritdoc}
21+
*/
22+
protected $fieldMapperClass = FilenameFieldMapper::class;
23+
24+
/**
25+
* {@inheritdoc}
26+
*/
27+
protected $supportedFields = [
28+
Filename::class,
29+
];
30+
31+
/**
32+
* {@inheritdoc}
33+
*/
34+
protected $validInput = [
35+
'sourcefile' => 'IMG_0001.JPG',
36+
'system:filename' => 'IMG_0002.JPG',
37+
];
38+
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
protected $outputAccessor = 'getFilename';
43+
44+
/**
45+
* @covers ::mapField
46+
* @group mapper
47+
*
48+
* @return void
49+
*/
50+
public function testMapFieldHasDataInOutput()
51+
{
52+
$field = reset($this->supportedFields);
53+
$output = new Exif;
54+
$mapper = new $this->fieldMapperClass();
55+
56+
$originalData = $output->{$this->outputAccessor}();
57+
$mapper->mapField($field, $this->validInput, $output);
58+
$newData = $output->{$this->outputAccessor}();
59+
60+
$this->assertNotSame($originalData, $newData);
61+
62+
$this->assertInstanceOf(
63+
Filename::class,
64+
$newData
65+
);
66+
67+
$this->assertEquals(
68+
$this->validInput['sourcefile'],
69+
$newData
70+
);
71+
}
72+
}

0 commit comments

Comments
 (0)