diff --git a/lib/Alchemy/Phrasea/Border/File.php b/lib/Alchemy/Phrasea/Border/File.php index d69fd6ea93..0008a92c1b 100644 --- a/lib/Alchemy/Phrasea/Border/File.php +++ b/lib/Alchemy/Phrasea/Border/File.php @@ -11,6 +11,7 @@ namespace Alchemy\Phrasea\Border; +use MediaVorus\Media\Media; use MediaVorus\MediaVorus; use PHPExiftool\Reader; use PHPExiftool\Writer; @@ -44,18 +45,18 @@ class File /** * Constructor * - * @param type $pathfile The path to the file + * @param Media $media The media * @param \collection $collection The destination collection - * @param type $originalName The original name of the file + * @param string $originalName The original name of the file * (if not provided, original name is * extracted from the pathfile) */ - public function __construct($pathfile, \collection $collection, $originalName = null) + public function __construct(Media $media, \collection $collection, $originalName = null) { - $this->media = MediaVorus::guess(new \SplFileInfo($pathfile)); + $this->media = $media; $this->collection = $collection; $this->attributes = array(); - $this->originalName = $originalName ? : pathinfo($pathfile, PATHINFO_BASENAME); + $this->originalName = $originalName ? : pathinfo($this->getPathfile(), PATHINFO_BASENAME); $this->ensureUUID(); } @@ -227,4 +228,26 @@ class File return $this; } + + /** + * Build the File package object + * + * @param string $pathfile The path to the file + * @param \collection $collection The destination collection + * @param string $originalName An optionnal original name (if + * different from the $pathfile filename) + * @throws \RuntimeException + * + * @return \Alchemy\Phrasea\Border\File + */ + public function buildFromPathfile($pathfile, \collection $collection, $originalName = null) + { + try { + $media = MediaVorus::guess(new \SplFileInfo($pathfile)); + } catch (\MediaVorus\Exception\FileNotFoundException $e) { + throw new \RuntimeException(sprintf('Unable to build media file from non existant %s', $pathfile)); + } + + return new File($media, $collection, $originalName); + } } diff --git a/tests/Alchemy/Phrasea/Border/Checker/FilenameTest.php b/tests/Alchemy/Phrasea/Border/Checker/FilenameTest.php index abb94a392e..7f379a5471 100644 --- a/tests/Alchemy/Phrasea/Border/Checker/FilenameTest.php +++ b/tests/Alchemy/Phrasea/Border/Checker/FilenameTest.php @@ -6,13 +6,14 @@ require_once __DIR__ . '/../../../../PhraseanetPHPUnitAbstract.class.inc'; use Alchemy\Phrasea\Border\File; -class FilenameTest extends \PhraseanetPHPUnitAbstract +class FilenameTest extends \PHPUnit_Framework_TestCase { /** * @var Filename */ protected $object; protected $filename; + protected $media; public function setUp() { @@ -20,10 +21,12 @@ class FilenameTest extends \PhraseanetPHPUnitAbstract $this->object = new Filename; $this->filename = __DIR__ . '/../../../../../tmp/test001.CR2'; copy(__DIR__ . '/../../../../testfiles/test001.CR2', $this->filename); + $this->media = \MediaVorus\MediaVorus::guess(new \SplFileInfo($this->filename)); } public function tearDown() { + $this->media = null; if (file_exists($this->filename)) { unlink($this->filename); } @@ -35,7 +38,7 @@ class FilenameTest extends \PhraseanetPHPUnitAbstract */ public function testCheck() { - $response = $this->object->check(self::$core['EM'], new File($this->filename, self::$collection)); + $response = $this->object->check(self::$core['EM'], new File($this->media, self::$collection)); $this->assertInstanceOf('\\Alchemy\\Phrasea\\Border\\Checker\\Response', $response); @@ -47,7 +50,7 @@ class FilenameTest extends \PhraseanetPHPUnitAbstract */ public function testCheckNoFile() { - $mock = $this->getMock('\\Alchemy\\Phrasea\\Border\\File', array('getOriginalName'), array($this->filename, self::$collection)); + $mock = $this->getMock('\\Alchemy\\Phrasea\\Border\\File', array('getOriginalName'), array($this->media, self::$collection)); $mock ->expects($this->once()) diff --git a/tests/Alchemy/Phrasea/Border/Checker/ResponseTest.php b/tests/Alchemy/Phrasea/Border/Checker/ResponseTest.php index a506c92f0f..12bb64b157 100644 --- a/tests/Alchemy/Phrasea/Border/Checker/ResponseTest.php +++ b/tests/Alchemy/Phrasea/Border/Checker/ResponseTest.php @@ -40,7 +40,7 @@ class ResponseTest extends \PHPUnit_Framework_TestCase /** * @covers Alchemy\Phrasea\Border\Checker\Checker - * @covers Alchemy\Phrasea\Border\Checker\Response::getCheck + * @covers Alchemy\Phrasea\Border\Checker\Response::getChecker */ public function testGetCheck() { diff --git a/tests/Alchemy/Phrasea/Border/Checker/Sha256Test.php b/tests/Alchemy/Phrasea/Border/Checker/Sha256Test.php index 81cb19964b..c63b41591e 100644 --- a/tests/Alchemy/Phrasea/Border/Checker/Sha256Test.php +++ b/tests/Alchemy/Phrasea/Border/Checker/Sha256Test.php @@ -13,6 +13,7 @@ class Sha256Test extends \PhraseanetPHPUnitAbstract */ protected $object; protected $filename; + protected $media; public function setUp() { @@ -20,10 +21,12 @@ class Sha256Test extends \PhraseanetPHPUnitAbstract $this->object = new Sha256; $this->filename = __DIR__ . '/../../../../../tmp/test001.CR2'; copy(__DIR__ . '/../../../../testfiles/test001.CR2', $this->filename); + $this->media = \MediaVorus\MediaVorus::guess(new \SplFileInfo($this->filename)); } public function tearDown() { + $this->media = null; if (file_exists($this->filename)) { unlink($this->filename); } @@ -35,7 +38,7 @@ class Sha256Test extends \PhraseanetPHPUnitAbstract */ public function testCheck() { - $mock = $this->getMock('\\Alchemy\\Phrasea\\Border\\File', array('getSha256'), array($this->filename, self::$collection)); + $mock = $this->getMock('\\Alchemy\\Phrasea\\Border\\File', array('getSha256'), array($this->media, self::$collection)); $mock ->expects($this->once()) @@ -55,7 +58,7 @@ class Sha256Test extends \PhraseanetPHPUnitAbstract */ public function testCheckNoFile() { - $mock = $this->getMock('\\Alchemy\\Phrasea\\Border\\File', array('getSha256'), array($this->filename, self::$collection)); + $mock = $this->getMock('\\Alchemy\\Phrasea\\Border\\File', array('getSha256'), array($this->media, self::$collection)); $mock ->expects($this->once()) diff --git a/tests/Alchemy/Phrasea/Border/Checker/UUIDTest.php b/tests/Alchemy/Phrasea/Border/Checker/UUIDTest.php index 77739a9a6a..a419f6b5d2 100644 --- a/tests/Alchemy/Phrasea/Border/Checker/UUIDTest.php +++ b/tests/Alchemy/Phrasea/Border/Checker/UUIDTest.php @@ -13,6 +13,7 @@ class UUIDTest extends \PhraseanetPHPUnitAbstract */ protected $object; protected $filename; + protected $media; public function setUp() { @@ -20,10 +21,12 @@ class UUIDTest extends \PhraseanetPHPUnitAbstract $this->object = new UUID; $this->filename = __DIR__ . '/../../../../../tmp/test001.CR2'; copy(__DIR__ . '/../../../../testfiles/test001.CR2', $this->filename); + $this->media = \MediaVorus\MediaVorus::guess(new \SplFileInfo($this->filename)); } public function tearDown() { + $this->media = null; if (file_exists($this->filename)) { unlink($this->filename); } @@ -35,7 +38,7 @@ class UUIDTest extends \PhraseanetPHPUnitAbstract */ public function testCheck() { - $response = $this->object->check(self::$core['EM'], new File($this->filename, self::$collection)); + $response = $this->object->check(self::$core['EM'], new File($this->media, self::$collection)); $this->assertInstanceOf('\\Alchemy\\Phrasea\\Border\\Checker\\Response', $response); @@ -47,7 +50,7 @@ class UUIDTest extends \PhraseanetPHPUnitAbstract */ public function testCheckNoFile() { - $mock = $this->getMock('\\Alchemy\\Phrasea\\Border\\File', array('getUUID'), array($this->filename, self::$collection)); + $mock = $this->getMock('\\Alchemy\\Phrasea\\Border\\File', array('getUUID'), array($this->media, self::$collection)); $mock ->expects($this->once()) diff --git a/tests/Alchemy/Phrasea/Border/FileTest.php b/tests/Alchemy/Phrasea/Border/FileTest.php index 8714f50a33..c1cb462150 100644 --- a/tests/Alchemy/Phrasea/Border/FileTest.php +++ b/tests/Alchemy/Phrasea/Border/FileTest.php @@ -11,6 +11,7 @@ class FileTest extends \PhraseanetPHPUnitAbstract */ protected $object; protected $filename; + protected $media; /** * @covers Alchemy\Phrasea\Border\File::__construct @@ -20,7 +21,10 @@ class FileTest extends \PhraseanetPHPUnitAbstract parent::setUp(); $this->filename = __DIR__ . '/../../../../tmp/iphone_pic.jpg'; copy(__DIR__ . '/../../../testfiles/iphone_pic.jpg', $this->filename); - $this->object = new File($this->filename, self::$collection, 'originalName.txt'); + + $this->media = \MediaVorus\MediaVorus::guess(new \SplFileInfo($this->filename)); + + $this->object = new File($this->media, self::$collection, 'originalName.txt'); } /** @@ -58,7 +62,7 @@ class FileTest extends \PhraseanetPHPUnitAbstract copy(__DIR__ . '/../../../testfiles/p4logo.jpg', $file); - $borderFile = new File($file, self::$collection); + $borderFile = new File(\MediaVorus\MediaVorus::guess(new \SplFileInfo($file)), self::$collection); $uuid = $borderFile->getUUID(); $this->assertTrue(\uuid::is_valid($uuid)); @@ -122,7 +126,7 @@ class FileTest extends \PhraseanetPHPUnitAbstract */ public function testOriginalNameAuto() { - $object = new File($this->filename, self::$collection); + $object = new File(\MediaVorus\MediaVorus::guess(new \SplFileInfo($this->filename)), self::$collection); $this->assertSame('iphone_pic.jpg', $object->getOriginalName()); } diff --git a/tests/Alchemy/Phrasea/Border/ManagerTest.php b/tests/Alchemy/Phrasea/Border/ManagerTest.php index 3a8bbae449..cfddf646b2 100644 --- a/tests/Alchemy/Phrasea/Border/ManagerTest.php +++ b/tests/Alchemy/Phrasea/Border/ManagerTest.php @@ -69,7 +69,7 @@ class ManagerTest extends \PhraseanetPHPUnitAbstract $records[] = $record; }; - $this->assertEquals(Manager::RECORD_CREATED, $this->object->process($this->session, new File(self::$file1, self::$collection), $postProcessRecord)); + $this->assertEquals(Manager::RECORD_CREATED, $this->object->process($this->session, File::buildFromPathfile(self::$file1, self::$collection), $postProcessRecord)); $shaChecker = new Checker\Sha256(); $this->object->registerChecker($shaChecker); @@ -82,7 +82,7 @@ class ManagerTest extends \PhraseanetPHPUnitAbstract $records[] = $element; }; - $this->assertEquals(Manager::LAZARET_CREATED, $this->object->process($this->session, new File(self::$file1, self::$collection), $postProcess)); + $this->assertEquals(Manager::LAZARET_CREATED, $this->object->process($this->session, File::buildFromPathfile(self::$file1, self::$collection), $postProcess)); $postProcess = function($element, $visa, $code) use ($phpunit, &$records) { $phpunit->assertInstanceOf('\\record_adapter', $element); @@ -91,7 +91,7 @@ class ManagerTest extends \PhraseanetPHPUnitAbstract $records[] = $element; }; - $this->assertEquals(Manager::RECORD_CREATED, $this->object->process($this->session, new File(self::$file1, self::$collection), $postProcess, Manager::FORCE_RECORD)); + $this->assertEquals(Manager::RECORD_CREATED, $this->object->process($this->session, File::buildFromPathfile(self::$file1, self::$collection), $postProcess, Manager::FORCE_RECORD)); foreach ($records as $record) { if ($record instanceof \record_adapter) { @@ -110,8 +110,8 @@ class ManagerTest extends \PhraseanetPHPUnitAbstract $postProcessRecord = function($record) use(&$records) { $records[] = $record; }; - $this->assertEquals(Manager::LAZARET_CREATED, $this->object->process($this->session, new File(self::$file1, self::$collection), NULL, Manager::FORCE_LAZARET)); - $this->assertEquals(Manager::RECORD_CREATED, $this->object->process($this->session, new File(self::$file1, self::$collection), $postProcessRecord)); + $this->assertEquals(Manager::LAZARET_CREATED, $this->object->process($this->session, File::buildFromPathfile(self::$file1, self::$collection), NULL, Manager::FORCE_LAZARET)); + $this->assertEquals(Manager::RECORD_CREATED, $this->object->process($this->session, File::buildFromPathfile(self::$file1, self::$collection), $postProcessRecord)); foreach ($records as $record) { if ($record instanceof \record_adapter) { @@ -125,7 +125,7 @@ class ManagerTest extends \PhraseanetPHPUnitAbstract */ public function testLazaretAttributes() { - $file = new File(self::$file1, self::$collection); + $file = File::buildFromPathfile(self::$file1, self::$collection); $objectNameTag = new \PHPExiftool\Driver\Tag\IPTC\ObjectName(); $monoValue = new \PHPExiftool\Driver\Value\Mono('title'); @@ -165,16 +165,16 @@ class ManagerTest extends \PhraseanetPHPUnitAbstract $records[] = $record; }; - $visa = $this->object->getVisa(new File(self::$file1, self::$collection)); + $visa = $this->object->getVisa(File::buildFromPathfile(self::$file1, self::$collection)); $this->assertInstanceOf('\\Alchemy\\Phrasea\\Border\\Visa', $visa); $this->assertTrue($visa->isValid()); - $this->object->process($this->session, new File(self::$file1, self::$collection), $postProcessRecord); + $this->object->process($this->session, File::buildFromPathfile(self::$file1, self::$collection), $postProcessRecord); - $visa = $this->object->getVisa(new File(self::$file1, self::$collection)); + $visa = $this->object->getVisa(File::buildFromPathfile(self::$file1, self::$collection)); $this->assertInstanceOf('\\Alchemy\\Phrasea\\Border\\Visa', $visa); @@ -182,7 +182,7 @@ class ManagerTest extends \PhraseanetPHPUnitAbstract $this->object->registerChecker(new Checker\Sha256()); - $visa = $this->object->getVisa(new File(self::$file1, self::$collection)); + $visa = $this->object->getVisa(File::buildFromPathfile(self::$file1, self::$collection)); $this->assertInstanceOf('\\Alchemy\\Phrasea\\Border\\Visa', $visa);