|
| 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