Skip to content
This repository has been archived by the owner on Sep 22, 2024. It is now read-only.

Commit

Permalink
add test coverage for resetting pivot changes
Browse files Browse the repository at this point in the history
  • Loading branch information
pactode committed Aug 5, 2019
1 parent f35c3aa commit 2d5f0c1
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions tests/PivotEventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,31 @@ public function it_dispatches_pivot_update_events()
Event::assertDispatched('eloquent.pivotUpdated: '.User::class);
}

/** @test */
public function it_resets_pivot_changes_after_an_event_has_been_dispatched()
{
$user = $this->createUser();
$roleA = $this->createRole(['name' => 'role-a']);
$roleB = $this->createRole(['name' => 'role-b']);

$calledTimes = 0;

User::pivotAttached(function ($model) use (&$calledTimes) {
$this->assertCount(1, $model->getPivotChanges());
$calledTimes++;
});

$user->roles()->attach($roleA->id);
$this->assertCount(0, $user->getPivotChanges());

$user->roles()->attach($roleB->id);
$this->assertCount(0, $user->getPivotChanges());

$this->assertCount(2, $user->roles);

$this->assertEquals(2, $calledTimes);
}

protected function createRole(array $overwrites = [])
{
return Role::create(array_merge([
Expand Down

0 comments on commit 2d5f0c1

Please sign in to comment.