From a539919cf8b3a9473b65ba3d79ccda9702d1f85f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Burnichon?= Date: Fri, 5 Feb 2016 14:10:43 +0100 Subject: [PATCH] Add tested booker class --- .../Filesystem/LazaretFilesystemService.php | 39 ++------- .../Phrasea/Filesystem/LazaretPathBooker.php | 87 +++++++++++++++++++ .../Tests/Phrasea/Border/ManagerTest.php | 25 ------ .../Filesystem/LazaretPathBookerTest.php | 50 +++++++++++ 4 files changed, 146 insertions(+), 55 deletions(-) create mode 100644 lib/Alchemy/Phrasea/Filesystem/LazaretPathBooker.php create mode 100644 tests/Alchemy/Tests/Phrasea/Filesystem/LazaretPathBookerTest.php diff --git a/lib/Alchemy/Phrasea/Filesystem/LazaretFilesystemService.php b/lib/Alchemy/Phrasea/Filesystem/LazaretFilesystemService.php index 927760b5c2..113bf6839b 100644 --- a/lib/Alchemy/Phrasea/Filesystem/LazaretFilesystemService.php +++ b/lib/Alchemy/Phrasea/Filesystem/LazaretFilesystemService.php @@ -24,21 +24,22 @@ class LazaretFilesystemService */ private $filesystem; - /** - * @var string - */ - private $tmpPath; - /** * @var Alchemyst */ private $alchemyst; + /** + * @var LazaretPathBooker + */ + private $booker; + public function __construct(Filesystem $filesystem, $tmpPath, Alchemyst $alchemyst) { $this->filesystem = $filesystem; - $this->tmpPath = $tmpPath; $this->alchemyst = $alchemyst; + + $this->booker = new LazaretPathBooker($filesystem, $tmpPath); } /** @@ -48,10 +49,10 @@ class LazaretFilesystemService */ public function writeLazaret(File $file) { - $lazaretPathname = $this->bookLazaretPathfile($file->getOriginalName()); + $lazaretPathname = $this->booker->bookFile($file->getOriginalName()); $this->filesystem->copy($file->getFile()->getRealPath(), $lazaretPathname, true); - $lazaretPathnameThumb = $this->bookLazaretPathfile($file->getOriginalName(), 'thumb'); + $lazaretPathnameThumb = $this->booker->bookFile($file->getOriginalName(), 'thumb'); try { $this->alchemyst->turnInto($file->getFile()->getPathname(), $lazaretPathnameThumb, $this->createThumbnailSpecification()); } catch (ExceptionInterface $e) { @@ -61,28 +62,6 @@ class LazaretFilesystemService return new PersistedLazaretInformation($lazaretPathname, $lazaretPathnameThumb); } - private function bookLazaretPathfile($filename, $suffix = '') - { - $output = $this->tmpPath .'/lzrt_' . substr($filename, 0, 3) . '_' . $suffix . '.' . pathinfo($filename, PATHINFO_EXTENSION); - $infos = pathinfo($output); - $n = 0; - - while (true) { - $output = sprintf('%s/%s-%d%s', $infos['dirname'], $infos['filename'], ++ $n, (isset($infos['extension']) ? '.' . $infos['extension'] : '')); - - try { - if (! $this->filesystem->exists($output)) { - $this->filesystem->touch($output); - break; - } - } catch (IOException $e) { - - } - } - - return realpath($output); - } - /** * @return ImageSpecification */ diff --git a/lib/Alchemy/Phrasea/Filesystem/LazaretPathBooker.php b/lib/Alchemy/Phrasea/Filesystem/LazaretPathBooker.php new file mode 100644 index 0000000000..824ed965cc --- /dev/null +++ b/lib/Alchemy/Phrasea/Filesystem/LazaretPathBooker.php @@ -0,0 +1,87 @@ +filesystem = $filesystem; + $this->tmpPath = $tmpPath; + + if (!is_callable($pathResolver)) { + throw new \LogicException('pathResolver should be callable'); + } + + $this->pathResolver = $pathResolver; + } + + /** + * @param string $filename + * @param string $suffix + * @return string + */ + public function bookFile($filename, $suffix = '') + { + $output = $this->tmpPath .'/lzrt_' . substr($filename, 0, 3) . '_' . $suffix . '.' . pathinfo($filename, PATHINFO_EXTENSION); + $infos = pathinfo($output); + $n = 0; + + while (true) { + $output = sprintf('%s/%s-%d%s', $infos['dirname'], $infos['filename'], ++ $n, (isset($infos['extension']) ? '.' . $infos['extension'] : '')); + + try { + if (! $this->filesystem->exists($output)) { + $this->filesystem->touch($output); + break; + } + } catch (IOException $e) { + + } + } + + return $this->resolvePath($output); + } + + /** + * @param string $path + * @return string + */ + private function resolvePath($path) + { + $callable = $this->pathResolver; + + return $callable($path); + } +} diff --git a/tests/Alchemy/Tests/Phrasea/Border/ManagerTest.php b/tests/Alchemy/Tests/Phrasea/Border/ManagerTest.php index 89aed867c3..1239ba3adc 100644 --- a/tests/Alchemy/Tests/Phrasea/Border/ManagerTest.php +++ b/tests/Alchemy/Tests/Phrasea/Border/ManagerTest.php @@ -639,35 +639,10 @@ class ManagerTest extends \PhraseanetAuthenticatedWebTestCase $this->object->unregisterChecker($uuidChecker); $this->assertEquals([$shaChecker, $filenameChecker], $this->object->getCheckers()); } - - /** - * @covers Alchemy\Phrasea\Border\Manager::bookLazaretPathfile - */ - public function testBookLazaretPathfile() - { - $manager = new ManagerTester(self::$DI['app']); - - $file1 = $manager->bookLazaretPathfileTester('babebibobu.txt'); - $file2 = $manager->bookLazaretPathfileTester('babebibobu.txt'); - - $this->assertNotEquals($file2, $file1); - - $this->assertTrue(file_exists($file1)); - $this->assertTrue(file_exists($file2)); - - unlink($file1); - unlink($file2); - } } class ManagerTester extends Manager { - - public function bookLazaretPathfileTester($filename) - { - return parent::bookLazaretPathfile($filename); - } - public function addMediaAttributesTester($file) { return parent::addMediaAttributes($file); diff --git a/tests/Alchemy/Tests/Phrasea/Filesystem/LazaretPathBookerTest.php b/tests/Alchemy/Tests/Phrasea/Filesystem/LazaretPathBookerTest.php new file mode 100644 index 0000000000..b07fdfebde --- /dev/null +++ b/tests/Alchemy/Tests/Phrasea/Filesystem/LazaretPathBookerTest.php @@ -0,0 +1,50 @@ +root = vfsStream::setup(); + } + + /** + * @covers Alchemy\Phrasea\Border\Manager::bookLazaretPathfile + */ + public function testBookFile() + { + $mockedPathResolver = function ($path) { + return $path; + }; + + $sut = new LazaretPathBooker(new Filesystem(), $this->root->url(), $mockedPathResolver); + + $file1 = $sut->bookFile('babebibobu.txt'); + $file2 = $sut->bookFile('babebibobu.txt'); + + $this->assertNotEquals($file2, $file1); + + $this->assertCount(2, $this->root->getChildren()); + $this->assertTrue($this->root->hasChild(vfsStream::path($file1))); + $this->assertTrue($this->root->hasChild(vfsStream::path($file2))); + } +}