123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446 |
- <?php
- /**
- * This file is part of the PHPExiftool package.
- *
- * (c) Alchemy <support@alchemy.fr>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace PHPExiftool\Test;
- use PHPExiftool\Reader;
- abstract class AbstractReaderTest extends \PHPUnit_Framework_TestCase {
- /**
- * @var Reader
- */
- protected $object;
- protected static $tmpDir;
- protected static $disableSymLinkTest = false;
- public static function setUpBeforeClass()
- {
- parent::setUpBeforeClass();
- $tmpDir = __DIR__ . '/tmp';
- if (defined('PHP_WINDOWS_VERSION_BUILD')) {
- $command = 'rmdir /q /s ' . escapeshellarg($tmpDir);
- } else {
- $command = 'rmdir -Rf ' . escapeshellarg($tmpDir);
- }
- $process = new \Symfony\Component\Process\Process($command);
- $process->run();
- if (!is_dir($tmpDir)) {
- mkdir($tmpDir);
- }
- self::$tmpDir = $tmpDir . '/exiftool_reader';
- if (!is_dir(self::$tmpDir)) {
- mkdir(self::$tmpDir);
- }
- copy(__DIR__.'/../../../files/ExifTool.jpg', self::$tmpDir . '/test2.jpg');
- copy(__DIR__.'/../../../files/ExifTool.jpg', self::$tmpDir . '/test.jpg');
- if (!is_dir(self::$tmpDir . '/dir')) {
- mkdir(self::$tmpDir . '/dir');
- }
- if (!is_dir(self::$tmpDir . '/usr')) {
- mkdir(self::$tmpDir . '/usr');
- }
- $tmpDir2 = $tmpDir . '/exiftool_reader2';
- if (!is_dir($tmpDir2)) {
- mkdir($tmpDir2);
- }
- copy(__DIR__.'/../../../files/ExifTool.jpg', $tmpDir2 . '/test2.jpg');
- if (defined('PHP_WINDOWS_VERSION_BUILD')) {
- self::$disableSymLinkTest = true;
- } elseif (!is_link(self::$tmpDir . '/symlink')) {
- if (!@symlink($tmpDir2, self::$tmpDir . '/symlink')) {
- self::$disableSymLinkTest = true;
- }
- }
- copy(__DIR__.'/../../../files/plop/CanonRaw.cr2', self::$tmpDir . '/dir/CanonRaw.cr2');
- $tmpDir3 = $tmpDir . '/exiftool_reader3';
- if (!is_dir($tmpDir3)) {
- mkdir($tmpDir3);
- }
- if (!is_dir($tmpDir3 . '/.svn')) {
- mkdir($tmpDir3 . '/.svn');
- }
- if (!is_dir($tmpDir3 . '/.roro')) {
- mkdir($tmpDir3 . '/.roro');
- }
- if (!is_dir($tmpDir3 . '/.git')) {
- mkdir($tmpDir3 . '/.git');
- }
- touch($tmpDir3 . '/.git/config');
- touch($tmpDir3 . '/.roro/.roro.tmp');
- copy(__DIR__.'/../../../files/ExifTool.jpg', $tmpDir3 . '/.exiftool.jpg');
- }
- /**
- * @covers PHPExiftool\Reader::__construct
- */
- protected function setUp()
- {
- parent::setUp();
- $this->object = $this->getReader();
- }
- /**
- * @covers PHPExiftool\Reader::__destruct
- */
- protected function tearDown()
- {
- $this->object = null;
- parent::tearDown();
- }
- /**
- * @covers PHPExiftool\Reader::getIterator
- */
- public function testGetIterator()
- {
- $file = self::$tmpDir . '/test.jpg';
- $this->assertInstanceOf('\\Iterator', $this->object->files($file)->getIterator());
- }
- /**
- * @covers PHPExiftool\Reader::append
- * @covers PHPExiftool\Reader::all
- */
- public function testAppend()
- {
- $file1 = self::$tmpDir . '/test.jpg';
- $file2 = self::$tmpDir . '/test2.jpg';
- $file3 = self::$tmpDir . '/dir/CanonRaw.cr2';
- $this->assertEquals(1, count($this->object->files($file1)->all()));
- $reader = $this->getReader();
- $reader->files(array($file2, $file3));
- $this->assertEquals(3, count($this->object->append($reader)->all()));
- }
- /**
- * @covers PHPExiftool\Reader::sort
- * @covers PHPExiftool\Reader::all
- */
- public function testSort()
- {
- $file1 = self::$tmpDir . '/test.jpg';
- $file2 = self::$tmpDir . '/test2.jpg';
- $file3 = self::$tmpDir . '/dir/CanonRaw.cr2';
- $reader = $this->getReader();
- $reader->files(array($file3, $file2, $file1));
- $reader->sort(array('directory', 'filename', 'cigarette'));
- $results = array();
- foreach ($reader->all() as $entity) {
- $results[] = basename($entity->getFile());
- }
- $this->assertSame(array('test.jpg', 'test2.jpg', 'CanonRaw.cr2'), $results);
- }
- /**
- * @covers PHPExiftool\Reader::files
- * @covers PHPExiftool\Reader::buildQuery
- */
- public function testFiles()
- {
- $file = self::$tmpDir . '/test.jpg';
- $this->object->files($file);
- $file = $this->object->files(self::$tmpDir . '/test.jpg')->first()->getFile();
- $this->assertEquals(realpath($file), realpath($file));
- }
- /**
- * @covers PHPExiftool\Reader::resetResults
- */
- public function testResetFilters()
- {
- $file = self::$tmpDir . '/test.jpg';
- $this->object->files($file)->all();
- $file = self::$tmpDir . '/test2.jpg';
- $this->object->files($file)->all();
- $this->assertEquals(2, count($this->object->all()));
- }
- /**
- * @covers PHPExiftool\Reader::ignoreDotFiles
- * @covers PHPExiftool\Reader::all
- */
- public function testIgnoreVCS()
- {
- $this->object->in(self::$tmpDir . '3');
- $this->assertEquals(1, count($this->object->all()));
- }
- /**
- * @covers PHPExiftool\Reader::ignoreDotFiles
- * @covers PHPExiftool\Reader::all
- */
- public function testIgnoreDotFiles()
- {
- $this->object->in(self::$tmpDir . '3');
- $this->assertEquals(1, count($this->object->all()));
- $this->object->ignoreDotFiles()->in(self::$tmpDir . '3');
- $this->assertEquals(0, count($this->object->all()));
- }
- /**
- * @covers PHPExiftool\Reader::in
- * @covers PHPExiftool\Reader::buildQuery
- * @covers PHPExiftool\Reader::all
- */
- public function testIn()
- {
- $reader = $this->getReader();
- $reader->in(self::$tmpDir);
- $this->assertEquals(3, count($reader->all()));
- $reader = $this->getReader();
- $reader->in(self::$tmpDir . '/dir');
- $this->assertEquals(1, count($reader->all()));
- $reader = $this->getReader();
- $reader->in(__DIR__ . '/../../../../vendor/phpexiftool/exiftool/');
- foreach ($reader as $file) {
- $this->assertEquals(basename($file->getFile()), $file->getMetadatas()->get('System:FileName')->getValue()->asString());
- }
- }
- /**
- * @covers PHPExiftool\Reader::exclude
- * @covers PHPExiftool\Reader::computeExcludeDirs
- * @covers PHPExiftool\Reader::buildQuery
- * @covers PHPExiftool\Reader::all
- */
- public function testExclude()
- {
- $reader = $this->getReader();
- $reader
- ->in(self::$tmpDir)
- ->exclude(self::$tmpDir . '/dir');
- $this->assertEquals(2, count($reader->all()));
- }
- /**
- * @dataProvider getExclude
- * @covers PHPExiftool\Reader::computeExcludeDirs
- * @covers PHPExiftool\Reader::all
- */
- public function testComputeExcludeDirs($dir)
- {
- $reader = $this->getReader();
- $reader
- ->in(self::$tmpDir)
- ->exclude($dir)
- ->all();
- }
- public function getExclude()
- {
- return array(
- array(self::$tmpDir . '/dir/'),
- array(self::$tmpDir . '/dir'),
- array('dir'),
- array('/dir'),
- array('/usr'),
- array('usr'),
- array('dir/'),
- );
- }
- /**
- * @dataProvider getWrongExclude
- * @covers PHPExiftool\Reader::computeExcludeDirs
- * @covers \PHPExiftool\Exception\RuntimeException
- * @expectedException \PHPExiftool\Exception\RuntimeException
- */
- public function testComputeExcludeDirsFail($dir)
- {
- $reader = $this->getReader();
- $reader
- ->in(self::$tmpDir)
- ->exclude($dir)
- ->all();
- }
- public function getWrongExclude()
- {
- return array(
- array(self::$tmpDir . '/dir/dir2'),
- array(self::$tmpDir . '/dirlo'),
- array('dir/dir2'),
- array('/usr/local'),
- array('usr/local'),
- array('/tmp'),
- );
- }
- /**
- * @covers PHPExiftool\Reader::extensions
- * @covers PHPExiftool\Reader::buildQuery
- * @covers PHPExiftool\Reader::buildQueryAndExecute
- */
- public function testExtensions()
- {
- $reader = $this->getReader();
- $reader->in(self::$tmpDir);
- $this->assertEquals(3, count($reader->all()));
- $reader = $this->getReader();
- $reader->in(self::$tmpDir)->notRecursive()->extensions(array('cr2'));
- $this->assertEquals(0, count($reader->all()));
- $reader = $this->getReader();
- $reader->in(self::$tmpDir)->extensions(array('cr2'));
- $this->assertEquals(1, count($reader->all()));
- $reader = $this->getReader();
- $reader->in(self::$tmpDir)->extensions(array('jpg'));
- $this->assertEquals(2, count($reader->all()));
- $reader = $this->getReader();
- $reader->in(self::$tmpDir)->extensions('jpg')->extensions('cr2');
- $this->assertEquals(3, count($reader->all()));
- $reader = $this->getReader();
- $reader->in(self::$tmpDir)->extensions(array('jpg'), false);
- $this->assertEquals(1, count($reader->all()));
- $reader = $this->getReader();
- $reader->in(self::$tmpDir)->extensions(array('cr2', 'jpg'), false)->notRecursive();
- $this->assertEquals(0, count($reader->all()));
- }
- /**
- * @covers PHPExiftool\Reader::extensions
- * @covers \PHPExiftool\Exception\LogicException
- * @expectedException \PHPExiftool\Exception\LogicException
- */
- public function testExtensionsMisUse()
- {
- $reader = $this->getReader();
- $reader->extensions('exiftool')->extensions('jpg', false);
- }
- /**
- * @covers PHPExiftool\Reader::followSymLinks
- */
- public function testFollowSymLinks()
- {
- if (self::$disableSymLinkTest) {
- $this->markTestSkipped('This system does not support symlinks');
- }
- $reader = $this->getReader();
- $reader->in(self::$tmpDir)
- ->followSymLinks();
- $this->assertInstanceOf('\\Doctrine\\Common\\Collections\\ArrayCollection', $reader->all());
- $this->assertEquals(4, count($reader->all()));
- }
- /**
- * @covers PHPExiftool\Reader::notRecursive
- * @covers PHPExiftool\Reader::buildQuery
- */
- public function testNotRecursive()
- {
- $reader = $this->getReader();
- $reader->in(self::$tmpDir)->notRecursive();
- $this->assertEquals(2, count($reader->all()));
- }
- /**
- * @covers PHPExiftool\Reader::getOneOrNull
- */
- public function testGetOneOrNull()
- {
- $reader = $this->getReader();
- $reader->in(self::$tmpDir)->notRecursive()->extensions(array('jpg', 'cr2'), false);
- $this->assertNull($reader->getOneOrNull());
- }
- /**
- * @covers PHPExiftool\Reader::first
- * @covers \PHPExiftool\Exception\EmptyCollectionException
- * @expectedException \PHPExiftool\Exception\EmptyCollectionException
- */
- public function testFirstEmpty()
- {
- $reader = $this->getReader();
- $reader->in(self::$tmpDir)->notRecursive()->extensions(array('jpg', 'cr2'), false);
- $reader->first();
- }
- /**
- * @covers PHPExiftool\Reader::first
- */
- public function testFirst()
- {
- $reader = $this->getReader();
- $reader
- ->in(self::$tmpDir);
- $this->assertInstanceOf('\\PHPExiftool\\FileEntity', $reader->first());
- }
- /**
- * @covers PHPExiftool\Reader::buildQuery
- * @expectedException \PHPExiftool\Exception\LogicException
- */
- public function testFail()
- {
- $reader = $this->getReader();
- $reader->all();
- }
- /**
- * @covers PHPExiftool\Reader::all
- * @covers PHPExiftool\Reader::buildQueryAndExecute
- */
- public function testAll()
- {
- $reader = $this->getReader();
- $reader->in(self::$tmpDir);
- $this->assertInstanceOf('\\Doctrine\\Common\\Collections\\ArrayCollection', $reader->all());
- $this->assertEquals(3, count($reader->all()));
- }
- abstract protected function getReader();
- }
|