xtremer360
7 months ago

Assertions with Datetime of MorphByMany Relationships

Posted 7 months ago by xtremer360

Hi all. I'm trying to figure out what my assertion would look like for test below.

Currently, my Stable factory when created will create a default wrestler for its group. As you can see with the test below after the wrestler is created for the stable I am retrieving the current wrestler(s) for the stable. The reason for the ->whereNull('left_at') constraint is because I need to constrain it to wrestlers that are currently in the stable. Then after that line, I am creating two new stable wrestlers to join the stable.

I run the patch request to the update method for a stable and pass along an array of ids of the new wrestlers to join the stable. In the update method, I have also provided I am trying to update the current collection of wrestlers in the stable as who was passed in and marking those wrestlers that were previously in the stable as having left the stable.

What I am wanting to achieve is with my assertion try and test that those wrestlers that are no longer in the stable as having left. What is making this difficult is because if this isn't the first time a wrestler doesn't join and leave the stable. I don't want it updating that first occurrence with the current date timestamp.

Any suggestions on how I should construct my assertion? Also, any suggestions on how I could refactor to hiding some of the controller code behind a method or two as that is a practice I subscribe to. Hiding eloquent calls to the models responsible.

/** @test */
    public function wrestlers_in_a_stable_that_are_not_included_request_are_marked_as_left()
    {
        $this->actAs('administrator');
        $stable = factory(Stable::class)->create();
        $formerWrestlers = $stable->wrestlers()->whereNull('left_at')->get();
        $newStableWrestlers = factory(Wrestler::class, 2)->create();

        $response = $this->patch(route('stables.update', $stable), $this->validParams([
            'wrestlers' => $newStableWrestlers->modelKeys(),
        ]));

        tap($stable->fresh()->wrestlers()->whereNull('left_at')->get(), function ($stableWrestlers) use ($formerWrestlers) {
            $this->assertEquals($stableWrestlers->modelKeys(), $formerWrestlers->modelKeys());
        });
    }
public function update(UpdateStableRequest $request, Stable $stable)
    {
        $stable->update($request->except('wrestlers', 'tagteams'));

        $newStableWrestlers = $request->input('wrestlers');
        $newStableTagTeams = $request->input('tagteams');

        $currentStableWrestlers = $stable->wrestlers()->whereNull('left_at')->get()->pluck('id');
        $currentStableTagTeams = $stable->tagteams()->whereNull('left_at')->get()->pluck('id');

        $formerStableWrestlers = $currentStableWrestlers->diff(collect($newStableWrestlers));
        $formerStableTagTeams = $currentStableTagTeams->diff(collect($newStableTagTeams));

        $stable->wrestlers()->updateExistingPivot($formerStableWrestlers, ['left_at' => now()]);
        $stable->tagteams()->updateExistingPivot($formerStableTagTeams, ['left_at' => now()]);

        $stable->wrestlers()->syncWithoutDetaching($newStableWrestlers);
        $stable->tagteams()->syncWithoutDetaching($newStableTagTeams);

        return redirect()->route('stables.index');
    }
// Stable Model
/**
     * Get all wrestlers that have been members of the stable.
     *
     * @return \Illuminate\Database\Eloquent\Relations\MorphByMany
     */
    public function wrestlers()
    {
        return $this->morphedByMany(Wrestler::class, 'member');
    }

Please sign in or create an account to participate in this conversation.