Skip to content

Commit

Permalink
Add fetch reactions by Reacterable model
Browse files Browse the repository at this point in the history
  • Loading branch information
antonkomarev committed Apr 2, 2023
1 parent 9a324d9 commit aef13fa
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 6 deletions.
4 changes: 3 additions & 1 deletion contracts/Reactant/Facades/Reactant.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@

interface Reactant
{
public function getReactions(): iterable;
public function getReactions(
Reacterable | null $reacterable = null,
): iterable;

public function getReactionCounters(): iterable;

Expand Down
7 changes: 7 additions & 0 deletions contracts/Reactant/Models/Reactant.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ public function getReactable(): Reactable;
*/
public function getReactions(): iterable;

/**
* @return iterable|\Cog\Contracts\Love\Reaction\Models\Reaction[]
*/
public function getReactionsBy(
Reacter $reacter,
): iterable;

/**
* @return iterable|\Cog\Contracts\Love\Reactant\ReactionCounter\Models\ReactionCounter[]
*/
Expand Down
13 changes: 10 additions & 3 deletions src/Reactant/Facades/Reactant.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,16 @@ public function __construct(
/**
* @return iterable|\Cog\Contracts\Love\Reaction\Models\Reaction[]
*/
public function getReactions(): iterable
{
return $this->reactant->getReactions();
public function getReactions(
ReacterableInterface | null $reacterable = null,
): iterable {
if ($reacterable === null) {
return $this->reactant->getReactions();
}

return $this->reactant->getReactionsBy(
$reacterable->getLoveReacter(),
);
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/Reactant/Models/NullReactant.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Cog\Contracts\Love\Reactant\Models\Reactant as ReactantInterface;
use Cog\Contracts\Love\Reactant\ReactionCounter\Models\ReactionCounter as ReactionCounterInterface;
use Cog\Contracts\Love\Reactant\ReactionTotal\Models\ReactionTotal as ReactionTotalInterface;
use Cog\Contracts\Love\Reacter\Models\Reacter;
use Cog\Contracts\Love\Reacter\Models\Reacter as ReacterInterface;
use Cog\Contracts\Love\ReactionType\Models\ReactionType as ReactionTypeInterface;
use Cog\Laravel\Love\Reactant\ReactionCounter\Models\NullReactionCounter;
Expand Down Expand Up @@ -51,6 +52,12 @@ public function getReactions(): iterable
return new Collection();
}

public function getReactionsBy(
Reacter $reacter,
): iterable {
return new Collection();
}

public function getReactionCounters(): iterable
{
return new Collection();
Expand Down
22 changes: 22 additions & 0 deletions src/Reactant/Models/Reactant.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Cog\Contracts\Love\Reactant\ReactionCounter\Models\ReactionCounter as ReactionCounterInterface;
use Cog\Contracts\Love\Reactant\ReactionTotal\Exceptions\ReactionTotalDuplicate;
use Cog\Contracts\Love\Reactant\ReactionTotal\Models\ReactionTotal as ReactionTotalInterface;
use Cog\Contracts\Love\Reacter\Models\Reacter;
use Cog\Contracts\Love\Reacter\Models\Reacter as ReacterInterface;
use Cog\Contracts\Love\Reaction\Models\Reaction as ReactionInterface;
use Cog\Contracts\Love\ReactionType\Models\ReactionType as ReactionTypeInterface;
Expand All @@ -33,6 +34,7 @@
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Support\Collection;

final class Reactant extends Model implements
ReactantInterface
Expand Down Expand Up @@ -96,6 +98,26 @@ public function getReactions(): iterable
return $this->getAttribute('reactions');
}

/**
* @return iterable|\Cog\Contracts\Love\Reaction\Models\Reaction[]
*/
public function getReactionsBy(
Reacter $reacter,
): iterable {
if ($reacter->isNull()) {
return new Collection();
}

// TODO: Test if relation was loaded partially
if ($this->relationLoaded('reactions')) {
return $this
->getAttribute('reactions')
->contains(fn (ReactionInterface $reaction) => $reaction->isByReacter($reacter));
}

return $this->reactions()->where('reacter_id', $reacter->getId())->get();
}

public function getReactionCounters(): iterable
{
return $this->getAttribute('reactionCounters');
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 @@ -19,6 +19,7 @@
use Cog\Laravel\Love\Reactant\ReactionCounter\Models\ReactionCounter;
use Cog\Laravel\Love\Reactant\ReactionTotal\Models\NullReactionTotal;
use Cog\Laravel\Love\Reactant\ReactionTotal\Models\ReactionTotal;
use Cog\Laravel\Love\Reacter\Models\Reacter;
use Cog\Laravel\Love\Reaction\Models\Reaction;
use Cog\Laravel\Love\ReactionType\Models\ReactionType;
use Cog\Tests\Laravel\Love\Stubs\Models\User;
Expand All @@ -43,6 +44,27 @@ public function it_can_get_reactions(): void
$this->assertTrue($assertReactions->get(1)->is($reactions->get(1)));
}

/** @test */
public function it_can_get_reactions_by_user(): void
{
$reactant = Reactant::factory()->create();
$reacter = Reacter::factory()->create();
$reactions = Reaction::factory()->count(2)->create([
'reactant_id' => $reactant->getId(),
'reacter_id' => $reacter->getId(),
]);
Reaction::factory()->count(3)->create([
'reactant_id' => $reactant->getId(),
]);
$reactantFacade = new ReactantFacade($reactant);

$assertReactions = $reactantFacade->getReactions();

$this->assertCount(2, $reactions);
$this->assertTrue($assertReactions->get(0)->is($reactions->get(0)));
$this->assertTrue($assertReactions->get(1)->is($reactions->get(1)));
}

/** @test */
public function it_can_get_reaction_counters(): void
{
Expand Down
10 changes: 8 additions & 2 deletions tests/Unit/Reactant/Models/NullReactantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
use Cog\Laravel\Love\Reactant\Models\Reactant;
use Cog\Laravel\Love\Reactant\ReactionCounter\Models\NullReactionCounter;
use Cog\Laravel\Love\Reactant\ReactionTotal\Models\NullReactionTotal;
use Cog\Laravel\Love\Reacter\Models\NullReacter;
use Cog\Laravel\Love\Reacter\Models\Reacter;
use Cog\Laravel\Love\ReactionType\Models\ReactionType;
use Cog\Tests\Laravel\Love\Stubs\Models\Article;
use Cog\Tests\Laravel\Love\Stubs\Models\User;
use Cog\Tests\Laravel\Love\TestCase;
use Illuminate\Support\Collection;
use TypeError;
Expand Down Expand Up @@ -56,17 +58,21 @@ public function it_can_get_reactions(): void
$reactions = $reactant->getReactions();

$this->assertCount(0, $reactions);
$this->assertInstanceOf(Collection::class, $reactions);
$this->assertIsIterable($reactions);
}

/** @test */
public function it_can_get_reactions_collection(): void
public function it_can_get_reactions_by_user(): void
{
$reactant = new NullReactant(new Article());
$reacter = new NullReacter(new User());

$reactions = $reactant->getReactions();
$reactions = $reactant->getReactionsBy($reacter);

$this->assertCount(0, $reactions);
$this->assertInstanceOf(Collection::class, $reactions);
$this->assertIsIterable($reactions);
}

/** @test */
Expand Down
39 changes: 39 additions & 0 deletions tests/Unit/Reactant/Models/ReactantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Cog\Laravel\Love\ReactionType\Models\ReactionType;
use Cog\Tests\Laravel\Love\Stubs\Models\Article;
use Cog\Tests\Laravel\Love\Stubs\Models\Bot;
use Cog\Tests\Laravel\Love\Stubs\Models\User;
use Cog\Tests\Laravel\Love\TestCase;
use Illuminate\Support\Facades\Event;
use TypeError;
Expand Down Expand Up @@ -200,6 +201,44 @@ public function it_can_get_reactions(): void
$this->assertTrue($assertReactions->get(1)->is($reactions->get(1)));
}

/** @test */
public function it_can_get_reactions_by_reacter(): void
{
$reactionType = ReactionType::factory()->create();
$reactant = Reactant::factory()->create();
$reacter = Reacter::factory()->create();

$reactions = Reaction::factory()->count(2)->create([
'reaction_type_id' => $reactionType->getId(),
'reactant_id' => $reactant->getId(),
'reacter_id' => $reacter->getId(),
]);
Reaction::factory()->count(3)->create([
'reaction_type_id' => $reactionType->getId(),
'reactant_id' => $reactant->getId(),
]);

$assertReactions = $reactant->getReactionsBy($reacter);
$this->assertTrue($assertReactions->get(0)->is($reactions->get(0)));
$this->assertTrue($assertReactions->get(1)->is($reactions->get(1)));
}

/** @test */
public function it_can_get_reactions_by_null_reacter(): void
{
$reactionType = ReactionType::factory()->create();
$reactant = Reactant::factory()->create();
$nullReacter = new NullReacter(new User());

$reactions = Reaction::factory()->count(3)->create([
'reaction_type_id' => $reactionType->getId(),
'reactant_id' => $reactant->getId(),
]);

$assertReactions = $reactant->getReactionsBy($nullReacter);
$this->assertCount(0, $assertReactions);
}

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

0 comments on commit aef13fa

Please sign in to comment.