Skip to content

Commit

Permalink
Remove ReactionCounter & ReactionTotal observers (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
antonkomarev authored Apr 9, 2023
1 parent f75363d commit 6bf6cd6
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 72 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
6 changes: 0 additions & 6 deletions src/LoveServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -65,8 +61,6 @@ private function shouldLoadDefaultMigrations(): bool
private function registerObservers(): void
{
Reaction::observe(ReactionObserver::class);
ReactionCounter::observe(ReactionCounterObserver::class);
ReactionTotal::observe(ReactionTotalObserver::class);
}

/**
Expand Down
12 changes: 12 additions & 0 deletions src/Reactant/ReactionCounter/Models/ReactionCounter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
31 changes: 0 additions & 31 deletions src/Reactant/ReactionCounter/Observers/ReactionCounterObserver.php

This file was deleted.

12 changes: 12 additions & 0 deletions src/Reactant/ReactionTotal/Models/ReactionTotal.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
31 changes: 0 additions & 31 deletions src/Reactant/ReactionTotal/Observers/ReactionTotalObserver.php

This file was deleted.

28 changes: 26 additions & 2 deletions tests/Unit/Reactant/ReactionCounter/Models/ReactionCounterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);

Expand Down Expand Up @@ -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,
]);

Expand Down Expand Up @@ -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
{
Expand Down
28 changes: 26 additions & 2 deletions tests/Unit/Reactant/ReactionTotal/Models/ReactionTotalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);

Expand Down Expand Up @@ -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,
]);

Expand Down Expand Up @@ -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
{
Expand Down

0 comments on commit 6bf6cd6

Please sign in to comment.