ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
ValuesTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
25use ILIAS\Tests\KeyValueStorage\RefineryHelper;
26use PHPUnit\Framework\Attributes\DataProvider;
27use PHPUnit\Framework\TestCase;
28
29class ValuesTest extends TestCase
30{
31 use RefineryHelper;
32
33 private Values $values;
34
35 protected function setUp(): void
36 {
37 $this->values = new Values($this->refinery());
38 }
39
40 #[DataProvider('roundTrippableValues')]
41 public function testValuesSurviveARoundTrip(mixed $value): void
42 {
43 $this->assertSame($value, $this->values->decode($this->values->encode($value)));
44 }
45
49 public static function roundTrippableValues(): array
50 {
51 return [
52 'null' => [null],
53 'true' => [true],
54 'false' => [false],
55 'int' => [42],
56 'float' => [1.5],
57 'string' => ['title'],
58 'empty string' => [''],
59 'list' => [['a', 'b']],
60 'map' => [['sort' => 'title', 'direction' => 'asc']],
61 'nested' => [['filter' => ['status' => ['open', 'closed'], 'limit' => 10]]],
62 'empty array' => [[]],
63 ];
64 }
65
67 {
68 $value = new class () implements \JsonSerializable {
72 public function jsonSerialize(): array
73 {
74 return ['a' => 1];
75 }
76 };
77
78 $this->assertSame('{"a":1}', $this->values->encode($value));
79 $this->assertSame(['a' => 1], $this->values->decode($this->values->encode($value)));
80 }
81
83 {
84 $value = new class () implements \JsonSerializable {
85 public function jsonSerialize(): string
86 {
87 return 'title';
88 }
89 };
90
91 $this->assertSame('title', $this->values->decode($this->values->encode($value)));
92 }
93
95 {
96 $inner = new class () implements \JsonSerializable {
100 public function jsonSerialize(): array
101 {
102 return ['a' => 1];
103 }
104 };
105
106 $this->assertSame(
107 ['wrapped' => ['a' => 1]],
108 $this->values->decode($this->values->encode(['wrapped' => $inner]))
109 );
110 }
111
113 {
114 $value = new class () implements \JsonSerializable {
118 public function jsonSerialize(): array
119 {
120 return ['bad' => new \stdClass()];
121 }
122 };
123
124 $this->expectException(\InvalidArgumentException::class);
125
126 $this->values->encode($value);
127 }
128
129 #[DataProvider('unstorableValues')]
130 public function testUnstorableValuesAreRejected(mixed $value): void
131 {
132 $this->expectException(\InvalidArgumentException::class);
133
134 $this->values->encode($value);
135 }
136
140 public static function unstorableValues(): array
141 {
142 return [
143 'plain object' => [new \stdClass()],
144 'object in array' => [['a' => new \stdClass()]],
145 'closure' => [static fn() => null],
146 'not a number' => [NAN],
147 ];
148 }
149
150 public function testStoredGarbageIsReported(): void
151 {
152 $this->expectException(InvalidStoredValueException::class);
153
154 $this->values->decode('{not json');
155 }
156}
Translates between the values a consumer works with and the strings a repository stores.
Definition: Values.php:38