Skip to content

Commit

Permalink
Fix discarded attribute violation reporter not accepting multiple pro…
Browse files Browse the repository at this point in the history
…perty names (#890)
  • Loading branch information
stayallive committed Apr 24, 2024
1 parent 44345a9 commit 0c2fc7b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
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

0 comments on commit 0c2fc7b

Please sign in to comment.