diff --git a/CHANGELOG.md b/CHANGELOG.md index 54f0087f..e99de887 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,8 +11,9 @@ All notable changes to `laravel-love` will be documented in this file. ### Removed -- ([#247]) Removed `ReactionCounterObserver` -- ([#247]) Removed `ReactionTotalObserver` +- ([#247]) Removed `ReactionCounterObserver` class +- ([#247]) Removed `ReactionTotalObserver` class +- ([#248]) Removed `ReactionObserver::creating` method ## [9.0.0] - 2023-02-24 @@ -588,6 +589,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 +[#248]: https://github.com/cybercog/laravel-love/pull/248 [#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 diff --git a/src/Reaction/Models/Reaction.php b/src/Reaction/Models/Reaction.php index 96359d54..a45660a5 100644 --- a/src/Reaction/Models/Reaction.php +++ b/src/Reaction/Models/Reaction.php @@ -115,7 +115,7 @@ public function setRateAttribute( throw RateOutOfRange::withValueBetween($rate, self::RATE_MIN, self::RATE_MAX); } - $this->attributes['rate'] = $rate; + $this->attributes['rate'] = $rate ?? self::RATE_DEFAULT; } public function isOfType( diff --git a/src/Reaction/Observers/ReactionObserver.php b/src/Reaction/Observers/ReactionObserver.php index f4c2f6d8..3f38b96c 100644 --- a/src/Reaction/Observers/ReactionObserver.php +++ b/src/Reaction/Observers/ReactionObserver.php @@ -28,14 +28,6 @@ public function __construct( $this->eventDispatcher = $eventDispatcher; } - public function creating( - Reaction $reaction, - ): void { - if ($reaction->getAttributeValue('rate') === null) { - $reaction->setAttribute('rate', Reaction::RATE_DEFAULT); - } - } - public function created( Reaction $reaction, ): void { diff --git a/tests/Unit/Reactant/ReactionCounter/Models/ReactionCounterTest.php b/tests/Unit/Reactant/ReactionCounter/Models/ReactionCounterTest.php index 9dcb551f..ce9dc525 100644 --- a/tests/Unit/Reactant/ReactionCounter/Models/ReactionCounterTest.php +++ b/tests/Unit/Reactant/ReactionCounter/Models/ReactionCounterTest.php @@ -232,7 +232,7 @@ public function it_can_check_is_not_reaction_of_type(): void } /** @test */ - public function it_can_create_model_with_zero_count(): void + public function it_can_create_counter_with_zero_count(): void { $counter1 = ReactionCounter::factory()->create(); $counter2 = ReactionCounter::factory()->create([ @@ -244,7 +244,7 @@ public function it_can_create_model_with_zero_count(): void } /** @test */ - public function it_can_create_model_with_zero_weight(): void + public function it_can_create_counter_with_zero_weight(): void { $counter1 = ReactionCounter::factory()->create(); $counter2 = ReactionCounter::factory()->create([ diff --git a/tests/Unit/Reactant/ReactionTotal/Models/ReactionTotalTest.php b/tests/Unit/Reactant/ReactionTotal/Models/ReactionTotalTest.php index 8758e0d6..75194c61 100644 --- a/tests/Unit/Reactant/ReactionTotal/Models/ReactionTotalTest.php +++ b/tests/Unit/Reactant/ReactionTotal/Models/ReactionTotalTest.php @@ -155,7 +155,7 @@ public function it_throws_exception_on_get_reactant_when_reactant_is_null(): voi } /** @test */ - public function it_can_create_model_with_zero_count(): void + public function it_can_create_total_with_zero_count(): void { $total1 = ReactionTotal::factory()->create(); $total2 = ReactionTotal::factory()->create([ @@ -167,7 +167,7 @@ public function it_can_create_model_with_zero_count(): void } /** @test */ - public function it_can_create_model_with_zero_weight(): void + public function it_can_create_total_with_zero_weight(): void { $total1 = ReactionTotal::factory()->create(); $total2 = ReactionTotal::factory()->create([ diff --git a/tests/Unit/Reaction/Models/ReactionTest.php b/tests/Unit/Reaction/Models/ReactionTest.php index 37fe5179..89e9fc49 100644 --- a/tests/Unit/Reaction/Models/ReactionTest.php +++ b/tests/Unit/Reaction/Models/ReactionTest.php @@ -116,7 +116,7 @@ public function it_casts_string_rate_to_float(): void /** @test */ public function it_casts_null_rate_to_default_value(): void { - $reaction = Reaction::factory()->create([ + $reaction = new Reaction([ 'rate' => null, ]); @@ -131,6 +131,18 @@ public function it_casts_not_set_rate_to_default_value(): void $this->assertSame(Reaction::RATE_DEFAULT, $reaction->getRate()); } + /** @test */ + public function it_can_create_reaction_with_default_value(): void + { + $reaction1 = Reaction::factory()->create(); + $reaction2 = Reaction::factory()->create([ + 'rate' => null, + ]); + + $this->assertSame(Reaction::RATE_DEFAULT, $reaction1->getRate()); + $this->assertSame(Reaction::RATE_DEFAULT, $reaction2->getRate()); + } + /** @test */ public function it_can_belong_to_type(): void {