forked from getsentry/sentry-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContextTest.php
115 lines (79 loc) · 2.88 KB
/
ContextTest.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
declare(strict_types=1);
namespace Sentry\Tests\Context;
use PHPUnit\Framework\TestCase;
use Sentry\Context\Context;
class ContextTest extends TestCase
{
public function testConstructor(): void
{
$context = new Context(['foo' => 'bar']);
$this->assertSame(['foo' => 'bar'], $context->toArray());
}
public function testMerge(): void
{
$context = new Context([
'foo' => 'bar',
'bar' => [
'foobar' => 'barfoo',
],
]);
$context->merge(['bar' => ['barfoo' => 'foobar']], true);
$this->assertSame(['foo' => 'bar', 'bar' => ['foobar' => 'barfoo', 'barfoo' => 'foobar']], $context->toArray());
$context->merge(['bar' => 'foo']);
$this->assertSame(['foo' => 'bar', 'bar' => 'foo'], $context->toArray());
}
public function testSetData(): void
{
$context = new Context(['foo' => 'bar']);
$context->setData(['bar' => 'foo']);
$this->assertSame(['foo' => 'bar', 'bar' => 'foo'], $context->toArray());
$context->setData(['foo' => ['bar' => 'baz']]);
$this->assertSame(['foo' => ['bar' => 'baz'], 'bar' => 'foo'], $context->toArray());
}
public function testReplaceData(): void
{
$context = new Context(['foo' => 'bar']);
$context->replaceData(['bar' => 'foo']);
$this->assertSame(['bar' => 'foo'], $context->toArray());
}
public function testClear(): void
{
$context = new Context(['foo' => 'bar']);
$this->assertSame(['foo' => 'bar'], $context->toArray());
$context->clear();
$this->assertSame([], $context->toArray());
}
public function testIsEmpty(): void
{
$context = new Context();
$this->assertTrue($context->isEmpty());
$context->setData(['foo' => 'bar']);
$this->assertFalse($context->isEmpty());
}
public function testJsonSerialize(): void
{
$context = new Context(['foo' => 'bar']);
$this->assertSame('{"foo":"bar"}', json_encode($context));
}
public function testArrayLikeBehaviour(): void
{
$context = new Context();
// Accessing a key that does not exists in the data object should behave
// like accessing a non-existent key of an array
@$context['foo'];
$error = error_get_last();
$this->assertIsArray($error);
$this->assertSame('Undefined index: foo', $error['message']);
$context['foo'] = 'bar';
$this->assertTrue(isset($context['foo']));
$this->assertSame('bar', $context['foo']);
unset($context['foo']);
$this->assertArrayNotHasKey('foo', $context);
}
public function testGetIterator(): void
{
$context = new Context(['foo' => 'bar', 'bar' => 'foo']);
$this->assertSame($context->toArray(), iterator_to_array($context));
}
}