Extracted post history services

This commit is contained in:
Marcin Kurczewski 2014-10-25 23:53:46 +02:00
parent 6efca63310
commit a9d8383f7d
9 changed files with 241 additions and 204 deletions

View file

@ -6,7 +6,7 @@ use Szurubooru\Helpers\MimeHelper;
use Szurubooru\Privilege; use Szurubooru\Privilege;
use Szurubooru\Services\AuthService; use Szurubooru\Services\AuthService;
use Szurubooru\Services\FavoritesService; use Szurubooru\Services\FavoritesService;
use Szurubooru\Services\HistoryService; use Szurubooru\Services\PostHistoryService;
use Szurubooru\Services\PostNotesService; use Szurubooru\Services\PostNotesService;
use Szurubooru\Services\PrivilegeService; use Szurubooru\Services\PrivilegeService;
use Szurubooru\Services\ScoreService; use Szurubooru\Services\ScoreService;
@ -23,7 +23,7 @@ class PostViewProxy extends AbstractViewProxy
private $privilegeService; private $privilegeService;
private $authService; private $authService;
private $historyService; private $postHistoryService;
private $favoritesService; private $favoritesService;
private $scoreService; private $scoreService;
private $postNotesService; private $postNotesService;
@ -35,7 +35,7 @@ class PostViewProxy extends AbstractViewProxy
public function __construct( public function __construct(
PrivilegeService $privilegeService, PrivilegeService $privilegeService,
AuthService $authService, AuthService $authService,
HistoryService $historyService, PostHistoryService $postHistoryService,
FavoritesService $favoritesService, FavoritesService $favoritesService,
ScoreService $scoreService, ScoreService $scoreService,
PostNotesService $postNotesService, PostNotesService $postNotesService,
@ -46,7 +46,7 @@ class PostViewProxy extends AbstractViewProxy
{ {
$this->privilegeService = $privilegeService; $this->privilegeService = $privilegeService;
$this->authService = $authService; $this->authService = $authService;
$this->historyService = $historyService; $this->postHistoryService = $postHistoryService;
$this->favoritesService = $favoritesService; $this->favoritesService = $favoritesService;
$this->scoreService = $scoreService; $this->scoreService = $scoreService;
$this->postNotesService = $postNotesService; $this->postNotesService = $postNotesService;
@ -102,7 +102,7 @@ class PostViewProxy extends AbstractViewProxy
if (!empty($config[self::FETCH_HISTORY])) if (!empty($config[self::FETCH_HISTORY]))
{ {
if ($this->privilegeService->hasPrivilege(Privilege::VIEW_HISTORY)) if ($this->privilegeService->hasPrivilege(Privilege::VIEW_HISTORY))
$result->history = $this->snapshotViewProxy->fromArray($this->historyService->getPostHistory($post)); $result->history = $this->snapshotViewProxy->fromArray($this->postHistoryService->getPostHistory($post));
else else
$result->history = []; $result->history = [];
} }

View file

@ -1,39 +1,26 @@
<?php <?php
namespace Szurubooru\Services; namespace Szurubooru\Services;
use Szurubooru\Dao\GlobalParamDao;
use Szurubooru\Dao\SnapshotDao; use Szurubooru\Dao\SnapshotDao;
use Szurubooru\Dao\TransactionManager; use Szurubooru\Dao\TransactionManager;
use Szurubooru\Entities\GlobalParam;
use Szurubooru\Entities\Post;
use Szurubooru\Entities\Snapshot; use Szurubooru\Entities\Snapshot;
use Szurubooru\Helpers\EnumHelper;
use Szurubooru\SearchServices\Filters\SnapshotFilter; use Szurubooru\SearchServices\Filters\SnapshotFilter;
use Szurubooru\SearchServices\Requirements\Requirement;
use Szurubooru\SearchServices\Requirements\RequirementSingleValue;
use Szurubooru\Services\AuthService; use Szurubooru\Services\AuthService;
use Szurubooru\Services\TimeService; use Szurubooru\Services\TimeService;
use Szurubooru\Validator;
class HistoryService class HistoryService
{ {
private $validator;
private $snapshotDao; private $snapshotDao;
private $globalParamDao;
private $timeService; private $timeService;
private $authService; private $authService;
private $transactionManager; private $transactionManager;
public function __construct( public function __construct(
Validator $validator,
SnapshotDao $snapshotDao, SnapshotDao $snapshotDao,
GlobalParamDao $globalParamDao,
TransactionManager $transactionManager, TransactionManager $transactionManager,
TimeService $timeService, TimeService $timeService,
AuthService $authService) AuthService $authService)
{ {
$this->validator = $validator;
$this->snapshotDao = $snapshotDao; $this->snapshotDao = $snapshotDao;
$this->globalParamDao = $globalParamDao;
$this->timeService = $timeService; $this->timeService = $timeService;
$this->authService = $authService; $this->authService = $authService;
$this->transactionManager = $transactionManager; $this->transactionManager = $transactionManager;
@ -48,23 +35,6 @@ class HistoryService
return $this->transactionManager->rollback($transactionFunc); return $this->transactionManager->rollback($transactionFunc);
} }
public function getPostHistory(Post $post)
{
$filter = new SnapshotFilter();
$requirement = new Requirement();
$requirement->setType(SnapshotFilter::REQUIREMENT_PRIMARY_KEY);
$requirement->setValue(new RequirementSingleValue($post->getId()));
$filter->addRequirement($requirement);
$requirement = new Requirement();
$requirement->setType(SnapshotFilter::REQUIREMENT_TYPE);
$requirement->setValue(new RequirementSingleValue(Snapshot::TYPE_POST));
$filter->addRequirement($requirement);
return $this->getFiltered($filter)->getEntities();
}
public function saveSnapshot(Snapshot $snapshot) public function saveSnapshot(Snapshot $snapshot)
{ {
$transactionFunc = function() use ($snapshot) $transactionFunc = function() use ($snapshot)
@ -92,57 +62,6 @@ class HistoryService
return $this->transactionManager->commit($transactionFunc); return $this->transactionManager->commit($transactionFunc);
} }
public function getPostDeleteSnapshot(Post $post)
{
$snapshot = $this->getPostSnapshot($post);
$snapshot->setData([]);
$snapshot->setOperation(Snapshot::OPERATION_DELETE);
return $snapshot;
}
public function getPostChangeSnapshot(Post $post)
{
static $featuredPostParam = null;
if ($featuredPostParam === null)
$featuredPostParam = $this->globalParamDao->findByKey(GlobalParam::KEY_FEATURED_POST);
$isFeatured = ($featuredPostParam and intval($featuredPostParam->getValue()) === $post->getId());
$flags = [];
if ($post->getFlags() & Post::FLAG_LOOP)
$flags[] = 'loop';
$data =
[
'source' => $post->getSource(),
'safety' => EnumHelper::postSafetyToString($post->getSafety()),
'contentChecksum' => $post->getContentChecksum(),
'featured' => $isFeatured,
'tags' =>
array_map(
function ($tag)
{
return $tag->getName();
},
$post->getTags()),
'relations' =>
array_map(
function ($post)
{
return $post->getId();
},
$post->getRelatedPosts()),
'flags' => $flags,
];
$snapshot = $this->getPostSnapshot($post);
$snapshot->setOperation(Snapshot::OPERATION_CHANGE);
$snapshot->setData($data);
return $snapshot;
}
public function getSnapshotDataDifference($newData, $oldData) public function getSnapshotDataDifference($newData, $oldData)
{ {
$diffFunction = function($base, $other) $diffFunction = function($base, $other)
@ -172,12 +91,4 @@ class HistoryService
'-' => $diffFunction($oldData, $newData), '-' => $diffFunction($oldData, $newData),
]; ];
} }
private function getPostSnapshot(Post $post)
{
$snapshot = new Snapshot();
$snapshot->setType(Snapshot::TYPE_POST);
$snapshot->setPrimaryKey($post->getId());
return $snapshot;
}
} }

View file

@ -0,0 +1,58 @@
<?php
namespace Szurubooru\Services;
use Szurubooru\Dao\TransactionManager;
use Szurubooru\Entities\Post;
use Szurubooru\Entities\Snapshot;
use Szurubooru\SearchServices\Filters\SnapshotFilter;
use Szurubooru\SearchServices\Requirements\Requirement;
use Szurubooru\SearchServices\Requirements\RequirementSingleValue;
use Szurubooru\Services\HistoryService;
use Szurubooru\Services\PostSnapshotProvider;
class PostHistoryService
{
private $transactionManager;
private $historyService;
private $postSnapshotProvider;
public function __construct(
TransactionManager $transactionManager,
HistoryService $historyService,
PostSnapshotProvider $postSnapshotProvider)
{
$this->transactionManager = $transactionManager;
$this->historyService = $historyService;
$this->postSnapshotProvider = $postSnapshotProvider;
}
public function getPostHistory(Post $post)
{
$transactionFunc = function() use ($post)
{
$filter = new SnapshotFilter();
$requirement = new Requirement();
$requirement->setType(SnapshotFilter::REQUIREMENT_PRIMARY_KEY);
$requirement->setValue(new RequirementSingleValue($post->getId()));
$filter->addRequirement($requirement);
$requirement = new Requirement();
$requirement->setType(SnapshotFilter::REQUIREMENT_TYPE);
$requirement->setValue(new RequirementSingleValue(Snapshot::TYPE_POST));
$filter->addRequirement($requirement);
return $this->historyService->getFiltered($filter)->getEntities();
};
return $this->transactionManager->rollback($transactionFunc);
}
public function savePostChange(Post $post)
{
$this->historyService->saveSnapshot($this->postSnapshotProvider->getPostChangeSnapshot($post));
}
public function savePostDeletion(Post $post)
{
$this->historyService->saveSnapshot($this->postSnapshotProvider->getPostDeleteSnapshot($post));
}
}

View file

@ -5,7 +5,7 @@ use Szurubooru\Dao\PostNoteDao;
use Szurubooru\Entities\Post; use Szurubooru\Entities\Post;
use Szurubooru\Entities\PostNote; use Szurubooru\Entities\PostNote;
use Szurubooru\FormData\PostNoteFormData; use Szurubooru\FormData\PostNoteFormData;
use Szurubooru\Services\HistoryService; use Szurubooru\Services\PostHistoryService;
use Szurubooru\Validator; use Szurubooru\Validator;
class PostNotesService class PostNotesService
@ -13,18 +13,18 @@ class PostNotesService
private $validator; private $validator;
private $transactionManager; private $transactionManager;
private $postNoteDao; private $postNoteDao;
private $historyService; private $postHistoryService;
public function __construct( public function __construct(
Validator $validator, Validator $validator,
TransactionManager $transactionManager, TransactionManager $transactionManager,
PostNoteDao $postNoteDao, PostNoteDao $postNoteDao,
HistoryService $historyService) PostHistoryService $postHistoryService)
{ {
$this->validator = $validator; $this->validator = $validator;
$this->transactionManager = $transactionManager; $this->transactionManager = $transactionManager;
$this->postNoteDao = $postNoteDao; $this->postNoteDao = $postNoteDao;
$this->historyService = $historyService; $this->postHistoryService = $postHistoryService;
} }
public function getById($postNoteId) public function getById($postNoteId)
@ -58,7 +58,7 @@ class PostNotesService
$this->updatePostNoteWithFormData($postNote, $formData); $this->updatePostNoteWithFormData($postNote, $formData);
$this->postNoteDao->save($postNote); $this->postNoteDao->save($postNote);
$this->historyService->saveSnapshot($this->historyService->getPostChangeSnapshot($post)); $this->postHistoryService->savePostChange($post);
return $postNote; return $postNote;
}; };
return $this->transactionManager->commit($transactionFunc); return $this->transactionManager->commit($transactionFunc);
@ -71,7 +71,7 @@ class PostNotesService
$this->updatePostNoteWithFormData($postNote, $formData); $this->updatePostNoteWithFormData($postNote, $formData);
$this->postNoteDao->save($postNote); $this->postNoteDao->save($postNote);
$this->historyService->saveSnapshot($this->historyService->getPostChangeSnapshot($postNote->getPost())); $this->postHistoryService->savePostChange($postNote->getPost());
return $postNote; return $postNote;
}; };
return $this->transactionManager->commit($transactionFunc); return $this->transactionManager->commit($transactionFunc);

View file

@ -17,7 +17,7 @@ use Szurubooru\SearchServices\Requirements\Requirement;
use Szurubooru\SearchServices\Requirements\RequirementCompositeValue; use Szurubooru\SearchServices\Requirements\RequirementCompositeValue;
use Szurubooru\SearchServices\Requirements\RequirementSingleValue; use Szurubooru\SearchServices\Requirements\RequirementSingleValue;
use Szurubooru\Services\AuthService; use Szurubooru\Services\AuthService;
use Szurubooru\Services\HistoryService; use Szurubooru\Services\PostHistoryService;
use Szurubooru\Services\ImageConverter; use Szurubooru\Services\ImageConverter;
use Szurubooru\Services\ImageManipulation\ImageManipulator; use Szurubooru\Services\ImageManipulation\ImageManipulator;
use Szurubooru\Services\NetworkingService; use Szurubooru\Services\NetworkingService;
@ -36,7 +36,7 @@ class PostService
private $authService; private $authService;
private $networkingService; private $networkingService;
private $tagService; private $tagService;
private $historyService; private $postHistoryService;
private $imageConverter; private $imageConverter;
private $imageManipulator; private $imageManipulator;
@ -50,7 +50,7 @@ class PostService
TimeService $timeService, TimeService $timeService,
NetworkingService $networkingService, NetworkingService $networkingService,
TagService $tagService, TagService $tagService,
HistoryService $historyService, PostHistoryService $postHistoryService,
ImageConverter $imageConverter, ImageConverter $imageConverter,
ImageManipulator $imageManipulator) ImageManipulator $imageManipulator)
{ {
@ -63,7 +63,7 @@ class PostService
$this->authService = $authService; $this->authService = $authService;
$this->networkingService = $networkingService; $this->networkingService = $networkingService;
$this->tagService = $tagService; $this->tagService = $tagService;
$this->historyService = $historyService; $this->postHistoryService = $postHistoryService;
$this->imageConverter = $imageConverter; $this->imageConverter = $imageConverter;
$this->imageManipulator = $imageManipulator; $this->imageManipulator = $imageManipulator;
} }
@ -103,27 +103,6 @@ class PostService
return $this->transactionManager->rollback($transactionFunc); return $this->transactionManager->rollback($transactionFunc);
} }
public function getHistory(Post $post)
{
$transactionFunc = function() use ($post)
{
$filter = new SnapshotFilter();
$requirement = new Requirement();
$requirement->setType(SnapshotFilter::REQUIREMENT_PRIMARY_KEY);
$requirement->setValue(new RequirementSingleValue($post->getId()));
$filter->addRequirement($requirement);
$requirement = new Requirement();
$requirement->setType(SnapshotFilter::REQUIREMENT_TYPE);
$requirement->setValue(new RequirementSingleValue(Snapshot::TYPE_POST));
$filter->addRequirement($requirement);
return $this->historyService->getFiltered($filter)->getEntities();
};
return $this->transactionManager->rollback($transactionFunc);
}
public function createPost(UploadFormData $formData) public function createPost(UploadFormData $formData)
{ {
$transactionFunc = function() use ($formData) $transactionFunc = function() use ($formData)
@ -144,7 +123,7 @@ class PostService
$savedPost = $this->postDao->save($post); $savedPost = $this->postDao->save($post);
$this->historyService->saveSnapshot($this->historyService->getPostChangeSnapshot($savedPost)); $this->postHistoryService->savePostChange($savedPost);
return $savedPost; return $savedPost;
}; };
$ret = $this->transactionManager->commit($transactionFunc); $ret = $this->transactionManager->commit($transactionFunc);
@ -185,7 +164,7 @@ class PostService
if (count($formData->flags) > 0) if (count($formData->flags) > 0)
$this->updatePostFlags($post, $formData->flags); $this->updatePostFlags($post, $formData->flags);
$this->historyService->saveSnapshot($this->historyService->getPostChangeSnapshot($post)); $this->postHistoryService->savePostChange($post);
return $this->postDao->save($post); return $this->postDao->save($post);
}; };
$ret = $this->transactionManager->commit($transactionFunc); $ret = $this->transactionManager->commit($transactionFunc);
@ -335,7 +314,7 @@ class PostService
{ {
$transactionFunc = function() use ($post) $transactionFunc = function() use ($post)
{ {
$this->historyService->saveSnapshot($this->historyService->getPostDeleteSnapshot($post)); $this->postHistoryService->savePostDeletion($post);
$this->postDao->deleteById($post->getId()); $this->postDao->deleteById($post->getId());
}; };
$this->transactionManager->commit($transactionFunc); $this->transactionManager->commit($transactionFunc);

View file

@ -0,0 +1,76 @@
<?php
namespace Szurubooru\Services;
use Szurubooru\Dao\GlobalParamDao;
use Szurubooru\Entities\GlobalParam;
use Szurubooru\Entities\Post;
use Szurubooru\Entities\Snapshot;
use Szurubooru\Helpers\EnumHelper;
class PostSnapshotProvider
{
private $globalParamDao;
public function __construct(GlobalParamDao $globalParamDao)
{
$this->globalParamDao = $globalParamDao;
}
public function getPostChangeSnapshot(Post $post)
{
static $featuredPostParam = null;
if ($featuredPostParam === null)
$featuredPostParam = $this->globalParamDao->findByKey(GlobalParam::KEY_FEATURED_POST);
$isFeatured = ($featuredPostParam and intval($featuredPostParam->getValue()) === $post->getId());
$flags = [];
if ($post->getFlags() & Post::FLAG_LOOP)
$flags[] = 'loop';
$data =
[
'source' => $post->getSource(),
'safety' => EnumHelper::postSafetyToString($post->getSafety()),
'contentChecksum' => $post->getContentChecksum(),
'featured' => $isFeatured,
'tags' =>
array_map(
function ($tag)
{
return $tag->getName();
},
$post->getTags()),
'relations' =>
array_map(
function ($post)
{
return $post->getId();
},
$post->getRelatedPosts()),
'flags' => $flags,
];
$snapshot = $this->getPostSnapshot($post);
$snapshot->setOperation(Snapshot::OPERATION_CHANGE);
$snapshot->setData($data);
return $snapshot;
}
public function getPostDeleteSnapshot(Post $post)
{
$snapshot = $this->getPostSnapshot($post);
$snapshot->setData([]);
$snapshot->setOperation(Snapshot::OPERATION_DELETE);
return $snapshot;
}
private function getPostSnapshot(Post $post)
{
$snapshot = new Snapshot();
$snapshot->setType(Snapshot::TYPE_POST);
$snapshot->setPrimaryKey($post->getId());
return $snapshot;
}
}

View file

@ -1,23 +1,15 @@
<?php <?php
namespace Szurubooru\Tests\Services; namespace Szurubooru\Tests\Services;
use Szurubooru\Dao\GlobalParamDao;
use Szurubooru\Dao\SnapshotDao; use Szurubooru\Dao\SnapshotDao;
use Szurubooru\Dao\TransactionManager; use Szurubooru\Dao\TransactionManager;
use Szurubooru\Entities\GlobalParam;
use Szurubooru\Entities\Post;
use Szurubooru\Entities\Snapshot;
use Szurubooru\Entities\Tag;
use Szurubooru\Services\AuthService; use Szurubooru\Services\AuthService;
use Szurubooru\Services\HistoryService; use Szurubooru\Services\HistoryService;
use Szurubooru\Services\TimeService; use Szurubooru\Services\TimeService;
use Szurubooru\Tests\AbstractTestCase; use Szurubooru\Tests\AbstractTestCase;
use Szurubooru\Validator;
final class HistoryServiceTest extends AbstractTestCase final class HistoryServiceTest extends AbstractTestCase
{ {
private $validatorMock;
private $snapshotDaoMock; private $snapshotDaoMock;
private $globalParamDaoMock;
private $timeServiceMock; private $timeServiceMock;
private $authServiceMock; private $authServiceMock;
private $transactionManagerMock; private $transactionManagerMock;
@ -112,69 +104,12 @@ final class HistoryServiceTest extends AbstractTestCase
public function setUp() public function setUp()
{ {
parent::setUp(); parent::setUp();
$this->validatorMock = $this->mock(Validator::class);
$this->snapshotDaoMock = $this->mock(SnapshotDao::class); $this->snapshotDaoMock = $this->mock(SnapshotDao::class);
$this->globalParamDaoMock = $this->mock(GlobalParamDao::class);
$this->transactionManagerMock = $this->mock(TransactionManager::class); $this->transactionManagerMock = $this->mock(TransactionManager::class);
$this->timeServiceMock = $this->mock(TimeService::class); $this->timeServiceMock = $this->mock(TimeService::class);
$this->authServiceMock = $this->mock(AuthService::class); $this->authServiceMock = $this->mock(AuthService::class);
} }
public function testPostChangeSnapshot()
{
$tag1 = new Tag();
$tag2 = new Tag();
$tag1->setName('tag1');
$tag2->setName('tag2');
$post1 = new Post(1);
$post2 = new Post(2);
$post = new Post(5);
$post->setTags([$tag1, $tag2]);
$post->setRelatedPosts([$post1, $post2]);
$post->setContentChecksum('checksum');
$post->setSafety(Post::POST_SAFETY_SKETCHY);
$post->setSource('amazing source');
$post->setFlags(Post::FLAG_LOOP);
$historyService = $this->getHistoryService();
$snapshot = $historyService->getPostChangeSnapshot($post);
$this->assertEquals([
'source' => 'amazing source',
'safety' => 'sketchy',
'contentChecksum' => 'checksum',
'featured' => false,
'tags' => ['tag1', 'tag2'],
'relations' => [1, 2],
'flags' => ['loop'],
], $snapshot->getData());
$this->assertEquals(Snapshot::TYPE_POST, $snapshot->getType());
$this->assertEquals(5, $snapshot->getPrimaryKey());
return $post;
}
/**
* @depends testPostChangeSnapshot
*/
public function testPostChangeSnapshotFeature($post)
{
$param = new GlobalParam;
$param->setValue($post->getId());
$this->globalParamDaoMock
->expects($this->once())
->method('findByKey')
->with(GlobalParam::KEY_FEATURED_POST)
->willReturn($param);
$historyService = $this->getHistoryService();
$snapshot = $historyService->getPostChangeSnapshot($post);
$this->assertTrue($snapshot->getData()['featured']);
}
/** /**
* @dataProvider snapshotDataDifferenceProvider * @dataProvider snapshotDataDifferenceProvider
*/ */
@ -187,9 +122,7 @@ final class HistoryServiceTest extends AbstractTestCase
private function getHistoryService() private function getHistoryService()
{ {
return new HistoryService( return new HistoryService(
$this->validatorMock,
$this->snapshotDaoMock, $this->snapshotDaoMock,
$this->globalParamDaoMock,
$this->transactionManagerMock, $this->transactionManagerMock,
$this->timeServiceMock, $this->timeServiceMock,
$this->authServiceMock); $this->authServiceMock);

View file

@ -8,7 +8,7 @@ use Szurubooru\Entities\User;
use Szurubooru\FormData\UploadFormData; use Szurubooru\FormData\UploadFormData;
use Szurubooru\Injector; use Szurubooru\Injector;
use Szurubooru\Services\AuthService; use Szurubooru\Services\AuthService;
use Szurubooru\Services\HistoryService; use Szurubooru\Services\PostHistoryService;
use Szurubooru\Services\ImageConverter; use Szurubooru\Services\ImageConverter;
use Szurubooru\Services\ImageManipulation\ImageManipulator; use Szurubooru\Services\ImageManipulation\ImageManipulator;
use Szurubooru\Services\NetworkingService; use Szurubooru\Services\NetworkingService;
@ -29,7 +29,7 @@ final class PostServiceTest extends AbstractDatabaseTestCase
private $timeServiceMock; private $timeServiceMock;
private $networkingServiceMock; private $networkingServiceMock;
private $tagService; private $tagService;
private $historyServiceMock; private $postHistoryServiceMock;
private $imageConverterMock; private $imageConverterMock;
private $imageManipulatorMock; private $imageManipulatorMock;
@ -45,7 +45,7 @@ final class PostServiceTest extends AbstractDatabaseTestCase
$this->timeServiceMock = $this->mock(TimeService::class); $this->timeServiceMock = $this->mock(TimeService::class);
$this->networkingServiceMock = $this->mock(NetworkingService::class); $this->networkingServiceMock = $this->mock(NetworkingService::class);
$this->tagService = Injector::get(TagService::class); $this->tagService = Injector::get(TagService::class);
$this->historyServiceMock = $this->mock(HistoryService::class); $this->postHistoryServiceMock = $this->mock(PostHistoryService::class);
$this->configMock->set('database/maxPostSize', 1000000); $this->configMock->set('database/maxPostSize', 1000000);
$this->imageConverterMock = $this->mock(ImageConverter::class); $this->imageConverterMock = $this->mock(ImageConverter::class);
$this->imageManipulatorMock = $this->mock(ImageManipulator::class); $this->imageManipulatorMock = $this->mock(ImageManipulator::class);
@ -61,7 +61,7 @@ final class PostServiceTest extends AbstractDatabaseTestCase
$this->postDaoMock->expects($this->once())->method('save')->will($this->returnArgument(0)); $this->postDaoMock->expects($this->once())->method('save')->will($this->returnArgument(0));
$this->authServiceMock->expects($this->once())->method('getLoggedInUser')->willReturn(new User(5)); $this->authServiceMock->expects($this->once())->method('getLoggedInUser')->willReturn(new User(5));
$this->historyServiceMock->expects($this->once())->method('getPostChangeSnapshot')->willReturn(new Snapshot()); $this->postHistoryServiceMock->expects($this->once())->method('savePostChange')->willReturn(new Snapshot());
$this->imageConverterMock->expects($this->never())->method('createImageFromBuffer'); $this->imageConverterMock->expects($this->never())->method('createImageFromBuffer');
$this->postService = $this->getPostService(); $this->postService = $this->getPostService();
@ -91,7 +91,7 @@ final class PostServiceTest extends AbstractDatabaseTestCase
$this->postDaoMock->expects($this->once())->method('save')->will($this->returnArgument(0)); $this->postDaoMock->expects($this->once())->method('save')->will($this->returnArgument(0));
$this->imageManipulatorMock->expects($this->once())->method('getImageWidth')->willReturn(640); $this->imageManipulatorMock->expects($this->once())->method('getImageWidth')->willReturn(640);
$this->imageManipulatorMock->expects($this->once())->method('getImageHeight')->willReturn(480); $this->imageManipulatorMock->expects($this->once())->method('getImageHeight')->willReturn(480);
$this->historyServiceMock->expects($this->once())->method('getPostChangeSnapshot')->willReturn(new Snapshot()); $this->postHistoryServiceMock->expects($this->once())->method('savePostChange')->willReturn(new Snapshot());
$this->postService = $this->getPostService(); $this->postService = $this->getPostService();
$savedPost = $this->postService->createPost($formData); $savedPost = $this->postService->createPost($formData);
@ -112,7 +112,7 @@ final class PostServiceTest extends AbstractDatabaseTestCase
$formData->contentFileName = 'blah'; $formData->contentFileName = 'blah';
$this->postDaoMock->expects($this->once())->method('save')->will($this->returnArgument(0)); $this->postDaoMock->expects($this->once())->method('save')->will($this->returnArgument(0));
$this->historyServiceMock->expects($this->once())->method('getPostChangeSnapshot')->willReturn(new Snapshot()); $this->postHistoryServiceMock->expects($this->once())->method('savePostChange')->willReturn(new Snapshot());
$this->imageConverterMock->expects($this->once())->method('createImageFromBuffer'); $this->imageConverterMock->expects($this->once())->method('createImageFromBuffer');
$this->postService = $this->getPostService(); $this->postService = $this->getPostService();
@ -132,7 +132,7 @@ final class PostServiceTest extends AbstractDatabaseTestCase
$formData->contentFileName = 'blah'; $formData->contentFileName = 'blah';
$this->postDaoMock->expects($this->once())->method('save')->will($this->returnArgument(0)); $this->postDaoMock->expects($this->once())->method('save')->will($this->returnArgument(0));
$this->historyServiceMock->expects($this->once())->method('getPostChangeSnapshot')->willReturn(new Snapshot()); $this->postHistoryServiceMock->expects($this->once())->method('savePostChange')->willReturn(new Snapshot());
$this->imageConverterMock->expects($this->once())->method('createImageFromBuffer'); $this->imageConverterMock->expects($this->once())->method('createImageFromBuffer');
$this->postService = $this->getPostService(); $this->postService = $this->getPostService();
@ -196,7 +196,7 @@ final class PostServiceTest extends AbstractDatabaseTestCase
$this->postDaoMock->expects($this->once())->method('save')->will($this->returnArgument(0)); $this->postDaoMock->expects($this->once())->method('save')->will($this->returnArgument(0));
$this->authServiceMock->expects($this->never())->method('getLoggedInUser'); $this->authServiceMock->expects($this->never())->method('getLoggedInUser');
$this->historyServiceMock->expects($this->once())->method('getPostChangeSnapshot')->willReturn(new Snapshot()); $this->postHistoryServiceMock->expects($this->once())->method('savePostChange')->willReturn(new Snapshot());
$this->postService = $this->getPostService(); $this->postService = $this->getPostService();
$savedPost = $this->postService->createPost($formData); $savedPost = $this->postService->createPost($formData);
@ -215,7 +215,7 @@ final class PostServiceTest extends AbstractDatabaseTestCase
$this->timeServiceMock, $this->timeServiceMock,
$this->networkingServiceMock, $this->networkingServiceMock,
$this->tagService, $this->tagService,
$this->historyServiceMock, $this->postHistoryServiceMock,
$this->imageConverterMock, $this->imageConverterMock,
$this->imageManipulatorMock); $this->imageManipulatorMock);
} }

View file

@ -0,0 +1,80 @@
<?php
namespace Szurubooru\Tests\Services;
use Szurubooru\Dao\GlobalParamDao;
use Szurubooru\Entities\GlobalParam;
use Szurubooru\Entities\Post;
use Szurubooru\Entities\Snapshot;
use Szurubooru\Entities\Tag;
use Szurubooru\Services\PostSnapshotProvider;
use Szurubooru\Tests\AbstractTestCase;
class PostSnapshotProviderTest extends AbstractTestCase
{
private $globalParamDaoMock;
public function setUp()
{
parent::setUp();
$this->globalParamDaoMock = $this->mock(GlobalParamDao::class);
}
public function testPostChangeSnapshot()
{
$tag1 = new Tag();
$tag2 = new Tag();
$tag1->setName('tag1');
$tag2->setName('tag2');
$post1 = new Post(1);
$post2 = new Post(2);
$post = new Post(5);
$post->setTags([$tag1, $tag2]);
$post->setRelatedPosts([$post1, $post2]);
$post->setContentChecksum('checksum');
$post->setSafety(Post::POST_SAFETY_SKETCHY);
$post->setSource('amazing source');
$post->setFlags(Post::FLAG_LOOP);
$postSnapshotProvider = $this->getPostSnapshotProvider();
$snapshot = $postSnapshotProvider->getPostChangeSnapshot($post);
$this->assertEquals([
'source' => 'amazing source',
'safety' => 'sketchy',
'contentChecksum' => 'checksum',
'featured' => false,
'tags' => ['tag1', 'tag2'],
'relations' => [1, 2],
'flags' => ['loop'],
], $snapshot->getData());
$this->assertEquals(Snapshot::TYPE_POST, $snapshot->getType());
$this->assertEquals(5, $snapshot->getPrimaryKey());
return $post;
}
/**
* @depends testPostChangeSnapshot
*/
public function testPostChangeSnapshotFeature($post)
{
$param = new GlobalParam;
$param->setValue($post->getId());
$this->globalParamDaoMock
->expects($this->once())
->method('findByKey')
->with(GlobalParam::KEY_FEATURED_POST)
->willReturn($param);
$postSnapshotProvider = $this->getPostSnapshotProvider();
$snapshot = $postSnapshotProvider->getPostChangeSnapshot($post);
$this->assertTrue($snapshot->getData()['featured']);
}
private function getPostSnapshotProvider()
{
return new PostSnapshotProvider($this->globalParamDaoMock);
}
}