diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 5b37da0..118c153 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -1,6 +1,10 @@ name: Tests -on: [push, pull_request] +on: + pull_request: + push: + branches: + - "master" jobs: PHPStan: @@ -39,7 +43,7 @@ jobs: - name: PHPStan tests run: vendor/bin/phpstan analyze -l 8 -a vendor/yiisoft/yii2/Yii.php --no-progress src - PHPUnit: + Infection: name: PHP ${{ matrix.php }} runs-on: ubuntu-latest strategy: @@ -56,7 +60,7 @@ jobs: with: php-version: ${{ matrix.php }} extensions: mbstring, intl, mysql - coverage: none + coverage: "pcov" env: COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }} update: true @@ -80,5 +84,13 @@ jobs: if: matrix.php == '7.4' run: composer update --prefer-dist --no-interaction --no-progress --optimize-autoloader - - name: PHPUnit tests - run: vendor/bin/phpunit --verbose + - name: Run PHPUnit + if: matrix.php == '8.0' + run: vendor/bin/phpunit + + - name: Run Infection with PHPUnit + if: matrix.php == '7.4' + run: | + mkdir -p build/logs + vendor/bin/phpunit --coverage-xml=build/logs/coverage-xml --log-junit=build/logs/junit.xml + vendor/bin/infection --threads=2 --coverage=build/logs --show-mutations --no-progress diff --git a/README.md b/README.md index e050a73..9779e67 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,4 @@ There are some rules: - each repository knows how to handle one single storage unit and not more, - components operate on repositories, not on identifiers. -TODOs: - - [ ] Infection - When API is ready, I'll start preparing the client. diff --git a/infection.json.dist b/infection.json.dist index bfd12da..c79a010 100644 --- a/infection.json.dist +++ b/infection.json.dist @@ -10,6 +10,6 @@ "mutators": { "@default": true }, - "minMsi": 95, + "minMsi": 100, "minCoveredMsi": 100 } diff --git a/phpunit.xml b/phpunit.xml.dist similarity index 100% rename from phpunit.xml rename to phpunit.xml.dist diff --git a/src/Services/Category/CategoryArchiver.php b/src/Services/Category/CategoryArchiver.php index ae770b0..1fcc102 100644 --- a/src/Services/Category/CategoryArchiver.php +++ b/src/Services/Category/CategoryArchiver.php @@ -4,6 +4,7 @@ namespace Podium\Api\Services\Category; +use InvalidArgumentException; use Podium\Api\Components\PodiumResponse; use Podium\Api\Events\ArchiveEvent; use Podium\Api\Interfaces\ArchiverInterface; @@ -38,7 +39,17 @@ private function beforeArchive(): bool */ public function archive(RepositoryInterface $category): PodiumResponse { - if (!$category instanceof CategoryRepositoryInterface || !$this->beforeArchive()) { + if (!$category instanceof CategoryRepositoryInterface) { + return PodiumResponse::error( + [ + 'exception' => new InvalidArgumentException( + 'Category must be instance of Podium\Api\Interfaces\CategoryRepositoryInterface!' + ), + ] + ); + } + + if (!$this->beforeArchive()) { return PodiumResponse::error(); } @@ -94,7 +105,17 @@ private function beforeRevive(): bool */ public function revive(RepositoryInterface $category): PodiumResponse { - if (!$category instanceof CategoryRepositoryInterface || !$this->beforeRevive()) { + if (!$category instanceof CategoryRepositoryInterface) { + return PodiumResponse::error( + [ + 'exception' => new InvalidArgumentException( + 'Category must be instance of Podium\Api\Interfaces\CategoryRepositoryInterface!' + ), + ] + ); + } + + if (!$this->beforeRevive()) { return PodiumResponse::error(); } diff --git a/src/Services/Category/CategoryRemover.php b/src/Services/Category/CategoryRemover.php index ba944fa..3c15b1a 100644 --- a/src/Services/Category/CategoryRemover.php +++ b/src/Services/Category/CategoryRemover.php @@ -4,6 +4,7 @@ namespace Podium\Api\Services\Category; +use InvalidArgumentException; use Podium\Api\Components\PodiumResponse; use Podium\Api\Events\RemoveEvent; use Podium\Api\Interfaces\CategoryRepositoryInterface; @@ -36,7 +37,17 @@ private function beforeRemove(): bool */ public function remove(RepositoryInterface $category): PodiumResponse { - if (!$category instanceof CategoryRepositoryInterface || !$this->beforeRemove()) { + if (!$category instanceof CategoryRepositoryInterface) { + return PodiumResponse::error( + [ + 'exception' => new InvalidArgumentException( + 'Category must be instance of Podium\Api\Interfaces\CategoryRepositoryInterface!' + ), + ] + ); + } + + if (!$this->beforeRemove()) { return PodiumResponse::error(); } diff --git a/src/Services/Category/CategorySorter.php b/src/Services/Category/CategorySorter.php index 4f87e82..77a60d1 100644 --- a/src/Services/Category/CategorySorter.php +++ b/src/Services/Category/CategorySorter.php @@ -4,6 +4,7 @@ namespace Podium\Api\Services\Category; +use InvalidArgumentException; use Podium\Api\Components\PodiumResponse; use Podium\Api\Events\SortEvent; use Podium\Api\Interfaces\CategoryRepositoryInterface; @@ -39,11 +40,27 @@ private function beforeReplace(): bool */ public function replace(RepositoryInterface $firstCategory, RepositoryInterface $secondCategory): PodiumResponse { - if ( - !$firstCategory instanceof CategoryRepositoryInterface - || !$secondCategory instanceof CategoryRepositoryInterface - || !$this->beforeReplace() - ) { + if (!$firstCategory instanceof CategoryRepositoryInterface) { + return PodiumResponse::error( + [ + 'exception' => new InvalidArgumentException( + 'First category must be instance of Podium\Api\Interfaces\CategoryRepositoryInterface!' + ), + ] + ); + } + + if (!$secondCategory instanceof CategoryRepositoryInterface) { + return PodiumResponse::error( + [ + 'exception' => new InvalidArgumentException( + 'Second category must be instance of Podium\Api\Interfaces\CategoryRepositoryInterface!' + ), + ] + ); + } + + if (!$this->beforeReplace()) { return PodiumResponse::error(); } @@ -98,7 +115,17 @@ private function beforeSort(): bool */ public function sort(RepositoryInterface $category): PodiumResponse { - if (!$category instanceof CategoryRepositoryInterface || !$this->beforeSort()) { + if (!$category instanceof CategoryRepositoryInterface) { + return PodiumResponse::error( + [ + 'exception' => new InvalidArgumentException( + 'Category must be instance of Podium\Api\Interfaces\CategoryRepositoryInterface!' + ), + ] + ); + } + + if (!$this->beforeSort()) { return PodiumResponse::error(); } diff --git a/src/Services/Forum/ForumArchiver.php b/src/Services/Forum/ForumArchiver.php index b50d448..bfca34b 100644 --- a/src/Services/Forum/ForumArchiver.php +++ b/src/Services/Forum/ForumArchiver.php @@ -4,6 +4,7 @@ namespace Podium\Api\Services\Forum; +use InvalidArgumentException; use Podium\Api\Components\PodiumResponse; use Podium\Api\Events\ArchiveEvent; use Podium\Api\Interfaces\ArchiverInterface; @@ -38,7 +39,17 @@ private function beforeArchive(): bool */ public function archive(RepositoryInterface $forum): PodiumResponse { - if (!$forum instanceof ForumRepositoryInterface || !$this->beforeArchive()) { + if (!$forum instanceof ForumRepositoryInterface) { + return PodiumResponse::error( + [ + 'exception' => new InvalidArgumentException( + 'Forum must be instance of Podium\Api\Interfaces\ForumRepositoryInterface!' + ), + ] + ); + } + + if (!$this->beforeArchive()) { return PodiumResponse::error(); } @@ -94,7 +105,17 @@ private function beforeRevive(): bool */ public function revive(RepositoryInterface $forum): PodiumResponse { - if (!$forum instanceof ForumRepositoryInterface || !$this->beforeRevive()) { + if (!$forum instanceof ForumRepositoryInterface) { + return PodiumResponse::error( + [ + 'exception' => new InvalidArgumentException( + 'Forum must be instance of Podium\Api\Interfaces\ForumRepositoryInterface!' + ), + ] + ); + } + + if (!$this->beforeRevive()) { return PodiumResponse::error(); } diff --git a/src/Services/Forum/ForumBuilder.php b/src/Services/Forum/ForumBuilder.php index 1471168..383bafe 100644 --- a/src/Services/Forum/ForumBuilder.php +++ b/src/Services/Forum/ForumBuilder.php @@ -4,6 +4,7 @@ namespace Podium\Api\Services\Forum; +use InvalidArgumentException; use Podium\Api\Components\PodiumResponse; use Podium\Api\Events\BuildEvent; use Podium\Api\Interfaces\CategorisedBuilderInterface; @@ -44,11 +45,27 @@ public function create( RepositoryInterface $category, array $data = [] ): PodiumResponse { - if ( - !$forum instanceof ForumRepositoryInterface - || !$category instanceof CategoryRepositoryInterface - || !$this->beforeCreate() - ) { + if (!$forum instanceof ForumRepositoryInterface) { + return PodiumResponse::error( + [ + 'exception' => new InvalidArgumentException( + 'Forum must be instance of Podium\Api\Interfaces\ForumRepositoryInterface!' + ), + ] + ); + } + + if (!$category instanceof CategoryRepositoryInterface) { + return PodiumResponse::error( + [ + 'exception' => new InvalidArgumentException( + 'Category must be instance of Podium\Api\Interfaces\CategoryRepositoryInterface!' + ), + ] + ); + } + + if (!$this->beforeCreate()) { return PodiumResponse::error(); } @@ -100,7 +117,17 @@ private function beforeEdit(): bool */ public function edit(RepositoryInterface $forum, array $data = []): PodiumResponse { - if (!$forum instanceof ForumRepositoryInterface || !$this->beforeEdit()) { + if (!$forum instanceof ForumRepositoryInterface) { + return PodiumResponse::error( + [ + 'exception' => new InvalidArgumentException( + 'Forum must be instance of Podium\Api\Interfaces\ForumRepositoryInterface!' + ), + ] + ); + } + + if (!$this->beforeEdit()) { return PodiumResponse::error(); } diff --git a/src/Services/Forum/ForumMover.php b/src/Services/Forum/ForumMover.php index 04d0143..a048a68 100644 --- a/src/Services/Forum/ForumMover.php +++ b/src/Services/Forum/ForumMover.php @@ -4,6 +4,7 @@ namespace Podium\Api\Services\Forum; +use InvalidArgumentException; use Podium\Api\Components\PodiumResponse; use Podium\Api\Events\MoveEvent; use Podium\Api\Interfaces\CategoryRepositoryInterface; @@ -37,11 +38,27 @@ private function beforeMove(): bool */ public function move(RepositoryInterface $forum, RepositoryInterface $category): PodiumResponse { - if ( - !$forum instanceof ForumRepositoryInterface - || !$category instanceof CategoryRepositoryInterface - || !$this->beforeMove() - ) { + if (!$forum instanceof ForumRepositoryInterface) { + return PodiumResponse::error( + [ + 'exception' => new InvalidArgumentException( + 'Forum must be instance of Podium\Api\Interfaces\ForumRepositoryInterface!' + ), + ] + ); + } + + if (!$category instanceof CategoryRepositoryInterface) { + return PodiumResponse::error( + [ + 'exception' => new InvalidArgumentException( + 'Category must be instance of Podium\Api\Interfaces\CategoryRepositoryInterface!' + ), + ] + ); + } + + if (!$this->beforeMove()) { return PodiumResponse::error(); } diff --git a/src/Services/Forum/ForumRemover.php b/src/Services/Forum/ForumRemover.php index 4d7fcdf..651d37c 100644 --- a/src/Services/Forum/ForumRemover.php +++ b/src/Services/Forum/ForumRemover.php @@ -4,6 +4,7 @@ namespace Podium\Api\Services\Forum; +use InvalidArgumentException; use Podium\Api\Components\PodiumResponse; use Podium\Api\Events\RemoveEvent; use Podium\Api\Interfaces\ForumRepositoryInterface; @@ -36,7 +37,17 @@ private function beforeRemove(): bool */ public function remove(RepositoryInterface $forum): PodiumResponse { - if (!$forum instanceof ForumRepositoryInterface || !$this->beforeRemove()) { + if (!$forum instanceof ForumRepositoryInterface) { + return PodiumResponse::error( + [ + 'exception' => new InvalidArgumentException( + 'Forum must be instance of Podium\Api\Interfaces\ForumRepositoryInterface!' + ), + ] + ); + } + + if (!$this->beforeRemove()) { return PodiumResponse::error(); } diff --git a/src/Services/Forum/ForumSorter.php b/src/Services/Forum/ForumSorter.php index 3e344df..fd80a85 100644 --- a/src/Services/Forum/ForumSorter.php +++ b/src/Services/Forum/ForumSorter.php @@ -4,6 +4,7 @@ namespace Podium\Api\Services\Forum; +use InvalidArgumentException; use Podium\Api\Components\PodiumResponse; use Podium\Api\Events\SortEvent; use Podium\Api\Interfaces\ForumRepositoryInterface; @@ -39,11 +40,27 @@ private function beforeReplace(): bool */ public function replace(RepositoryInterface $firstForum, RepositoryInterface $secondForum): PodiumResponse { - if ( - !$firstForum instanceof ForumRepositoryInterface - || !$secondForum instanceof ForumRepositoryInterface - || !$this->beforeReplace() - ) { + if (!$firstForum instanceof ForumRepositoryInterface) { + return PodiumResponse::error( + [ + 'exception' => new InvalidArgumentException( + 'First forum must be instance of Podium\Api\Interfaces\ForumRepositoryInterface!' + ), + ] + ); + } + + if (!$secondForum instanceof ForumRepositoryInterface) { + return PodiumResponse::error( + [ + 'exception' => new InvalidArgumentException( + 'Second forum must be instance of Podium\Api\Interfaces\ForumRepositoryInterface!' + ), + ] + ); + } + + if (!$this->beforeReplace()) { return PodiumResponse::error(); } @@ -98,7 +115,17 @@ private function beforeSort(): bool */ public function sort(RepositoryInterface $forum): PodiumResponse { - if (!$forum instanceof ForumRepositoryInterface || !$this->beforeSort()) { + if (!$forum instanceof ForumRepositoryInterface) { + return PodiumResponse::error( + [ + 'exception' => new InvalidArgumentException( + 'Forum must be instance of Podium\Api\Interfaces\ForumRepositoryInterface!' + ), + ] + ); + } + + if (!$this->beforeSort()) { return PodiumResponse::error(); } diff --git a/src/Services/Group/GroupBuilder.php b/src/Services/Group/GroupBuilder.php index 413736e..e60a6d7 100644 --- a/src/Services/Group/GroupBuilder.php +++ b/src/Services/Group/GroupBuilder.php @@ -4,6 +4,7 @@ namespace Podium\Api\Services\Group; +use InvalidArgumentException; use Podium\Api\Components\PodiumResponse; use Podium\Api\Events\BuildEvent; use Podium\Api\Interfaces\BuilderInterface; @@ -38,7 +39,17 @@ private function beforeCreate(): bool */ public function create(RepositoryInterface $group, array $data = []): PodiumResponse { - if (!$group instanceof GroupRepositoryInterface || !$this->beforeCreate()) { + if (!$group instanceof GroupRepositoryInterface) { + return PodiumResponse::error( + [ + 'exception' => new InvalidArgumentException( + 'Group must be instance of Podium\Api\Interfaces\GroupRepositoryInterface!' + ), + ] + ); + } + + if (!$this->beforeCreate()) { return PodiumResponse::error(); } @@ -90,7 +101,17 @@ private function beforeEdit(): bool */ public function edit(RepositoryInterface $group, array $data = []): PodiumResponse { - if (!$group instanceof GroupRepositoryInterface || !$this->beforeEdit()) { + if (!$group instanceof GroupRepositoryInterface) { + return PodiumResponse::error( + [ + 'exception' => new InvalidArgumentException( + 'Group must be instance of Podium\Api\Interfaces\GroupRepositoryInterface!' + ), + ] + ); + } + + if (!$this->beforeEdit()) { return PodiumResponse::error(); } diff --git a/src/Services/Group/GroupRemover.php b/src/Services/Group/GroupRemover.php index b0e39ea..bdbf645 100644 --- a/src/Services/Group/GroupRemover.php +++ b/src/Services/Group/GroupRemover.php @@ -4,6 +4,7 @@ namespace Podium\Api\Services\Group; +use InvalidArgumentException; use Podium\Api\Components\PodiumResponse; use Podium\Api\Events\RemoveEvent; use Podium\Api\Interfaces\GroupRepositoryInterface; @@ -36,7 +37,17 @@ private function beforeRemove(): bool */ public function remove(RepositoryInterface $group): PodiumResponse { - if (!$group instanceof GroupRepositoryInterface || !$this->beforeRemove()) { + if (!$group instanceof GroupRepositoryInterface) { + return PodiumResponse::error( + [ + 'exception' => new InvalidArgumentException( + 'Group must be instance of Podium\Api\Interfaces\GroupRepositoryInterface!' + ), + ] + ); + } + + if (!$this->beforeRemove()) { return PodiumResponse::error(); } diff --git a/src/Services/Logger/LoggerRemover.php b/src/Services/Logger/LoggerRemover.php index f228e90..2d0ce6d 100644 --- a/src/Services/Logger/LoggerRemover.php +++ b/src/Services/Logger/LoggerRemover.php @@ -4,6 +4,7 @@ namespace Podium\Api\Services\Logger; +use InvalidArgumentException; use Podium\Api\Components\PodiumResponse; use Podium\Api\Events\RemoveEvent; use Podium\Api\Interfaces\LogRepositoryInterface; @@ -36,7 +37,17 @@ private function beforeRemove(): bool */ public function remove(RepositoryInterface $log): PodiumResponse { - if (!$log instanceof LogRepositoryInterface || !$this->beforeRemove()) { + if (!$log instanceof LogRepositoryInterface) { + return PodiumResponse::error( + [ + 'exception' => new InvalidArgumentException( + 'Log must be instance of Podium\Api\Interfaces\LogRepositoryInterface!' + ), + ] + ); + } + + if (!$this->beforeRemove()) { return PodiumResponse::error(); } diff --git a/src/Services/Member/MemberRemover.php b/src/Services/Member/MemberRemover.php index 33f1a6e..cd62571 100644 --- a/src/Services/Member/MemberRemover.php +++ b/src/Services/Member/MemberRemover.php @@ -4,6 +4,7 @@ namespace Podium\Api\Services\Member; +use InvalidArgumentException; use Podium\Api\Components\PodiumResponse; use Podium\Api\Events\RemoveEvent; use Podium\Api\Interfaces\MemberRepositoryInterface; @@ -36,7 +37,17 @@ private function beforeRemove(): bool */ public function remove(RepositoryInterface $member): PodiumResponse { - if (!$member instanceof MemberRepositoryInterface || !$this->beforeRemove()) { + if (!$member instanceof MemberRepositoryInterface) { + return PodiumResponse::error( + [ + 'exception' => new InvalidArgumentException( + 'Member must be instance of Podium\Api\Interfaces\MemberRepositoryInterface!' + ), + ] + ); + } + + if (!$this->beforeRemove()) { return PodiumResponse::error(); } diff --git a/src/Services/Poll/PollRemover.php b/src/Services/Poll/PollRemover.php index 3008d53..62fafcb 100644 --- a/src/Services/Poll/PollRemover.php +++ b/src/Services/Poll/PollRemover.php @@ -4,6 +4,7 @@ namespace Podium\Api\Services\Poll; +use InvalidArgumentException; use Podium\Api\Components\PodiumResponse; use Podium\Api\Events\RemoveEvent; use Podium\Api\Interfaces\PollPostRepositoryInterface; @@ -33,7 +34,17 @@ private function beforeRemove(): bool */ public function remove(RepositoryInterface $post): PodiumResponse { - if (!$post instanceof PollPostRepositoryInterface || !$this->beforeRemove()) { + if (!$post instanceof PollPostRepositoryInterface) { + return PodiumResponse::error( + [ + 'exception' => new InvalidArgumentException( + 'Post must be instance of Podium\Api\Interfaces\PollPostRepositoryInterface!' + ), + ] + ); + } + + if (!$this->beforeRemove()) { return PodiumResponse::error(); } diff --git a/src/Services/Post/PostArchiver.php b/src/Services/Post/PostArchiver.php index a444403..a188e48 100644 --- a/src/Services/Post/PostArchiver.php +++ b/src/Services/Post/PostArchiver.php @@ -4,6 +4,7 @@ namespace Podium\Api\Services\Post; +use InvalidArgumentException; use Podium\Api\Components\PodiumResponse; use Podium\Api\Events\ArchiveEvent; use Podium\Api\Interfaces\ArchiverInterface; @@ -38,7 +39,17 @@ private function beforeArchive(): bool */ public function archive(RepositoryInterface $post): PodiumResponse { - if (!$post instanceof PostRepositoryInterface || !$this->beforeArchive()) { + if (!$post instanceof PostRepositoryInterface) { + return PodiumResponse::error( + [ + 'exception' => new InvalidArgumentException( + 'Post must be instance of Podium\Api\Interfaces\PostRepositoryInterface!' + ), + ] + ); + } + + if (!$this->beforeArchive()) { return PodiumResponse::error(); } @@ -94,7 +105,17 @@ private function beforeRevive(): bool */ public function revive(RepositoryInterface $post): PodiumResponse { - if (!$post instanceof PostRepositoryInterface || !$this->beforeRevive()) { + if (!$post instanceof PostRepositoryInterface) { + return PodiumResponse::error( + [ + 'exception' => new InvalidArgumentException( + 'Post must be instance of Podium\Api\Interfaces\PostRepositoryInterface!' + ), + ] + ); + } + + if (!$this->beforeRevive()) { return PodiumResponse::error(); } diff --git a/src/Services/Post/PostBuilder.php b/src/Services/Post/PostBuilder.php index eb5d893..a5d6915 100644 --- a/src/Services/Post/PostBuilder.php +++ b/src/Services/Post/PostBuilder.php @@ -4,6 +4,7 @@ namespace Podium\Api\Services\Post; +use InvalidArgumentException; use Podium\Api\Components\PodiumResponse; use Podium\Api\Events\BuildEvent; use Podium\Api\Interfaces\CategorisedBuilderInterface; @@ -46,11 +47,27 @@ public function create( RepositoryInterface $thread, array $data = [] ): PodiumResponse { - if ( - !$post instanceof PostRepositoryInterface - || !$thread instanceof ThreadRepositoryInterface - || !$this->beforeCreate() - ) { + if (!$post instanceof PostRepositoryInterface) { + return PodiumResponse::error( + [ + 'exception' => new InvalidArgumentException( + 'Post must be instance of Podium\Api\Interfaces\PostRepositoryInterface!' + ), + ] + ); + } + + if (!$thread instanceof ThreadRepositoryInterface) { + return PodiumResponse::error( + [ + 'exception' => new InvalidArgumentException( + 'Thread must be instance of Podium\Api\Interfaces\ThreadRepositoryInterface!' + ), + ] + ); + } + + if (!$this->beforeCreate()) { return PodiumResponse::error(); } @@ -111,7 +128,17 @@ private function beforeEdit(): bool */ public function edit(RepositoryInterface $post, array $data = []): PodiumResponse { - if (!$post instanceof PostRepositoryInterface || !$this->beforeEdit()) { + if (!$post instanceof PostRepositoryInterface) { + return PodiumResponse::error( + [ + 'exception' => new InvalidArgumentException( + 'Post must be instance of Podium\Api\Interfaces\PostRepositoryInterface!' + ), + ] + ); + } + + if (!$this->beforeEdit()) { return PodiumResponse::error(); } diff --git a/src/Services/Post/PostMover.php b/src/Services/Post/PostMover.php index 3f2884f..ff5d272 100644 --- a/src/Services/Post/PostMover.php +++ b/src/Services/Post/PostMover.php @@ -4,6 +4,7 @@ namespace Podium\Api\Services\Post; +use InvalidArgumentException; use Podium\Api\Components\PodiumResponse; use Podium\Api\Events\MoveEvent; use Podium\Api\Interfaces\ForumRepositoryInterface; @@ -39,11 +40,27 @@ private function beforeMove(): bool */ public function move(RepositoryInterface $post, RepositoryInterface $thread): PodiumResponse { - if ( - !$post instanceof PostRepositoryInterface - || !$thread instanceof ThreadRepositoryInterface - || !$this->beforeMove() - ) { + if (!$post instanceof PostRepositoryInterface) { + return PodiumResponse::error( + [ + 'exception' => new InvalidArgumentException( + 'Post must be instance of Podium\Api\Interfaces\PostRepositoryInterface!' + ), + ] + ); + } + + if (!$thread instanceof ThreadRepositoryInterface) { + return PodiumResponse::error( + [ + 'exception' => new InvalidArgumentException( + 'Thread must be instance of Podium\Api\Interfaces\ThreadRepositoryInterface!' + ), + ] + ); + } + + if (!$this->beforeMove()) { return PodiumResponse::error(); } diff --git a/src/Services/Post/PostPinner.php b/src/Services/Post/PostPinner.php index ac60619..1a5ae0b 100644 --- a/src/Services/Post/PostPinner.php +++ b/src/Services/Post/PostPinner.php @@ -4,6 +4,7 @@ namespace Podium\Api\Services\Post; +use InvalidArgumentException; use Podium\Api\Components\PodiumResponse; use Podium\Api\Events\PinEvent; use Podium\Api\Interfaces\PinnerInterface; @@ -38,7 +39,17 @@ private function beforePin(): bool */ public function pin(RepositoryInterface $post): PodiumResponse { - if (!$post instanceof PostRepositoryInterface || !$this->beforePin()) { + if (!$post instanceof PostRepositoryInterface) { + return PodiumResponse::error( + [ + 'exception' => new InvalidArgumentException( + 'Post must be instance of Podium\Api\Interfaces\PostRepositoryInterface!' + ), + ] + ); + } + + if (!$this->beforePin()) { return PodiumResponse::error(); } @@ -90,7 +101,17 @@ private function beforeUnpin(): bool */ public function unpin(RepositoryInterface $post): PodiumResponse { - if (!$post instanceof PostRepositoryInterface || !$this->beforeUnpin()) { + if (!$post instanceof PostRepositoryInterface) { + return PodiumResponse::error( + [ + 'exception' => new InvalidArgumentException( + 'Post must be instance of Podium\Api\Interfaces\PostRepositoryInterface!' + ), + ] + ); + } + + if (!$this->beforeUnpin()) { return PodiumResponse::error(); } diff --git a/src/Services/Post/PostRemover.php b/src/Services/Post/PostRemover.php index adcf9e5..ae01bd7 100644 --- a/src/Services/Post/PostRemover.php +++ b/src/Services/Post/PostRemover.php @@ -4,6 +4,7 @@ namespace Podium\Api\Services\Post; +use InvalidArgumentException; use Podium\Api\Components\PodiumResponse; use Podium\Api\Events\RemoveEvent; use Podium\Api\Interfaces\ForumRepositoryInterface; @@ -36,7 +37,17 @@ private function beforeRemove(): bool */ public function remove(RepositoryInterface $post): PodiumResponse { - if (!$post instanceof PostRepositoryInterface || !$this->beforeRemove()) { + if (!$post instanceof PostRepositoryInterface) { + return PodiumResponse::error( + [ + 'exception' => new InvalidArgumentException( + 'Post must be instance of Podium\Api\Interfaces\PostRepositoryInterface!' + ), + ] + ); + } + + if (!$this->beforeRemove()) { return PodiumResponse::error(); } diff --git a/src/Services/Rank/RankBuilder.php b/src/Services/Rank/RankBuilder.php index bb1db2a..d1286f7 100644 --- a/src/Services/Rank/RankBuilder.php +++ b/src/Services/Rank/RankBuilder.php @@ -4,6 +4,7 @@ namespace Podium\Api\Services\Rank; +use InvalidArgumentException; use Podium\Api\Components\PodiumResponse; use Podium\Api\Events\BuildEvent; use Podium\Api\Interfaces\BuilderInterface; @@ -38,7 +39,17 @@ private function beforeCreate(): bool */ public function create(RepositoryInterface $rank, array $data = []): PodiumResponse { - if (!$rank instanceof RankRepositoryInterface || !$this->beforeCreate()) { + if (!$rank instanceof RankRepositoryInterface) { + return PodiumResponse::error( + [ + 'exception' => new InvalidArgumentException( + 'Rank must be instance of Podium\Api\Interfaces\RankRepositoryInterface!' + ), + ] + ); + } + + if (!$this->beforeCreate()) { return PodiumResponse::error(); } @@ -90,7 +101,17 @@ private function beforeEdit(): bool */ public function edit(RepositoryInterface $rank, array $data = []): PodiumResponse { - if (!$rank instanceof RankRepositoryInterface || !$this->beforeEdit()) { + if (!$rank instanceof RankRepositoryInterface) { + return PodiumResponse::error( + [ + 'exception' => new InvalidArgumentException( + 'Rank must be instance of Podium\Api\Interfaces\RankRepositoryInterface!' + ), + ] + ); + } + + if (!$this->beforeEdit()) { return PodiumResponse::error(); } diff --git a/src/Services/Rank/RankRemover.php b/src/Services/Rank/RankRemover.php index 6b24390..8fb1d5b 100644 --- a/src/Services/Rank/RankRemover.php +++ b/src/Services/Rank/RankRemover.php @@ -4,6 +4,7 @@ namespace Podium\Api\Services\Rank; +use InvalidArgumentException; use Podium\Api\Components\PodiumResponse; use Podium\Api\Events\RemoveEvent; use Podium\Api\Interfaces\RankRepositoryInterface; @@ -36,7 +37,17 @@ private function beforeRemove(): bool */ public function remove(RepositoryInterface $rank): PodiumResponse { - if (!$rank instanceof RankRepositoryInterface || !$this->beforeRemove()) { + if (!$rank instanceof RankRepositoryInterface) { + return PodiumResponse::error( + [ + 'exception' => new InvalidArgumentException( + 'Rank must be instance of Podium\Api\Interfaces\RankRepositoryInterface!' + ), + ] + ); + } + + if (!$this->beforeRemove()) { return PodiumResponse::error(); } diff --git a/src/Services/Thread/ThreadArchiver.php b/src/Services/Thread/ThreadArchiver.php index e467f1b..61e7745 100644 --- a/src/Services/Thread/ThreadArchiver.php +++ b/src/Services/Thread/ThreadArchiver.php @@ -4,6 +4,7 @@ namespace Podium\Api\Services\Thread; +use InvalidArgumentException; use Podium\Api\Components\PodiumResponse; use Podium\Api\Events\ArchiveEvent; use Podium\Api\Interfaces\ArchiverInterface; @@ -38,7 +39,17 @@ private function beforeArchive(): bool */ public function archive(RepositoryInterface $thread): PodiumResponse { - if (!$thread instanceof ThreadRepositoryInterface || !$this->beforeArchive()) { + if (!$thread instanceof ThreadRepositoryInterface) { + return PodiumResponse::error( + [ + 'exception' => new InvalidArgumentException( + 'Thread must be instance of Podium\Api\Interfaces\ThreadRepositoryInterface!' + ), + ] + ); + } + + if (!$this->beforeArchive()) { return PodiumResponse::error(); } @@ -94,7 +105,17 @@ private function beforeRevive(): bool */ public function revive(RepositoryInterface $thread): PodiumResponse { - if (!$thread instanceof ThreadRepositoryInterface || !$this->beforeRevive()) { + if (!$thread instanceof ThreadRepositoryInterface) { + return PodiumResponse::error( + [ + 'exception' => new InvalidArgumentException( + 'Thread must be instance of Podium\Api\Interfaces\ThreadRepositoryInterface!' + ), + ] + ); + } + + if (!$this->beforeRevive()) { return PodiumResponse::error(); } diff --git a/src/Services/Thread/ThreadBuilder.php b/src/Services/Thread/ThreadBuilder.php index ca4d083..a4d97c6 100644 --- a/src/Services/Thread/ThreadBuilder.php +++ b/src/Services/Thread/ThreadBuilder.php @@ -4,6 +4,7 @@ namespace Podium\Api\Services\Thread; +use InvalidArgumentException; use Podium\Api\Components\PodiumResponse; use Podium\Api\Events\BuildEvent; use Podium\Api\Interfaces\CategorisedBuilderInterface; @@ -45,11 +46,27 @@ public function create( RepositoryInterface $forum, array $data = [] ): PodiumResponse { - if ( - !$thread instanceof ThreadRepositoryInterface - || !$forum instanceof ForumRepositoryInterface - || !$this->beforeCreate() - ) { + if (!$thread instanceof ThreadRepositoryInterface) { + return PodiumResponse::error( + [ + 'exception' => new InvalidArgumentException( + 'Thread must be instance of Podium\Api\Interfaces\ThreadRepositoryInterface!' + ), + ] + ); + } + + if (!$forum instanceof ForumRepositoryInterface) { + return PodiumResponse::error( + [ + 'exception' => new InvalidArgumentException( + 'Forum must be instance of Podium\Api\Interfaces\ForumRepositoryInterface!' + ), + ] + ); + } + + if (!$this->beforeCreate()) { return PodiumResponse::error(); } @@ -105,7 +122,17 @@ private function beforeEdit(): bool */ public function edit(RepositoryInterface $thread, array $data = []): PodiumResponse { - if (!$thread instanceof ThreadRepositoryInterface || !$this->beforeEdit()) { + if (!$thread instanceof ThreadRepositoryInterface) { + return PodiumResponse::error( + [ + 'exception' => new InvalidArgumentException( + 'Thread must be instance of Podium\Api\Interfaces\ThreadRepositoryInterface!' + ), + ] + ); + } + + if (!$this->beforeEdit()) { return PodiumResponse::error(); } diff --git a/src/Services/Thread/ThreadMover.php b/src/Services/Thread/ThreadMover.php index 70944f9..62a6532 100644 --- a/src/Services/Thread/ThreadMover.php +++ b/src/Services/Thread/ThreadMover.php @@ -4,6 +4,7 @@ namespace Podium\Api\Services\Thread; +use InvalidArgumentException; use Podium\Api\Components\PodiumResponse; use Podium\Api\Events\MoveEvent; use Podium\Api\Interfaces\ForumRepositoryInterface; @@ -38,11 +39,27 @@ private function beforeMove(): bool */ public function move(RepositoryInterface $thread, RepositoryInterface $forum): PodiumResponse { - if ( - !$thread instanceof ThreadRepositoryInterface - || !$forum instanceof ForumRepositoryInterface - || !$this->beforeMove() - ) { + if (!$thread instanceof ThreadRepositoryInterface) { + return PodiumResponse::error( + [ + 'exception' => new InvalidArgumentException( + 'Thread must be instance of Podium\Api\Interfaces\ThreadRepositoryInterface!' + ), + ] + ); + } + + if (!$forum instanceof ForumRepositoryInterface) { + return PodiumResponse::error( + [ + 'exception' => new InvalidArgumentException( + 'Forum must be instance of Podium\Api\Interfaces\ForumRepositoryInterface!' + ), + ] + ); + } + + if (!$this->beforeMove()) { return PodiumResponse::error(); } diff --git a/src/Services/Thread/ThreadPinner.php b/src/Services/Thread/ThreadPinner.php index cb04d19..8935d0e 100644 --- a/src/Services/Thread/ThreadPinner.php +++ b/src/Services/Thread/ThreadPinner.php @@ -4,6 +4,7 @@ namespace Podium\Api\Services\Thread; +use InvalidArgumentException; use Podium\Api\Components\PodiumResponse; use Podium\Api\Events\PinEvent; use Podium\Api\Interfaces\PinnerInterface; @@ -38,7 +39,17 @@ private function beforePin(): bool */ public function pin(RepositoryInterface $thread): PodiumResponse { - if (!$thread instanceof ThreadRepositoryInterface || !$this->beforePin()) { + if (!$thread instanceof ThreadRepositoryInterface) { + return PodiumResponse::error( + [ + 'exception' => new InvalidArgumentException( + 'Thread must be instance of Podium\Api\Interfaces\ThreadRepositoryInterface!' + ), + ] + ); + } + + if (!$this->beforePin()) { return PodiumResponse::error(); } @@ -90,7 +101,17 @@ private function beforeUnpin(): bool */ public function unpin(RepositoryInterface $thread): PodiumResponse { - if (!$thread instanceof ThreadRepositoryInterface || !$this->beforeUnpin()) { + if (!$thread instanceof ThreadRepositoryInterface) { + return PodiumResponse::error( + [ + 'exception' => new InvalidArgumentException( + 'Thread must be instance of Podium\Api\Interfaces\ThreadRepositoryInterface!' + ), + ] + ); + } + + if (!$this->beforeUnpin()) { return PodiumResponse::error(); } diff --git a/src/Services/Thread/ThreadRemover.php b/src/Services/Thread/ThreadRemover.php index 77a4fe4..6beec36 100644 --- a/src/Services/Thread/ThreadRemover.php +++ b/src/Services/Thread/ThreadRemover.php @@ -4,6 +4,7 @@ namespace Podium\Api\Services\Thread; +use InvalidArgumentException; use Podium\Api\Components\PodiumResponse; use Podium\Api\Events\RemoveEvent; use Podium\Api\Interfaces\ForumRepositoryInterface; @@ -38,7 +39,17 @@ private function beforeRemove(): bool */ public function remove(RepositoryInterface $thread): PodiumResponse { - if (!$thread instanceof ThreadRepositoryInterface || !$this->beforeRemove()) { + if (!$thread instanceof ThreadRepositoryInterface) { + return PodiumResponse::error( + [ + 'exception' => new InvalidArgumentException( + 'Thread must be instance of Podium\Api\Interfaces\ThreadRepositoryInterface!' + ), + ] + ); + } + + if (!$this->beforeRemove()) { return PodiumResponse::error(); } diff --git a/tests/Functional/Forum/ForumMoverTest.php b/tests/Functional/Forum/ForumMoverTest.php index 61a0a0e..c312571 100644 --- a/tests/Functional/Forum/ForumMoverTest.php +++ b/tests/Functional/Forum/ForumMoverTest.php @@ -30,12 +30,14 @@ public function testMoveShouldTriggerBeforeAndAfterEventsWhenMovingIsDone(): voi $this->eventsRaised[ForumMover::EVENT_BEFORE_MOVING] = $event instanceof MoveEvent; }; Event::on(ForumMover::class, ForumMover::EVENT_BEFORE_MOVING, $beforeHandler); - $afterHandler = function () { - $this->eventsRaised[ForumMover::EVENT_AFTER_MOVING] = true; + $afterHandler = function ($event) { + $this->eventsRaised[ForumMover::EVENT_AFTER_MOVING] = $event instanceof MoveEvent + && 11 === $event->repository->getId(); }; Event::on(ForumMover::class, ForumMover::EVENT_AFTER_MOVING, $afterHandler); $forum = $this->createMock(ForumRepositoryInterface::class); + $forum->method('getId')->willReturn(11); $forum->method('move')->willReturn(true); $this->service->move($forum, $this->createMock(CategoryRepositoryInterface::class)); diff --git a/tests/Functional/Group/GroupKeeperTest.php b/tests/Functional/Group/GroupKeeperTest.php index 7de4d35..9f21a3a 100644 --- a/tests/Functional/Group/GroupKeeperTest.php +++ b/tests/Functional/Group/GroupKeeperTest.php @@ -32,7 +32,8 @@ public function testJoinShouldTriggerBeforeAndAfterEventsWhenCreatingGroupMember }; Event::on(GroupKeeper::class, GroupKeeper::EVENT_BEFORE_JOINING, $beforeHandler); $afterHandler = function ($event) { - $this->eventsRaised[GroupKeeper::EVENT_AFTER_JOINING] = $event instanceof GroupEvent; + $this->eventsRaised[GroupKeeper::EVENT_AFTER_JOINING] = $event instanceof GroupEvent + && $event->repository instanceof GroupMemberRepositoryInterface; }; Event::on(GroupKeeper::class, GroupKeeper::EVENT_AFTER_JOINING, $afterHandler); diff --git a/tests/Functional/Member/MemberAcquaintanceTest.php b/tests/Functional/Member/MemberAcquaintanceTest.php index 55ff66f..c0663e4 100644 --- a/tests/Functional/Member/MemberAcquaintanceTest.php +++ b/tests/Functional/Member/MemberAcquaintanceTest.php @@ -31,7 +31,8 @@ public function testBefriendShouldTriggerBeforeAndAfterEventsWhenBefriendingIsDo }; Event::on(MemberAcquaintance::class, MemberAcquaintance::EVENT_BEFORE_BEFRIENDING, $beforeHandler); $afterHandler = function ($event) { - $this->eventsRaised[MemberAcquaintance::EVENT_AFTER_BEFRIENDING] = $event instanceof AcquaintanceEvent; + $this->eventsRaised[MemberAcquaintance::EVENT_AFTER_BEFRIENDING] = $event instanceof AcquaintanceEvent + && $event->repository instanceof AcquaintanceRepositoryInterface; }; Event::on(MemberAcquaintance::class, MemberAcquaintance::EVENT_AFTER_BEFRIENDING, $afterHandler); @@ -234,7 +235,8 @@ public function testIgnoreShouldTriggerBeforeAndAfterEventsWhenIgnoringIsDone(): }; Event::on(MemberAcquaintance::class, MemberAcquaintance::EVENT_BEFORE_IGNORING, $beforeHandler); $afterHandler = function ($event) { - $this->eventsRaised[MemberAcquaintance::EVENT_AFTER_IGNORING] = $event instanceof AcquaintanceEvent; + $this->eventsRaised[MemberAcquaintance::EVENT_AFTER_IGNORING] = $event instanceof AcquaintanceEvent + && $event->repository instanceof AcquaintanceRepositoryInterface; }; Event::on(MemberAcquaintance::class, MemberAcquaintance::EVENT_AFTER_IGNORING, $afterHandler); diff --git a/tests/Functional/Post/PostLikerTest.php b/tests/Functional/Post/PostLikerTest.php index e839e3b..217261d 100644 --- a/tests/Functional/Post/PostLikerTest.php +++ b/tests/Functional/Post/PostLikerTest.php @@ -32,7 +32,8 @@ public function testThumbUpShouldTriggerBeforeAndAfterEventsWhenUpIsDone(): void }; Event::on(PostLiker::class, PostLiker::EVENT_BEFORE_THUMB_UP, $beforeHandler); $afterHandler = function ($event) { - $this->eventsRaised[PostLiker::EVENT_AFTER_THUMB_UP] = $event instanceof ThumbEvent; + $this->eventsRaised[PostLiker::EVENT_AFTER_THUMB_UP] = $event instanceof ThumbEvent + && $event->repository instanceof ThumbRepositoryInterface; }; Event::on(PostLiker::class, PostLiker::EVENT_AFTER_THUMB_UP, $afterHandler); @@ -131,7 +132,8 @@ public function testThumbDownShouldTriggerBeforeAndAfterEventsWhenDownIsDone(): }; Event::on(PostLiker::class, PostLiker::EVENT_BEFORE_THUMB_DOWN, $beforeHandler); $afterHandler = function ($event) { - $this->eventsRaised[PostLiker::EVENT_AFTER_THUMB_DOWN] = $event instanceof ThumbEvent; + $this->eventsRaised[PostLiker::EVENT_AFTER_THUMB_DOWN] = $event instanceof ThumbEvent + && $event->repository instanceof ThumbRepositoryInterface; }; Event::on(PostLiker::class, PostLiker::EVENT_AFTER_THUMB_DOWN, $afterHandler); diff --git a/tests/Functional/Post/PostMoverTest.php b/tests/Functional/Post/PostMoverTest.php index 73ef75c..865b2b2 100644 --- a/tests/Functional/Post/PostMoverTest.php +++ b/tests/Functional/Post/PostMoverTest.php @@ -31,13 +31,15 @@ public function testMoveShouldTriggerBeforeAndAfterEventsWhenMovingIsDone(): voi $this->eventsRaised[PostMover::EVENT_BEFORE_MOVING] = $event instanceof MoveEvent; }; Event::on(PostMover::class, PostMover::EVENT_BEFORE_MOVING, $beforeHandler); - $afterHandler = function () { - $this->eventsRaised[PostMover::EVENT_AFTER_MOVING] = true; + $afterHandler = function ($event) { + $this->eventsRaised[PostMover::EVENT_AFTER_MOVING] = $event instanceof MoveEvent + && 77 === $event->repository->getId(); }; Event::on(PostMover::class, PostMover::EVENT_AFTER_MOVING, $afterHandler); $post = $this->createMock(PostRepositoryInterface::class); $post->method('move')->willReturn(true); + $post->method('getId')->willReturn(77); $thread = $this->createMock(ThreadRepositoryInterface::class); $thread->method('updateCounters')->willReturn(true); $forum = $this->createMock(ForumRepositoryInterface::class); diff --git a/tests/Functional/Thread/ThreadBookmarkerTest.php b/tests/Functional/Thread/ThreadBookmarkerTest.php index b11c0ae..0570937 100644 --- a/tests/Functional/Thread/ThreadBookmarkerTest.php +++ b/tests/Functional/Thread/ThreadBookmarkerTest.php @@ -33,7 +33,8 @@ public function testMarkShouldTriggerBeforeAndAfterEventsWhenMarkingIsDone(): vo }; Event::on(ThreadBookmarker::class, ThreadBookmarker::EVENT_BEFORE_MARKING, $beforeHandler); $afterHandler = function ($event) { - $this->eventsRaised[ThreadBookmarker::EVENT_AFTER_MARKING] = $event instanceof BookmarkEvent; + $this->eventsRaised[ThreadBookmarker::EVENT_AFTER_MARKING] = $event instanceof BookmarkEvent + && $event->repository instanceof BookmarkRepositoryInterface; }; Event::on(ThreadBookmarker::class, ThreadBookmarker::EVENT_AFTER_MARKING, $afterHandler); diff --git a/tests/Functional/Thread/ThreadMoverTest.php b/tests/Functional/Thread/ThreadMoverTest.php index d86e8cf..d5f9d9a 100644 --- a/tests/Functional/Thread/ThreadMoverTest.php +++ b/tests/Functional/Thread/ThreadMoverTest.php @@ -30,13 +30,15 @@ public function testMoveShouldTriggerBeforeAndAfterEventsWhenMovingIsDone(): voi $this->eventsRaised[ThreadMover::EVENT_BEFORE_MOVING] = $event instanceof MoveEvent; }; Event::on(ThreadMover::class, ThreadMover::EVENT_BEFORE_MOVING, $beforeHandler); - $afterHandler = function () { - $this->eventsRaised[ThreadMover::EVENT_AFTER_MOVING] = true; + $afterHandler = function ($event) { + $this->eventsRaised[ThreadMover::EVENT_AFTER_MOVING] = $event instanceof MoveEvent + && 54 === $event->repository->getId(); }; Event::on(ThreadMover::class, ThreadMover::EVENT_AFTER_MOVING, $afterHandler); $thread = $this->createMock(ThreadRepositoryInterface::class); $thread->method('move')->willReturn(true); + $thread->method('getId')->willReturn(54); $forum = $this->createMock(ForumRepositoryInterface::class); $forum->method('updateCounters')->willReturn(true); $thread->method('getParent')->willReturn($forum); diff --git a/tests/Functional/Thread/ThreadSubscriberTest.php b/tests/Functional/Thread/ThreadSubscriberTest.php index 90fd7b8..b74cbd4 100644 --- a/tests/Functional/Thread/ThreadSubscriberTest.php +++ b/tests/Functional/Thread/ThreadSubscriberTest.php @@ -32,7 +32,8 @@ public function testSubscribeShouldTriggerBeforeAndAfterEventsWhenSubscribingIsD }; Event::on(ThreadSubscriber::class, ThreadSubscriber::EVENT_BEFORE_SUBSCRIBING, $beforeHandler); $afterHandler = function ($event) { - $this->eventsRaised[ThreadSubscriber::EVENT_AFTER_SUBSCRIBING] = $event instanceof SubscriptionEvent; + $this->eventsRaised[ThreadSubscriber::EVENT_AFTER_SUBSCRIBING] = $event instanceof SubscriptionEvent + && $event->repository instanceof SubscriptionRepositoryInterface; }; Event::on(ThreadSubscriber::class, ThreadSubscriber::EVENT_AFTER_SUBSCRIBING, $afterHandler); diff --git a/tests/Unit/Category/CategoryArchiverTest.php b/tests/Unit/Category/CategoryArchiverTest.php index b6d415c..c24c7aa 100644 --- a/tests/Unit/Category/CategoryArchiverTest.php +++ b/tests/Unit/Category/CategoryArchiverTest.php @@ -27,7 +27,10 @@ public function testArchiveShouldReturnErrorWhenRepositoryIsWrong(): void $result = $this->service->archive($this->createMock(RepositoryInterface::class)); self::assertFalse($result->getResult()); - self::assertEmpty($result->getErrors()); + self::assertSame( + 'Category must be instance of Podium\Api\Interfaces\CategoryRepositoryInterface!', + $result->getErrors()['exception']->getMessage() + ); } public function testArchiveShouldReturnErrorWhenArchivingErrored(): void @@ -97,7 +100,10 @@ public function testReviveShouldReturnErrorWhenRepositoryIsWrong(): void $result = $this->service->revive($this->createMock(RepositoryInterface::class)); self::assertFalse($result->getResult()); - self::assertEmpty($result->getErrors()); + self::assertSame( + 'Category must be instance of Podium\Api\Interfaces\CategoryRepositoryInterface!', + $result->getErrors()['exception']->getMessage() + ); } public function testReviveShouldReturnErrorWhenRevivingErrored(): void diff --git a/tests/Unit/Category/CategoryRemoverTest.php b/tests/Unit/Category/CategoryRemoverTest.php index 568f168..b3a03c1 100644 --- a/tests/Unit/Category/CategoryRemoverTest.php +++ b/tests/Unit/Category/CategoryRemoverTest.php @@ -25,7 +25,10 @@ public function testRemoveShouldReturnErrorWhenRepositoryIsWrong(): void $result = $this->service->remove($this->createMock(RepositoryInterface::class)); self::assertFalse($result->getResult()); - self::assertEmpty($result->getErrors()); + self::assertSame( + 'Category must be instance of Podium\Api\Interfaces\CategoryRepositoryInterface!', + $result->getErrors()['exception']->getMessage() + ); } public function testRemoveShouldReturnErrorWhenRemovingErrored(): void diff --git a/tests/Unit/Category/CategorySorterTest.php b/tests/Unit/Category/CategorySorterTest.php index c6aa0b4..b815ab7 100644 --- a/tests/Unit/Category/CategorySorterTest.php +++ b/tests/Unit/Category/CategorySorterTest.php @@ -28,7 +28,10 @@ public function testReplaceShouldReturnErrorWhenFirstRepositoryIsWrong(): void ); self::assertFalse($result->getResult()); - self::assertEmpty($result->getErrors()); + self::assertSame( + 'First category must be instance of Podium\Api\Interfaces\CategoryRepositoryInterface!', + $result->getErrors()['exception']->getMessage() + ); } public function testReplaceShouldReturnErrorWhenSecondRepositoryIsWrong(): void @@ -39,49 +42,76 @@ public function testReplaceShouldReturnErrorWhenSecondRepositoryIsWrong(): void ); self::assertFalse($result->getResult()); - self::assertEmpty($result->getErrors()); + self::assertSame( + 'Second category must be instance of Podium\Api\Interfaces\CategoryRepositoryInterface!', + $result->getErrors()['exception']->getMessage() + ); + } + + public function testReplaceShouldReturnSuccessWhenReplacingIsDone(): void + { + $this->transaction->expects(self::once())->method('commit'); + + $category = $this->createMock(CategoryRepositoryInterface::class); + $category->method('getOrder')->willReturn(1); + $category->method('setOrder')->willReturn(true); + $result = $this->service->replace($category, $category); + + self::assertTrue($result->getResult()); } - public function testReplaceShouldReturnErrorWhenSettingFirstOrderErrored(): void + public function testReplaceShouldReturnErrorWhenReplacingThrowsException(): void { $this->transaction->expects(self::once())->method('rollBack'); + $this->logger->expects(self::once())->method('log')->with( + self::callback( + static function (array $data) { + return 3 === count($data) + && 'Exception while replacing categories order' === $data[0] + && 'exc' === $data[1]; + } + ), + 1, + 'podium' + ); $category = $this->createMock(CategoryRepositoryInterface::class); $category->method('getOrder')->willReturn(1); - $category->method('setOrder')->willReturn(false); + $category->method('setOrder')->willThrowException(new Exception('exc')); $result = $this->service->replace($category, $category); self::assertFalse($result->getResult()); + self::assertSame('exc', $result->getErrors()['exception']->getMessage()); } - public function testReplaceShouldReturnErrorWhenSettingSecondOrderErrored(): void + public function testReplaceShouldReturnErrorWhenSettingFirstCategoryOrderErrored(): void { $this->transaction->expects(self::once())->method('rollBack'); + $this->logger->expects(self::once())->method('log')->with( + self::callback( + static function (array $data) { + return 3 === count($data) + && 'Exception while replacing categories order' === $data[0] + && 'Error while setting new category order!' === $data[1]; + } + ), + 1, + 'podium' + ); $category1 = $this->createMock(CategoryRepositoryInterface::class); $category1->method('getOrder')->willReturn(1); - $category1->method('setOrder')->willReturn(true); + $category1->method('setOrder')->willReturn(false); $category2 = $this->createMock(CategoryRepositoryInterface::class); $category2->method('getOrder')->willReturn(2); - $category2->method('setOrder')->willReturn(false); + $category2->method('setOrder')->willReturn(true); $result = $this->service->replace($category1, $category2); self::assertFalse($result->getResult()); + self::assertSame('Error while setting new category order!', $result->getErrors()['exception']->getMessage()); } - public function testReplaceShouldReturnSuccessWhenReplacingIsDone(): void - { - $this->transaction->expects(self::once())->method('commit'); - - $category = $this->createMock(CategoryRepositoryInterface::class); - $category->method('getOrder')->willReturn(1); - $category->method('setOrder')->willReturn(true); - $result = $this->service->replace($category, $category); - - self::assertTrue($result->getResult()); - } - - public function testReplaceShouldReturnErrorWhenReplacingThrowsException(): void + public function testReplaceShouldReturnErrorWhenSettingSecondCategoryOrderErrored(): void { $this->transaction->expects(self::once())->method('rollBack'); $this->logger->expects(self::once())->method('log')->with( @@ -89,20 +119,34 @@ public function testReplaceShouldReturnErrorWhenReplacingThrowsException(): void static function (array $data) { return 3 === count($data) && 'Exception while replacing categories order' === $data[0] - && 'exc' === $data[1]; + && 'Error while setting new category order!' === $data[1]; } ), 1, 'podium' ); - $category = $this->createMock(CategoryRepositoryInterface::class); - $category->method('getOrder')->willReturn(1); - $category->method('setOrder')->willThrowException(new Exception('exc')); - $result = $this->service->replace($category, $category); + $category1 = $this->createMock(CategoryRepositoryInterface::class); + $category1->method('getOrder')->willReturn(1); + $category1->method('setOrder')->willReturn(true); + $category2 = $this->createMock(CategoryRepositoryInterface::class); + $category2->method('getOrder')->willReturn(2); + $category2->method('setOrder')->willReturn(false); + $result = $this->service->replace($category1, $category2); self::assertFalse($result->getResult()); - self::assertSame('exc', $result->getErrors()['exception']->getMessage()); + self::assertSame('Error while setting new category order!', $result->getErrors()['exception']->getMessage()); + } + + public function testSortShouldReturnErrorWhenRepositoryIsWrong(): void + { + $result = $this->service->sort($this->createMock(RepositoryInterface::class)); + + self::assertFalse($result->getResult()); + self::assertSame( + 'Category must be instance of Podium\Api\Interfaces\CategoryRepositoryInterface!', + $result->getErrors()['exception']->getMessage() + ); } public function testSortShouldReturnErrorWhenSortingErrored(): void diff --git a/tests/Unit/Forum/ForumArchiverTest.php b/tests/Unit/Forum/ForumArchiverTest.php index 8d361fb..7d7f030 100644 --- a/tests/Unit/Forum/ForumArchiverTest.php +++ b/tests/Unit/Forum/ForumArchiverTest.php @@ -25,7 +25,10 @@ public function testArchiveShouldReturnErrorWhenRepositoryIsWrong(): void $result = $this->service->archive($this->createMock(RepositoryInterface::class)); self::assertFalse($result->getResult()); - self::assertEmpty($result->getErrors()); + self::assertSame( + 'Forum must be instance of Podium\Api\Interfaces\ForumRepositoryInterface!', + $result->getErrors()['exception']->getMessage() + ); } public function testArchiveShouldReturnErrorWhenArchivingErrored(): void @@ -92,7 +95,10 @@ public function testReviveShouldReturnErrorWhenRepositoryIsWrong(): void $result = $this->service->revive($this->createMock(RepositoryInterface::class)); self::assertFalse($result->getResult()); - self::assertEmpty($result->getErrors()); + self::assertSame( + 'Forum must be instance of Podium\Api\Interfaces\ForumRepositoryInterface!', + $result->getErrors()['exception']->getMessage() + ); } public function testReviveShouldReturnErrorWhenRevivingErrored(): void diff --git a/tests/Unit/Forum/ForumBuilderTest.php b/tests/Unit/Forum/ForumBuilderTest.php index 11ef5ed..81572dd 100644 --- a/tests/Unit/Forum/ForumBuilderTest.php +++ b/tests/Unit/Forum/ForumBuilderTest.php @@ -22,15 +22,34 @@ protected function setUp(): void $this->service = new ForumBuilder(); } - public function testCreateShouldReturnErrorWhenRepositoryIsWrong(): void + public function testCreateShouldReturnErrorWhenForumRepositoryIsWrong(): void { $result = $this->service->create( $this->createMock(RepositoryInterface::class), $this->createMock(MemberRepositoryInterface::class), + $this->createMock(CategoryRepositoryInterface::class) + ); + + self::assertFalse($result->getResult()); + self::assertSame( + 'Forum must be instance of Podium\Api\Interfaces\ForumRepositoryInterface!', + $result->getErrors()['exception']->getMessage() + ); + } + + public function testCreateShouldReturnErrorWhenCategoryRepositoryIsWrong(): void + { + $result = $this->service->create( + $this->createMock(ForumRepositoryInterface::class), + $this->createMock(MemberRepositoryInterface::class), $this->createMock(RepositoryInterface::class) ); self::assertFalse($result->getResult()); + self::assertSame( + 'Category must be instance of Podium\Api\Interfaces\CategoryRepositoryInterface!', + $result->getErrors()['exception']->getMessage() + ); } public function testCreateShouldReturnErrorWhenCreatingErrored(): void @@ -95,6 +114,10 @@ public function testEditShouldReturnErrorWhenRepositoryIsWrong(): void $result = $this->service->edit($this->createMock(RepositoryInterface::class)); self::assertFalse($result->getResult()); + self::assertSame( + 'Forum must be instance of Podium\Api\Interfaces\ForumRepositoryInterface!', + $result->getErrors()['exception']->getMessage() + ); } public function testEditShouldReturnErrorWhenEditingErrored(): void diff --git a/tests/Unit/Forum/ForumMoverTest.php b/tests/Unit/Forum/ForumMoverTest.php index d164b8d..e8b0135 100644 --- a/tests/Unit/Forum/ForumMoverTest.php +++ b/tests/Unit/Forum/ForumMoverTest.php @@ -29,7 +29,10 @@ public function testMoveShouldReturnErrorWhenForumRepositoryIsWrong(): void ); self::assertFalse($result->getResult()); - self::assertEmpty($result->getErrors()); + self::assertSame( + 'Forum must be instance of Podium\Api\Interfaces\ForumRepositoryInterface!', + $result->getErrors()['exception']->getMessage() + ); } public function testMoveShouldReturnErrorWhenCategoryRepositoryIsWrong(): void @@ -40,7 +43,10 @@ public function testMoveShouldReturnErrorWhenCategoryRepositoryIsWrong(): void ); self::assertFalse($result->getResult()); - self::assertEmpty($result->getErrors()); + self::assertSame( + 'Category must be instance of Podium\Api\Interfaces\CategoryRepositoryInterface!', + $result->getErrors()['exception']->getMessage() + ); } public function testMoveShouldReturnErrorWhenMovingErrored(): void diff --git a/tests/Unit/Forum/ForumRemoverTest.php b/tests/Unit/Forum/ForumRemoverTest.php index c597881..8045458 100644 --- a/tests/Unit/Forum/ForumRemoverTest.php +++ b/tests/Unit/Forum/ForumRemoverTest.php @@ -25,7 +25,10 @@ public function testRemoveShouldReturnErrorWhenRepositoryIsWrong(): void $result = $this->service->remove($this->createMock(RepositoryInterface::class)); self::assertFalse($result->getResult()); - self::assertEmpty($result->getErrors()); + self::assertSame( + 'Forum must be instance of Podium\Api\Interfaces\ForumRepositoryInterface!', + $result->getErrors()['exception']->getMessage() + ); } public function testRemoveShouldReturnErrorWhenRemovingErrored(): void diff --git a/tests/Unit/Forum/ForumSorterTest.php b/tests/Unit/Forum/ForumSorterTest.php index 591e354..d68ace8 100644 --- a/tests/Unit/Forum/ForumSorterTest.php +++ b/tests/Unit/Forum/ForumSorterTest.php @@ -28,7 +28,10 @@ public function testReplaceShouldReturnErrorWhenFirstRepositoryIsWrong(): void ); self::assertFalse($result->getResult()); - self::assertEmpty($result->getErrors()); + self::assertSame( + 'First forum must be instance of Podium\Api\Interfaces\ForumRepositoryInterface!', + $result->getErrors()['exception']->getMessage() + ); } public function testReplaceShouldReturnErrorWhenSecondRepositoryIsWrong(): void @@ -39,24 +42,53 @@ public function testReplaceShouldReturnErrorWhenSecondRepositoryIsWrong(): void ); self::assertFalse($result->getResult()); - self::assertEmpty($result->getErrors()); + self::assertSame( + 'Second forum must be instance of Podium\Api\Interfaces\ForumRepositoryInterface!', + $result->getErrors()['exception']->getMessage() + ); } public function testReplaceShouldReturnErrorWhenSettingFirstOrderErrored(): void { $this->transaction->expects(self::once())->method('rollBack'); + $this->logger->expects(self::once())->method('log')->with( + self::callback( + static function (array $data) { + return 3 === count($data) + && 'Exception while replacing forums order' === $data[0] + && 'Error while setting new forum order!' === $data[1]; + } + ), + 1, + 'podium' + ); - $forum = $this->createMock(ForumRepositoryInterface::class); - $forum->method('getOrder')->willReturn(1); - $forum->method('setOrder')->willReturn(false); - $result = $this->service->replace($forum, $forum); + $forum1 = $this->createMock(ForumRepositoryInterface::class); + $forum1->method('getOrder')->willReturn(1); + $forum1->method('setOrder')->willReturn(false); + $forum2 = $this->createMock(ForumRepositoryInterface::class); + $forum2->method('getOrder')->willReturn(2); + $forum2->method('setOrder')->willReturn(true); + $result = $this->service->replace($forum1, $forum2); self::assertFalse($result->getResult()); + self::assertSame('Error while setting new forum order!', $result->getErrors()['exception']->getMessage()); } public function testReplaceShouldReturnErrorWhenSettingSecondOrderErrored(): void { $this->transaction->expects(self::once())->method('rollBack'); + $this->logger->expects(self::once())->method('log')->with( + self::callback( + static function (array $data) { + return 3 === count($data) + && 'Exception while replacing forums order' === $data[0] + && 'Error while setting new forum order!' === $data[1]; + } + ), + 1, + 'podium' + ); $forum1 = $this->createMock(ForumRepositoryInterface::class); $forum1->method('getOrder')->willReturn(1); @@ -67,6 +99,7 @@ public function testReplaceShouldReturnErrorWhenSettingSecondOrderErrored(): voi $result = $this->service->replace($forum1, $forum2); self::assertFalse($result->getResult()); + self::assertSame('Error while setting new forum order!', $result->getErrors()['exception']->getMessage()); } public function testReplaceShouldReturnSuccessWhenReplacingIsDone(): void @@ -105,6 +138,17 @@ static function (array $data) { self::assertSame('exc', $result->getErrors()['exception']->getMessage()); } + public function testSortShouldReturnErrorWhenRepositoryIsWrong(): void + { + $result = $this->service->sort($this->createMock(RepositoryInterface::class)); + + self::assertFalse($result->getResult()); + self::assertSame( + 'Forum must be instance of Podium\Api\Interfaces\ForumRepositoryInterface!', + $result->getErrors()['exception']->getMessage() + ); + } + public function testSortShouldReturnErrorWhenSortingErrored(): void { $this->transaction->expects(self::once())->method('rollBack'); diff --git a/tests/Unit/Group/GroupBuilderTest.php b/tests/Unit/Group/GroupBuilderTest.php index 1ae17e1..9fbe68d 100644 --- a/tests/Unit/Group/GroupBuilderTest.php +++ b/tests/Unit/Group/GroupBuilderTest.php @@ -38,7 +38,10 @@ public function testCreateShouldReturnErrorWhenRepositoryIsWrong(): void $result = $this->service->create($this->createMock(RepositoryInterface::class)); self::assertFalse($result->getResult()); - self::assertEmpty($result->getErrors()); + self::assertSame( + 'Group must be instance of Podium\Api\Interfaces\GroupRepositoryInterface!', + $result->getErrors()['exception']->getMessage() + ); } public function testCreateShouldReturnSuccessWhenCreatingIsDone(): void @@ -91,7 +94,10 @@ public function testEditShouldReturnErrorWhenRepositoryIsWrong(): void $result = $this->service->edit($this->createMock(RepositoryInterface::class)); self::assertFalse($result->getResult()); - self::assertEmpty($result->getErrors()); + self::assertSame( + 'Group must be instance of Podium\Api\Interfaces\GroupRepositoryInterface!', + $result->getErrors()['exception']->getMessage() + ); } public function testEditShouldReturnSuccessWhenEditingIsDone(): void diff --git a/tests/Unit/Group/GroupRemoverTest.php b/tests/Unit/Group/GroupRemoverTest.php index 564e6c7..abbd524 100644 --- a/tests/Unit/Group/GroupRemoverTest.php +++ b/tests/Unit/Group/GroupRemoverTest.php @@ -25,7 +25,10 @@ public function testRemoveShouldReturnErrorWhenRepositoryIsWrong(): void $result = $this->service->remove($this->createMock(RepositoryInterface::class)); self::assertFalse($result->getResult()); - self::assertEmpty($result->getErrors()); + self::assertSame( + 'Group must be instance of Podium\Api\Interfaces\GroupRepositoryInterface!', + $result->getErrors()['exception']->getMessage() + ); } public function testRemoveShouldReturnErrorWhenRemovingErrored(): void diff --git a/tests/Unit/Logger/LoggerRemoverTest.php b/tests/Unit/Logger/LoggerRemoverTest.php index 37bfd81..b73f1c7 100644 --- a/tests/Unit/Logger/LoggerRemoverTest.php +++ b/tests/Unit/Logger/LoggerRemoverTest.php @@ -25,7 +25,10 @@ public function testRemoveShouldReturnErrorWhenRepositoryIsWrong(): void $result = $this->service->remove($this->createMock(RepositoryInterface::class)); self::assertFalse($result->getResult()); - self::assertEmpty($result->getErrors()); + self::assertSame( + 'Log must be instance of Podium\Api\Interfaces\LogRepositoryInterface!', + $result->getErrors()['exception']->getMessage() + ); } public function testRemoveShouldReturnErrorWhenRemovingErrored(): void diff --git a/tests/Unit/Member/MemberRemoverTest.php b/tests/Unit/Member/MemberRemoverTest.php index 0af1134..dc09083 100644 --- a/tests/Unit/Member/MemberRemoverTest.php +++ b/tests/Unit/Member/MemberRemoverTest.php @@ -25,7 +25,10 @@ public function testRemoveShouldReturnErrorWhenRepositoryIsWrong(): void $result = $this->service->remove($this->createMock(RepositoryInterface::class)); self::assertFalse($result->getResult()); - self::assertEmpty($result->getErrors()); + self::assertSame( + 'Member must be instance of Podium\Api\Interfaces\MemberRepositoryInterface!', + $result->getErrors()['exception']->getMessage() + ); } public function testRemoveShouldReturnErrorWhenRemovingErrored(): void diff --git a/tests/Unit/Poll/PollRemoverTest.php b/tests/Unit/Poll/PollRemoverTest.php index 604dc54..2ba7d59 100644 --- a/tests/Unit/Poll/PollRemoverTest.php +++ b/tests/Unit/Poll/PollRemoverTest.php @@ -26,7 +26,10 @@ public function testRemoveShouldReturnErrorWhenRepositoryIsWrong(): void $result = $this->service->remove($this->createMock(RepositoryInterface::class)); self::assertFalse($result->getResult()); - self::assertEmpty($result->getErrors()); + self::assertSame( + 'Post must be instance of Podium\Api\Interfaces\PollPostRepositoryInterface!', + $result->getErrors()['exception']->getMessage() + ); } public function testRemoveShouldReturnErrorWhenRemovingErrored(): void diff --git a/tests/Unit/Post/PostArchiverTest.php b/tests/Unit/Post/PostArchiverTest.php index 1845b4a..38ce043 100644 --- a/tests/Unit/Post/PostArchiverTest.php +++ b/tests/Unit/Post/PostArchiverTest.php @@ -25,7 +25,10 @@ public function testArchiveShouldReturnErrorWhenRepositoryIsWrong(): void $result = $this->service->archive($this->createMock(RepositoryInterface::class)); self::assertFalse($result->getResult()); - self::assertEmpty($result->getErrors()); + self::assertSame( + 'Post must be instance of Podium\Api\Interfaces\PostRepositoryInterface!', + $result->getErrors()['exception']->getMessage() + ); } public function testArchiveShouldReturnErrorWhenArchivingErrored(): void @@ -93,7 +96,10 @@ public function testReviveShouldReturnErrorWhenRepositoryIsWrong(): void $result = $this->service->revive($this->createMock(RepositoryInterface::class)); self::assertFalse($result->getResult()); - self::assertEmpty($result->getErrors()); + self::assertSame( + 'Post must be instance of Podium\Api\Interfaces\PostRepositoryInterface!', + $result->getErrors()['exception']->getMessage() + ); } public function testReviveShouldReturnErrorWhenRevivingErrored(): void diff --git a/tests/Unit/Post/PostBuilderTest.php b/tests/Unit/Post/PostBuilderTest.php index 753c6a2..0e1e91c 100644 --- a/tests/Unit/Post/PostBuilderTest.php +++ b/tests/Unit/Post/PostBuilderTest.php @@ -23,15 +23,34 @@ protected function setUp(): void $this->service = new PostBuilder(); } - public function testCreateShouldReturnErrorWhenRepositoryIsWrong(): void + public function testCreateShouldReturnErrorWhenPostRepositoryIsWrong(): void { $result = $this->service->create( $this->createMock(RepositoryInterface::class), $this->createMock(MemberRepositoryInterface::class), + $this->createMock(ThreadRepositoryInterface::class) + ); + + self::assertFalse($result->getResult()); + self::assertSame( + 'Post must be instance of Podium\Api\Interfaces\PostRepositoryInterface!', + $result->getErrors()['exception']->getMessage() + ); + } + + public function testCreateShouldReturnErrorWhenThreadRepositoryIsWrong(): void + { + $result = $this->service->create( + $this->createMock(PostRepositoryInterface::class), + $this->createMock(MemberRepositoryInterface::class), $this->createMock(RepositoryInterface::class) ); self::assertFalse($result->getResult()); + self::assertSame( + 'Thread must be instance of Podium\Api\Interfaces\ThreadRepositoryInterface!', + $result->getErrors()['exception']->getMessage() + ); } public function testCreateShouldReturnErrorWhenCreatingErrored(): void @@ -58,9 +77,9 @@ public function testCreateShouldReturnSuccessWhenCreatingIsDone(): void $post = $this->createMock(PostRepositoryInterface::class); $post->method('create')->willReturn(true); $thread = $this->createMock(ThreadRepositoryInterface::class); - $thread->method('updateCounters')->willReturn(true); + $thread->method('updateCounters')->with(1)->willReturn(true); $forum = $this->createMock(ForumRepositoryInterface::class); - $forum->method('updateCounters')->willReturn(true); + $forum->method('updateCounters')->with(0, 1)->willReturn(true); $thread->method('getParent')->willReturn($forum); $result = $this->service->create($post, $this->createMock(MemberRepositoryInterface::class), $thread); @@ -128,6 +147,10 @@ public function testEditShouldReturnErrorWhenRepositoryIsWrong(): void $result = $this->service->edit($this->createMock(RepositoryInterface::class)); self::assertFalse($result->getResult()); + self::assertSame( + 'Post must be instance of Podium\Api\Interfaces\PostRepositoryInterface!', + $result->getErrors()['exception']->getMessage() + ); } public function testEditShouldReturnErrorWhenEditingErrored(): void diff --git a/tests/Unit/Post/PostLikerTest.php b/tests/Unit/Post/PostLikerTest.php index 9a7439e..1283f97 100644 --- a/tests/Unit/Post/PostLikerTest.php +++ b/tests/Unit/Post/PostLikerTest.php @@ -282,6 +282,7 @@ public function testThumbResetShouldReturnErrorWhenResetErrored(): void $thumb = $this->createMock(ThumbRepositoryInterface::class); $thumb->method('fetchOne')->willReturn(true); $thumb->method('reset')->willReturn(false); + $thumb->method('getErrors')->willReturn([2]); $result = $this->service->thumbReset( $thumb, $this->createMock(PostRepositoryInterface::class), @@ -289,6 +290,7 @@ public function testThumbResetShouldReturnErrorWhenResetErrored(): void ); self::assertFalse($result->getResult()); + self::assertSame([2], $result->getErrors()); } public function testThumbResetShouldReturnErrorWhenPostIsNotRated(): void diff --git a/tests/Unit/Post/PostMoverTest.php b/tests/Unit/Post/PostMoverTest.php index 39049a4..a06f8ea 100644 --- a/tests/Unit/Post/PostMoverTest.php +++ b/tests/Unit/Post/PostMoverTest.php @@ -30,7 +30,10 @@ public function testMoveShouldReturnErrorWhenPostRepositoryIsWrong(): void ); self::assertFalse($result->getResult()); - self::assertEmpty($result->getErrors()); + self::assertSame( + 'Post must be instance of Podium\Api\Interfaces\PostRepositoryInterface!', + $result->getErrors()['exception']->getMessage() + ); } public function testMoveShouldReturnErrorWhenThreadRepositoryIsWrong(): void @@ -41,7 +44,10 @@ public function testMoveShouldReturnErrorWhenThreadRepositoryIsWrong(): void ); self::assertFalse($result->getResult()); - self::assertEmpty($result->getErrors()); + self::assertSame( + 'Thread must be instance of Podium\Api\Interfaces\ThreadRepositoryInterface!', + $result->getErrors()['exception']->getMessage() + ); } public function testMoveShouldReturnErrorWhenMovingErrored(): void @@ -60,15 +66,21 @@ public function testMoveShouldReturnSuccessWhenMovingIsDone(): void { $this->transaction->expects(self::once())->method('commit'); + $oldForum = $this->createMock(ForumRepositoryInterface::class); + $oldForum->method('updateCounters')->with(0, -1)->willReturn(true); + $oldThread = $this->createMock(ThreadRepositoryInterface::class); + $oldThread->method('updateCounters')->with(-1)->willReturn(true); + $oldThread->method('getParent')->willReturn($oldForum); $post = $this->createMock(PostRepositoryInterface::class); $post->method('move')->willReturn(true); - $thread = $this->createMock(ThreadRepositoryInterface::class); - $thread->method('updateCounters')->willReturn(true); - $forum = $this->createMock(ForumRepositoryInterface::class); - $forum->method('updateCounters')->willReturn(true); - $thread->method('getParent')->willReturn($forum); - $post->method('getParent')->willReturn($thread); - $result = $this->service->move($post, $thread); + $post->method('getParent')->willReturn($oldThread); + + $newForum = $this->createMock(ForumRepositoryInterface::class); + $newForum->method('updateCounters')->with(0, 1)->willReturn(true); + $newThread = $this->createMock(ThreadRepositoryInterface::class); + $newThread->method('updateCounters')->with(1)->willReturn(true); + $newThread->method('getParent')->willReturn($newForum); + $result = $this->service->move($post, $newThread); self::assertTrue($result->getResult()); } diff --git a/tests/Unit/Post/PostPinnerTest.php b/tests/Unit/Post/PostPinnerTest.php index 8ec396a..d37a1ac 100644 --- a/tests/Unit/Post/PostPinnerTest.php +++ b/tests/Unit/Post/PostPinnerTest.php @@ -25,7 +25,10 @@ public function testPinShouldReturnErrorWhenPostRepositoryIsWrong(): void $result = $this->service->pin($this->createMock(RepositoryInterface::class)); self::assertFalse($result->getResult()); - self::assertEmpty($result->getErrors()); + self::assertSame( + 'Post must be instance of Podium\Api\Interfaces\PostRepositoryInterface!', + $result->getErrors()['exception']->getMessage() + ); } public function testPinShouldReturnErrorWhenPinningErrored(): void @@ -78,7 +81,10 @@ public function testUnpinShouldReturnErrorWhenPostRepositoryIsWrong(): void $result = $this->service->unpin($this->createMock(RepositoryInterface::class)); self::assertFalse($result->getResult()); - self::assertEmpty($result->getErrors()); + self::assertSame( + 'Post must be instance of Podium\Api\Interfaces\PostRepositoryInterface!', + $result->getErrors()['exception']->getMessage() + ); } public function testUnpinShouldReturnErrorWhenUnpinningErrored(): void diff --git a/tests/Unit/Post/PostRemoverTest.php b/tests/Unit/Post/PostRemoverTest.php index 6a4e228..5ed2dd9 100644 --- a/tests/Unit/Post/PostRemoverTest.php +++ b/tests/Unit/Post/PostRemoverTest.php @@ -27,7 +27,10 @@ public function testRemoveShouldReturnErrorWhenRepositoryIsWrong(): void $result = $this->service->remove($this->createMock(RepositoryInterface::class)); self::assertFalse($result->getResult()); - self::assertEmpty($result->getErrors()); + self::assertSame( + 'Post must be instance of Podium\Api\Interfaces\PostRepositoryInterface!', + $result->getErrors()['exception']->getMessage() + ); } public function testRemoveShouldReturnErrorWhenRemovingErrored(): void @@ -64,9 +67,9 @@ public function testRemoveShouldReturnSuccessWhenRemovingIsDone(): void $post->method('isArchived')->willReturn(true); $post->method('delete')->willReturn(true); $thread = $this->createMock(ThreadRepositoryInterface::class); - $thread->method('updateCounters')->willReturn(true); + $thread->method('updateCounters')->with(-1)->willReturn(true); $forum = $this->createMock(ForumRepositoryInterface::class); - $forum->method('updateCounters')->willReturn(true); + $forum->method('updateCounters')->with(0, -1)->willReturn(true); $thread->method('getParent')->willReturn($forum); $post->method('getParent')->willReturn($thread); $result = $this->service->remove($post); diff --git a/tests/Unit/Rank/RankBuilderTest.php b/tests/Unit/Rank/RankBuilderTest.php index 84d4db8..8667b90 100644 --- a/tests/Unit/Rank/RankBuilderTest.php +++ b/tests/Unit/Rank/RankBuilderTest.php @@ -38,7 +38,10 @@ public function testCreateShouldReturnErrorWhenRepositoryIsWrong(): void $result = $this->service->create($this->createMock(RepositoryInterface::class)); self::assertFalse($result->getResult()); - self::assertEmpty($result->getErrors()); + self::assertSame( + 'Rank must be instance of Podium\Api\Interfaces\RankRepositoryInterface!', + $result->getErrors()['exception']->getMessage() + ); } public function testCreateShouldReturnSuccessWhenCreatingIsDone(): void @@ -91,7 +94,10 @@ public function testEditShouldReturnErrorWhenRepositoryIsWrong(): void $result = $this->service->edit($this->createMock(RepositoryInterface::class)); self::assertFalse($result->getResult()); - self::assertEmpty($result->getErrors()); + self::assertSame( + 'Rank must be instance of Podium\Api\Interfaces\RankRepositoryInterface!', + $result->getErrors()['exception']->getMessage() + ); } public function testEditShouldReturnSuccessWhenEditingIsDone(): void diff --git a/tests/Unit/Rank/RankRemoverTest.php b/tests/Unit/Rank/RankRemoverTest.php index 689fe31..818368a 100644 --- a/tests/Unit/Rank/RankRemoverTest.php +++ b/tests/Unit/Rank/RankRemoverTest.php @@ -25,7 +25,10 @@ public function testRemoveShouldReturnErrorWhenRepositoryIsWrong(): void $result = $this->service->remove($this->createMock(RepositoryInterface::class)); self::assertFalse($result->getResult()); - self::assertEmpty($result->getErrors()); + self::assertSame( + 'Rank must be instance of Podium\Api\Interfaces\RankRepositoryInterface!', + $result->getErrors()['exception']->getMessage() + ); } public function testRemoveShouldReturnErrorWhenRemovingErrored(): void diff --git a/tests/Unit/ServiceExceptionTest.php b/tests/Unit/ServiceExceptionTest.php index 07c20e1..cd75785 100644 --- a/tests/Unit/ServiceExceptionTest.php +++ b/tests/Unit/ServiceExceptionTest.php @@ -4,6 +4,7 @@ namespace Podium\Tests\Unit; +use Exception; use PHPUnit\Framework\TestCase; use Podium\Api\Services\ServiceException; @@ -20,4 +21,23 @@ public function testErrorList(): void self::assertSame([1], $exc->getErrorList()); } + + public function testDefaults(): void + { + $exc = new ServiceException(); + + self::assertSame([], $exc->getErrorList()); + self::assertSame('', $exc->getMessage()); + self::assertSame(0, $exc->getCode()); + self::assertNull($exc->getPrevious()); + } + + public function testExceptionArguments(): void + { + $exc = new ServiceException([], 'test', 2, new Exception()); + + self::assertSame('test', $exc->getMessage()); + self::assertSame(2, $exc->getCode()); + self::assertNotNull($exc->getPrevious()); + } } diff --git a/tests/Unit/Thread/ThreadArchiverTest.php b/tests/Unit/Thread/ThreadArchiverTest.php index 82de65f..37e610c 100644 --- a/tests/Unit/Thread/ThreadArchiverTest.php +++ b/tests/Unit/Thread/ThreadArchiverTest.php @@ -25,7 +25,10 @@ public function testArchiveShouldReturnErrorWhenRepositoryIsWrong(): void $result = $this->service->archive($this->createMock(RepositoryInterface::class)); self::assertFalse($result->getResult()); - self::assertEmpty($result->getErrors()); + self::assertSame( + 'Thread must be instance of Podium\Api\Interfaces\ThreadRepositoryInterface!', + $result->getErrors()['exception']->getMessage() + ); } public function testArchiveShouldReturnErrorWhenArchivingErrored(): void @@ -93,7 +96,10 @@ public function testReviveShouldReturnErrorWhenRepositoryIsWrong(): void $result = $this->service->revive($this->createMock(RepositoryInterface::class)); self::assertFalse($result->getResult()); - self::assertEmpty($result->getErrors()); + self::assertSame( + 'Thread must be instance of Podium\Api\Interfaces\ThreadRepositoryInterface!', + $result->getErrors()['exception']->getMessage() + ); } public function testReviveShouldReturnErrorWhenRevivingErrored(): void @@ -110,6 +116,18 @@ public function testReviveShouldReturnErrorWhenRevivingErrored(): void self::assertSame([1], $result->getErrors()); } + public function testReviveShouldReturnErrorWhenThreadIsNotArchived(): void + { + $this->transaction->expects(self::once())->method('rollBack'); + + $thread = $this->createMock(ThreadRepositoryInterface::class); + $thread->method('isArchived')->willReturn(false); + $result = $this->service->revive($thread); + + self::assertFalse($result->getResult()); + self::assertSame('thread.not.archived', $result->getErrors()['api']); + } + public function testReviveShouldReturnSuccessWhenRevivingIsDone(): void { $this->transaction->expects(self::once())->method('commit'); diff --git a/tests/Unit/Thread/ThreadBookmarkerTest.php b/tests/Unit/Thread/ThreadBookmarkerTest.php index a67e75c..c982d5d 100644 --- a/tests/Unit/Thread/ThreadBookmarkerTest.php +++ b/tests/Unit/Thread/ThreadBookmarkerTest.php @@ -60,6 +60,25 @@ public function testMarkShouldReturnTrueIfBookmarkIsSeenAfterPostCreation(): voi self::assertTrue($result->getResult()); } + public function testMarkShouldReturnTrueIfBookmarkIsSeenAtTheTimeOfPostCreation(): void + { + $this->transaction->expects(self::once())->method('commit'); + + $bookmark = $this->createMock(BookmarkRepositoryInterface::class); + $bookmark->method('fetchOne')->willReturn(true); + $bookmark->expects(self::never())->method('prepare'); + $bookmark->method('getLastSeen')->willReturn(2); + $bookmark->expects(self::never())->method('mark'); + + $post = $this->createMock(PostRepositoryInterface::class); + $post->method('getParent')->willReturn($this->createMock(ThreadRepositoryInterface::class)); + $post->method('getCreatedAt')->willReturn(2); + + $result = $this->service->mark($bookmark, $post, $this->createMock(MemberRepositoryInterface::class)); + + self::assertTrue($result->getResult()); + } + public function testMarkShouldPrepareBookmarkWhenItDoesntExist(): void { $this->transaction->expects(self::once())->method('commit'); @@ -79,6 +98,26 @@ public function testMarkShouldPrepareBookmarkWhenItDoesntExist(): void self::assertTrue($result->getResult()); } + public function testMarkShouldReturnErrorWhenMarkingErrored(): void + { + $this->transaction->expects(self::once())->method('rollBack'); + + $bookmark = $this->createMock(BookmarkRepositoryInterface::class); + $bookmark->method('fetchOne')->willReturn(true); + $bookmark->method('getLastSeen')->willReturn(1); + $bookmark->method('mark')->willReturn(false); + $bookmark->method('getErrors')->willReturn([3]); + + $post = $this->createMock(PostRepositoryInterface::class); + $post->method('getParent')->willReturn($this->createMock(ThreadRepositoryInterface::class)); + $post->method('getCreatedAt')->willReturn(2); + + $result = $this->service->mark($bookmark, $post, $this->createMock(MemberRepositoryInterface::class)); + + self::assertFalse($result->getResult()); + self::assertSame([3], $result->getErrors()); + } + public function testMarkShouldReturnErrorWhenMarkingThrowsException(): void { $this->transaction->expects(self::once())->method('rollBack'); diff --git a/tests/Unit/Thread/ThreadBuilderTest.php b/tests/Unit/Thread/ThreadBuilderTest.php index 20f2811..e70e00a 100644 --- a/tests/Unit/Thread/ThreadBuilderTest.php +++ b/tests/Unit/Thread/ThreadBuilderTest.php @@ -22,15 +22,34 @@ protected function setUp(): void $this->service = new ThreadBuilder(); } - public function testCreateShouldReturnErrorWhenRepositoryIsWrong(): void + public function testCreateShouldReturnErrorWhenThreadRepositoryIsWrong(): void { $result = $this->service->create( $this->createMock(RepositoryInterface::class), $this->createMock(MemberRepositoryInterface::class), + $this->createMock(ForumRepositoryInterface::class) + ); + + self::assertFalse($result->getResult()); + self::assertSame( + 'Thread must be instance of Podium\Api\Interfaces\ThreadRepositoryInterface!', + $result->getErrors()['exception']->getMessage() + ); + } + + public function testCreateShouldReturnErrorWhenForumRepositoryIsWrong(): void + { + $result = $this->service->create( + $this->createMock(ThreadRepositoryInterface::class), + $this->createMock(MemberRepositoryInterface::class), $this->createMock(RepositoryInterface::class) ); self::assertFalse($result->getResult()); + self::assertSame( + 'Forum must be instance of Podium\Api\Interfaces\ForumRepositoryInterface!', + $result->getErrors()['exception']->getMessage() + ); } public function testCreateShouldReturnErrorWhenCreatingErrored(): void @@ -57,7 +76,7 @@ public function testCreateShouldReturnSuccessWhenCreatingIsDone(): void $thread = $this->createMock(ThreadRepositoryInterface::class); $thread->method('create')->willReturn(true); $forum = $this->createMock(ForumRepositoryInterface::class); - $forum->method('updateCounters')->willReturn(true); + $forum->method('updateCounters')->with(1, 0)->willReturn(true); $result = $this->service->create($thread, $this->createMock(MemberRepositoryInterface::class), $forum); self::assertTrue($result->getResult()); @@ -118,6 +137,10 @@ public function testEditShouldReturnErrorWhenRepositoryIsWrong(): void $result = $this->service->edit($this->createMock(RepositoryInterface::class)); self::assertFalse($result->getResult()); + self::assertSame( + 'Thread must be instance of Podium\Api\Interfaces\ThreadRepositoryInterface!', + $result->getErrors()['exception']->getMessage() + ); } public function testEditShouldReturnErrorWhenEditingErrored(): void diff --git a/tests/Unit/Thread/ThreadMoverTest.php b/tests/Unit/Thread/ThreadMoverTest.php index 0048898..3201cf5 100644 --- a/tests/Unit/Thread/ThreadMoverTest.php +++ b/tests/Unit/Thread/ThreadMoverTest.php @@ -29,7 +29,10 @@ public function testMoveShouldReturnErrorWhenThreadRepositoryIsWrong(): void ); self::assertFalse($result->getResult()); - self::assertEmpty($result->getErrors()); + self::assertSame( + 'Thread must be instance of Podium\Api\Interfaces\ThreadRepositoryInterface!', + $result->getErrors()['exception']->getMessage() + ); } public function testMoveShouldReturnErrorWhenForumRepositoryIsWrong(): void @@ -40,7 +43,10 @@ public function testMoveShouldReturnErrorWhenForumRepositoryIsWrong(): void ); self::assertFalse($result->getResult()); - self::assertEmpty($result->getErrors()); + self::assertSame( + 'Forum must be instance of Podium\Api\Interfaces\ForumRepositoryInterface!', + $result->getErrors()['exception']->getMessage() + ); } public function testMoveShouldReturnErrorWhenMovingErrored(): void @@ -61,11 +67,15 @@ public function testMoveShouldReturnSuccessWhenMovingIsDone(): void $thread = $this->createMock(ThreadRepositoryInterface::class); $thread->method('move')->willReturn(true); - $forum = $this->createMock(ForumRepositoryInterface::class); - $forum->method('updateCounters')->willReturn(true); - $thread->method('getParent')->willReturn($forum); + $thread->method('getPostsCount')->willReturn(9); + $newForum = $this->createMock(ForumRepositoryInterface::class); + $newForum->method('updateCounters')->with(1, 9)->willReturn(true); + + $oldForum = $this->createMock(ForumRepositoryInterface::class); + $oldForum->method('updateCounters')->with(-1, -9)->willReturn(true); + $thread->method('getParent')->willReturn($oldForum); $thread->method('updateCounters')->willReturn(true); - $result = $this->service->move($thread, $forum); + $result = $this->service->move($thread, $newForum); self::assertTrue($result->getResult()); } diff --git a/tests/Unit/Thread/ThreadPinnerTest.php b/tests/Unit/Thread/ThreadPinnerTest.php index c50d0a7..91bc490 100644 --- a/tests/Unit/Thread/ThreadPinnerTest.php +++ b/tests/Unit/Thread/ThreadPinnerTest.php @@ -25,7 +25,10 @@ public function testPinShouldReturnErrorWhenThreadRepositoryIsWrong(): void $result = $this->service->pin($this->createMock(RepositoryInterface::class)); self::assertFalse($result->getResult()); - self::assertEmpty($result->getErrors()); + self::assertSame( + 'Thread must be instance of Podium\Api\Interfaces\ThreadRepositoryInterface!', + $result->getErrors()['exception']->getMessage() + ); } public function testPinShouldReturnErrorWhenPinningErrored(): void @@ -78,7 +81,10 @@ public function testUnpinShouldReturnErrorWhenThreadRepositoryIsWrong(): void $result = $this->service->unpin($this->createMock(RepositoryInterface::class)); self::assertFalse($result->getResult()); - self::assertEmpty($result->getErrors()); + self::assertSame( + 'Thread must be instance of Podium\Api\Interfaces\ThreadRepositoryInterface!', + $result->getErrors()['exception']->getMessage() + ); } public function testUnpinShouldReturnErrorWhenUnpinningErrored(): void diff --git a/tests/Unit/Thread/ThreadRemoverTest.php b/tests/Unit/Thread/ThreadRemoverTest.php index bda19b4..9d5406b 100644 --- a/tests/Unit/Thread/ThreadRemoverTest.php +++ b/tests/Unit/Thread/ThreadRemoverTest.php @@ -26,7 +26,10 @@ public function testRemoveShouldReturnErrorWhenRepositoryIsWrong(): void $result = $this->service->remove($this->createMock(RepositoryInterface::class)); self::assertFalse($result->getResult()); - self::assertEmpty($result->getErrors()); + self::assertSame( + 'Thread must be instance of Podium\Api\Interfaces\ThreadRepositoryInterface!', + $result->getErrors()['exception']->getMessage() + ); } public function testRemoveShouldReturnErrorWhenRemovingErrored(): void @@ -62,8 +65,9 @@ public function testRemoveShouldReturnSuccessWhenRemovingIsDone(): void $thread = $this->createMock(ThreadRepositoryInterface::class); $thread->method('isArchived')->willReturn(true); $thread->method('delete')->willReturn(true); + $thread->method('getPostsCount')->willReturn(1); $forum = $this->createMock(ForumRepositoryInterface::class); - $forum->method('updateCounters')->willReturn(true); + $forum->method('updateCounters')->with(-1, -1)->willReturn(true); $thread->method('getParent')->willReturn($forum); $result = $this->service->remove($thread);