Skip to content

Commit 7708592

Browse files
committed
ExposureTime FieldMapper & unit tests
Signed-off-by: Tom Van Herreweghe <[email protected]>
1 parent 23335fb commit 7708592

File tree

2 files changed

+231
-0
lines changed

2 files changed

+231
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
/**
3+
* Mapper for mapping data between raw input and a ExposureTime 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\ExposureTime;
16+
use PHPExif\Common\Mapper\FieldMapper;
17+
use PHPExif\Common\Mapper\GuardInvalidArgumentsForExifTrait;
18+
19+
/**
20+
* Mapper
21+
*
22+
* @category PHPExif
23+
* @package Common
24+
*/
25+
class ExposureTimeFieldMapper implements FieldMapper
26+
{
27+
use GuardInvalidArgumentsForExifTrait;
28+
29+
/**
30+
* @var array
31+
*/
32+
private $validKeys = [
33+
'exififd:exposuretime',
34+
'composite:shutterspeed',
35+
];
36+
37+
/**
38+
* Getter for validKeys
39+
*
40+
* @return array
41+
*/
42+
public function getValidKeys()
43+
{
44+
return $this->validKeys;
45+
}
46+
47+
/**
48+
* Setter for validKeys
49+
*
50+
* @param array $validKeys
51+
*/
52+
public function setValidKeys(array $validKeys)
53+
{
54+
$this->validKeys = $validKeys;
55+
}
56+
57+
/**
58+
* {@inheritDoc}
59+
*/
60+
public function getSupportedFields()
61+
{
62+
return array(
63+
ExposureTime::class,
64+
);
65+
}
66+
67+
/**
68+
* {@inheritDoc}
69+
*/
70+
public function mapField($field, array $input, &$output)
71+
{
72+
$this->guardInvalidArguments($field, $input, $output);
73+
74+
foreach ($this->validKeys as $key) {
75+
if (!array_key_exists($key, $input)) {
76+
continue;
77+
}
78+
79+
$shutterSpeed = new ExposureTime($input[$key]);
80+
$output = $output->withExposureTime($shutterSpeed);
81+
break;
82+
}
83+
}
84+
}
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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\ExposureTimeFieldMapper;
7+
use PHPExif\Common\Data\Exif;
8+
use PHPExif\Common\Data\ValueObject\ExposureTime;
9+
10+
/**
11+
* Class: ExposureTimeFieldMapperTest
12+
*
13+
* @see \PHPUnit_Framework_TestCase
14+
* @coversDefaultClass \PHPExif\Adapter\Exiftool\Reader\Mapper\Exif\ExposureTimeFieldMapper
15+
* @covers ::<!public>
16+
*/
17+
class ExposureTimeFieldMapperTest extends BaseFieldMapperTest
18+
{
19+
/**
20+
* FQCN of the fieldmapper being tested
21+
*
22+
* @var mixed
23+
*/
24+
protected $fieldMapperClass = ExposureTimeFieldMapper::class;
25+
26+
/**
27+
* List of supported fields
28+
*
29+
* @var array
30+
*/
31+
protected $supportedFields = [
32+
ExposureTime::class,
33+
];
34+
35+
/**
36+
* Valid input data
37+
*
38+
* @var array
39+
*/
40+
protected $validInput = [
41+
'exififd:exposuretime' => '1/60',
42+
'composite:shutterspeed' => '1/80',
43+
];
44+
45+
/**
46+
* @covers ::mapField
47+
* @group mapper
48+
*
49+
* @return void
50+
*/
51+
public function testMapFieldHasDataInOutput()
52+
{
53+
$field = reset($this->supportedFields);
54+
$output = new Exif;
55+
$mapper = new $this->fieldMapperClass();
56+
57+
$originalData = $output->getExposureTime();
58+
$mapper->mapField($field, $this->validInput, $output);
59+
$newData = $output->getExposureTime();
60+
61+
$this->assertNotSame($originalData, $newData);
62+
63+
$this->assertInstanceOf(
64+
ExposureTime::class,
65+
$newData
66+
);
67+
68+
$this->assertEquals(
69+
$this->validInput['exififd:exposuretime'],
70+
$newData
71+
);
72+
}
73+
74+
/**
75+
* @covers ::mapField
76+
* @group mapper
77+
*
78+
* @return void
79+
*/
80+
public function testMapFieldTraversesSetOfKeys()
81+
{
82+
$field = reset($this->supportedFields);
83+
$output = new Exif;
84+
$mapper = new $this->fieldMapperClass();
85+
86+
$keys = array_keys($this->validInput);
87+
foreach ($this->validInput as $key => $value) {
88+
unset($this->validInput[$key]);
89+
array_shift($keys);
90+
91+
if (count($keys) === 0) {
92+
break;
93+
}
94+
95+
$newKey = $keys[0];
96+
97+
$originalData = $output->getExposureTime();
98+
$mapper->mapField($field, $this->validInput, $output);
99+
$newData = $output->getExposureTime();
100+
101+
$this->assertNotSame($originalData, $newData);
102+
103+
$this->assertInstanceOf(
104+
ExposureTime::class,
105+
$newData
106+
);
107+
108+
$this->assertEquals(
109+
$this->validInput[$newKey],
110+
$newData
111+
);
112+
}
113+
}
114+
115+
/**
116+
* @covers ::getValidKeys
117+
* @group mapper
118+
*
119+
* @return void
120+
*/
121+
public function testGetValidKeysReturnsArray()
122+
{
123+
$mapper = new $this->fieldMapperClass();
124+
$this->assertInternalType(
125+
'array',
126+
$mapper->getValidKeys()
127+
);
128+
}
129+
130+
/**
131+
* @covers ::setValidKeys
132+
* @group mapper
133+
*
134+
* @return void
135+
*/
136+
public function testSetValidKeysSetsCorrectData()
137+
{
138+
$mapper = new $this->fieldMapperClass();
139+
$data = [
140+
'foo', 'bar', 'baz',
141+
];
142+
143+
$this->assertNotEquals($data, $mapper->getValidKeys());
144+
$mapper->setValidKeys($data);
145+
$this->assertEquals($data, $mapper->getValidKeys());
146+
}
147+
}

0 commit comments

Comments
 (0)