-
Notifications
You must be signed in to change notification settings - Fork 49
feat(activity): model UserReaction + factory e relação no Timeline (#542) #560
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b0d29b0
feat(reaction): add UserReaction model (#542)
BrunaDomingues 32d35a0
feat(reaction): add ReactionFactory for UserReaction (#542)
BrunaDomingues b21493c
feat(activity): add userReactions relation on Timeline (#542)
BrunaDomingues 785bdd1
test(activity): cover UserReaction factory and timeline relation (#542)
BrunaDomingues 7786c06
refactor(activity): rename factory to UserReactionFactory and expand …
BrunaDomingues 792e3a3
test(activity): isolate unique violation in a savepoint for pgsql (#542)
BrunaDomingues File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
app-modules/activity/database/factories/UserReactionFactory.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace He4rt\Activity\Database\Factories; | ||
|
|
||
| use He4rt\Activity\Reaction\Enums\TimelineReaction; | ||
| use He4rt\Activity\Reaction\Models\UserReaction; | ||
| use He4rt\Activity\Timeline\Timeline; | ||
| use He4rt\Identity\User\Models\User; | ||
| use Illuminate\Database\Eloquent\Factories\Factory; | ||
|
|
||
| /** @extends Factory<UserReaction> */ | ||
| final class UserReactionFactory extends Factory | ||
| { | ||
| protected $model = UserReaction::class; | ||
|
|
||
| /** @return array<string, mixed> */ | ||
| public function definition(): array | ||
| { | ||
| return [ | ||
| 'user_id' => User::factory(), | ||
| 'timeline_id' => Timeline::factory(), | ||
| 'reaction' => fake()->randomElement(TimelineReaction::cases()), | ||
| ]; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace He4rt\Activity\Reaction\Models; | ||
|
|
||
| use Carbon\CarbonInterface; | ||
| use He4rt\Activity\Database\Factories\UserReactionFactory; | ||
| use He4rt\Activity\Reaction\Enums\TimelineReaction; | ||
| use He4rt\Activity\Timeline\Timeline; | ||
| use He4rt\Identity\User\Models\User; | ||
| use Illuminate\Database\Eloquent\Attributes\Table; | ||
| use Illuminate\Database\Eloquent\Attributes\UseFactory; | ||
| use Illuminate\Database\Eloquent\Concerns\HasUuids; | ||
| use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
| use Illuminate\Database\Eloquent\Model; | ||
| use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
|
|
||
| /** | ||
| * @property string $id | ||
| * @property string $user_id | ||
| * @property string $timeline_id | ||
| * @property TimelineReaction $reaction | ||
| * @property CarbonInterface|null $created_at | ||
| * @property CarbonInterface|null $updated_at | ||
| * @property-read User $user | ||
| * @property-read Timeline $timeline | ||
| */ | ||
| #[UseFactory(factoryClass: UserReactionFactory::class)] | ||
| #[Table(name: 'activity_user_reactions')] | ||
| final class UserReaction extends Model | ||
| { | ||
| /** @use HasFactory<UserReactionFactory> */ | ||
| use HasFactory; | ||
| use HasUuids; | ||
|
|
||
| /** @return BelongsTo<User, $this> */ | ||
| public function user(): BelongsTo | ||
| { | ||
| return $this->belongsTo(User::class); | ||
| } | ||
|
|
||
| /** @return BelongsTo<Timeline, $this> */ | ||
| public function timeline(): BelongsTo | ||
| { | ||
| return $this->belongsTo(Timeline::class); | ||
| } | ||
|
|
||
| /** @return array<string, string> */ | ||
| protected function casts(): array | ||
| { | ||
| return [ | ||
| 'reaction' => TimelineReaction::class, | ||
| ]; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
app-modules/activity/tests/Unit/Reaction/UserReactionTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use He4rt\Activity\Reaction\Enums\TimelineReaction; | ||
| use He4rt\Activity\Reaction\Models\UserReaction; | ||
| use He4rt\Activity\Timeline\Timeline; | ||
| use He4rt\Identity\User\Models\User; | ||
| use Illuminate\Database\QueryException; | ||
| use Illuminate\Foundation\Testing\RefreshDatabase; | ||
| use Illuminate\Support\Facades\DB; | ||
|
|
||
| uses(RefreshDatabase::class); | ||
|
|
||
| it('cria uma linha válida via factory com reaction como enum', function (): void { | ||
| UserReaction::factory()->create(); | ||
|
|
||
| $reaction = UserReaction::query()->first(); | ||
|
|
||
| expect($reaction)->not->toBeNull() | ||
| ->and($reaction->reaction)->toBeInstanceOf(TimelineReaction::class); | ||
|
|
||
| $this->assertDatabaseCount('activity_user_reactions', 1); | ||
| }); | ||
|
|
||
| it('pertence ao usuário que reagiu', function (): void { | ||
| $user = User::factory()->create(); | ||
| $reaction = UserReaction::factory()->for($user)->create(); | ||
|
|
||
| expect($reaction->user)->toBeInstanceOf(User::class) | ||
| ->and($reaction->user->id)->toBe($user->id); | ||
| }); | ||
|
|
||
| it('devolve as reações do post com os usuários donos', function (): void { | ||
| $timeline = Timeline::factory()->create(); | ||
| $users = User::factory()->count(3)->create(); | ||
|
|
||
| foreach ($users as $user) { | ||
| UserReaction::factory() | ||
| ->for($timeline) | ||
| ->for($user) | ||
| ->create(); | ||
| } | ||
|
|
||
| $owners = $timeline->userReactions()->with('user')->get()->map( | ||
| fn (UserReaction $reaction): string => $reaction->user->id, | ||
| ); | ||
|
|
||
| expect($timeline->userReactions)->toHaveCount(3) | ||
| ->and($owners->all())->toEqualCanonicalizing($users->pluck('id')->all()); | ||
| }); | ||
|
|
||
| it('rejeita segunda reação do mesmo usuário no mesmo post', function (): void { | ||
| $user = User::factory()->create(); | ||
| $timeline = Timeline::factory()->create(); | ||
|
|
||
| UserReaction::factory()->for($user)->for($timeline)->create(); | ||
|
|
||
| expect(fn () => DB::transaction( | ||
| fn () => UserReaction::factory()->for($user)->for($timeline)->create(), | ||
| ))->toThrow(QueryException::class); | ||
|
|
||
| $this->assertDatabaseCount('activity_user_reactions', 1); | ||
| }); | ||
|
|
||
| it('rejeita reaction fora do conjunto do enum', function (): void { | ||
| expect(fn () => UserReaction::factory()->create(['reaction' => 'xpto'])) | ||
| ->toThrow(ValueError::class); | ||
| }); | ||
|
BrunaDomingues marked this conversation as resolved.
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.