Skip to content

Commit 9824634

Browse files
committed
Refactored {get,set}ValidKeys to Trait; Refactored unit tests
Signed-off-by: Tom Van Herreweghe <[email protected]>
1 parent 7708592 commit 9824634

File tree

9 files changed

+216
-193
lines changed

9 files changed

+216
-193
lines changed

src/Reader/Mapper/Exif/ApertureFieldMapper.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,21 @@
2020
* Mapper
2121
*
2222
* @category PHPExif
23-
* @package Common
23+
* @package Exiftool
2424
*/
2525
class ApertureFieldMapper implements FieldMapper
2626
{
2727
use GuardInvalidArgumentsForExifTrait;
28+
use ValidKeysTrait;
29+
30+
/**
31+
* @var array
32+
*/
33+
private $validKeys = [
34+
'composite:aperture',
35+
'exififd:fnumber',
36+
'exififd:aperturevalue',
37+
];
2838

2939
/**
3040
* {@inheritDoc}
@@ -43,14 +53,13 @@ public function mapField($field, array $input, &$output)
4353
{
4454
$this->guardInvalidArguments($field, $input, $output);
4555

46-
if (!array_key_exists('composite:aperture', $input)) {
47-
return;
48-
}
49-
50-
$aperture = new Aperture(
51-
$input['composite:aperture']
52-
);
56+
foreach ($this->validKeys as $key) {
57+
if (!array_key_exists($key, $input)) {
58+
continue;
59+
}
5360

54-
$output = $output->withAperture($aperture);
61+
$aperture = new Aperture($input[$key]);
62+
$output = $output->withAperture($aperture);
63+
}
5564
}
5665
}

src/Reader/Mapper/Exif/DateTimeFieldMapper.php

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@
2020
* Mapper
2121
*
2222
* @category PHPExif
23-
* @package Common
23+
* @package Exiftool
2424
*/
2525
class DateTimeFieldMapper implements FieldMapper
2626
{
2727
use GuardInvalidArgumentsForExifTrait;
28+
use ValidKeysTrait;
2829

2930
/**
3031
* @var array
@@ -38,26 +39,6 @@ class DateTimeFieldMapper implements FieldMapper
3839
'ifd0:modifydate',
3940
];
4041

41-
/**
42-
* Getter for validKeys
43-
*
44-
* @return array
45-
*/
46-
public function getValidKeys()
47-
{
48-
return $this->validKeys;
49-
}
50-
51-
/**
52-
* Setter for validKeys
53-
*
54-
* @param array $validKeys
55-
*/
56-
public function setValidKeys(array $validKeys)
57-
{
58-
$this->validKeys = $validKeys;
59-
}
60-
6142
/**
6243
* {@inheritDoc}
6344
*/

src/Reader/Mapper/Exif/ExposureTimeFieldMapper.php

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@
2020
* Mapper
2121
*
2222
* @category PHPExif
23-
* @package Common
23+
* @package Exiftool
2424
*/
2525
class ExposureTimeFieldMapper implements FieldMapper
2626
{
2727
use GuardInvalidArgumentsForExifTrait;
28+
use ValidKeysTrait;
2829

2930
/**
3031
* @var array
@@ -34,26 +35,6 @@ class ExposureTimeFieldMapper implements FieldMapper
3435
'composite:shutterspeed',
3536
];
3637

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-
5738
/**
5839
* {@inheritDoc}
5940
*/
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* Trait with methods to manipulate the $validKeys property
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+
/**
15+
* Common methods to manipulate the validKeys property
16+
*
17+
* @category PHPExif
18+
* @package Exiftool
19+
*/
20+
trait ValidKeysTrait
21+
{
22+
/**
23+
* Getter for validKeys
24+
*
25+
* @return array
26+
*/
27+
public function getValidKeys()
28+
{
29+
return (!property_exists($this, 'validKeys')) ? [] : $this->validKeys;
30+
}
31+
32+
/**
33+
* Setter for validKeys
34+
*
35+
* @param array $validKeys
36+
*/
37+
public function setValidKeys(array $validKeys)
38+
{
39+
$this->validKeys = $validKeys;
40+
}
41+
}

tests/unit/Reader/Mapper/Exif/ApertureFieldMapperTest.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,29 @@
1717
class ApertureFieldMapperTest extends BaseFieldMapperTest
1818
{
1919
/**
20-
* FQCN of the fieldmapper being tested
21-
*
22-
* @var mixed
20+
* {@inheritdoc}
2321
*/
2422
protected $fieldMapperClass = ApertureFieldMapper::class;
2523

2624
/**
27-
* List of supported fields
28-
*
29-
* @var array
25+
* {@inheritdoc}
3026
*/
3127
protected $supportedFields = [
3228
Aperture::class,
3329
];
3430

3531
/**
36-
* Valid input data
37-
*
38-
* @var array
32+
* {@inheritdoc}
3933
*/
4034
protected $validInput = [
4135
'composite:aperture' => 5.0,
4236
];
4337

38+
/**
39+
* {@inheritdoc}
40+
*/
41+
protected $outputAccessor = 'getAperture';
42+
4443
/**
4544
* @covers ::mapField
4645
* @group mapper
@@ -53,9 +52,9 @@ public function testMapFieldHasDataInOutput()
5352
$output = new Exif;
5453
$mapper = new $this->fieldMapperClass();
5554

56-
$originalData = $output->getAperture();
55+
$originalData = $output->{$this->outputAccessor}();
5756
$mapper->mapField($field, $this->validInput, $output);
58-
$newData = $output->getAperture();
57+
$newData = $output->{$this->outputAccessor}();
5958

6059
$this->assertNotSame($originalData, $newData);
6160

tests/unit/Reader/Mapper/Exif/BaseFieldMapperTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ abstract class BaseFieldMapperTest extends \PHPUnit_Framework_TestCase
3535
*/
3636
protected $validInput = [];
3737

38+
/**
39+
* Which method should be used on the output to access the
40+
* mapped data?
41+
*
42+
* @var string
43+
*/
44+
protected $outputAccessor;
45+
3846
/**
3947
* @covers ::getSupportedFields
4048
* @group mapper
@@ -134,4 +142,47 @@ public function testMapFieldYieldsSameOutputForInvalidInput()
134142
$newHash
135143
);
136144
}
145+
146+
/**
147+
* @covers ::mapField
148+
* @group mapper
149+
*
150+
* @return void
151+
*/
152+
public function testMapFieldTraversesSetOfKeys()
153+
{
154+
$field = reset($this->supportedFields);
155+
$output = new Exif;
156+
$mapper = new $this->fieldMapperClass();
157+
158+
$keys = array_keys($this->validInput);
159+
foreach ($this->validInput as $key => $value) {
160+
unset($this->validInput[$key]);
161+
array_shift($keys);
162+
163+
if (count($keys) === 0) {
164+
break;
165+
}
166+
167+
$newKey = $keys[0];
168+
169+
$originalData = $output->{$this->outputAccessor}();
170+
$mapper->mapField($field, $this->validInput, $output);
171+
$newData = $output->{$this->outputAccessor}();
172+
173+
$this->assertNotSame($originalData, $newData);
174+
175+
if (null !== $originalData) {
176+
$this->assertInstanceOf(
177+
get_class($originalData),
178+
$newData
179+
);
180+
}
181+
182+
$this->assertEquals(
183+
$this->validInput[$newKey],
184+
$newData
185+
);
186+
}
187+
}
137188
}

tests/unit/Reader/Mapper/Exif/DateTimeFieldMapperTest.php

Lines changed: 12 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,19 @@
1717
class DateTimeFieldMapperTest extends BaseFieldMapperTest
1818
{
1919
/**
20-
* FQCN of the fieldmapper being tested
21-
*
22-
* @var mixed
20+
* {@inheritdoc}
2321
*/
2422
protected $fieldMapperClass = DateTimeFieldMapper::class;
2523

2624
/**
27-
* List of supported fields
28-
*
29-
* @var array
25+
* {@inheritdoc}
3026
*/
3127
protected $supportedFields = [
3228
DateTimeImmutable::class,
3329
];
3430

3531
/**
36-
* Valid input data
37-
*
38-
* @var array
32+
* {@inheritdoc}
3933
*/
4034
protected $validInput = [
4135
'system:filemodifydate' => '2016-11-17 20:00:00',
@@ -46,6 +40,11 @@ class DateTimeFieldMapperTest extends BaseFieldMapperTest
4640
'ifd0:modifydate' => '2016-11-17 20:05:00',
4741
];
4842

43+
/**
44+
* {@inheritdoc}
45+
*/
46+
protected $outputAccessor = 'getCreationDate';
47+
4948
/**
5049
* @covers ::mapField
5150
* @group mapper
@@ -58,9 +57,9 @@ public function testMapFieldHasDataInOutput()
5857
$output = new Exif;
5958
$mapper = new $this->fieldMapperClass();
6059

61-
$originalData = $output->getCreationDate();
60+
$originalData = $output->{$this->outputAccessor}();
6261
$mapper->mapField($field, $this->validInput, $output);
63-
$newData = $output->getCreationDate();
62+
$newData = $output->{$this->outputAccessor}();
6463

6564
$this->assertNotSame($originalData, $newData);
6665

@@ -98,9 +97,9 @@ public function testMapFieldTraversesSetOfKeys()
9897

9998
$newKey = $keys[0];
10099

101-
$originalData = $output->getCreationDate();
100+
$originalData = $output->{$this->outputAccessor}();
102101
$mapper->mapField($field, $this->validInput, $output);
103-
$newData = $output->getCreationDate();
102+
$newData = $output->{$this->outputAccessor}();
104103

105104
$this->assertNotSame($originalData, $newData);
106105

@@ -115,37 +114,4 @@ public function testMapFieldTraversesSetOfKeys()
115114
);
116115
}
117116
}
118-
119-
/**
120-
* @covers ::getValidKeys
121-
* @group mapper
122-
*
123-
* @return void
124-
*/
125-
public function testGetValidKeysReturnsArray()
126-
{
127-
$mapper = new $this->fieldMapperClass();
128-
$this->assertInternalType(
129-
'array',
130-
$mapper->getValidKeys()
131-
);
132-
}
133-
134-
/**
135-
* @covers ::setValidKeys
136-
* @group mapper
137-
*
138-
* @return void
139-
*/
140-
public function testSetValidKeysSetsCorrectData()
141-
{
142-
$mapper = new $this->fieldMapperClass();
143-
$data = [
144-
'foo', 'bar', 'baz',
145-
];
146-
147-
$this->assertNotEquals($data, $mapper->getValidKeys());
148-
$mapper->setValidKeys($data);
149-
$this->assertEquals($data, $mapper->getValidKeys());
150-
}
151117
}

0 commit comments

Comments
 (0)