From 6bf6cd6ecf95b7af864fe855f2f1eefb7e160950 Mon Sep 17 00:00:00 2001 From: Anton Komarev <1849174+antonkomarev@users.noreply.github.com> Date: Sun, 9 Apr 2023 21:11:35 +0300 Subject: [PATCH] Remove ReactionCounter & ReactionTotal observers (#247) --- CHANGELOG.md | 8 +++++ src/LoveServiceProvider.php | 6 ---- .../Models/ReactionCounter.php | 12 +++++++ .../Observers/ReactionCounterObserver.php | 31 ------------------- .../ReactionTotal/Models/ReactionTotal.php | 12 +++++++ .../Observers/ReactionTotalObserver.php | 31 ------------------- .../Models/ReactionCounterTest.php | 28 +++++++++++++++-- .../Models/ReactionTotalTest.php | 28 +++++++++++++++-- 8 files changed, 84 insertions(+), 72 deletions(-) delete mode 100644 src/Reactant/ReactionCounter/Observers/ReactionCounterObserver.php delete mode 100644 src/Reactant/ReactionTotal/Observers/ReactionTotalObserver.php diff --git a/CHANGELOG.md b/CHANGELOG.md index ab414362..54f0087f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,9 +4,16 @@ All notable changes to `laravel-love` will be documented in this file. ## [Unreleased] +### Added + - ([#240]) Added `getReactionsBy` method to Reacter Facade - ([#240]) Added `getReactionsBy` method to Reacter Model +### Removed + +- ([#247]) Removed `ReactionCounterObserver` +- ([#247]) Removed `ReactionTotalObserver` + ## [9.0.0] - 2023-02-24 Code has breaking changes because of Eloquent Model local scopes refactoring. @@ -581,6 +588,7 @@ Follow [upgrade instructions](UPGRADING.md#from-v5-to-v6) to migrate database to [1.1.1]: https://github.com/cybercog/laravel-love/compare/1.1.0...1.1.1 [1.1.0]: https://github.com/cybercog/laravel-love/compare/1.0.0...1.1.0 +[#247]: https://github.com/cybercog/laravel-love/pull/247 [#240]: https://github.com/cybercog/laravel-love/pull/240 [#234]: https://github.com/cybercog/laravel-love/pull/234 [#233]: https://github.com/cybercog/laravel-love/pull/233 diff --git a/src/LoveServiceProvider.php b/src/LoveServiceProvider.php index e780ee86..2dcc243b 100644 --- a/src/LoveServiceProvider.php +++ b/src/LoveServiceProvider.php @@ -21,10 +21,6 @@ use Cog\Laravel\Love\Console\Commands\SetupReacterable; use Cog\Laravel\Love\Console\Commands\UpgradeV5ToV6; use Cog\Laravel\Love\Console\Commands\UpgradeV7ToV8; -use Cog\Laravel\Love\Reactant\ReactionCounter\Models\ReactionCounter; -use Cog\Laravel\Love\Reactant\ReactionCounter\Observers\ReactionCounterObserver; -use Cog\Laravel\Love\Reactant\ReactionTotal\Models\ReactionTotal; -use Cog\Laravel\Love\Reactant\ReactionTotal\Observers\ReactionTotalObserver; use Cog\Laravel\Love\Reaction\Models\Reaction; use Cog\Laravel\Love\Reaction\Observers\ReactionObserver; use Illuminate\Support\Facades\Config; @@ -65,8 +61,6 @@ private function shouldLoadDefaultMigrations(): bool private function registerObservers(): void { Reaction::observe(ReactionObserver::class); - ReactionCounter::observe(ReactionCounterObserver::class); - ReactionTotal::observe(ReactionTotalObserver::class); } /** diff --git a/src/Reactant/ReactionCounter/Models/ReactionCounter.php b/src/Reactant/ReactionCounter/Models/ReactionCounter.php index 10b15880..5b570bfc 100644 --- a/src/Reactant/ReactionCounter/Models/ReactionCounter.php +++ b/src/Reactant/ReactionCounter/Models/ReactionCounter.php @@ -123,4 +123,16 @@ public function decrementWeight( ): void { $this->decrement('weight', $amount); } + + public function setCountAttribute( + int | null $count, + ): void { + $this->attributes['count'] = $count ?? self::COUNT_DEFAULT; + } + + public function setWeightAttribute( + float | null $weight, + ): void { + $this->attributes['weight'] = $weight ?? self::WEIGHT_DEFAULT; + } } diff --git a/src/Reactant/ReactionCounter/Observers/ReactionCounterObserver.php b/src/Reactant/ReactionCounter/Observers/ReactionCounterObserver.php deleted file mode 100644 index 846c6a1e..00000000 --- a/src/Reactant/ReactionCounter/Observers/ReactionCounterObserver.php +++ /dev/null @@ -1,31 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -declare(strict_types=1); - -namespace Cog\Laravel\Love\Reactant\ReactionCounter\Observers; - -use Cog\Laravel\Love\Reactant\ReactionCounter\Models\ReactionCounter; - -final class ReactionCounterObserver -{ - public function creating( - ReactionCounter $counter, - ): void { - if ($counter->getAttributeValue('count') === null) { - $counter->setAttribute('count', ReactionCounter::COUNT_DEFAULT); - } - - if ($counter->getAttributeValue('weight') === null) { - $counter->setAttribute('weight', ReactionCounter::WEIGHT_DEFAULT); - } - } -} diff --git a/src/Reactant/ReactionTotal/Models/ReactionTotal.php b/src/Reactant/ReactionTotal/Models/ReactionTotal.php index defdfdcd..30d82109 100644 --- a/src/Reactant/ReactionTotal/Models/ReactionTotal.php +++ b/src/Reactant/ReactionTotal/Models/ReactionTotal.php @@ -98,4 +98,16 @@ public function decrementWeight( ): void { $this->decrement('weight', $amount); } + + public function setCountAttribute( + int | null $count, + ): void { + $this->attributes['count'] = $count ?? self::COUNT_DEFAULT; + } + + public function setWeightAttribute( + float | null $weight, + ): void { + $this->attributes['weight'] = $weight ?? self::WEIGHT_DEFAULT; + } } diff --git a/src/Reactant/ReactionTotal/Observers/ReactionTotalObserver.php b/src/Reactant/ReactionTotal/Observers/ReactionTotalObserver.php deleted file mode 100644 index e35ceb76..00000000 --- a/src/Reactant/ReactionTotal/Observers/ReactionTotalObserver.php +++ /dev/null @@ -1,31 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -declare(strict_types=1); - -namespace Cog\Laravel\Love\Reactant\ReactionTotal\Observers; - -use Cog\Laravel\Love\Reactant\ReactionTotal\Models\ReactionTotal; - -final class ReactionTotalObserver -{ - public function creating( - ReactionTotal $total, - ): void { - if ($total->getAttributeValue('count') === null) { - $total->setAttribute('count', ReactionTotal::COUNT_DEFAULT); - } - - if ($total->getAttributeValue('weight') === null) { - $total->setAttribute('weight', ReactionTotal::WEIGHT_DEFAULT); - } - } -} diff --git a/tests/Unit/Reactant/ReactionCounter/Models/ReactionCounterTest.php b/tests/Unit/Reactant/ReactionCounter/Models/ReactionCounterTest.php index 4beb34f7..9dcb551f 100644 --- a/tests/Unit/Reactant/ReactionCounter/Models/ReactionCounterTest.php +++ b/tests/Unit/Reactant/ReactionCounter/Models/ReactionCounterTest.php @@ -74,7 +74,7 @@ public function it_casts_string_count_to_int(): void /** @test */ public function it_casts_null_count_to_zero(): void { - $counter = ReactionCounter::factory()->create([ + $counter = new ReactionCounter([ 'count' => null, ]); @@ -116,7 +116,7 @@ public function it_casts_string_weight_to_float(): void /** @test */ public function it_casts_null_weight_to_zero(): void { - $counter = ReactionCounter::factory()->create([ + $counter = new ReactionCounter([ 'weight' => null, ]); @@ -231,6 +231,30 @@ public function it_can_check_is_not_reaction_of_type(): void $this->assertFalse($false); } + /** @test */ + public function it_can_create_model_with_zero_count(): void + { + $counter1 = ReactionCounter::factory()->create(); + $counter2 = ReactionCounter::factory()->create([ + 'count' => null, + ]); + + $this->assertSame(0, $counter1->getCount()); + $this->assertSame(0, $counter2->getCount()); + } + + /** @test */ + public function it_can_create_model_with_zero_weight(): void + { + $counter1 = ReactionCounter::factory()->create(); + $counter2 = ReactionCounter::factory()->create([ + 'weight' => null, + ]); + + $this->assertSame(0.0, $counter1->getWeight()); + $this->assertSame(0.0, $counter2->getWeight()); + } + /** @test */ public function it_can_increment_count(): void { diff --git a/tests/Unit/Reactant/ReactionTotal/Models/ReactionTotalTest.php b/tests/Unit/Reactant/ReactionTotal/Models/ReactionTotalTest.php index dc3b77f8..8758e0d6 100644 --- a/tests/Unit/Reactant/ReactionTotal/Models/ReactionTotalTest.php +++ b/tests/Unit/Reactant/ReactionTotal/Models/ReactionTotalTest.php @@ -63,7 +63,7 @@ public function it_casts_string_count_to_int(): void /** @test */ public function it_casts_null_count_to_zero(): void { - $total = ReactionTotal::factory()->create([ + $total = new ReactionTotal([ 'count' => null, ]); @@ -105,7 +105,7 @@ public function it_casts_string_weight_to_float(): void /** @test */ public function it_casts_null_weight_to_zero(): void { - $total = ReactionTotal::factory()->create([ + $total = new ReactionTotal([ 'weight' => null, ]); @@ -154,6 +154,30 @@ public function it_throws_exception_on_get_reactant_when_reactant_is_null(): voi $total->getReactant(); } + /** @test */ + public function it_can_create_model_with_zero_count(): void + { + $total1 = ReactionTotal::factory()->create(); + $total2 = ReactionTotal::factory()->create([ + 'count' => null, + ]); + + $this->assertSame(0, $total1->getCount()); + $this->assertSame(0, $total2->getCount()); + } + + /** @test */ + public function it_can_create_model_with_zero_weight(): void + { + $total1 = ReactionTotal::factory()->create(); + $total2 = ReactionTotal::factory()->create([ + 'weight' => null, + ]); + + $this->assertSame(0.0, $total1->getWeight()); + $this->assertSame(0.0, $total2->getWeight()); + } + /** @test */ public function it_can_increment_count(): void {