diff --git a/src/Dao/PostDao.php b/src/Dao/PostDao.php index efce8518..373f4613 100644 --- a/src/Dao/PostDao.php +++ b/src/Dao/PostDao.php @@ -230,8 +230,6 @@ class PostDao extends AbstractDao implements ICrudDao { $this->fpdo->deleteFrom('postTags')->where('postId', $post->getId())->where('tagId', $tagId)->execute(); } - - $this->tagDao->exportJson(); } private function syncPostRelations(\Szurubooru\Entities\Post $post) diff --git a/src/Dao/TagDao.php b/src/Dao/TagDao.php index 71409f02..47204286 100644 --- a/src/Dao/TagDao.php +++ b/src/Dao/TagDao.php @@ -3,18 +3,13 @@ namespace Szurubooru\Dao; class TagDao extends AbstractDao implements ICrudDao { - private $fileService; - public function __construct( - \Szurubooru\DatabaseConnection $databaseConnection, - \Szurubooru\Services\FileService $fileService) + \Szurubooru\DatabaseConnection $databaseConnection) { parent::__construct( $databaseConnection, 'tags', new \Szurubooru\Dao\EntityConverters\TagEntityConverter()); - - $this->fileService = $fileService; } public function findByNames($tagNames) @@ -36,21 +31,4 @@ class TagDao extends AbstractDao implements ICrudDao $arrayEntities = iterator_to_array($query); return $this->arrayToEntities($arrayEntities); } - - public function exportJson() - { - $tags = []; - foreach ($this->findAll() as $tag) - { - $tags[$tag->getName()] = $tag->getUsages(); - } - $json = json_encode($tags); - $this->fileService->save('tags.json', $json); - } - - protected function afterBatchSave(array $entities) - { - if (count($entities) > 0) - $this->exportJson(); - } } diff --git a/src/Services/PostService.php b/src/Services/PostService.php index cce16898..047c0961 100644 --- a/src/Services/PostService.php +++ b/src/Services/PostService.php @@ -271,6 +271,7 @@ class PostService $tags[] = $tag; } $tags = $this->tagService->createTags($tags); + $this->tagService->exportJson(); $post->setTags($tags); } diff --git a/src/Services/TagService.php b/src/Services/TagService.php index ec5d8d51..7b4a1a16 100644 --- a/src/Services/TagService.php +++ b/src/Services/TagService.php @@ -5,16 +5,19 @@ class TagService { private $transactionManager; private $tagDao; + private $fileService; private $timeService; public function __construct( \Szurubooru\Dao\TransactionManager $transactionManager, \Szurubooru\Dao\TagDao $tagDao, - \Szurubooru\Services\TimeService $timeService) + \Szurubooru\Services\TimeService $timeService, + \Szurubooru\Services\FileService $fileService) { $this->transactionManager = $transactionManager; $this->tagDao = $tagDao; $this->timeService = $timeService; + $this->fileService = $fileService; } public function getFiltered(\Szurubooru\SearchServices\Filters\TagFilter $filter) @@ -26,6 +29,17 @@ class TagService return $this->transactionManager->rollback($transactionFunc); } + public function exportJson() + { + $tags = []; + foreach ($this->tagDao->findAll() as $tag) + { + $tags[$tag->getName()] = $tag->getUsages(); + } + $json = json_encode($tags); + $this->fileService->save('tags.json', $json); + } + public function createTags(array $tags) { $transactionFunc = function() use ($tags) diff --git a/src/Upgrades/Upgrade12.php b/src/Upgrades/Upgrade12.php index ff55c46d..5ec1920c 100644 --- a/src/Upgrades/Upgrade12.php +++ b/src/Upgrades/Upgrade12.php @@ -3,15 +3,15 @@ namespace Szurubooru\Upgrades; class Upgrade12 implements IUpgrade { - private $tagDao; + private $tagService; - public function __construct(\Szurubooru\Dao\TagDao $tagDao) + public function __construct(\Szurubooru\Services\TagService $tagService) { - $this->tagDao = $tagDao; + $this->tagService = $tagService; } public function run(\Szurubooru\DatabaseConnection $databaseConnection) { - $this->tagDao->exportJson(); + $this->tagService->exportJson(); } } diff --git a/tests/AbstractDatabaseTestCase.php b/tests/AbstractDatabaseTestCase.php index ab2a14d2..6b7275bd 100644 --- a/tests/AbstractDatabaseTestCase.php +++ b/tests/AbstractDatabaseTestCase.php @@ -13,16 +13,24 @@ abstract class AbstractDatabaseTestCase extends \Szurubooru\Tests\AbstractTestCa $config->set('database/user', ''); $config->set('database/password', ''); - $fileServiceMock = $this->mock(\Szurubooru\Services\FileService::class); + $fileService = $this->prepareFileService(); $this->databaseConnection = new \Szurubooru\DatabaseConnection($config); \Szurubooru\Injector::set(\Szurubooru\DatabaseConnection::class, $this->databaseConnection); - \Szurubooru\Injector::set(\Szurubooru\Services\FileService::class, $fileServiceMock); + \Szurubooru\Injector::set(\Szurubooru\Services\FileService::class, $fileService); $upgradeRepository = \Szurubooru\Injector::get(\Szurubooru\Upgrades\UpgradeRepository::class); $upgradeService = new \Szurubooru\Services\UpgradeService($config, $this->databaseConnection, $upgradeRepository); $upgradeService->runUpgradesQuiet(); } + private function prepareFileService() + { + $testDirectory = $this->createTestDirectory(); + $configMock = $this->mockConfig(null, $testDirectory); + $httpHelper = \Szurubooru\Injector::get(\Szurubooru\Helpers\HttpHelper::class); + return new \Szurubooru\Services\FileService($configMock, $httpHelper); + } + public function tearDown() { parent::tearDown(); diff --git a/tests/Dao/PostDaoTest.php b/tests/Dao/PostDaoTest.php index 95362d47..771e7fe4 100644 --- a/tests/Dao/PostDaoTest.php +++ b/tests/Dao/PostDaoTest.php @@ -251,11 +251,10 @@ final class PostDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase [$post->getThumbnailSourceContentPath()]); $this->fileServiceMock - ->expects($this->exactly(2)) + ->expects($this->exactly(1)) ->method('save') ->withConsecutive( - [$post->getContentPath(), 'whatever'], - ['tags.json', '[]']); + [$post->getContentPath(), 'whatever']); $postDao->save($post); } @@ -275,12 +274,11 @@ final class PostDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase [$post->getThumbnailSourceContentPath()]); $this->fileServiceMock - ->expects($this->exactly(3)) + ->expects($this->exactly(2)) ->method('save') ->withConsecutive( [$post->getContentPath(), 'whatever'], - [$post->getThumbnailSourceContentPath(), 'an image of sharks'], - ['tags.json', '[]']); + [$post->getThumbnailSourceContentPath(), 'an image of sharks']); $postDao->save($post); } diff --git a/tests/Dao/TagDaoTest.php b/tests/Dao/TagDaoTest.php index f7c09030..d7d3835f 100644 --- a/tests/Dao/TagDaoTest.php +++ b/tests/Dao/TagDaoTest.php @@ -3,12 +3,9 @@ namespace Szurubooru\Tests\Dao; final class TagDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase { - private $fileServiceMock; - public function setUp() { parent::setUp(); - $this->fileServiceMock = $this->mock(\Szurubooru\Services\FileService::class); } public function testFindByPostIds() @@ -38,31 +35,8 @@ final class TagDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase $this->assertEntitiesEqual($expected, $actual); } - public function testExportSingle() - { - $tag1 = new \Szurubooru\Entities\Tag(); - $tag1->setName('test'); - $tag1->setCreationTime(date('c')); - $this->fileServiceMock->expects($this->once())->method('save')->with('tags.json', '{"test":0}'); - $tagDao = $this->getTagDao(); - $tagDao->save($tag1); - } - - public function testExportMultiple() - { - $tag1 = new \Szurubooru\Entities\Tag(); - $tag1->setName('test1'); - $tag1->setCreationTime(date('c')); - $tag2 = new \Szurubooru\Entities\Tag(); - $tag2->setName('test2'); - $tag2->setCreationTime(date('c')); - $this->fileServiceMock->expects($this->once())->method('save')->with('tags.json', '{"test1":0,"test2":0}'); - $tagDao = $this->getTagDao(); - $tagDao->batchSave([$tag1, $tag2]); - } - private function getTagDao() { - return new \Szurubooru\Dao\TagDao($this->databaseConnection, $this->fileServiceMock); + return new \Szurubooru\Dao\TagDao($this->databaseConnection); } } diff --git a/tests/Services/TagServiceTest.php b/tests/Services/TagServiceTest.php index e06863db..9d82f2a4 100644 --- a/tests/Services/TagServiceTest.php +++ b/tests/Services/TagServiceTest.php @@ -64,6 +64,38 @@ class TagServiceTest extends \Szurubooru\Tests\AbstractDatabaseTestCase $this->assertEquals('test3', $result[1]->getName()); } + public function testExportSingle() + { + $tag1 = new \Szurubooru\Entities\Tag(); + $tag1->setName('test'); + $tag1->setCreationTime(date('c')); + $fileService = $this->getFileService(); + $tagService = $this->getTagService(); + $tagService->createTags([$tag1]); + $tagService->exportJson(); + $this->assertEquals('{"test":0}', $fileService->load('tags.json')); + } + + public function testExportMultiple() + { + $tag1 = new \Szurubooru\Entities\Tag(); + $tag1->setName('test1'); + $tag1->setCreationTime(date('c')); + $tag2 = new \Szurubooru\Entities\Tag(); + $tag2->setName('test2'); + $tag2->setCreationTime(date('c')); + $fileService = $this->getFileService(); + $tagService = $this->getTagService(); + $tagService->createTags([$tag1, $tag2]); + $tagService->exportJson(); + $this->assertEquals('{"test1":0,"test2":0}', $fileService->load('tags.json')); + } + + private function getFileService() + { + return \Szurubooru\Injector::get(\Szurubooru\Services\FileService::class); + } + private function getTagService() { return \Szurubooru\Injector::get(\Szurubooru\Services\TagService::class);