ILIAS  trunk Revision v12.0_alpha-1540-g00f839d5fa1
DMapTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
25namespace ILIAS\Data;
26
30use PHPUnit\Framework\TestCase;
31use PHPUnit\Framework\Attributes\DataProvider;
32
33class DMapTest extends TestCase
34{
35 protected DMap $m;
36 protected DValue $k;
37 protected Description $v;
38
39 public function setUp(): void
40 {
41 $this->k = $this->createMock(DValue::class);
42 $this->v = $this->createMock(Description::class);
43 $this->m = new DMap(
44 $this->createMock(\ILIAS\Data\Text\SimpleDocumentMarkdown::class),
45 $this->k,
46 $this->v
47 );
48 }
49
50 #[DataProvider('obviousNoMatchProvider')]
51 public function testObviouslyNotMatching($data): void
52 {
53 $res = $this->m->getPrimitiveRepresentation($data);
54
55 $this->assertInstanceOf(\Closure::class, $res);
56 $errors = iterator_to_array($res());
57 $this->assertCount(1, $errors);
58 $this->assertTrue(is_string($errors[0]));
59 }
60
61 public static function obviousNoMatchProvider(): array
62 {
63 return [
64 [1], ["1"], [null], [true], [new \StdClass()], [new \DateTimeImmutable()]
65 ];
66 }
67
68 public function testEmptyMatches(): void
69 {
70 $res = $this->m->getPrimitiveRepresentation([]);
71
72 $this->assertEquals([], $res);
73 }
74
75 public function testForwardsToSubDescriptions(): void
76 {
77 $data = [
78 "a" => 1,
79 "b" => 2
80 ];
81
82 $keys = ["c", "d"];
83 $this->k->expects($this->exactly(2))
84 ->method("getPrimitiveRepresentation")
85 ->willReturnCallback(function ($v) use (&$keys) {
86 array_push($keys, $v);
87 return array_shift($keys);
88 });
89
90 $values = [3, 4];
91 $this->v->expects($this->exactly(2))
92 ->method("getPrimitiveRepresentation")
93 ->willReturnCallback(function ($v) use (&$values) {
94 array_push($values, $v);
95 return array_shift($values);
96 });
97
98 $expected = [
99 "c" => 3,
100 "d" => 4
101 ];
102
103 $res = $this->m->getPrimitiveRepresentation($data);
104
105 $this->assertEquals($expected, $res);
106 $this->assertEquals(["a", "b"], $keys);
107 $this->assertEquals([1, 2], $values);
108 }
109
110 public function testFailsOnKeyFailure(): void
111 {
112 $data = ["a" => 1];
113
114 $this->v
115 ->method("getPrimitiveRepresentation")
116 ->willReturn(1);
117
118 $this->k
119 ->method("getPrimitiveRepresentation")
120 ->willReturn(fn() => yield "FAILURE");
121
122 $res = $this->m->getPrimitiveRepresentation($data);
123
124 $this->assertInstanceOf(\Closure::class, $res);
125 $errors = iterator_to_array($res());
126 $this->assertCount(1, $errors);
127 $this->assertTrue(is_string($errors[0]));
128 $this->assertTrue(str_contains($errors[0], "FAILURE"));
129 }
130
131 public function testFailsOnValueFailure(): void
132 {
133 $data = ["a" => 1];
134
135 $this->v
136 ->method("getPrimitiveRepresentation")
137 ->willReturn(fn() => yield "FAILURE");
138
139 $this->k
140 ->method("getPrimitiveRepresentation")
141 ->willReturn("a");
142
143 $res = $this->m->getPrimitiveRepresentation($data);
144
145 $this->assertInstanceOf(\Closure::class, $res);
146 $errors = iterator_to_array($res());
147 $this->assertCount(1, $errors);
148 $this->assertTrue(is_string($errors[0]));
149 $this->assertTrue(str_contains($errors[0], "FAILURE"));
150 }
151}
Description $v
Definition: DMapTest.php:37
static obviousNoMatchProvider()
Definition: DMapTest.php:61
testForwardsToSubDescriptions()
Definition: DMapTest.php:75
testObviouslyNotMatching($data)
Definition: DMapTest.php:51
This describes some datastructure in terms of standard data structures such as primitives,...
Definition: Description.php:33
$res
Definition: ltiservices.php:69
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.