Skip to content

Commit dc599ef

Browse files
authored
test: Add tests for ExceptionDataBag (#1358)
1 parent e4db1c4 commit dc599ef

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

tests/ExceptionDataBagTest.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sentry\Tests;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Sentry\ExceptionDataBag;
9+
use Sentry\ExceptionMechanism;
10+
use Sentry\Frame;
11+
use Sentry\Stacktrace;
12+
13+
final class ExceptionDataBagTest extends TestCase
14+
{
15+
/**
16+
* @dataProvider constructorDataProvider
17+
*/
18+
public function testConstructor(array $constructorArgs, string $expectedType, string $expectedValue, ?Stacktrace $expectedStackTrace, ?ExceptionMechanism $expectedExceptionMechansim)
19+
{
20+
$exceptionDataBag = new ExceptionDataBag(...$constructorArgs);
21+
22+
$this->assertSame($expectedType, $exceptionDataBag->getType());
23+
$this->assertSame($expectedValue, $exceptionDataBag->getValue());
24+
$this->assertSame($expectedStackTrace, $exceptionDataBag->getStacktrace());
25+
$this->assertSame($expectedExceptionMechansim, $exceptionDataBag->getMechanism());
26+
}
27+
28+
public function constructorDataProvider(): \Generator
29+
{
30+
yield [
31+
[
32+
new \RuntimeException('foo bar'),
33+
null,
34+
null,
35+
],
36+
\RuntimeException::class,
37+
'foo bar',
38+
null,
39+
null,
40+
];
41+
42+
$strackTarce = new Stacktrace([
43+
new Frame('test_function', '/path/to/file', 10, null, '/path/to/file'),
44+
]);
45+
$exceptionMechansim = new ExceptionMechanism(ExceptionMechanism::TYPE_GENERIC, false);
46+
47+
yield [
48+
[
49+
new \RuntimeException('foo bar'),
50+
$strackTarce,
51+
$exceptionMechansim,
52+
],
53+
\RuntimeException::class,
54+
'foo bar',
55+
$strackTarce,
56+
$exceptionMechansim,
57+
];
58+
}
59+
60+
public function testSetType(): void
61+
{
62+
$exceptionDataBag = new ExceptionDataBag(new \RuntimeException());
63+
64+
$exceptionDataBag->setType('foo bar');
65+
66+
$this->assertSame('foo bar', $exceptionDataBag->getType());
67+
}
68+
69+
public function testSetValue(): void
70+
{
71+
$exceptionDataBag = new ExceptionDataBag(new \RuntimeException());
72+
73+
$exceptionDataBag->setValue('foo bar');
74+
75+
$this->assertSame('foo bar', $exceptionDataBag->getValue());
76+
}
77+
78+
public function testSetStacktrace(): void
79+
{
80+
$exceptionDataBag = new ExceptionDataBag(new \RuntimeException());
81+
82+
$stacktrace = new Stacktrace([
83+
new Frame('test_function', '/path/to/file', 10, null, '/path/to/file'),
84+
]);
85+
86+
$exceptionDataBag->setStacktrace($stacktrace);
87+
88+
$this->assertSame($stacktrace, $exceptionDataBag->getStacktrace());
89+
}
90+
91+
public function testSetMechanism(): void
92+
{
93+
$exceptionDataBag = new ExceptionDataBag(new \RuntimeException());
94+
$exceptionMechanism = new ExceptionMechanism(ExceptionMechanism::TYPE_GENERIC, false);
95+
96+
$exceptionDataBag->setMechanism($exceptionMechanism);
97+
98+
$this->assertSame($exceptionMechanism, $exceptionDataBag->getMechanism());
99+
}
100+
}

0 commit comments

Comments
 (0)