Skip to content

Commit dc0f460

Browse files
committed
Add Apperture FieldMapper & unit tests
Signed-off-by: Tom Van Herreweghe <[email protected]>
1 parent 0a85b5d commit dc0f460

File tree

5 files changed

+265
-165
lines changed

5 files changed

+265
-165
lines changed

src/Reader/Mapper.php

Lines changed: 0 additions & 53 deletions
This file was deleted.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* Mapper for mapping data between raw input and an Aperture 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\Aperture;
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 ApertureFieldMapper implements FieldMapper
26+
{
27+
use GuardInvalidArgumentsForExifTrait;
28+
29+
/**
30+
* {@inheritDoc}
31+
*/
32+
public function getSupportedFields()
33+
{
34+
return array(
35+
Aperture::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+
if (!array_key_exists('composite:aperture', $input)) {
47+
return;
48+
}
49+
50+
$aperture = new Aperture(
51+
$input['composite:aperture']
52+
);
53+
54+
$output = $output->withAperture($aperture);
55+
}
56+
}
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\ApertureFieldMapper;
7+
use PHPExif\Common\Data\Exif;
8+
use PHPExif\Common\Data\ValueObject\Aperture;
9+
10+
/**
11+
* Class: ApertureFieldMapperTest
12+
*
13+
* @see \PHPUnit_Framework_TestCase
14+
* @coversDefaultClass \PHPExif\Adapter\Exiftool\Reader\Mapper\Exif\ApertureFieldMapper
15+
* @covers ::<!public>
16+
*/
17+
class ApertureFieldMapperTest extends BaseFieldMapperTest
18+
{
19+
/**
20+
* FQCN of the fieldmapper being tested
21+
*
22+
* @var mixed
23+
*/
24+
protected $fieldMapperClass = ApertureFieldMapper::class;
25+
26+
/**
27+
* List of supported fields
28+
*
29+
* @var array
30+
*/
31+
protected $supportedFields = [
32+
Aperture::class,
33+
];
34+
35+
/**
36+
* Valid input data
37+
*
38+
* @var array
39+
*/
40+
protected $validInput = [
41+
'composite:aperture' => 5.0,
42+
];
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->getAperture();
57+
$mapper->mapField($field, $this->validInput, $output);
58+
$newData = $output->getAperture();
59+
60+
$this->assertNotSame($originalData, $newData);
61+
62+
$this->assertInstanceOf(
63+
Aperture::class,
64+
$newData
65+
);
66+
67+
$this->assertEquals(
68+
'f/' . $this->validInput['composite:aperture'],
69+
(string) $newData
70+
);
71+
}
72+
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
3+
namespace Tests\PHPExif\Adapter\Exiftool\Reader\Mapper\Exif;
4+
5+
use Mockery as m;
6+
use PHPExif\Common\Data\Exif;
7+
use PHPExif\Common\Exception\Mapper\UnsupportedFieldException;
8+
use PHPExif\Common\Exception\Mapper\UnsupportedOutputException;
9+
10+
/**
11+
* Class: BaseFieldMapperTest
12+
*
13+
* @see \PHPUnit_Framework_TestCase
14+
*/
15+
abstract class BaseFieldMapperTest extends \PHPUnit_Framework_TestCase
16+
{
17+
/**
18+
* FQCN of the fieldmapper being tested
19+
*
20+
* @var mixed
21+
*/
22+
protected $fieldMapperClass;
23+
24+
/**
25+
* List of supported fields
26+
*
27+
* @var array
28+
*/
29+
protected $supportedFields = [];
30+
31+
/**
32+
* Valid input data
33+
*
34+
* @var array
35+
*/
36+
protected $validInput = [];
37+
38+
/**
39+
* @covers ::getSupportedFields
40+
* @group mapper
41+
*
42+
* @return void
43+
*/
44+
public function testGetSupportedFieldsReturnsExpectedArray()
45+
{
46+
$mapper = new $this->fieldMapperClass();
47+
$actual = $mapper->getSupportedFields();
48+
49+
$this->assertInternalType('array', $actual);
50+
51+
$this->assertEquals($this->supportedFields, $actual);
52+
}
53+
54+
/**
55+
* @covers ::mapField
56+
* @group mapper
57+
*
58+
* @return void
59+
*/
60+
public function testMapFieldThrowsExceptionForUnsupportedField()
61+
{
62+
$this->setExpectedException(UnsupportedFieldException::class);
63+
64+
$field = 'foo';
65+
$input = [];
66+
$output = new Exif;
67+
$mapper = new $this->fieldMapperClass();
68+
69+
$mapper->mapField($field, $input, $output);
70+
}
71+
72+
/**
73+
* @covers ::mapField
74+
* @group mapper
75+
*
76+
* @return void
77+
*/
78+
public function testMapFieldThrowsExceptionForUnsupportedOutput()
79+
{
80+
$this->setExpectedException(UnsupportedOutputException::class);
81+
82+
$field = reset($this->supportedFields);
83+
$input = [];
84+
$output = new \stdClass;
85+
$mapper = new $this->fieldMapperClass();
86+
87+
$mapper->mapField($field, $input, $output);
88+
}
89+
90+
/**
91+
* @covers ::mapField
92+
* @group mapper
93+
*
94+
* @return void
95+
*/
96+
public function testMapFieldYieldsNewOutputForValidInput()
97+
{
98+
$field = reset($this->supportedFields);
99+
$output = new Exif;
100+
$mapper = new $this->fieldMapperClass();
101+
102+
$originalHash = spl_object_hash($output);
103+
104+
$mapper->mapField($field, $this->validInput, $output);
105+
106+
$newHash = spl_object_hash($output);
107+
108+
$this->assertNotEquals(
109+
$originalHash,
110+
$newHash
111+
);
112+
}
113+
114+
/**
115+
* @covers ::mapField
116+
* @group mapper
117+
*
118+
* @return void
119+
*/
120+
public function testMapFieldYieldsSameOutputForInvalidInput()
121+
{
122+
$field = reset($this->supportedFields);
123+
$output = new Exif;
124+
$mapper = new $this->fieldMapperClass();
125+
126+
$originalHash = spl_object_hash($output);
127+
128+
$mapper->mapField($field, [], $output);
129+
130+
$newHash = spl_object_hash($output);
131+
132+
$this->assertEquals(
133+
$originalHash,
134+
$newHash
135+
);
136+
}
137+
}

0 commit comments

Comments
 (0)