Skip to content

Commit 248d904

Browse files
authored
Merge pull request #19 from tattersoftware/update-multiple
fix: Update ID conversion
2 parents 625706f + 6899999 commit 248d904

File tree

4 files changed

+58
-8
lines changed

4 files changed

+58
-8
lines changed

src/Traits/AuditsTrait.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,15 @@ protected function auditInsert(array $data)
7676
// record successful update events
7777
protected function auditUpdate(array $data)
7878
{
79-
$audit = [
80-
'source' => $this->table,
81-
'source_id' => $data['id'],
82-
'event' => 'update',
83-
'summary' => count($data['data']) . ' fields',
84-
];
85-
service('audits')->add($audit);
79+
foreach ($data['id'] as $sourceId) {
80+
$audit = [
81+
'source' => $this->table,
82+
'source_id' => $sourceId,
83+
'event' => 'update',
84+
'summary' => count($data['data']) . ' fields',
85+
];
86+
service('audits')->add($audit);
87+
}
8688

8789
return $data;
8890
}

tests/_support/DatabaseTestCase.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ protected function setUp(): void
6262
$this->fabricator = new Fabricator($this->model);
6363
}
6464

65+
protected function tearDown(): void
66+
{
67+
parent::tearDown();
68+
69+
$this->resetServices();
70+
}
71+
6572
/**
6673
* Asserts that an audit with the given properties is in the queue
6774
*

tests/unit/LibraryTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
use Tests\Support\DatabaseTestCase;
4+
use Tests\Support\Models\WidgetModel;
5+
6+
/**
7+
* @internal
8+
*/
9+
final class LibraryTest extends DatabaseTestCase
10+
{
11+
public function testSaves()
12+
{
13+
$widget = fake(WidgetModel::class);
14+
15+
$this->seeInDatabase('widgets', (array) $widget);
16+
}
17+
}

tests/unit/TraitTest.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function testUpdateAddsAudit()
4040

4141
$expected = [
4242
'source' => 'widgets',
43-
'source_id' => [$widgetId],
43+
'source_id' => $widgetId,
4444
'event' => 'update',
4545
'summary' => '2 fields',
4646
'user_id' => 0,
@@ -51,4 +51,28 @@ public function testUpdateAddsAudit()
5151
$this->assertCount(2, $queue);
5252
$this->seeAudit($expected);
5353
}
54+
55+
public function testUpdateAddsMultiple()
56+
{
57+
$widget1 = fake(WidgetModel::class);
58+
$widget2 = fake(WidgetModel::class);
59+
$ids = [$widget1->id, $widget2->id]; // @phpstan-ignore-line
60+
61+
$this->model->update($ids, [
62+
'name' => 'Banana Widget',
63+
]);
64+
65+
$expected = [
66+
'source' => 'widgets',
67+
'source_id' => $ids[0],
68+
'event' => 'update',
69+
'summary' => '2 fields',
70+
'user_id' => 0,
71+
];
72+
73+
$queue = service('audits')->getQueue();
74+
75+
$this->assertCount(4, $queue);
76+
$this->seeAudit($expected);
77+
}
5478
}

0 commit comments

Comments
 (0)