-
-
Notifications
You must be signed in to change notification settings - Fork 184
/
ModelViolationReportersTest.php
100 lines (73 loc) · 3.48 KB
/
ModelViolationReportersTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
namespace Sentry\Integration;
use Illuminate\Database\Eloquent\MissingAttributeException;
use Illuminate\Database\Eloquent\Model;
use Sentry\Laravel\Integration;
use Sentry\Laravel\Tests\TestCase;
/**
* Since large parts of the violation reporters are shared between the different types of violations,
* we try to test only a single type of violation reporter to keep the test cases a bit smaller when possible.
*/
class ModelViolationReportersTest extends TestCase
{
protected function setUp(): void
{
if (!class_exists(MissingAttributeException::class)) {
$this->markTestSkipped('Laravel introduced model violations in version 9.');
}
parent::setUp();
}
public function testModelViolationReportersCanBeRegistered(): void
{
$this->expectNotToPerformAssertions();
Model::handleLazyLoadingViolationUsing(Integration::lazyLoadingViolationReporter());
Model::handleMissingAttributeViolationUsing(Integration::missingAttributeViolationReporter());
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;
$reporter = Integration::missingAttributeViolationReporter(static function () use (&$callbackCalled) {
$callbackCalled = true;
}, false, false);
$reporter(new ViolationReporterTestModel, 'attribute');
$this->assertTrue($callbackCalled);
}
public function testViolationReporterIsNotReportingDuplicateEvents(): void
{
$reporter = Integration::missingAttributeViolationReporter(null, true, false);
$reporter(new ViolationReporterTestModel, 'attribute');
$reporter(new ViolationReporterTestModel, 'attribute');
$this->assertCount(1, $this->getCapturedSentryEvents());
}
public function testViolationReporterIsReportingDuplicateEventsIfConfigured(): void
{
$reporter = Integration::missingAttributeViolationReporter(null, false, false);
$reporter(new ViolationReporterTestModel, 'attribute');
$reporter(new ViolationReporterTestModel, 'attribute');
$this->assertCount(2, $this->getCapturedSentryEvents());
}
}
class ViolationReporterTestModel extends Model
{
}