Split FileService to NetworkingService and FileDao

This commit is contained in:
Marcin Kurczewski 2014-10-08 19:39:20 +02:00
parent 608eb2e917
commit 3744f0429a
30 changed files with 290 additions and 212 deletions

1
TODO
View file

@ -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:

View file

@ -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;

View file

@ -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;

View file

@ -1,27 +1,27 @@
<?php
namespace Szurubooru\Controllers;
use Szurubooru\Helpers\HttpHelper;
use Szurubooru\Dao\PublicFileDao;
use Szurubooru\Router;
use Szurubooru\Services\FileService;
use Szurubooru\Services\NetworkingService;
use Szurubooru\Services\PostService;
use Szurubooru\Services\ThumbnailService;
final class PostContentController extends AbstractController
{
private $fileDao;
private $postService;
private $fileService;
private $httpHelper;
private $networkingService;
private $thumbnailService;
public function __construct(
PublicFileDao $fileDao,
PostService $postService,
FileService $fileService,
HttpHelper $httpHelper,
NetworkingService $networkingService,
ThumbnailService $thumbnailService)
{
$this->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));
}
}

View file

@ -1,27 +1,31 @@
<?php
namespace Szurubooru\Controllers;
use Szurubooru\Dao\PublicFileDao;
use Szurubooru\Entities\User;
use Szurubooru\Helpers\HttpHelper;
use Szurubooru\Router;
use Szurubooru\Services\FileService;
use Szurubooru\Services\NetworkingService;
use Szurubooru\Services\ThumbnailService;
use Szurubooru\Services\UserService;
final class UserAvatarController extends AbstractController
{
private $fileDao;
private $userService;
private $fileService;
private $networkingService;
private $httpHelper;
private $thumbnailService;
public function __construct(
PublicFileDao $fileDao,
UserService $userService,
FileService $fileService,
NetworkingService $networkingService,
HttpHelper $httpHelper,
ThumbnailService $thumbnailService)
{
$this->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()

View file

@ -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;

53
src/Dao/FileDao.php Normal file
View file

@ -0,0 +1,53 @@
<?php
namespace Szurubooru\Dao;
use Szurubooru\Dao\IFileDao;
class FileDao implements IFileDao
{
private $directory;
public function __construct($directory)
{
$this->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);
}
}

11
src/Dao/IBatchDao.php Normal file
View file

@ -0,0 +1,11 @@
<?php
namespace Szurubooru\Dao;
interface IBatchDao
{
public function findAll();
public function deleteAll();
public function batchSave(array $objects);
}

View file

@ -3,15 +3,9 @@ namespace Szurubooru\Dao;
interface ICrudDao
{
public function findAll();
public function findById($objectId);
public function save(&$object);
public function batchSave(array $object);
public function deleteById($objectId);
public function deleteAll();
}

13
src/Dao/IFileDao.php Normal file
View file

@ -0,0 +1,13 @@
<?php
namespace Szurubooru\Dao;
interface IFileDao
{
public function load($fileName);
public function save($fileName, $contents);
public function delete($fileName);
public function exists($fileName);
}

View file

@ -1,6 +1,7 @@
<?php
namespace Szurubooru\Dao;
use Szurubooru\Dao\EntityConverters\PostEntityConverter;
use Szurubooru\Dao\PublicFileDao;
use Szurubooru\Dao\TagDao;
use Szurubooru\Dao\UserDao;
use Szurubooru\DatabaseConnection;
@ -8,21 +9,20 @@ use Szurubooru\Entities\Entity;
use Szurubooru\Entities\Post;
use Szurubooru\SearchServices\Filters\PostFilter;
use Szurubooru\SearchServices\Requirements\Requirement;
use Szurubooru\Services\FileService;
use Szurubooru\Services\ThumbnailService;
class PostDao extends AbstractDao implements ICrudDao
{
private $tagDao;
private $userDao;
private $fileService;
private $fileDao;
private $thumbnailService;
public function __construct(
DatabaseConnection $databaseConnection,
TagDao $tagDao,
UserDao $userDao,
FileService $fileService,
PublicFileDao $fileDao,
ThumbnailService $thumbnailService)
{
parent::__construct(
@ -32,7 +32,7 @@ class PostDao extends AbstractDao implements ICrudDao
$this->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);
}

13
src/Dao/PublicFileDao.php Normal file
View file

@ -0,0 +1,13 @@
<?php
namespace Szurubooru\Dao;
use Szurubooru\Config;
use Szurubooru\Dao\FileDao;
use Szurubooru\Dao\IFileDao;
class PublicFileDao extends FileDao implements IFileDao
{
public function __construct(Config $config)
{
parent::__construct($config->getPublicDataDirectory());
}
}

View file

@ -1,10 +1,10 @@
<?php
namespace Szurubooru\Dao;
use Szurubooru\Dao\EntityConverters\UserEntityConverter;
use Szurubooru\Dao\PublicFileDao;
use Szurubooru\DatabaseConnection;
use Szurubooru\Entities\Entity;
use Szurubooru\Entities\User;
use Szurubooru\Services\FileService;
use Szurubooru\Services\ThumbnailService;
class UserDao extends AbstractDao implements ICrudDao
@ -12,12 +12,12 @@ class UserDao extends AbstractDao implements ICrudDao
const ORDER_NAME = 'name';
const ORDER_REGISTRATION_TIME = 'registrationTime';
private $fileService;
private $fileDao;
private $thumbnailService;
public function __construct(
DatabaseConnection $databaseConnection,
FileService $fileService,
PublicFileDao $fileDao,
ThumbnailService $thumbnailService)
{
parent::__construct(
@ -25,7 +25,7 @@ class UserDao extends AbstractDao implements ICrudDao
'users',
new UserEntityConverter());
$this->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);
}
}

View file

@ -1,23 +1,18 @@
<?php
namespace Szurubooru\Services;
use Szurubooru\Config;
use Szurubooru\Helpers\HttpHelper;
class FileService
class NetworkingService
{
private $dataDirectory;
private $httpHelper;
public function __construct(Config $config, HttpHelper $httpHelper)
public function __construct(HttpHelper $httpHelper)
{
$this->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);

View file

@ -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);
}
}

View file

@ -1,29 +1,29 @@
<?php
namespace Szurubooru\Services;
use Szurubooru\Dao\PublicFileDao;
use Szurubooru\Dao\TagDao;
use Szurubooru\Dao\TransactionManager;
use Szurubooru\Entities\Tag;
use Szurubooru\SearchServices\Filters\TagFilter;
use Szurubooru\Services\FileService;
use Szurubooru\Services\TimeService;
class TagService
{
private $transactionManager;
private $tagDao;
private $fileService;
private $fileDao;
private $timeService;
public function __construct(
TransactionManager $transactionManager,
TagDao $tagDao,
TimeService $timeService,
FileService $fileService)
PublicFileDao $fileDao,
TimeService $timeService)
{
$this->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()

View file

@ -1,23 +1,23 @@
<?php
namespace Szurubooru\Services;
use Szurubooru\Config;
use Szurubooru\Services\FileService;
use Szurubooru\Dao\PublicFileDao;
use Szurubooru\Services\ThumbnailGenerators\IThumbnailGenerator;
use Szurubooru\Services\ThumbnailGenerators\SmartThumbnailGenerator;
class ThumbnailService
{
private $config;
private $fileService;
private $fileDao;
private $thumbnailGenerator;
public function __construct(
Config $config,
FileService $fileService,
PublicFileDao $fileDao,
SmartThumbnailGenerator $thumbnailGenerator)
{
$this->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;

View file

@ -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;

View file

@ -1,26 +1,26 @@
<?php
namespace Szurubooru\Upgrades;
use Szurubooru\Dao\PostDao;
use Szurubooru\Dao\PublicFileDao;
use Szurubooru\DatabaseConnection;
use Szurubooru\Entities\Post;
use Szurubooru\Helpers\MimeHelper;
use Szurubooru\Services\FileService;
use Szurubooru\Services\PostService;
class Upgrade04 implements IUpgrade
{
private $postDao;
private $postService;
private $fileService;
private $fileDao;
public function __construct(
PostDao $postDao,
PostService $postService,
FileService $fileService)
PublicFileDao $fileDao)
{
$this->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);

View file

@ -1,11 +1,11 @@
<?php
namespace Szurubooru\Tests;
use Szurubooru\Dao\PublicFileDao;
use Szurubooru\DatabaseConnection;
use Szurubooru\Entities\Post;
use Szurubooru\Entities\User;
use Szurubooru\Helpers\HttpHelper;
use Szurubooru\Injector;
use Szurubooru\Services\FileService;
use Szurubooru\Services\UpgradeService;
use Szurubooru\Tests\AbstractTestCase;
use Szurubooru\Upgrades\UpgradeRepository;
@ -22,24 +22,15 @@ abstract class AbstractDatabaseTestCase extends AbstractTestCase
$config->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);
}
}

61
tests/Dao/FileDaoTest.php Normal file
View file

@ -0,0 +1,61 @@
<?php
namespace Szurubooru\Tests\Services;
use Szurubooru\Dao\FileDao;
use Szurubooru\Tests\AbstractTestCase;
final class FileDaoTest extends AbstractTestCase
{
public function testSaving()
{
$testDirectory = $this->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'));
}
}

View file

@ -1,17 +1,17 @@
<?php
namespace Szurubooru\Tests\Dao;
use Szurubooru\Dao\PostDao;
use Szurubooru\Dao\PublicFileDao;
use Szurubooru\Dao\TagDao;
use Szurubooru\Dao\UserDao;
use Szurubooru\Entities\Tag;
use Szurubooru\Entities\User;
use Szurubooru\Services\FileService;
use Szurubooru\Services\ThumbnailService;
use Szurubooru\Tests\AbstractDatabaseTestCase;
final class PostDaoTest extends AbstractDatabaseTestCase
{
private $fileServiceMock;
private $fileDaoMock;
private $thumbnailServiceMock;
private $tagDao;
private $userDao;
@ -19,16 +19,14 @@ final class PostDaoTest extends AbstractDatabaseTestCase
public function setUp()
{
parent::setUp();
$this->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);
}

View file

@ -1,21 +1,21 @@
<?php
namespace Szurubooru\Tests\Dao;
use Szurubooru\Dao\PublicFileDao;
use Szurubooru\Dao\UserDao;
use Szurubooru\SearchServices\Filters\UserFilter;
use Szurubooru\SearchServices\Result;
use Szurubooru\Services\FileService;
use Szurubooru\Services\ThumbnailService;
use Szurubooru\Tests\AbstractDatabaseTestCase;
final class UserDaoFilterTest extends AbstractDatabaseTestCase
{
private $fileServiceMock;
private $fileDaoMock;
private $thumbnailServiceMock;
public function setUp()
{
parent::setUp();
$this->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);
}
}

View file

@ -1,21 +1,21 @@
<?php
namespace Szurubooru\Tests\Dao;
use Szurubooru\Dao\PublicFileDao;
use Szurubooru\Dao\UserDao;
use Szurubooru\Entities\User;
use Szurubooru\Services\FileService;
use Szurubooru\Services\ThumbnailService;
use Szurubooru\Tests\AbstractDatabaseTestCase;
final class UserDaoTest extends AbstractDatabaseTestCase
{
private $fileServiceMock;
private $fileDaoMock;
private $thumbnailServiceMock;
public function setUp()
{
parent::setUp();
$this->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);
}
}

View file

@ -1,29 +0,0 @@
<?php
namespace Szurubooru\Tests\Services;
use Szurubooru\Helpers\HttpHelper;
use Szurubooru\Services\FileService;
use Szurubooru\Tests\AbstractTestCase;
final class FileServiceTest extends AbstractTestCase
{
public function testSaving()
{
$testDirectory = $this->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));
}
}

View file

@ -0,0 +1,16 @@
<?php
namespace Szurubooru\Tests\Services;
use Szurubooru\Helpers\HttpHelper;
use Szurubooru\Services\NetworkingService;
use Szurubooru\Tests\AbstractTestCase;
final class NetworkingServiceTest extends AbstractTestCase
{
public function testDownload()
{
$httpHelper = $this->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));
}
}

View file

@ -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);

View file

@ -1,8 +1,8 @@
<?php
namespace Szurubooru\Tests\Services;
use Szurubooru\Dao\PublicFileDao;
use Szurubooru\Entities\Tag;
use Szurubooru\Injector;
use Szurubooru\Services\FileService;
use Szurubooru\Services\TagService;
use Szurubooru\Tests\AbstractDatabaseTestCase;
@ -74,11 +74,11 @@ final class TagServiceTest extends AbstractDatabaseTestCase
$tag1 = new Tag();
$tag1->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()

View file

@ -1,6 +1,6 @@
<?php
namespace Szurubooru\Tests\Services;
use Szurubooru\Services\FileService;
use Szurubooru\Dao\PublicFileDao;
use Szurubooru\Services\ThumbnailGenerators\IThumbnailGenerator;
use Szurubooru\Services\ThumbnailGenerators\SmartThumbnailGenerator;
use Szurubooru\Services\ThumbnailService;
@ -9,7 +9,7 @@ use Szurubooru\Tests\AbstractTestCase;
final class ThumbnailServiceTest extends AbstractTestCase
{
private $configMock;
private $fileServiceMock;
private $fileDaoMock;
private $thumbnailGeneratorMock;
public function setUp()
@ -17,7 +17,7 @@ final class ThumbnailServiceTest extends AbstractTestCase
parent::setUp();
$this->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);
}
}

View file

@ -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);