Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow null Reacterable in Reactant facade #99

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,20 @@ Follow [upgrade instructions](UPGRADING.md#from-v7-to-v8) to migrate code & data
- ([#90]) Moved `count` & `weight` attributes default values of `ReactionCounter` to application level
- ([#90]) Moved `count` & `weight` attributes default values of `ReactionTotal` to application level
- ([#91]) Changed `getWeight` method return type from `int` to `float` in reactant's `ReactionCounter` model contract
- ([#91]) Changed `$amount` parameter type from `int` to `float` of `incrementWeight` method in reactant's `ReactionCounter` model contract
- ([#91]) Changed `$amount` parameter type from `int` to `float` of `decrementWeight` method in reactant's `ReactionCounter` model contract
- ([#91]) Changed `$amount` parameter type from `int` to `float` of `incrementWeight` method in reactant's `ReactionCounter` model contract
- ([#91]) Changed `$amount` parameter type from `int` to `float` of `decrementWeight` method in reactant's `ReactionCounter` model contract
- ([#91]) Changed `getWeight` method return type from `int` to `float` in reactant's `ReactionTotal` model contract
- ([#91]) Changed `$amount` parameter type from `int` to `float` of `incrementWeight` method in reactant's `ReactionTotal` model contract
- ([#91]) Changed `$amount` parameter type from `int` to `float` of `decrementWeight` method in reactant's `ReactionTotal` model contract
- ([#91]) Changed `$amount` parameter type from `int` to `float` of `incrementWeight` method in reactant's `ReactionTotal` model contract
- ([#91]) Changed `$amount` parameter type from `int` to `float` of `decrementWeight` method in reactant's `ReactionTotal` model contract
- ([#91]) Added `?float $rate` parameter to `reactTo` method in `Reacter` facade contract
- ([#91]) Added `?float $rate` parameter to `reactTo` method in `Reacter` model contract
- ([#91]) Added `getRate` method to `Reaction` model contract
- ([#91]) Changed `getWeight` method return type from `int` to `float` in `Reaction` model contract
- ([#91]) Changed `weight` column type to `DECIMIAL(13, 2)` in `love_reactant_reaction_counters` db table
- ([#91]) Changed `weight` column type to `DECIMIAL(13, 2)` in `love_reactant_reaction_totals` db table
- ([#96]) Changed signature of `love:recount` Artisan command to `love:recount {--model=} {--type=}`
- ([#99]) Make `Reacterable` parameter nullable in `isReactedBy` method of `Reactant` facade contract
- ([#99]) Make `Reacterable` parameter nullable in `isNotReactedBy` method of `Reactant` facade contract

### Removed

Expand Down Expand Up @@ -393,3 +395,4 @@ Follow [upgrade instructions](UPGRADING.md#from-v5-to-v6) to migrate database to
[#90]: https://github.com/cybercog/laravel-love/pull/90
[#91]: https://github.com/cybercog/laravel-love/pull/91
[#96]: https://github.com/cybercog/laravel-love/pull/96
[#99]: https://github.com/cybercog/laravel-love/pull/99
18 changes: 14 additions & 4 deletions contracts/Reactant/Facades/Reactant.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,21 @@ public function getReactions(): iterable;

public function getReactionCounters(): iterable;

public function getReactionCounterOfType(string $reactionTypeName): ReactionCounter;
public function getReactionCounterOfType(
string $reactionTypeName
): ReactionCounter;

public function getReactionTotal(): ReactionTotal;

public function isReactedBy(Reacterable $reacterable, ?string $reactionTypeName = null, ?float $rate = null): bool;

public function isNotReactedBy(Reacterable $reacterable, ?string $reactionTypeName = null, ?float $rate = null): bool;
public function isReactedBy(
?Reacterable $reacterable = null,
?string $reactionTypeName = null,
?float $rate = null
): bool;

public function isNotReactedBy(
?Reacterable $reacterable = null,
?string $reactionTypeName = null,
?float $rate = null
): bool;
}
12 changes: 10 additions & 2 deletions src/Reactant/Facades/Reactant.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,14 @@ public function getReactionTotal(): ReactionTotalContract
}

public function isReactedBy(
ReacterableContract $reacterable,
?ReacterableContract $reacterable = null,
?string $reactionTypeName = null,
?float $rate = null
): bool {
if (is_null($reacterable)) {
return false;
}

$reactionType = is_null($reactionTypeName) ? null : ReactionType::fromName($reactionTypeName);

return $this->reactant->isReactedBy(
Expand All @@ -67,10 +71,14 @@ public function isReactedBy(
}

public function isNotReactedBy(
ReacterableContract $reacterable,
?ReacterableContract $reacterable = null,
?string $reactionTypeName = null,
?float $rate = null
): bool {
if (is_null($reacterable)) {
return true;
}

$reactionType = is_null($reactionTypeName) ? null : ReactionType::fromName($reactionTypeName);

return $this->reactant->isNotReactedBy(
Expand Down
22 changes: 22 additions & 0 deletions tests/Unit/Reactant/Facades/ReactantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,17 @@ public function it_can_check_is_reacted_by_reacterable(): void
$this->assertTrue($isReacted);
}

/** @test */
public function it_can_check_is_reacted_by_reacterable_when_reacter_is_null(): void
{
$reactant = factory(Reactant::class)->create();
$reactantFacade = new ReactantFacade($reactant);

$isReacted = $reactantFacade->isReactedBy(null);

$this->assertFalse($isReacted);
}

/** @test */
public function it_can_check_is_reacted_by_reacterable_when_reacter_is_null_object(): void
{
Expand Down Expand Up @@ -223,6 +234,17 @@ public function it_can_check_is_not_reacted_by_reacterable(): void
$this->assertTrue($isNotReacted);
}

/** @test */
public function it_can_check_is_not_reacted_by_reacterable_when_reacter_is_null(): void
{
$reactant = factory(Reactant::class)->create();
$reactantFacade = new ReactantFacade($reactant);

$isNotReacted = $reactantFacade->isNotReactedBy(null);

$this->assertTrue($isNotReacted);
}

/** @test */
public function it_can_check_is_not_reacted_by_reacterable_when_reacter_is_null_object(): void
{
Expand Down