Skip to content

Commit

Permalink
[item] split test into multiple
Browse files Browse the repository at this point in the history
  • Loading branch information
rastislav-chynoransky authored and eronisko committed Sep 28, 2023
1 parent 046087a commit 14bbfed
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 34 deletions.
3 changes: 2 additions & 1 deletion app/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -779,8 +779,9 @@ public function getImageUrlAttribute()
return sprintf('%s%s', config('app.url'), $this->getImagePath());
}

public function syncMatchedAuthorities(\Illuminate\Support\Collection $idsWithPivotData)
public function syncMatchedAuthorities(\Illuminate\Support\Collection|array $idsWithPivotData)
{
$idsWithPivotData = collect($idsWithPivotData);
// Detach automatically-matched authorities that no longer match
$this->authorities()
->wherePivot('automatically_matched', true)
Expand Down
98 changes: 65 additions & 33 deletions tests/Models/ItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,43 +187,75 @@ protected function createFreeItem()
]);
}

public function testSyncMatchedAuthorities()
public function testSyncMatchedAuthoritiesUpdatesExisting()
{
$authorities = Authority::factory()
->count(5)
$item = Item::factory()->create();
[$authority1, $authority2] = Authority::factory()
->count(2)
->create();
$item->authorities()->sync([
$authority1->id => ['role' => 'author', 'automatically_matched' => true],
$authority2->id => ['role' => 'author', 'automatically_matched' => false],
]);

$item->syncMatchedAuthorities([
$authority1->id => ['role' => 'new-role-1'],
$authority2->id => ['role' => 'new-role-2'],
]);

$this->assertDatabaseHas('authority_item', [
'authority_id' => $authority1->id,
'item_id' => $item->id,
'role' => 'new-role-1',
'automatically_matched' => true,
]);
$this->assertDatabaseHas('authority_item', [
'authority_id' => $authority2->id,
'item_id' => $item->id,
'role' => 'new-role-2',
'automatically_matched' => false,
]);
}

public function testSyncMatchedAuthoritiesDeletesOnlyAutomaticallyMatched()
{
$item = Item::factory()->create();
[$authority1, $authority2] = Authority::factory()
->count(2)
->create();
$item->authorities()->sync([
// present in matched authorities
$authorities[0]->id => ['role' => 'author', 'automatically_matched' => true],
$authorities[1]->id => ['role' => 'author', 'automatically_matched' => false],
// missing from matched authorities
$authorities[2]->id => ['role' => 'author', 'automatically_matched' => true],
$authorities[3]->id => ['role' => 'author', 'automatically_matched' => false],
]);

$item->syncMatchedAuthorities(
collect([
$authorities[0]->id => ['role' => 'after'],
$authorities[1]->id => ['role' => 'after'],
$authorities[4]->id => ['role' => 'after'],
])
);
$authority1->id => ['automatically_matched' => true, 'role' => 'author'],
$authority2->id => ['automatically_matched' => false, 'role' => 'author'],
]);

$item->refresh();
$this->assertCount(4, $item->authorities);
$this->assertEquals(
[
$authorities[0]->id => ['role' => 'after', 'automatically_matched' => true],
$authorities[1]->id => ['role' => 'after', 'automatically_matched' => false],
$authorities[3]->id => ['role' => 'author', 'automatically_matched' => false],
$authorities[4]->id => ['role' => 'after', 'automatically_matched' => true],
],
$item->authorities
->pluck('pivot')
->keyBy('authority_id')
->map->only(['role', 'automatically_matched'])
->toArray()
);
$item->syncMatchedAuthorities([]);

$this->assertDatabaseMissing('authority_item', [
'authority_id' => $authority1->id,
'item_id' => $item->id,
]);
$this->assertDatabaseHas('authority_item', [
'authority_id' => $authority2->id,
'item_id' => $item->id,
'role' => 'author',
'automatically_matched' => false,
]);
}

public function testSyncMatchedAuthoritiesAddsNew()
{
$item = Item::factory()->create();
$authority = Authority::factory()->create();

$item->syncMatchedAuthorities([
$authority->id => ['role' => 'author'],
]);

$this->assertDatabaseHas('authority_item', [
'authority_id' => $authority->id,
'item_id' => $item->id,
'role' => 'author',
'automatically_matched' => true,
]);
}
}

0 comments on commit 14bbfed

Please sign in to comment.