AbstractReaderTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. <?php
  2. /**
  3. * This file is part of the PHPExiftool package.
  4. *
  5. * (c) Alchemy <support@alchemy.fr>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace PHPExiftool\Test;
  11. use PHPExiftool\Reader;
  12. abstract class AbstractReaderTest extends \PHPUnit_Framework_TestCase {
  13. /**
  14. * @var Reader
  15. */
  16. protected $object;
  17. protected static $tmpDir;
  18. protected static $disableSymLinkTest = false;
  19. public static function setUpBeforeClass()
  20. {
  21. parent::setUpBeforeClass();
  22. $tmpDir = __DIR__ . '/tmp';
  23. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  24. $command = 'rmdir /q /s ' . escapeshellarg($tmpDir);
  25. } else {
  26. $command = 'rmdir -Rf ' . escapeshellarg($tmpDir);
  27. }
  28. $process = new \Symfony\Component\Process\Process($command);
  29. $process->run();
  30. if (!is_dir($tmpDir)) {
  31. mkdir($tmpDir);
  32. }
  33. self::$tmpDir = $tmpDir . '/exiftool_reader';
  34. if (!is_dir(self::$tmpDir)) {
  35. mkdir(self::$tmpDir);
  36. }
  37. copy(__DIR__.'/../../../files/ExifTool.jpg', self::$tmpDir . '/test2.jpg');
  38. copy(__DIR__.'/../../../files/ExifTool.jpg', self::$tmpDir . '/test.jpg');
  39. if (!is_dir(self::$tmpDir . '/dir')) {
  40. mkdir(self::$tmpDir . '/dir');
  41. }
  42. if (!is_dir(self::$tmpDir . '/usr')) {
  43. mkdir(self::$tmpDir . '/usr');
  44. }
  45. $tmpDir2 = $tmpDir . '/exiftool_reader2';
  46. if (!is_dir($tmpDir2)) {
  47. mkdir($tmpDir2);
  48. }
  49. copy(__DIR__.'/../../../files/ExifTool.jpg', $tmpDir2 . '/test2.jpg');
  50. if (defined('PHP_WINDOWS_VERSION_BUILD')) {
  51. self::$disableSymLinkTest = true;
  52. } elseif (!is_link(self::$tmpDir . '/symlink')) {
  53. if (!@symlink($tmpDir2, self::$tmpDir . '/symlink')) {
  54. self::$disableSymLinkTest = true;
  55. }
  56. }
  57. copy(__DIR__.'/../../../files/plop/CanonRaw.cr2', self::$tmpDir . '/dir/CanonRaw.cr2');
  58. $tmpDir3 = $tmpDir . '/exiftool_reader3';
  59. if (!is_dir($tmpDir3)) {
  60. mkdir($tmpDir3);
  61. }
  62. if (!is_dir($tmpDir3 . '/.svn')) {
  63. mkdir($tmpDir3 . '/.svn');
  64. }
  65. if (!is_dir($tmpDir3 . '/.roro')) {
  66. mkdir($tmpDir3 . '/.roro');
  67. }
  68. if (!is_dir($tmpDir3 . '/.git')) {
  69. mkdir($tmpDir3 . '/.git');
  70. }
  71. touch($tmpDir3 . '/.git/config');
  72. touch($tmpDir3 . '/.roro/.roro.tmp');
  73. copy(__DIR__.'/../../../files/ExifTool.jpg', $tmpDir3 . '/.exiftool.jpg');
  74. }
  75. /**
  76. * @covers PHPExiftool\Reader::__construct
  77. */
  78. protected function setUp()
  79. {
  80. parent::setUp();
  81. $this->object = $this->getReader();
  82. }
  83. /**
  84. * @covers PHPExiftool\Reader::__destruct
  85. */
  86. protected function tearDown()
  87. {
  88. $this->object = null;
  89. parent::tearDown();
  90. }
  91. /**
  92. * @covers PHPExiftool\Reader::getIterator
  93. */
  94. public function testGetIterator()
  95. {
  96. $file = self::$tmpDir . '/test.jpg';
  97. $this->assertInstanceOf('\\Iterator', $this->object->files($file)->getIterator());
  98. }
  99. /**
  100. * @covers PHPExiftool\Reader::append
  101. * @covers PHPExiftool\Reader::all
  102. */
  103. public function testAppend()
  104. {
  105. $file1 = self::$tmpDir . '/test.jpg';
  106. $file2 = self::$tmpDir . '/test2.jpg';
  107. $file3 = self::$tmpDir . '/dir/CanonRaw.cr2';
  108. $this->assertEquals(1, count($this->object->files($file1)->all()));
  109. $reader = $this->getReader();
  110. $reader->files(array($file2, $file3));
  111. $this->assertEquals(3, count($this->object->append($reader)->all()));
  112. }
  113. /**
  114. * @covers PHPExiftool\Reader::sort
  115. * @covers PHPExiftool\Reader::all
  116. */
  117. public function testSort()
  118. {
  119. $file1 = self::$tmpDir . '/test.jpg';
  120. $file2 = self::$tmpDir . '/test2.jpg';
  121. $file3 = self::$tmpDir . '/dir/CanonRaw.cr2';
  122. $reader = $this->getReader();
  123. $reader->files(array($file3, $file2, $file1));
  124. $reader->sort(array('directory', 'filename', 'cigarette'));
  125. $results = array();
  126. foreach ($reader->all() as $entity) {
  127. $results[] = basename($entity->getFile());
  128. }
  129. $this->assertSame(array('test.jpg', 'test2.jpg', 'CanonRaw.cr2'), $results);
  130. }
  131. /**
  132. * @covers PHPExiftool\Reader::files
  133. * @covers PHPExiftool\Reader::buildQuery
  134. */
  135. public function testFiles()
  136. {
  137. $file = self::$tmpDir . '/test.jpg';
  138. $this->object->files($file);
  139. $file = $this->object->files(self::$tmpDir . '/test.jpg')->first()->getFile();
  140. $this->assertEquals(realpath($file), realpath($file));
  141. }
  142. /**
  143. * @covers PHPExiftool\Reader::resetResults
  144. */
  145. public function testResetFilters()
  146. {
  147. $file = self::$tmpDir . '/test.jpg';
  148. $this->object->files($file)->all();
  149. $file = self::$tmpDir . '/test2.jpg';
  150. $this->object->files($file)->all();
  151. $this->assertEquals(2, count($this->object->all()));
  152. }
  153. /**
  154. * @covers PHPExiftool\Reader::ignoreDotFiles
  155. * @covers PHPExiftool\Reader::all
  156. */
  157. public function testIgnoreVCS()
  158. {
  159. $this->object->in(self::$tmpDir . '3');
  160. $this->assertEquals(1, count($this->object->all()));
  161. }
  162. /**
  163. * @covers PHPExiftool\Reader::ignoreDotFiles
  164. * @covers PHPExiftool\Reader::all
  165. */
  166. public function testIgnoreDotFiles()
  167. {
  168. $this->object->in(self::$tmpDir . '3');
  169. $this->assertEquals(1, count($this->object->all()));
  170. $this->object->ignoreDotFiles()->in(self::$tmpDir . '3');
  171. $this->assertEquals(0, count($this->object->all()));
  172. }
  173. /**
  174. * @covers PHPExiftool\Reader::in
  175. * @covers PHPExiftool\Reader::buildQuery
  176. * @covers PHPExiftool\Reader::all
  177. */
  178. public function testIn()
  179. {
  180. $reader = $this->getReader();
  181. $reader->in(self::$tmpDir);
  182. $this->assertEquals(3, count($reader->all()));
  183. $reader = $this->getReader();
  184. $reader->in(self::$tmpDir . '/dir');
  185. $this->assertEquals(1, count($reader->all()));
  186. $reader = $this->getReader();
  187. $reader->in(__DIR__ . '/../../../../vendor/phpexiftool/exiftool/');
  188. foreach ($reader as $file) {
  189. $this->assertEquals(basename($file->getFile()), $file->getMetadatas()->get('System:FileName')->getValue()->asString());
  190. }
  191. }
  192. /**
  193. * @covers PHPExiftool\Reader::exclude
  194. * @covers PHPExiftool\Reader::computeExcludeDirs
  195. * @covers PHPExiftool\Reader::buildQuery
  196. * @covers PHPExiftool\Reader::all
  197. */
  198. public function testExclude()
  199. {
  200. $reader = $this->getReader();
  201. $reader
  202. ->in(self::$tmpDir)
  203. ->exclude(self::$tmpDir . '/dir');
  204. $this->assertEquals(2, count($reader->all()));
  205. }
  206. /**
  207. * @dataProvider getExclude
  208. * @covers PHPExiftool\Reader::computeExcludeDirs
  209. * @covers PHPExiftool\Reader::all
  210. */
  211. public function testComputeExcludeDirs($dir)
  212. {
  213. $reader = $this->getReader();
  214. $reader
  215. ->in(self::$tmpDir)
  216. ->exclude($dir)
  217. ->all();
  218. }
  219. public function getExclude()
  220. {
  221. return array(
  222. array(self::$tmpDir . '/dir/'),
  223. array(self::$tmpDir . '/dir'),
  224. array('dir'),
  225. array('/dir'),
  226. array('/usr'),
  227. array('usr'),
  228. array('dir/'),
  229. );
  230. }
  231. /**
  232. * @dataProvider getWrongExclude
  233. * @covers PHPExiftool\Reader::computeExcludeDirs
  234. * @covers \PHPExiftool\Exception\RuntimeException
  235. * @expectedException \PHPExiftool\Exception\RuntimeException
  236. */
  237. public function testComputeExcludeDirsFail($dir)
  238. {
  239. $reader = $this->getReader();
  240. $reader
  241. ->in(self::$tmpDir)
  242. ->exclude($dir)
  243. ->all();
  244. }
  245. public function getWrongExclude()
  246. {
  247. return array(
  248. array(self::$tmpDir . '/dir/dir2'),
  249. array(self::$tmpDir . '/dirlo'),
  250. array('dir/dir2'),
  251. array('/usr/local'),
  252. array('usr/local'),
  253. array('/tmp'),
  254. );
  255. }
  256. /**
  257. * @covers PHPExiftool\Reader::extensions
  258. * @covers PHPExiftool\Reader::buildQuery
  259. * @covers PHPExiftool\Reader::buildQueryAndExecute
  260. */
  261. public function testExtensions()
  262. {
  263. $reader = $this->getReader();
  264. $reader->in(self::$tmpDir);
  265. $this->assertEquals(3, count($reader->all()));
  266. $reader = $this->getReader();
  267. $reader->in(self::$tmpDir)->notRecursive()->extensions(array('cr2'));
  268. $this->assertEquals(0, count($reader->all()));
  269. $reader = $this->getReader();
  270. $reader->in(self::$tmpDir)->extensions(array('cr2'));
  271. $this->assertEquals(1, count($reader->all()));
  272. $reader = $this->getReader();
  273. $reader->in(self::$tmpDir)->extensions(array('jpg'));
  274. $this->assertEquals(2, count($reader->all()));
  275. $reader = $this->getReader();
  276. $reader->in(self::$tmpDir)->extensions('jpg')->extensions('cr2');
  277. $this->assertEquals(3, count($reader->all()));
  278. $reader = $this->getReader();
  279. $reader->in(self::$tmpDir)->extensions(array('jpg'), false);
  280. $this->assertEquals(1, count($reader->all()));
  281. $reader = $this->getReader();
  282. $reader->in(self::$tmpDir)->extensions(array('cr2', 'jpg'), false)->notRecursive();
  283. $this->assertEquals(0, count($reader->all()));
  284. }
  285. /**
  286. * @covers PHPExiftool\Reader::extensions
  287. * @covers \PHPExiftool\Exception\LogicException
  288. * @expectedException \PHPExiftool\Exception\LogicException
  289. */
  290. public function testExtensionsMisUse()
  291. {
  292. $reader = $this->getReader();
  293. $reader->extensions('exiftool')->extensions('jpg', false);
  294. }
  295. /**
  296. * @covers PHPExiftool\Reader::followSymLinks
  297. */
  298. public function testFollowSymLinks()
  299. {
  300. if (self::$disableSymLinkTest) {
  301. $this->markTestSkipped('This system does not support symlinks');
  302. }
  303. $reader = $this->getReader();
  304. $reader->in(self::$tmpDir)
  305. ->followSymLinks();
  306. $this->assertInstanceOf('\\Doctrine\\Common\\Collections\\ArrayCollection', $reader->all());
  307. $this->assertEquals(4, count($reader->all()));
  308. }
  309. /**
  310. * @covers PHPExiftool\Reader::notRecursive
  311. * @covers PHPExiftool\Reader::buildQuery
  312. */
  313. public function testNotRecursive()
  314. {
  315. $reader = $this->getReader();
  316. $reader->in(self::$tmpDir)->notRecursive();
  317. $this->assertEquals(2, count($reader->all()));
  318. }
  319. /**
  320. * @covers PHPExiftool\Reader::getOneOrNull
  321. */
  322. public function testGetOneOrNull()
  323. {
  324. $reader = $this->getReader();
  325. $reader->in(self::$tmpDir)->notRecursive()->extensions(array('jpg', 'cr2'), false);
  326. $this->assertNull($reader->getOneOrNull());
  327. }
  328. /**
  329. * @covers PHPExiftool\Reader::first
  330. * @covers \PHPExiftool\Exception\EmptyCollectionException
  331. * @expectedException \PHPExiftool\Exception\EmptyCollectionException
  332. */
  333. public function testFirstEmpty()
  334. {
  335. $reader = $this->getReader();
  336. $reader->in(self::$tmpDir)->notRecursive()->extensions(array('jpg', 'cr2'), false);
  337. $reader->first();
  338. }
  339. /**
  340. * @covers PHPExiftool\Reader::first
  341. */
  342. public function testFirst()
  343. {
  344. $reader = $this->getReader();
  345. $reader
  346. ->in(self::$tmpDir);
  347. $this->assertInstanceOf('\\PHPExiftool\\FileEntity', $reader->first());
  348. }
  349. /**
  350. * @covers PHPExiftool\Reader::buildQuery
  351. * @expectedException \PHPExiftool\Exception\LogicException
  352. */
  353. public function testFail()
  354. {
  355. $reader = $this->getReader();
  356. $reader->all();
  357. }
  358. /**
  359. * @covers PHPExiftool\Reader::all
  360. * @covers PHPExiftool\Reader::buildQueryAndExecute
  361. */
  362. public function testAll()
  363. {
  364. $reader = $this->getReader();
  365. $reader->in(self::$tmpDir);
  366. $this->assertInstanceOf('\\Doctrine\\Common\\Collections\\ArrayCollection', $reader->all());
  367. $this->assertEquals(3, count($reader->all()));
  368. }
  369. abstract protected function getReader();
  370. }