Skip to content

Commit f210459

Browse files
committed
Add attribute bag tests
1 parent d62da81 commit f210459

2 files changed

Lines changed: 77 additions & 1 deletion

File tree

src/Attributes/AttributeBag.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @phpstan-import-type AttributeValue from Attribute
99
* @phpstan-import-type AttributeSerialized from Attribute
1010
*/
11-
class AttributeBag
11+
class AttributeBag implements \JsonSerializable
1212
{
1313
/**
1414
* @var array<string, Attribute>
@@ -54,6 +54,14 @@ public function toArray(): array
5454
}, $this->attributes);
5555
}
5656

57+
/**
58+
* @return array<string, AttributeSerialized>
59+
*/
60+
public function jsonSerialize(): array
61+
{
62+
return $this->toArray();
63+
}
64+
5765
/**
5866
* @return array<string, AttributeValue>
5967
*/
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sentry\Tests\Attributes;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Sentry\Attributes\Attribute;
9+
use Sentry\Attributes\AttributeBag;
10+
11+
/**
12+
* @phpstan-import-type AttributeValue from Attribute
13+
* @phpstan-import-type AttributeSerialized from Attribute
14+
*/
15+
final class AttributeBagTest extends TestCase
16+
{
17+
public function testGettersAndSetters(): void
18+
{
19+
$bag = new AttributeBag();
20+
21+
$this->assertCount(0, $bag->all());
22+
23+
$bag->set('foo', 'bar');
24+
25+
$this->assertCount(1, $bag->all());
26+
$this->assertInstanceOf(Attribute::class, $bag->get('foo'));
27+
28+
$this->assertNull($bag->get('non-existing'));
29+
}
30+
31+
public function testSerializeAsJson(): void
32+
{
33+
$bag = new AttributeBag();
34+
$bag->set('foo', 'bar');
35+
36+
$this->assertEquals(
37+
['foo' => ['type' => 'string', 'value' => 'bar']],
38+
$bag->jsonSerialize()
39+
);
40+
41+
$this->assertEquals(
42+
'{"foo":{"type":"string","value":"bar"}}',
43+
json_encode($bag)
44+
);
45+
}
46+
47+
public function testSerializeAsArray(): void
48+
{
49+
$bag = new AttributeBag();
50+
$bag->set('foo', 'bar');
51+
52+
$this->assertEquals(
53+
['foo' => ['type' => 'string', 'value' => 'bar']],
54+
$bag->toArray()
55+
);
56+
}
57+
58+
public function testSerializeAsSimpleArray(): void
59+
{
60+
$bag = new AttributeBag();
61+
$bag->set('foo', 'bar');
62+
63+
$this->assertEquals(
64+
['foo' => 'bar'],
65+
$bag->toSimpleArray()
66+
);
67+
}
68+
}

0 commit comments

Comments
 (0)