Skip to content

Commit 0c2fc7b

Browse files
authored
Fix discarded attribute violation reporter not accepting multiple property names (#890)
1 parent 44345a9 commit 0c2fc7b

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/Sentry/Laravel/Integration/ModelViolations/ModelViolationReporter.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,13 @@ public function __construct(?callable $callback, bool $suppressDuplicateReports,
3535
$this->reportAfterResponse = $reportAfterResponse;
3636
}
3737

38-
public function __invoke(Model $model, string $property): void
38+
/** @param string|array<int, string> $propertyOrProperties */
39+
public function __invoke(Model $model, $propertyOrProperties): void
3940
{
41+
$property = is_array($propertyOrProperties)
42+
? implode(', ', $propertyOrProperties)
43+
: $propertyOrProperties;
44+
4045
if (!$this->shouldReport($model, $property)) {
4146
return;
4247
}

test/Sentry/Integration/ModelViolationReportersTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,36 @@ public function testModelViolationReportersCanBeRegistered(): void
3131
Model::handleDiscardedAttributeViolationUsing(Integration::discardedAttributeViolationReporter());
3232
}
3333

34+
public function testViolationReporterAcceptsSingleProperty(): void
35+
{
36+
$reporter = Integration::discardedAttributeViolationReporter(null, true, false);
37+
38+
$reporter(new ViolationReporterTestModel, 'foo');
39+
40+
$this->assertCount(1, $this->getCapturedSentryEvents());
41+
42+
$violation = $this->getLastSentryEvent()->getContexts()['violation'];
43+
44+
$this->assertSame('foo', $violation['attribute']);
45+
$this->assertSame('discarded_attribute', $violation['kind']);
46+
$this->assertSame(ViolationReporterTestModel::class, $violation['model']);
47+
}
48+
49+
public function testViolationReporterAcceptsListOfProperties(): void
50+
{
51+
$reporter = Integration::discardedAttributeViolationReporter(null, true, false);
52+
53+
$reporter(new ViolationReporterTestModel, ['foo', 'bar']);
54+
55+
$this->assertCount(1, $this->getCapturedSentryEvents());
56+
57+
$violation = $this->getLastSentryEvent()->getContexts()['violation'];
58+
59+
$this->assertSame('foo, bar', $violation['attribute']);
60+
$this->assertSame('discarded_attribute', $violation['kind']);
61+
$this->assertSame(ViolationReporterTestModel::class, $violation['model']);
62+
}
63+
3464
public function testViolationReporterPassesThroughToCallback(): void
3565
{
3666
$callbackCalled = false;

0 commit comments

Comments
 (0)