Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix discarded attribute violation reporter not accepting multiple property names #890

Merged
merged 2 commits into from Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -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<int, string> $propertyOrProperties */
public function __invoke(Model $model, $propertyOrProperties): void
{
$property = is_array($propertyOrProperties)
? implode(', ', $propertyOrProperties)
: $propertyOrProperties;

if (!$this->shouldReport($model, $property)) {
return;
}
Expand Down
30 changes: 30 additions & 0 deletions test/Sentry/Integration/ModelViolationReportersTest.php
Expand Up @@ -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;
Expand Down