From 0c2fc7ba26f7d31a5f1be676ad3f7e023f975c1b Mon Sep 17 00:00:00 2001 From: Alex Bouma Date: Wed, 24 Apr 2024 10:55:22 +0200 Subject: [PATCH] Fix discarded attribute violation reporter not accepting multiple property names (#890) --- .../ModelViolationReporter.php | 7 ++++- .../ModelViolationReportersTest.php | 30 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/Sentry/Laravel/Integration/ModelViolations/ModelViolationReporter.php b/src/Sentry/Laravel/Integration/ModelViolations/ModelViolationReporter.php index f2b3f4da..952920ed 100644 --- a/src/Sentry/Laravel/Integration/ModelViolations/ModelViolationReporter.php +++ b/src/Sentry/Laravel/Integration/ModelViolations/ModelViolationReporter.php @@ -35,8 +35,13 @@ public function __construct(?callable $callback, bool $suppressDuplicateReports, $this->reportAfterResponse = $reportAfterResponse; } - public function __invoke(Model $model, string $property): void + /** @param string|array $propertyOrProperties */ + public function __invoke(Model $model, $propertyOrProperties): void { + $property = is_array($propertyOrProperties) + ? implode(', ', $propertyOrProperties) + : $propertyOrProperties; + if (!$this->shouldReport($model, $property)) { return; } diff --git a/test/Sentry/Integration/ModelViolationReportersTest.php b/test/Sentry/Integration/ModelViolationReportersTest.php index 2e94d9ad..39002384 100644 --- a/test/Sentry/Integration/ModelViolationReportersTest.php +++ b/test/Sentry/Integration/ModelViolationReportersTest.php @@ -31,6 +31,36 @@ public function testModelViolationReportersCanBeRegistered(): void Model::handleDiscardedAttributeViolationUsing(Integration::discardedAttributeViolationReporter()); } + public function testViolationReporterAcceptsSingleProperty(): void + { + $reporter = Integration::discardedAttributeViolationReporter(null, true, false); + + $reporter(new ViolationReporterTestModel, 'foo'); + + $this->assertCount(1, $this->getCapturedSentryEvents()); + + $violation = $this->getLastSentryEvent()->getContexts()['violation']; + + $this->assertSame('foo', $violation['attribute']); + $this->assertSame('discarded_attribute', $violation['kind']); + $this->assertSame(ViolationReporterTestModel::class, $violation['model']); + } + + public function testViolationReporterAcceptsListOfProperties(): void + { + $reporter = Integration::discardedAttributeViolationReporter(null, true, false); + + $reporter(new ViolationReporterTestModel, ['foo', 'bar']); + + $this->assertCount(1, $this->getCapturedSentryEvents()); + + $violation = $this->getLastSentryEvent()->getContexts()['violation']; + + $this->assertSame('foo, bar', $violation['attribute']); + $this->assertSame('discarded_attribute', $violation['kind']); + $this->assertSame(ViolationReporterTestModel::class, $violation['model']); + } + public function testViolationReporterPassesThroughToCallback(): void { $callbackCalled = false;