From 3744f0429a1a3beff06126bc3982ba4dc3a5705c Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Wed, 8 Oct 2014 19:39:20 +0200 Subject: [PATCH] Split FileService to NetworkingService and FileDao --- TODO | 1 - src/Controllers/CommentController.php | 2 +- src/Controllers/FavoritesController.php | 2 +- src/Controllers/PostContentController.php | 22 +++---- src/Controllers/UserAvatarController.php | 16 +++-- src/Dao/AbstractDao.php | 2 +- src/Dao/FileDao.php | 53 ++++++++++++++++ src/Dao/IBatchDao.php | 11 ++++ src/Dao/ICrudDao.php | 6 -- src/Dao/IFileDao.php | 13 ++++ src/Dao/PostDao.php | 20 +++--- src/Dao/PublicFileDao.php | 13 ++++ src/Dao/UserDao.php | 16 ++--- ...{FileService.php => NetworkingService.php} | 51 +--------------- src/Services/PostService.php | 12 ++-- src/Services/TagService.php | 12 ++-- src/Services/ThumbnailService.php | 22 +++---- src/Services/UserService.php | 4 -- src/Upgrades/Upgrade04.php | 10 +-- tests/AbstractDatabaseTestCase.php | 20 +++--- tests/Dao/FileDaoTest.php | 61 +++++++++++++++++++ tests/Dao/PostDaoTest.php | 20 +++--- tests/Dao/UserDaoFilterTest.php | 8 +-- tests/Dao/UserDaoTest.php | 12 ++-- tests/Services/FileServiceTest.php | 29 --------- tests/Services/NetworkingServiceTest.php | 16 +++++ tests/Services/PostServiceTest.php | 8 +-- tests/Services/TagServiceTest.php | 14 ++--- tests/Services/ThumbnailServiceTest.php | 22 +++---- tests/Services/UserServiceTest.php | 4 -- 30 files changed, 290 insertions(+), 212 deletions(-) create mode 100644 src/Dao/FileDao.php create mode 100644 src/Dao/IBatchDao.php create mode 100644 src/Dao/IFileDao.php create mode 100644 src/Dao/PublicFileDao.php rename src/Services/{FileService.php => NetworkingService.php} (65%) create mode 100644 tests/Dao/FileDaoTest.php delete mode 100644 tests/Services/FileServiceTest.php create mode 100644 tests/Services/NetworkingServiceTest.php diff --git a/TODO b/TODO index 671816ac..ebab7e75 100644 --- a/TODO +++ b/TODO @@ -59,7 +59,6 @@ everything related to tags: - target tags (allow multiple) refactors: - - split FileService into FileService and FileDao - add enum validation in IValidatables (needs refactors of enums and possible disposal of EnumHelper in favor of something more subtle) - change content spinner to nprogress: diff --git a/src/Controllers/CommentController.php b/src/Controllers/CommentController.php index 00c08b87..8ca7f0d0 100644 --- a/src/Controllers/CommentController.php +++ b/src/Controllers/CommentController.php @@ -15,7 +15,7 @@ use Szurubooru\Services\CommentService; use Szurubooru\Services\PostService; use Szurubooru\Services\PrivilegeService; -class CommentController extends AbstractController +final class CommentController extends AbstractController { private $privilegeService; private $authService; diff --git a/src/Controllers/FavoritesController.php b/src/Controllers/FavoritesController.php index 2b6c9fc6..da8875c3 100644 --- a/src/Controllers/FavoritesController.php +++ b/src/Controllers/FavoritesController.php @@ -7,7 +7,7 @@ use Szurubooru\Services\FavoritesService; use Szurubooru\Services\PostService; use Szurubooru\Services\PrivilegeService; -class FavoritesController extends AbstractController +final class FavoritesController extends AbstractController { private $privilegeService; private $authService; diff --git a/src/Controllers/PostContentController.php b/src/Controllers/PostContentController.php index d4a523d4..4c4b0708 100644 --- a/src/Controllers/PostContentController.php +++ b/src/Controllers/PostContentController.php @@ -1,27 +1,27 @@ fileDao = $fileDao; $this->postService = $postService; - $this->fileService = $fileService; - $this->httpHelper = $httpHelper; + $this->networkingService = $networkingService; $this->thumbnailService = $thumbnailService; } @@ -34,7 +34,7 @@ final class PostContentController extends AbstractController public function getPostContent($postName) { $post = $this->postService->getByName($postName); - $this->fileService->serve($post->getContentPath()); + $this->networkingService->serve($this->fileDao->getFullPath($post->getContentPath())); } public function getPostThumbnail($postName, $size) @@ -42,11 +42,11 @@ final class PostContentController extends AbstractController $post = $this->postService->getByName($postName); $sourceName = $post->getThumbnailSourceContentPath(); - if (!$this->fileService->exists($sourceName)) + if (!$this->fileDao->exists($sourceName)) $sourceName = $post->getContentPath(); $this->thumbnailService->generateIfNeeded($sourceName, $size, $size); $thumbnailName = $this->thumbnailService->getThumbnailName($sourceName, $size, $size); - $this->fileService->serve($thumbnailName); + $this->networkingService->serve($this->fileDao->getFullPath($thumbnailName)); } } diff --git a/src/Controllers/UserAvatarController.php b/src/Controllers/UserAvatarController.php index d3ecfd5c..23b97d09 100644 --- a/src/Controllers/UserAvatarController.php +++ b/src/Controllers/UserAvatarController.php @@ -1,27 +1,31 @@ fileDao = $fileDao; $this->userService = $userService; - $this->fileService = $fileService; + $this->networkingService = $networkingService; $this->httpHelper = $httpHelper; $this->thumbnailService = $thumbnailService; } @@ -73,12 +77,12 @@ final class UserAvatarController extends AbstractController { $this->thumbnailService->generateIfNeeded($sourceName, $size, $size); $thumbnailName = $this->thumbnailService->getThumbnailName($sourceName, $size, $size); - $this->fileService->serve($thumbnailName); + $this->networkingService->serve($this->fileDao->getFullPath($thumbnailName)); } private function serveBlankFile($size) { - $this->serveFromFile($this->getBlankAvatarSourceContentPath(), $size); + $this->serveFromFile($this->fileDao->getFullPath($this->getBlankAvatarSourceContentPath()), $size); } private function getBlankAvatarSourceContentPath() diff --git a/src/Dao/AbstractDao.php b/src/Dao/AbstractDao.php index 5a1fc0c6..be87b67b 100644 --- a/src/Dao/AbstractDao.php +++ b/src/Dao/AbstractDao.php @@ -10,7 +10,7 @@ use Szurubooru\SearchServices\Requirements\RequirementRangedValue; use Szurubooru\SearchServices\Requirements\RequirementSingleValue; use Szurubooru\SearchServices\Result; -abstract class AbstractDao implements ICrudDao +abstract class AbstractDao implements ICrudDao, IBatchDao { protected $pdo; protected $fpdo; diff --git a/src/Dao/FileDao.php b/src/Dao/FileDao.php new file mode 100644 index 00000000..ff800e4c --- /dev/null +++ b/src/Dao/FileDao.php @@ -0,0 +1,53 @@ +directory = $directory; + } + + public function load($fileName) + { + $fullPath = $this->getFullPath($fileName); + return file_exists($fullPath) + ? file_get_contents($fullPath) + : null; + } + + public function save($fileName, $data) + { + $fullPath = $this->getFullPath($fileName); + $this->createFolders($fileName); + file_put_contents($fullPath, $data); + } + + public function delete($fileName) + { + $fullPath = $this->getFullPath($fileName); + if (file_exists($fullPath)) + unlink($fullPath); + } + + public function exists($fileName) + { + $fullPath = $this->getFullPath($fileName); + return file_exists($fullPath); + } + + public function getFullPath($fileName) + { + return $this->directory . DIRECTORY_SEPARATOR . $fileName; + } + + private function createFolders($fileName) + { + $fullPath = $this->getFullPath(dirname($fileName)); + if (!file_exists($fullPath)) + mkdir($fullPath, 0777, true); + } +} diff --git a/src/Dao/IBatchDao.php b/src/Dao/IBatchDao.php new file mode 100644 index 00000000..2e6a459f --- /dev/null +++ b/src/Dao/IBatchDao.php @@ -0,0 +1,11 @@ +tagDao = $tagDao; $this->userDao = $userDao; - $this->fileService = $fileService; + $this->fileDao = $fileDao; $this->thumbnailService = $thumbnailService; } @@ -63,14 +63,14 @@ class PostDao extends AbstractDao implements ICrudDao Post::LAZY_LOADER_CONTENT, function (Post $post) { - return $this->fileService->load($post->getContentPath()); + return $this->fileDao->load($post->getContentPath()); }); $post->setLazyLoader( Post::LAZY_LOADER_THUMBNAIL_SOURCE_CONTENT, function (Post $post) { - return $this->fileService->load($post->getThumbnailSourceContentPath()); + return $this->fileDao->load($post->getThumbnailSourceContentPath()); }); $post->setLazyLoader( @@ -194,9 +194,9 @@ class PostDao extends AbstractDao implements ICrudDao $targetPath = $post->getContentPath(); $content = $post->getContent(); if ($content) - $this->fileService->save($targetPath, $content); + $this->fileDao->save($targetPath, $content); else - $this->fileService->delete($targetPath, $content); + $this->fileDao->delete($targetPath, $content); $this->thumbnailService->deleteUsedThumbnails($targetPath); } @@ -205,9 +205,9 @@ class PostDao extends AbstractDao implements ICrudDao $targetPath = $post->getThumbnailSourceContentPath(); $content = $post->getThumbnailSourceContent(); if ($content) - $this->fileService->save($targetPath, $content); + $this->fileDao->save($targetPath, $content); else - $this->fileService->delete($targetPath); + $this->fileDao->delete($targetPath); $this->thumbnailService->deleteUsedThumbnails($targetPath); } diff --git a/src/Dao/PublicFileDao.php b/src/Dao/PublicFileDao.php new file mode 100644 index 00000000..4987d7a0 --- /dev/null +++ b/src/Dao/PublicFileDao.php @@ -0,0 +1,13 @@ +getPublicDataDirectory()); + } +} diff --git a/src/Dao/UserDao.php b/src/Dao/UserDao.php index c9de8f6e..6b0cd497 100644 --- a/src/Dao/UserDao.php +++ b/src/Dao/UserDao.php @@ -1,10 +1,10 @@ fileService = $fileService; + $this->fileDao = $fileDao; $this->thumbnailService = $thumbnailService; } @@ -62,7 +62,7 @@ class UserDao extends AbstractDao implements ICrudDao function(User $user) { $avatarSource = $user->getCustomAvatarSourceContentPath(); - return $this->fileService->load($avatarSource); + return $this->fileDao->load($avatarSource); }); } @@ -71,16 +71,16 @@ class UserDao extends AbstractDao implements ICrudDao $targetPath = $user->getCustomAvatarSourceContentPath(); $content = $user->getCustomAvatarSourceContent(); if ($content) - $this->fileService->save($targetPath, $content); + $this->fileDao->save($targetPath, $content); else - $this->fileService->delete($targetPath); + $this->fileDao->delete($targetPath); $this->thumbnailService->deleteUsedThumbnails($targetPath); } protected function afterDelete(Entity $user) { $avatarSource = $user->getCustomAvatarSourceContentPath(); - $this->fileService->delete($avatarSource); + $this->fileDao->delete($avatarSource); $this->thumbnailService->deleteUsedThumbnails($avatarSource); } } diff --git a/src/Services/FileService.php b/src/Services/NetworkingService.php similarity index 65% rename from src/Services/FileService.php rename to src/Services/NetworkingService.php index 272eda06..8432985f 100644 --- a/src/Services/FileService.php +++ b/src/Services/NetworkingService.php @@ -1,23 +1,18 @@ dataDirectory = $config->getPublicDataDirectory(); $this->httpHelper = $httpHelper; } - public function serve($target, $options = []) + public function serve($fullPath, $options = []) { - $fullPath = $this->getFullPath($target); - $daysToLive = isset($options->daysToLive) ? $options->daysToLive : 7; @@ -61,46 +56,6 @@ class FileService exit; } - public function createFolders($target) - { - $fullPath = $this->getFullPath(dirname($target)); - if (!file_exists($fullPath)) - mkdir($fullPath, 0777, true); - } - - public function exists($target) - { - $fullPath = $this->getFullPath($target); - return $target and file_exists($fullPath); - } - - public function delete($target) - { - $fullPath = $this->getFullPath($target); - if (file_exists($fullPath)) - unlink($fullPath); - } - - public function load($source) - { - if (!$this->exists($source)) - return null; - $fullPath = $this->getFullPath($source); - return file_get_contents($fullPath); - } - - public function save($destination, $data) - { - $this->createFolders($destination); - $finalDestination = $this->getFullPath($destination); - file_put_contents($finalDestination, $data); - } - - public function getFullPath($destination) - { - return $this->dataDirectory . DIRECTORY_SEPARATOR . $destination; - } - public function download($url, $maxBytes = null) { set_time_limit(60); diff --git a/src/Services/PostService.php b/src/Services/PostService.php index 7a84d8aa..549b018e 100644 --- a/src/Services/PostService.php +++ b/src/Services/PostService.php @@ -15,9 +15,9 @@ use Szurubooru\SearchServices\Filters\SnapshotFilter; use Szurubooru\SearchServices\Requirements\Requirement; use Szurubooru\SearchServices\Requirements\RequirementSingleValue; use Szurubooru\Services\AuthService; -use Szurubooru\Services\FileService; use Szurubooru\Services\HistoryService; use Szurubooru\Services\ImageManipulation\ImageManipulator; +use Szurubooru\Services\NetworkingService; use Szurubooru\Services\TagService; use Szurubooru\Services\TimeService; use Szurubooru\Validator; @@ -31,7 +31,7 @@ class PostService private $globalParamDao; private $timeService; private $authService; - private $fileService; + private $networkingService; private $tagService; private $historyService; private $imageManipulator; @@ -44,7 +44,7 @@ class PostService GlobalParamDao $globalParamDao, AuthService $authService, TimeService $timeService, - FileService $fileService, + NetworkingService $networkingService, TagService $tagService, HistoryService $historyService, ImageManipulator $imageManipulator) @@ -56,7 +56,7 @@ class PostService $this->globalParamDao = $globalParamDao; $this->timeService = $timeService; $this->authService = $authService; - $this->fileService = $fileService; + $this->networkingService = $networkingService; $this->tagService = $tagService; $this->historyService = $historyService; $this->imageManipulator = $imageManipulator; @@ -270,12 +270,12 @@ class PostService $this->assertNoPostWithThisContentChecksum($post); $youtubeThumbnailUrl = 'http://img.youtube.com/vi/' . $youtubeId . '/mqdefault.jpg'; - $youtubeThumbnail = $this->fileService->download($youtubeThumbnailUrl); + $youtubeThumbnail = $this->networkingService->download($youtubeThumbnailUrl); $post->setThumbnailSourceContent($youtubeThumbnail); } else { - $contents = $this->fileService->download($url); + $contents = $this->networkingService->download($url); $this->updatePostContentFromString($post, $contents); } } diff --git a/src/Services/TagService.php b/src/Services/TagService.php index 544ec803..1d857e81 100644 --- a/src/Services/TagService.php +++ b/src/Services/TagService.php @@ -1,29 +1,29 @@ transactionManager = $transactionManager; $this->tagDao = $tagDao; + $this->fileDao = $fileDao; $this->timeService = $timeService; - $this->fileService = $fileService; } public function getFiltered(TagFilter $filter) @@ -43,7 +43,7 @@ class TagService $tags[$tag->getName()] = $tag->getUsages(); } $json = json_encode($tags); - $this->fileService->save('tags.json', $json); + $this->fileDao->save('tags.json', $json); } public function deleteUnusedTags() diff --git a/src/Services/ThumbnailService.php b/src/Services/ThumbnailService.php index 304464bf..2f8dd0c1 100644 --- a/src/Services/ThumbnailService.php +++ b/src/Services/ThumbnailService.php @@ -1,23 +1,23 @@ config = $config; - $this->fileService = $fileService; + $this->fileDao = $fileDao; $this->thumbnailGenerator = $thumbnailGenerator; } @@ -27,7 +27,7 @@ class ThumbnailService { list ($width, $height) = $size; $target = $this->getThumbnailName($sourceName, $width, $height); - $this->fileService->delete($target); + $this->fileDao->delete($target); } } @@ -35,7 +35,7 @@ class ThumbnailService { $thumbnailName = $this->getThumbnailName($sourceName, $width, $height); - if (!$this->fileService->exists($thumbnailName)) + if (!$this->fileDao->exists($thumbnailName)) $this->generate($sourceName, $width, $height); } @@ -57,24 +57,24 @@ class ThumbnailService $thumbnailName = $this->getThumbnailName($sourceName, $width, $height); - $source = $this->fileService->load($sourceName); + $source = $this->fileDao->load($sourceName); $result = null; if (!$source) - $source = $this->fileService->load($this->getBlankThumbnailName()); + $source = $this->fileDao->load($this->getBlankThumbnailName()); if ($source) $result = $this->thumbnailGenerator->generate($source, $width, $height, $cropStyle); if (!$result) - $result = $this->fileService->load($this->getBlankThumbnailName()); + $result = $this->fileDao->load($this->getBlankThumbnailName()); - $this->fileService->save($thumbnailName, $result); + $this->fileDao->save($thumbnailName, $result); } public function getUsedThumbnailSizes() { - foreach (glob($this->fileService->getFullPath('thumbnails') . DIRECTORY_SEPARATOR . '*x*') as $fn) + foreach (glob($this->fileDao->getFullPath('thumbnails') . DIRECTORY_SEPARATOR . '*x*') as $fn) { if (!is_dir($fn)) continue; diff --git a/src/Services/UserService.php b/src/Services/UserService.php index 6c56ca0c..ceb87ced 100644 --- a/src/Services/UserService.php +++ b/src/Services/UserService.php @@ -10,7 +10,6 @@ use Szurubooru\FormData\UserEditFormData; use Szurubooru\Helpers\MimeHelper; use Szurubooru\SearchServices\Filters\UserFilter; use Szurubooru\Services\EmailService; -use Szurubooru\Services\FileService; use Szurubooru\Services\PasswordService; use Szurubooru\Services\ThumbnailService; use Szurubooru\Services\TimeService; @@ -25,7 +24,6 @@ class UserService private $userDao; private $passwordService; private $emailService; - private $fileService; private $thumbnailService; private $timeService; private $tokenService; @@ -37,7 +35,6 @@ class UserService UserDao $userDao, PasswordService $passwordService, EmailService $emailService, - FileService $fileService, ThumbnailService $thumbnailService, TimeService $timeService, TokenService $tokenService) @@ -48,7 +45,6 @@ class UserService $this->userDao = $userDao; $this->passwordService = $passwordService; $this->emailService = $emailService; - $this->fileService = $fileService; $this->thumbnailService = $thumbnailService; $this->timeService = $timeService; $this->tokenService = $tokenService; diff --git a/src/Upgrades/Upgrade04.php b/src/Upgrades/Upgrade04.php index 07d468a8..321f2b4c 100644 --- a/src/Upgrades/Upgrade04.php +++ b/src/Upgrades/Upgrade04.php @@ -1,26 +1,26 @@ postDao = $postDao; $this->postService = $postService; - $this->fileService = $fileService; + $this->fileDao = $fileDao; } public function run(DatabaseConnection $databaseConnection) @@ -32,7 +32,7 @@ class Upgrade04 implements IUpgrade { if ($post->getContentType() !== Post::POST_TYPE_YOUTUBE) { - $fullPath = $this->fileService->getFullPath($post->getContentPath()); + $fullPath = $this->fileDao->getFullPath($post->getContentPath()); $mime = MimeHelper::getMimeTypeFromFile($fullPath); $post->setContentMimeType($mime); $this->postDao->save($post); diff --git a/tests/AbstractDatabaseTestCase.php b/tests/AbstractDatabaseTestCase.php index c53cc4ed..35604d71 100644 --- a/tests/AbstractDatabaseTestCase.php +++ b/tests/AbstractDatabaseTestCase.php @@ -1,11 +1,11 @@ set('database/user', ''); $config->set('database/password', ''); - $fileService = $this->prepareFileService(); $this->databaseConnection = new DatabaseConnection($config); Injector::set(DatabaseConnection::class, $this->databaseConnection); - Injector::set(FileService::class, $fileService); + Injector::set(PublicFileDao::class, $this->preparePublicFileDao()); $upgradeRepository = Injector::get(UpgradeRepository::class); $upgradeService = new UpgradeService($config, $this->databaseConnection, $upgradeRepository); $upgradeService->runUpgradesQuiet(); } - private function prepareFileService() - { - $testDirectory = $this->createTestDirectory(); - $configMock = $this->mockConfig(null, $testDirectory); - $httpHelper = Injector::get(HttpHelper::class); - return new FileService($configMock, $httpHelper); - } - public function tearDown() { parent::tearDown(); @@ -68,4 +59,11 @@ abstract class AbstractDatabaseTestCase extends AbstractTestCase $user->setAccessRank(User::ACCESS_RANK_REGULAR_USER); return $user; } + + private function preparePublicFileDao() + { + $testDirectory = $this->createTestDirectory(); + $configMock = $this->mockConfig(null, $testDirectory); + return new PublicFileDao($configMock); + } } diff --git a/tests/Dao/FileDaoTest.php b/tests/Dao/FileDaoTest.php new file mode 100644 index 00000000..8e7da65e --- /dev/null +++ b/tests/Dao/FileDaoTest.php @@ -0,0 +1,61 @@ +createTestDirectory(); + $fileDao = new FileDao($testDirectory); + $fileDao->save('dog.txt', 'awesome dog'); + $expected = 'awesome dog'; + $actual = file_get_contents($testDirectory . DIRECTORY_SEPARATOR . 'dog.txt'); + $this->assertEquals($expected, $actual); + } + + public function testSavingSubfolders() + { + $testDirectory = $this->createTestDirectory(); + $fileDao = new FileDao($testDirectory); + $fileDao->save('friends/dog.txt', 'hot dog'); + $expected = 'hot dog'; + $actual = file_get_contents($testDirectory . DIRECTORY_SEPARATOR . 'friends/dog.txt'); + $this->assertEquals($expected, $actual); + } + + public function testLoading() + { + $testDirectory = $this->createTestDirectory(); + $fileDao = new FileDao($testDirectory); + $fileDao->save('dog.txt', 'awesome dog'); + $this->assertEquals('awesome dog', $fileDao->load('dog.txt')); + } + + public function testExists() + { + $testDirectory = $this->createTestDirectory(); + $fileDao = new FileDao($testDirectory); + $fileDao->save('dog.txt', 'awesome dog'); + $this->assertTrue($fileDao->exists('dog.txt')); + $this->assertFalse($fileDao->exists('fish.txt')); + } + + public function testLoadingUnexisting() + { + $testDirectory = $this->createTestDirectory(); + $fileDao = new FileDao($testDirectory); + $this->assertNull($fileDao->load('dog.txt')); + } + + public function testDeleting() + { + $testDirectory = $this->createTestDirectory(); + $fileDao = new FileDao($testDirectory); + $fileDao->save('dog.txt', 'awesome dog'); + $this->assertTrue(file_exists($testDirectory . DIRECTORY_SEPARATOR . 'dog.txt')); + $fileDao->delete('dog.txt'); + $this->assertFalse(file_exists($testDirectory . DIRECTORY_SEPARATOR . 'dog.txt')); + } +} diff --git a/tests/Dao/PostDaoTest.php b/tests/Dao/PostDaoTest.php index 40dcbe7e..2c3d4c17 100644 --- a/tests/Dao/PostDaoTest.php +++ b/tests/Dao/PostDaoTest.php @@ -1,17 +1,17 @@ fileServiceMock = $this->mock(FileService::class); + $this->fileDaoMock = $this->mock(PublicFileDao::class); $this->thumbnailServiceMock = $this->mock(ThumbnailService::class); - $this->tagDao = new TagDao( - $this->databaseConnection, - $this->fileServiceMock); + $this->tagDao = new TagDao($this->databaseConnection); $this->userDao = new UserDao( $this->databaseConnection, - $this->fileServiceMock, + $this->fileDaoMock, $this->thumbnailServiceMock); } @@ -236,7 +234,7 @@ final class PostDaoTest extends AbstractDatabaseTestCase $post = $postDao->findById($post->getId()); - $this->fileServiceMock + $this->fileDaoMock ->expects($this->once()) ->method('load') ->with($post->getContentPath()) @@ -258,7 +256,7 @@ final class PostDaoTest extends AbstractDatabaseTestCase [$post->getContentPath()], [$post->getThumbnailSourceContentPath()]); - $this->fileServiceMock + $this->fileDaoMock ->expects($this->exactly(1)) ->method('save') ->withConsecutive( @@ -281,7 +279,7 @@ final class PostDaoTest extends AbstractDatabaseTestCase [$post->getContentPath()], [$post->getThumbnailSourceContentPath()]); - $this->fileServiceMock + $this->fileDaoMock ->expects($this->exactly(2)) ->method('save') ->withConsecutive( @@ -298,7 +296,7 @@ final class PostDaoTest extends AbstractDatabaseTestCase $this->databaseConnection, $this->tagDao, $this->userDao, - $this->fileServiceMock, + $this->fileDaoMock, $this->thumbnailServiceMock); } diff --git a/tests/Dao/UserDaoFilterTest.php b/tests/Dao/UserDaoFilterTest.php index d9c802cd..9fa4456f 100644 --- a/tests/Dao/UserDaoFilterTest.php +++ b/tests/Dao/UserDaoFilterTest.php @@ -1,21 +1,21 @@ fileServiceMock = $this->mock(FileService::class); + $this->fileDaoMock = $this->mock(PublicFileDao::class); $this->thumbnailServiceMock = $this->mock(ThumbnailService::class); } @@ -148,7 +148,7 @@ final class UserDaoFilterTest extends AbstractDatabaseTestCase { return new UserDao( $this->databaseConnection, - $this->fileServiceMock, + $this->fileDaoMock, $this->thumbnailServiceMock); } } diff --git a/tests/Dao/UserDaoTest.php b/tests/Dao/UserDaoTest.php index 3a0c955a..9c65fee3 100644 --- a/tests/Dao/UserDaoTest.php +++ b/tests/Dao/UserDaoTest.php @@ -1,21 +1,21 @@ fileServiceMock = $this->mock(FileService::class); + $this->fileDaoMock = $this->mock(PublicFileDao::class); $this->thumbnailServiceMock = $this->mock(ThumbnailService::class); } @@ -69,7 +69,7 @@ final class UserDaoTest extends AbstractDatabaseTestCase $user = $userDao->findById($user->getId()); - $this->fileServiceMock + $this->fileDaoMock ->expects($this->once()) ->method('load') ->with($user->getCustomAvatarSourceContentPath())->willReturn('whatever'); @@ -93,7 +93,7 @@ final class UserDaoTest extends AbstractDatabaseTestCase return $subject == $user->getCustomAvatarSourceContentPath(); })); - $this->fileServiceMock + $this->fileDaoMock ->expects($this->once()) ->method('save') ->with($this->callback( @@ -110,7 +110,7 @@ final class UserDaoTest extends AbstractDatabaseTestCase { return new UserDao( $this->databaseConnection, - $this->fileServiceMock, + $this->fileDaoMock, $this->thumbnailServiceMock); } } diff --git a/tests/Services/FileServiceTest.php b/tests/Services/FileServiceTest.php deleted file mode 100644 index 64d53727..00000000 --- a/tests/Services/FileServiceTest.php +++ /dev/null @@ -1,29 +0,0 @@ -createTestDirectory(); - $configMock = $this->mockConfig(null, $testDirectory); - $httpHelper = $this->mock(HttpHelper::class); - $fileService = new FileService($configMock, $httpHelper); - $fileService->save('dog.txt', 'awesome dog'); - $expected = 'awesome dog'; - $actual = file_get_contents($testDirectory . DIRECTORY_SEPARATOR . 'dog.txt'); - $this->assertEquals($expected, $actual); - } - - public function testDownload() - { - $configMock = $this->mockConfig(); - $httpHelper = $this->mock(HttpHelper::class); - $fileService = new FileService($configMock, $httpHelper); - $content = $fileService->download('http://modernseoul.files.wordpress.com/2012/04/korean-alphabet-chart-modern-seoul.jpg'); - $this->assertGreaterThan(0, strlen($content)); - } -} diff --git a/tests/Services/NetworkingServiceTest.php b/tests/Services/NetworkingServiceTest.php new file mode 100644 index 00000000..1b63313c --- /dev/null +++ b/tests/Services/NetworkingServiceTest.php @@ -0,0 +1,16 @@ +mock(HttpHelper::class); + $networkingService = new NetworkingService($httpHelper); + $content = $networkingService->download('http://modernseoul.files.wordpress.com/2012/04/korean-alphabet-chart-modern-seoul.jpg'); + $this->assertGreaterThan(0, strlen($content)); + } +} diff --git a/tests/Services/PostServiceTest.php b/tests/Services/PostServiceTest.php index dc6dff07..4d93b09f 100644 --- a/tests/Services/PostServiceTest.php +++ b/tests/Services/PostServiceTest.php @@ -8,9 +8,9 @@ use Szurubooru\Entities\User; use Szurubooru\FormData\UploadFormData; use Szurubooru\Injector; use Szurubooru\Services\AuthService; -use Szurubooru\Services\FileService; use Szurubooru\Services\HistoryService; use Szurubooru\Services\ImageManipulation\ImageManipulator; +use Szurubooru\Services\NetworkingService; use Szurubooru\Services\PostService; use Szurubooru\Services\TagService; use Szurubooru\Services\TimeService; @@ -26,7 +26,7 @@ final class PostServiceTest extends AbstractDatabaseTestCase private $globalParamDaoMock; private $authServiceMock; private $timeServiceMock; - private $fileServiceMock; + private $networkingServiceMock; private $tagService; private $historyServiceMock; private $imageManipulatorMock; @@ -41,7 +41,7 @@ final class PostServiceTest extends AbstractDatabaseTestCase $this->globalParamDaoMock = $this->mock(GlobalParamDao::class); $this->authServiceMock = $this->mock(AuthService::class); $this->timeServiceMock = $this->mock(TimeService::class); - $this->fileServiceMock = $this->mock(FileService::class); + $this->networkingServiceMock = $this->mock(NetworkingService::class); $this->tagService = Injector::get(TagService::class); $this->historyServiceMock = $this->mock(HistoryService::class); $this->configMock->set('database/maxPostSize', 1000000); @@ -207,7 +207,7 @@ final class PostServiceTest extends AbstractDatabaseTestCase $this->globalParamDaoMock, $this->authServiceMock, $this->timeServiceMock, - $this->fileServiceMock, + $this->networkingServiceMock, $this->tagService, $this->historyServiceMock, $this->imageManipulatorMock); diff --git a/tests/Services/TagServiceTest.php b/tests/Services/TagServiceTest.php index 44321c01..52bbf6f9 100644 --- a/tests/Services/TagServiceTest.php +++ b/tests/Services/TagServiceTest.php @@ -1,8 +1,8 @@ setName('test'); $tag1->setCreationTime(date('c')); - $fileService = $this->getFileService(); + $fileDao = $this->getPublicFileDao(); $tagService = $this->getTagService(); $tagService->createTags([$tag1]); $tagService->exportJson(); - $this->assertEquals('{"test":0}', $fileService->load('tags.json')); + $this->assertEquals('{"test":0}', $fileDao->load('tags.json')); } public function testExportMultiple() @@ -89,16 +89,16 @@ final class TagServiceTest extends AbstractDatabaseTestCase $tag2 = new Tag(); $tag2->setName('test2'); $tag2->setCreationTime(date('c')); - $fileService = $this->getFileService(); + $fileDao = $this->getPublicFileDao(); $tagService = $this->getTagService(); $tagService->createTags([$tag1, $tag2]); $tagService->exportJson(); - $this->assertEquals('{"test1":0,"test2":0}', $fileService->load('tags.json')); + $this->assertEquals('{"test1":0,"test2":0}', $fileDao->load('tags.json')); } - private function getFileService() + private function getPublicFileDao() { - return Injector::get(FileService::class); + return Injector::get(PublicFileDao::class); } private function getTagService() diff --git a/tests/Services/ThumbnailServiceTest.php b/tests/Services/ThumbnailServiceTest.php index 9a1baced..1d84a80b 100644 --- a/tests/Services/ThumbnailServiceTest.php +++ b/tests/Services/ThumbnailServiceTest.php @@ -1,6 +1,6 @@ configMock = $this->mockConfig(); - $this->fileServiceMock = $this->mock(FileService::class); + $this->fileDaoMock = $this->mock(PublicFileDao::class); $this->thumbnailServiceMock = $this->mock(ThumbnailService::class); $this->thumbnailGeneratorMock = $this->mock(SmartThumbnailGenerator::class); } @@ -30,7 +30,7 @@ final class ThumbnailServiceTest extends AbstractTestCase mkdir($tempDirectory . DIRECTORY_SEPARATOR . 'something unexpected'); touch($tempDirectory . DIRECTORY_SEPARATOR . '15x15'); - $this->fileServiceMock->expects($this->once())->method('getFullPath')->with('thumbnails')->willReturn($tempDirectory); + $this->fileDaoMock->expects($this->once())->method('getFullPath')->with('thumbnails')->willReturn($tempDirectory); $thumbnailService = $this->getThumbnailService(); $expected = [[5, 5], [10, 10]]; @@ -50,8 +50,8 @@ final class ThumbnailServiceTest extends AbstractTestCase touch($tempDirectory . DIRECTORY_SEPARATOR . '5x5' . DIRECTORY_SEPARATOR . 'keep'); touch($tempDirectory . DIRECTORY_SEPARATOR . '10x10' . DIRECTORY_SEPARATOR . 'remove'); - $this->fileServiceMock->expects($this->once())->method('getFullPath')->with('thumbnails')->willReturn($tempDirectory); - $this->fileServiceMock->expects($this->exactly(2))->method('delete')->withConsecutive( + $this->fileDaoMock->expects($this->once())->method('getFullPath')->with('thumbnails')->willReturn($tempDirectory); + $this->fileDaoMock->expects($this->exactly(2))->method('delete')->withConsecutive( ['thumbnails' . DIRECTORY_SEPARATOR . '10x10' . DIRECTORY_SEPARATOR . 'remove'], ['thumbnails' . DIRECTORY_SEPARATOR . '5x5' . DIRECTORY_SEPARATOR . 'remove']); $thumbnailService = $this->getThumbnailService(); @@ -63,7 +63,7 @@ final class ThumbnailServiceTest extends AbstractTestCase { $this->configMock->set('misc/thumbnailCropStyle', 'outside'); - $this->fileServiceMock + $this->fileDaoMock ->expects($this->exactly(2)) ->method('load') ->withConsecutive( @@ -84,7 +84,7 @@ final class ThumbnailServiceTest extends AbstractTestCase IThumbnailGenerator::CROP_OUTSIDE) ->willReturn('generated thumbnail'); - $this->fileServiceMock + $this->fileDaoMock ->expects($this->once()) ->method('save') ->with('thumbnails' . DIRECTORY_SEPARATOR . '100x100' . DIRECTORY_SEPARATOR . 'nope', 'generated thumbnail'); @@ -97,7 +97,7 @@ final class ThumbnailServiceTest extends AbstractTestCase { $this->configMock->set('misc/thumbnailCropStyle', 'outside'); - $this->fileServiceMock + $this->fileDaoMock ->expects($this->exactly(3)) ->method('load') ->withConsecutive( @@ -120,7 +120,7 @@ final class ThumbnailServiceTest extends AbstractTestCase IThumbnailGenerator::CROP_OUTSIDE) ->willReturn(null); - $this->fileServiceMock + $this->fileDaoMock ->expects($this->once()) ->method('save') ->with('thumbnails' . DIRECTORY_SEPARATOR . '100x100' . DIRECTORY_SEPARATOR . 'nope', 'content of blank thumbnail (2)'); @@ -134,7 +134,7 @@ final class ThumbnailServiceTest extends AbstractTestCase { return new ThumbnailService( $this->configMock, - $this->fileServiceMock, + $this->fileDaoMock, $this->thumbnailGeneratorMock); } } diff --git a/tests/Services/UserServiceTest.php b/tests/Services/UserServiceTest.php index 5e26b544..caf11a5a 100644 --- a/tests/Services/UserServiceTest.php +++ b/tests/Services/UserServiceTest.php @@ -6,7 +6,6 @@ use Szurubooru\Entities\User; use Szurubooru\FormData\RegistrationFormData; use Szurubooru\FormData\UserEditFormData; use Szurubooru\Services\EmailService; -use Szurubooru\Services\FileService; use Szurubooru\Services\PasswordService; use Szurubooru\Services\ThumbnailService; use Szurubooru\Services\TimeService; @@ -23,7 +22,6 @@ final class UserServiceTest extends AbstractTestCase private $userDaoMock; private $passwordServiceMock; private $emailServiceMock; - private $fileServiceMock; private $thumbnailServiceMock; private $timeServiceMock; private $tokenServiceMock; @@ -37,7 +35,6 @@ final class UserServiceTest extends AbstractTestCase $this->userDaoMock = $this->mock(UserDao::class); $this->passwordServiceMock = $this->mock(PasswordService::class); $this->emailServiceMock = $this->mock(EmailService::class); - $this->fileServiceMock = $this->mock(FileService::class); $this->thumbnailServiceMock = $this->mock(ThumbnailService::class); $this->timeServiceMock = $this->mock(TimeService::class); $this->tokenServiceMock = $this->mock(TokenService::class); @@ -290,7 +287,6 @@ final class UserServiceTest extends AbstractTestCase $this->userDaoMock, $this->passwordServiceMock, $this->emailServiceMock, - $this->fileServiceMock, $this->thumbnailServiceMock, $this->timeServiceMock, $this->tokenServiceMock);