Skip to content

Commit 8460af5

Browse files
authored
function always return an Exif object + fix tests (#62)
1 parent 14c624b commit 8460af5

19 files changed

+891
-852
lines changed

composer.lock

Lines changed: 190 additions & 178 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/PHPExif/Adapter/FFprobe.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use InvalidArgumentException;
1616
use FFMpeg;
1717
use PHPExif\Mapper\FFprobe as MapperFFprobe;
18+
use RuntimeException;
1819
use Safe\Exceptions\ExecException;
1920

2021
use function Safe\exec;
@@ -91,9 +92,9 @@ public function getToolPath(): string
9192
* Reads & parses the EXIF data from given file
9293
*
9394
* @param string $file
94-
* @return \PHPExif\Exif|false Instance of Exif object with data
95+
* @return \PHPExif\Exif Instance of Exif object with data
9596
*/
96-
public function getExifFromFile(string $file): Exif|false
97+
public function getExifFromFile(string $file): Exif
9798
{
9899
$mimeType = mime_content_type($file);
99100

@@ -112,7 +113,7 @@ public function getExifFromFile(string $file): Exif|false
112113

113114
// file is not a video -> wrong adapter
114115
if (strpos($mimeType, 'video') !== 0) {
115-
return false;
116+
throw new RuntimeException('Could not read the video');
116117
}
117118

118119
$ffprobe = FFMpeg\FFProbe::create(array(

lib/PHPExif/Contracts/AdapterInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ interface AdapterInterface
2424
* @return \PHPExif\Exif Instance of Exif object with data
2525
* @throws \RuntimeException If the EXIF data could not be read
2626
*/
27-
public function getExifFromFile(string $file) : Exif|false;
27+
public function getExifFromFile(string $file) : Exif;
2828
}

lib/PHPExif/Contracts/HydratorInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ interface HydratorInterface
2222
* @param array $data
2323
* @return void
2424
*/
25-
public function hydrate($object, array $data);
25+
public function hydrate($object, array $data): void;
2626
}

lib/PHPExif/Contracts/ReaderInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ interface ReaderInterface
2323
* @param string $file
2424
* @return \PHPExif\Exif Instance of Exif object with data
2525
*/
26-
public function read(string $file) : Exif|false|string;
26+
public function read(string $file) : Exif;
2727
}

lib/PHPExif/Reader/Reader.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ public static function factory(ReaderType $type): Reader
6868
ReaderType::EXIFTOOL => new ExiftoolAdapter(),
6969
ReaderType::FFPROBE => new FFProbeAdapter(),
7070
ReaderType::IMAGICK => new ImageMagickAdapter(),
71-
default => throw new \InvalidArgumentException(sprintf('Unknown type "%1$s"', $type->value))
7271
};
7372
return new $classname($adapter);
7473
}
@@ -79,7 +78,7 @@ public static function factory(ReaderType $type): Reader
7978
* @param string $file
8079
* @return \PHPExif\Exif Instance of Exif object with data
8180
*/
82-
public function read(string $file): Exif|string|false
81+
public function read(string $file): Exif
8382
{
8483
return $this->getAdapter()->getExifFromFile($file);
8584
}
@@ -90,7 +89,7 @@ public function read(string $file): Exif|string|false
9089
* @param string $file
9190
* @return \PHPExif\Exif Instance of Exif object with data
9291
*/
93-
public function getExifFromFile(string $file): Exif|string|false
92+
public function getExifFromFile(string $file): Exif
9493
{
9594
return $this->read($file);
9695
}

tests/PHPExif/Adapter/AdapterAbstractTest.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use PHPExif\Adapter\AbstractAdapter;
34
use PHPExif\Adapter\Exiftool;
45
use PHPExif\Adapter\Native;
56

@@ -9,13 +10,13 @@
910
class AbstractAdapterTest extends PHPUnit\Framework\TestCase
1011
{
1112
/**
12-
* @var \PHPExif\Adapter\Exiftool|\PHPExif\Adapter\Native
13+
* @var Exiftool|Native
1314
*/
1415
protected Exiftool|Native $adapter;
1516

1617
protected function setUp(): void
1718
{
18-
$this->adapter = new \PHPExif\Adapter\Native();
19+
$this->adapter = new Native();
1920
}
2021

2122
/**
@@ -36,13 +37,13 @@ public function testSetOptionsCorrectlySetsProperties()
3637
{
3738
$expected = array(
3839
'requiredSections' => array('foo', 'bar', 'baz',),
39-
'includeThumbnail' => \PHPExif\Adapter\Native::INCLUDE_THUMBNAIL,
40-
'sectionsAsArrays' => \PHPExif\Adapter\Native::SECTIONS_AS_ARRAYS,
40+
'includeThumbnail' => Native::INCLUDE_THUMBNAIL,
41+
'sectionsAsArrays' => Native::SECTIONS_AS_ARRAYS,
4142
);
4243
$this->adapter->setOptions($expected);
4344

4445
foreach ($expected as $key => $value) {
45-
$reflProp = new \ReflectionProperty('\\PHPExif\\Adapter\\Native', $key);
46+
$reflProp = new \ReflectionProperty(Native::class, $key);
4647
$reflProp->setAccessible(true);
4748
$this->assertEquals($value, $reflProp->getValue($this->adapter));
4849
}
@@ -60,7 +61,7 @@ public function testSetOptionsIgnoresPropertiesWithoutSetters()
6061
$this->adapter->setOptions($expected);
6162

6263
foreach ($expected as $key => $value) {
63-
$reflProp = new \ReflectionProperty('\\PHPExif\\Adapter\\Native', $key);
64+
$reflProp = new \ReflectionProperty(Native::class, $key);
6465
$reflProp->setAccessible(true);
6566
$this->assertNotEquals($value, $reflProp->getValue($this->adapter));
6667
}
@@ -75,13 +76,13 @@ public function testConstructorSetsOptions()
7576
{
7677
$expected = array(
7778
'requiredSections' => array('foo', 'bar', 'baz',),
78-
'includeThumbnail' => \PHPExif\Adapter\Native::INCLUDE_THUMBNAIL,
79-
'sectionsAsArrays' => \PHPExif\Adapter\Native::SECTIONS_AS_ARRAYS,
79+
'includeThumbnail' => Native::INCLUDE_THUMBNAIL,
80+
'sectionsAsArrays' => Native::SECTIONS_AS_ARRAYS,
8081
);
81-
$adapter = new \PHPExif\Adapter\Native($expected);
82+
$adapter = new Native($expected);
8283

8384
foreach ($expected as $key => $value) {
84-
$reflProp = new \ReflectionProperty('\\PHPExif\\Adapter\\Native', $key);
85+
$reflProp = new \ReflectionProperty(Native::class, $key);
8586
$reflProp->setAccessible(true);
8687
$this->assertEquals($value, $reflProp->getValue($adapter));
8788
}
@@ -107,7 +108,7 @@ public function testSetMapperCorrectlySetsInProperty()
107108
$mapper = new \PHPExif\Mapper\Native();
108109
$this->adapter->setMapper($mapper);
109110

110-
$reflProp = new \ReflectionProperty('\\PHPExif\\Adapter\\AbstractAdapter', 'mapper');
111+
$reflProp = new \ReflectionProperty(AbstractAdapter::class, 'mapper');
111112
$reflProp->setAccessible(true);
112113
$this->assertSame($mapper, $reflProp->getValue($this->adapter));
113114
}
@@ -119,7 +120,7 @@ public function testSetMapperCorrectlySetsInProperty()
119120
public function testGetMapperCorrectlyReturnsFromProperty()
120121
{
121122
$mapper = new \PHPExif\Mapper\Native();
122-
$reflProp = new \ReflectionProperty('\\PHPExif\\Adapter\\AbstractAdapter', 'mapper');
123+
$reflProp = new \ReflectionProperty(AbstractAdapter::class, 'mapper');
123124
$reflProp->setAccessible(true);
124125
$reflProp->setValue($this->adapter, $mapper);
125126
$this->assertSame($mapper, $this->adapter->getMapper());
@@ -187,7 +188,7 @@ public function testSetHydratorCorrectlySetsInProperty()
187188
$hydrator = new \PHPExif\Hydrator\Mutator();
188189
$this->adapter->setHydrator($hydrator);
189190

190-
$reflProp = new \ReflectionProperty('\\PHPExif\\Adapter\\AbstractAdapter', 'hydrator');
191+
$reflProp = new \ReflectionProperty(AbstractAdapter::class, 'hydrator');
191192
$reflProp->setAccessible(true);
192193
$this->assertSame($hydrator, $reflProp->getValue($this->adapter));
193194
}
@@ -199,7 +200,7 @@ public function testSetHydratorCorrectlySetsInProperty()
199200
public function testGetHydratorCorrectlyReturnsFromProperty()
200201
{
201202
$hydrator = new \PHPExif\Hydrator\Mutator();
202-
$reflProp = new \ReflectionProperty('\\PHPExif\\Adapter\\AbstractAdapter', 'hydrator');
203+
$reflProp = new \ReflectionProperty(AbstractAdapter::class, 'hydrator');
203204
$reflProp->setAccessible(true);
204205
$reflProp->setValue($this->adapter, $hydrator);
205206
$this->assertSame($hydrator, $this->adapter->getHydrator());

tests/PHPExif/Adapter/ExiftoolProcOpenTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,20 @@ function proc_open($cmd, array $descriptorspec , &$pipes = array())
2424
}
2525

2626
/**
27-
* @covers \PHPExif\Adapter\Exiftool::<!public>
27+
* @covers Exiftool::<!public>
2828
*/
2929
class ExiftoolProcOpenTest extends \PHPUnit\Framework\TestCase
3030
{
3131
/**
32-
* @var \PHPExif\Adapter\Exiftool
32+
* @var Exiftool
3333
*/
3434
protected $adapter;
3535

3636
public function setUp() : void
3737
{
3838
global $mockProcOpen;
3939
$mockProcOpen = true;
40-
$this->adapter = new \PHPExif\Adapter\Exiftool();
40+
$this->adapter = new Exiftool();
4141
}
4242

4343
public function tearDown() : void
@@ -48,12 +48,12 @@ public function tearDown() : void
4848

4949
/**
5050
* @group exiftool
51-
* @covers \PHPExif\Adapter\Exiftool::getCliOutput
51+
* @covers Exiftool::getCliOutput
5252
*/
5353
public function testGetCliOutput()
5454
{
5555
$this->expectException('RuntimeException');
56-
$reflMethod = new \ReflectionMethod('\PHPExif\Adapter\Exiftool', 'getCliOutput');
56+
$reflMethod = new \ReflectionMethod(Exiftool::class, 'getCliOutput');
5757
$reflMethod->setAccessible(true);
5858

5959
$result = $reflMethod->invoke(

tests/PHPExif/Adapter/ExiftoolTest.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,24 @@
33
use PHPExif\Adapter\Exiftool;
44

55
/**
6-
* @covers \PHPExif\Adapter\Exiftool::<!public>
6+
* @covers Exiftool::<!public>
77
*/
88
class ExiftoolTest extends \PHPUnit\Framework\TestCase
99
{
1010
protected Exiftool $adapter;
1111

1212
public function setUp(): void
1313
{
14-
$this->adapter = new \PHPExif\Adapter\Exiftool();
14+
$this->adapter = new Exiftool();
1515
}
1616

1717
/**
1818
* @group exiftool
19-
* @covers \PHPExif\Adapter\Exiftool::getToolPath
19+
* @covers Exiftool::getToolPath
2020
*/
2121
public function testGetToolPathFromProperty()
2222
{
23-
$reflProperty = new \ReflectionProperty('\PHPExif\Adapter\Exiftool', 'toolPath');
23+
$reflProperty = new \ReflectionProperty(Exiftool::class, 'toolPath');
2424
$reflProperty->setAccessible(true);
2525
$expected = '/foo/bar/baz';
2626
$reflProperty->setValue($this->adapter, $expected);
@@ -30,11 +30,11 @@ public function testGetToolPathFromProperty()
3030

3131
/**
3232
* @group exiftool
33-
* @covers \PHPExif\Adapter\Exiftool::setToolPath
33+
* @covers Exiftool::setToolPath
3434
*/
3535
public function testSetToolPathInProperty()
3636
{
37-
$reflProperty = new \ReflectionProperty('\PHPExif\Adapter\Exiftool', 'toolPath');
37+
$reflProperty = new \ReflectionProperty(Exiftool::class, 'toolPath');
3838
$reflProperty->setAccessible(true);
3939

4040
$expected = '/tmp';
@@ -45,7 +45,7 @@ public function testSetToolPathInProperty()
4545

4646
/**
4747
* @group exiftool
48-
* @covers \PHPExif\Adapter\Exiftool::setToolPath
48+
* @covers Exiftool::setToolPath
4949
*/
5050
public function testSetToolPathThrowsException()
5151
{
@@ -56,7 +56,7 @@ public function testSetToolPathThrowsException()
5656

5757
/**
5858
* @group exiftool
59-
* @covers \PHPExif\Adapter\Exiftool::getToolPath
59+
* @covers Exiftool::getToolPath
6060
*/
6161
public function testGetToolPathLazyLoadsPath()
6262
{
@@ -65,11 +65,11 @@ public function testGetToolPathLazyLoadsPath()
6565

6666
/**
6767
* @group exiftool
68-
* @covers \PHPExif\Adapter\Exiftool::setNumeric
68+
* @covers Exiftool::setNumeric
6969
*/
7070
public function testSetNumericInProperty()
7171
{
72-
$reflProperty = new \ReflectionProperty('\PHPExif\Adapter\Exiftool', 'numeric');
72+
$reflProperty = new \ReflectionProperty(Exiftool::class, 'numeric');
7373
$reflProperty->setAccessible(true);
7474

7575
$expected = true;
@@ -81,11 +81,11 @@ public function testSetNumericInProperty()
8181
/**
8282
* @see URI http://www.sno.phy.queensu.ca/~phil/exiftool/faq.html#Q10
8383
* @group exiftool
84-
* @covers \PHPExif\Adapter\Exiftool::setEncoding
84+
* @covers Exiftool::setEncoding
8585
*/
8686
public function testSetEncodingInProperty()
8787
{
88-
$reflProperty = new \ReflectionProperty('\PHPExif\Adapter\Exiftool', 'encoding');
88+
$reflProperty = new \ReflectionProperty(Exiftool::class, 'encoding');
8989
$reflProperty->setAccessible(true);
9090

9191
$expected = array('iptc' => 'cp1250');
@@ -97,7 +97,7 @@ public function testSetEncodingInProperty()
9797

9898
/**
9999
* @group exiftool
100-
* @covers \PHPExif\Adapter\Exiftool::getExifFromFile
100+
* @covers Exiftool::getExifFromFile
101101
*/
102102
public function testGetExifFromFile()
103103
{
@@ -111,7 +111,7 @@ public function testGetExifFromFile()
111111

112112
/**
113113
* @group exiftool
114-
* @covers \PHPExif\Adapter\Exiftool::getExifFromFile
114+
* @covers Exiftool::getExifFromFile
115115
*/
116116
public function testGetExifFromFileWithUtf8()
117117
{
@@ -125,11 +125,11 @@ public function testGetExifFromFileWithUtf8()
125125

126126
/**
127127
* @group exiftool
128-
* @covers \PHPExif\Adapter\Exiftool::getCliOutput
128+
* @covers Exiftool::getCliOutput
129129
*/
130130
public function testGetCliOutput()
131131
{
132-
$reflMethod = new \ReflectionMethod('\PHPExif\Adapter\Exiftool', 'getCliOutput');
132+
$reflMethod = new \ReflectionMethod(Exiftool::class, 'getCliOutput');
133133
$reflMethod->setAccessible(true);
134134

135135
$result = $reflMethod->invoke(

tests/PHPExif/Adapter/FFprobeTest.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
<?php
2+
3+
use PHPExif\Adapter\FFprobe;
4+
use PHPExif\Exif;
5+
26
/**
37
* @covers \PHPExif\Adapter\Native::<!public>
48
*/
@@ -21,7 +25,7 @@ public function setUp(): void
2125
*/
2226
public function testGetToolPathFromProperty()
2327
{
24-
$reflProperty = new \ReflectionProperty('\PHPExif\Adapter\FFprobe', 'toolPath');
28+
$reflProperty = new \ReflectionProperty(FFprobe::class, 'toolPath');
2529
$reflProperty->setAccessible(true);
2630
$expected = '/foo/bar/baz';
2731
$reflProperty->setValue($this->adapter, $expected);
@@ -35,7 +39,7 @@ public function testGetToolPathFromProperty()
3539
*/
3640
public function testSetToolPathInProperty()
3741
{
38-
$reflProperty = new \ReflectionProperty('\PHPExif\Adapter\FFprobe', 'toolPath');
42+
$reflProperty = new \ReflectionProperty(FFprobe::class, 'toolPath');
3943
$reflProperty->setAccessible(true);
4044

4145
$expected = '/tmp';
@@ -71,13 +75,13 @@ public function testGetExifFromFileHasData()
7175
{
7276
$file = PHPEXIF_TEST_ROOT . '/files/IMG_3824.MOV';
7377
$result = $this->adapter->getExifFromFile($file);
74-
$this->assertInstanceOf('\PHPExif\Exif', $result);
78+
$this->assertInstanceOf(Exif::class, $result);
7579
$this->assertIsArray($result->getRawData());
7680
$this->assertNotEmpty($result->getRawData());
7781

7882
$file = PHPEXIF_TEST_ROOT . '/files/IMG_3825.MOV';
7983
$result = $this->adapter->getExifFromFile($file);
80-
$this->assertInstanceOf('\PHPExif\Exif', $result);
84+
$this->assertInstanceOf(Exif::class, $result);
8185
$this->assertIsArray($result->getRawData());
8286
$this->assertNotEmpty($result->getRawData());
8387
}
@@ -88,11 +92,9 @@ public function testGetExifFromFileHasData()
8892
*/
8993
public function testErrorImageUsed()
9094
{
91-
$file = PHPEXIF_TEST_ROOT . '/files/morning_glory_pool_500.jpg';;
92-
$result = $this->adapter->getExifFromFile($file);
93-
$this->assertIsBool($result);
94-
$this->assertEquals(false, $result);
95+
$file = PHPEXIF_TEST_ROOT . '/files/morning_glory_pool_500.jpg';
96+
$this->expectException(RuntimeException::class);
97+
$this->adapter->getExifFromFile($file);
9598
}
9699

97-
98100
}

0 commit comments

Comments
 (0)