Skip to content

Commit fc62382

Browse files
author
Tom Van Herreweghe
committed
Added tests for AdapterAbstract and refactored some code
1 parent b4f7cd4 commit fc62382

File tree

5 files changed

+211
-18
lines changed

5 files changed

+211
-18
lines changed

lib/PHPExif/Reader/Adapter/Native.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class Native extends AdapterAbstract
4545
*
4646
* @var array
4747
*/
48-
protected $sections = array();
48+
protected $requiredSections = array();
4949

5050
/**
5151
* Include the thumbnail in the EXIF data?
@@ -85,7 +85,7 @@ class Native extends AdapterAbstract
8585
*/
8686
public function getRequiredSections()
8787
{
88-
return $this->sections;
88+
return $this->requiredSections;
8989
}
9090

9191
/**
@@ -96,7 +96,7 @@ public function getRequiredSections()
9696
*/
9797
public function setRequiredSections(array $sections)
9898
{
99-
$this->sections = $sections;
99+
$this->requiredSections = $sections;
100100

101101
return $this;
102102
}
@@ -109,8 +109,8 @@ public function setRequiredSections(array $sections)
109109
*/
110110
public function addRequiredSection($section)
111111
{
112-
if (!in_array($section, $this->sections)) {
113-
array_push($this->sections, $section);
112+
if (!in_array($section, $this->requiredSections)) {
113+
array_push($this->requiredSections, $section);
114114
}
115115

116116
return $this;
@@ -145,7 +145,7 @@ public function getIncludeThumbnail()
145145
* @param boolean $value
146146
* @return \PHPExif\Reader Current instance for chaining
147147
*/
148-
public function setSectionsAsArray($value)
148+
public function setSectionsAsArrays($value)
149149
{
150150
$this->sectionsAsArrays = (bool) $value;
151151

@@ -157,7 +157,7 @@ public function setSectionsAsArray($value)
157157
*
158158
* @return boolean
159159
*/
160-
public function getSectionsAsArray()
160+
public function getSectionsAsArrays()
161161
{
162162
return $this->sectionsAsArrays;
163163
}
@@ -178,7 +178,7 @@ public function getExifFromFile($file)
178178
$data = @exif_read_data(
179179
$file,
180180
$sections,
181-
$this->getSectionsAsArray(),
181+
$this->getSectionsAsArrays(),
182182
$this->getIncludeThumbnail()
183183
);
184184

lib/PHPExif/Reader/AdapterAbstract.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public function getClassConstantsOfType($type)
9090
$type = strtoupper($type) . '_';
9191
foreach ($constants as $key => $value) {
9292
if (strpos($key, $type) === 0) {
93-
$list[] = $value;
93+
$list[$key] = $value;
9494
}
9595
}
9696
return $list;

tests/PHPExif/Reader/Adapter/ExiftoolTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,24 @@ public function testMapDataFocalLengthIsCalculated()
125125

126126
$this->assertEquals(18, $result[\PHPExif\Exif::FOCAL_LENGTH]);
127127
}
128+
129+
/**
130+
* @group exiftool
131+
* @covers \PHPExif\Reader\Adapter\Exiftool::getCliOutput
132+
*/
133+
public function testGetCliOutput()
134+
{
135+
$reflMethod = new \ReflectionMethod('\PHPExif\Reader\Adapter\Exiftool', 'getCliOutput');
136+
$reflMethod->setAccessible(true);
137+
138+
$result = $reflMethod->invoke(
139+
$this->adapter,
140+
sprintf(
141+
'%1$s -v',
142+
PHP_BINARY
143+
)
144+
);
145+
146+
$this->assertInternalType('string', $result);
147+
}
128148
}

tests/PHPExif/Reader/Adapter/NativeTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function testGetIncludeThumbnailHasDefaultValue()
5555
*/
5656
public function testGetRequiredSections()
5757
{
58-
$reflProperty = new \ReflectionProperty('\PHPExif\Reader\Adapter\Native', 'sections');
58+
$reflProperty = new \ReflectionProperty('\PHPExif\Reader\Adapter\Native', 'requiredSections');
5959
$reflProperty->setAccessible(true);
6060

6161
$this->assertEquals($reflProperty->getValue($this->adapter), $this->adapter->getRequiredSections());
@@ -67,7 +67,7 @@ public function testGetRequiredSections()
6767
*/
6868
public function testSetRequiredSections()
6969
{
70-
$reflProperty = new \ReflectionProperty('\PHPExif\Reader\Adapter\Native', 'sections');
70+
$reflProperty = new \ReflectionProperty('\PHPExif\Reader\Adapter\Native', 'requiredSections');
7171
$reflProperty->setAccessible(true);
7272

7373
$testData = array('foo', 'bar', 'baz');
@@ -84,7 +84,7 @@ public function testSetRequiredSections()
8484
*/
8585
public function testAddRequiredSection()
8686
{
87-
$reflProperty = new \ReflectionProperty('\PHPExif\Reader\Adapter\Native', 'sections');
87+
$reflProperty = new \ReflectionProperty('\PHPExif\Reader\Adapter\Native', 'requiredSections');
8888
$reflProperty->setAccessible(true);
8989

9090
$testData = array('foo', 'bar', 'baz');
@@ -139,43 +139,43 @@ public function testGetIptcData()
139139

140140
/**
141141
* @group native
142-
* @covers \PHPExif\Reader\Adapter\Native::setSectionsAsArray
142+
* @covers \PHPExif\Reader\Adapter\Native::setSectionsAsArrays
143143
*/
144144
public function testSetSectionsAsArrayInProperty()
145145
{
146146
$reflProperty = new \ReflectionProperty('\PHPExif\Reader\Adapter\Native', 'sectionsAsArrays');
147147
$reflProperty->setAccessible(true);
148148
$expected = \PHPExif\Reader\Adapter\Native::SECTIONS_AS_ARRAYS;
149-
$this->adapter->setSectionsAsArray($expected);
149+
$this->adapter->setSectionsAsArrays($expected);
150150
$actual = $reflProperty->getValue($this->adapter);
151151
$this->assertEquals($expected, $actual);
152152
}
153153

154154
/**
155155
* @group native
156-
* @covers \PHPExif\Reader\Adapter\Native::setSectionsAsArray
156+
* @covers \PHPExif\Reader\Adapter\Native::setSectionsAsArrays
157157
*/
158158
public function testSetSectionsAsArrayConvertsToBoolean()
159159
{
160160
$reflProperty = new \ReflectionProperty('\PHPExif\Reader\Adapter\Native', 'sectionsAsArrays');
161161
$reflProperty->setAccessible(true);
162162
$expected = \PHPExif\Reader\Adapter\Native::SECTIONS_AS_ARRAYS;
163-
$this->adapter->setSectionsAsArray('Foo');
163+
$this->adapter->setSectionsAsArrays('Foo');
164164
$actual = $reflProperty->getValue($this->adapter);
165165
$this->assertEquals($expected, $actual);
166166
}
167167

168168
/**
169169
* @group native
170-
* @covers \PHPExif\Reader\Adapter\Native::getSectionsAsArray
170+
* @covers \PHPExif\Reader\Adapter\Native::getSectionsAsArrays
171171
*/
172172
public function testGetSectionsAsArrayFromProperty()
173173
{
174174
$reflProperty = new \ReflectionProperty('\PHPExif\Reader\Adapter\Native', 'sectionsAsArrays');
175175
$reflProperty->setAccessible(true);
176176
$reflProperty->setValue($this->adapter, \PHPExif\Reader\Adapter\Native::SECTIONS_AS_ARRAYS);
177177

178-
$this->assertEquals(\PHPExif\Reader\Adapter\Native::SECTIONS_AS_ARRAYS, $this->adapter->getSectionsAsArray());
178+
$this->assertEquals(\PHPExif\Reader\Adapter\Native::SECTIONS_AS_ARRAYS, $this->adapter->getSectionsAsArrays());
179179
}
180180

181181
/**
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
<?php
2+
class AdapterAbstractTest extends \PHPUnit_Framework_TestCase
3+
{
4+
/**
5+
* @var \PHPExif\Reader\Adapter\Exiftool
6+
*/
7+
protected $adapter;
8+
9+
public function setUp()
10+
{
11+
$this->adapter = new \PHPExif\Reader\Adapter\Native();
12+
}
13+
14+
/**
15+
* @group adapter
16+
* @covers \PHPExif\Reader\AdapterAbstract::determinePropertyGetter
17+
*/
18+
public function testDeterminePropertyGetter()
19+
{
20+
$reflMethod = new \ReflectionMethod('\PHPExif\Reader\Adapter\Native', 'determinePropertyGetter');
21+
$reflMethod->setAccessible(true);
22+
23+
$result = $reflMethod->invoke(
24+
$this->adapter,
25+
'foo'
26+
);
27+
28+
$this->assertEquals('getFoo', $result);
29+
}
30+
31+
/**
32+
* @group adapter
33+
* @covers \PHPExif\Reader\AdapterAbstract::determinePropertySetter
34+
*/
35+
public function testDeterminePropertySetter()
36+
{
37+
$reflMethod = new \ReflectionMethod('\PHPExif\Reader\Adapter\Native', 'determinePropertySetter');
38+
$reflMethod->setAccessible(true);
39+
40+
$result = $reflMethod->invoke(
41+
$this->adapter,
42+
'foo'
43+
);
44+
45+
$this->assertEquals('setFoo', $result);
46+
}
47+
48+
/**
49+
* @group adapter
50+
* @covers \PHPExif\Reader\AdapterAbstract::getClassConstantsOfType
51+
*/
52+
public function testGetClassConstantsOfTypeAlwaysReturnsArray()
53+
{
54+
$result = $this->adapter->getClassConstantsOfType('sections');
55+
$this->assertInternalType('array', $result);
56+
$result = $this->adapter->getClassConstantsOfType('foo');
57+
$this->assertInternalType('array', $result);
58+
}
59+
60+
/**
61+
* @group adapter
62+
* @covers \PHPExif\Reader\AdapterAbstract::getClassConstantsOfType
63+
*/
64+
public function testGetClassConstantsOfTypeReturnsCorrectData()
65+
{
66+
$expected = array(
67+
'SECTIONS_AS_ARRAYS' => \PHPExif\Reader\Adapter\Native::SECTIONS_AS_ARRAYS,
68+
'SECTIONS_FLAT' => \PHPExif\Reader\Adapter\Native::SECTIONS_FLAT,
69+
);
70+
$actual = $this->adapter->getClassConstantsOfType('sections');
71+
$this->assertEquals($expected, $actual);
72+
}
73+
74+
/**
75+
* @group adapter
76+
* @covers \PHPExif\Reader\AdapterAbstract::toArray
77+
*/
78+
public function testToArrayReturnsPropertiesWithGetters()
79+
{
80+
$expected = array(
81+
'requiredSections',
82+
'includeThumbnail',
83+
'sectionsAsArrays',
84+
);
85+
$result = $this->adapter->toArray();
86+
$actual = array_keys($result);
87+
$this->assertEquals($expected, $actual);
88+
}
89+
90+
/**
91+
* @group adapter
92+
* @covers \PHPExif\Reader\AdapterAbstract::toArray
93+
*/
94+
public function testToArrayOmmitsPropertiesWithoutGetters()
95+
{
96+
$expected = array(
97+
'iptcMapping',
98+
);
99+
$result = $this->adapter->toArray();
100+
$actual = array_keys($result);
101+
$diff = array_diff($expected, $actual);
102+
$this->assertEquals($expected, $diff);
103+
}
104+
105+
/**
106+
* @group adapter
107+
* @covers \PHPExif\Reader\AdapterAbstract::setOptions
108+
*/
109+
public function testSetOptionsReturnsCurrentInstance()
110+
{
111+
$result = $this->adapter->setOptions(array());
112+
$this->assertSame($this->adapter, $result);
113+
}
114+
115+
/**
116+
* @group adapter
117+
* @covers \PHPExif\Reader\AdapterAbstract::setOptions
118+
*/
119+
public function testSetOptionsCorrectlySetsProperties()
120+
{
121+
$expected = array(
122+
'requiredSections' => array('foo', 'bar', 'baz',),
123+
'includeThumbnail' => \PHPExif\Reader\Adapter\Native::INCLUDE_THUMBNAIL,
124+
'sectionsAsArrays' => \PHPExif\Reader\Adapter\Native::SECTIONS_AS_ARRAYS,
125+
);
126+
$this->adapter->setOptions($expected);
127+
128+
foreach ($expected as $key => $value) {
129+
$reflProp = new \ReflectionProperty('\PHPExif\Reader\Adapter\Native', $key);
130+
$reflProp->setAccessible(true);
131+
$this->assertEquals($value, $reflProp->getValue($this->adapter));
132+
}
133+
}
134+
135+
/**
136+
* @group adapter
137+
* @covers \PHPExif\Reader\AdapterAbstract::setOptions
138+
*/
139+
public function testSetOptionsIgnoresPropertiesWithoutSetters()
140+
{
141+
$expected = array(
142+
'iptcMapping' => array('foo', 'bar', 'baz'),
143+
);
144+
$this->adapter->setOptions($expected);
145+
146+
foreach ($expected as $key => $value) {
147+
$reflProp = new \ReflectionProperty('\PHPExif\Reader\Adapter\Native', $key);
148+
$reflProp->setAccessible(true);
149+
$this->assertNotEquals($value, $reflProp->getValue($this->adapter));
150+
}
151+
}
152+
153+
154+
/**
155+
* @group adapter
156+
* @covers \PHPExif\Reader\AdapterAbstract::__construct
157+
*/
158+
public function testConstructorSetsOptions()
159+
{
160+
$expected = array(
161+
'requiredSections' => array('foo', 'bar', 'baz',),
162+
'includeThumbnail' => \PHPExif\Reader\Adapter\Native::INCLUDE_THUMBNAIL,
163+
'sectionsAsArrays' => \PHPExif\Reader\Adapter\Native::SECTIONS_AS_ARRAYS,
164+
);
165+
$adapter = new \PHPExif\Reader\Adapter\Native($expected);
166+
167+
foreach ($expected as $key => $value) {
168+
$reflProp = new \ReflectionProperty('\PHPExif\Reader\Adapter\Native', $key);
169+
$reflProp->setAccessible(true);
170+
$this->assertEquals($value, $reflProp->getValue($adapter));
171+
}
172+
}
173+
}

0 commit comments

Comments
 (0)