ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
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;
31
32class DMapTest extends TestCase
33{
34 protected DMap $m;
35 protected DValue $k;
36 protected Description $v;
37
38 public function setUp(): void
39 {
40 $this->k = $this->createMock(DValue::class);
41 $this->v = $this->createMock(Description::class);
42 $this->m = new DMap(
43 $this->createMock(\ILIAS\Data\Text\SimpleDocumentMarkdown::class),
44 $this->k,
45 $this->v
46 );
47 }
48
52 public function testObviouslyNotMatching($data): void
53 {
54 $res = $this->m->getPrimitiveRepresentation($data);
55
56 $this->assertInstanceOf(\Closure::class, $res);
57 $errors = iterator_to_array($res());
58 $this->assertCount(1, $errors);
59 $this->assertTrue(is_string($errors[0]));
60 }
61
62 public static function obviousNoMatchProvider(): array
63 {
64 return [
65 [1], ["1"], [null], [true], [new \StdClass()], [new \DateTimeImmutable()]
66 ];
67 }
68
69 public function testEmptyMatches(): void
70 {
71 $res = $this->m->getPrimitiveRepresentation([]);
72
73 $this->assertEquals([], $res);
74 }
75
76 public function testForwardsToSubDescriptions(): void
77 {
78 $data = [
79 "a" => 1,
80 "b" => 2
81 ];
82
83 $keys = ["c", "d"];
84 $this->k->expects($this->exactly(2))
85 ->method("getPrimitiveRepresentation")
86 ->willReturnCallback(function ($v) use (&$keys) {
87 array_push($keys, $v);
88 return array_shift($keys);
89 });
90
91 $values = [3, 4];
92 $this->v->expects($this->exactly(2))
93 ->method("getPrimitiveRepresentation")
94 ->willReturnCallback(function ($v) use (&$values) {
95 array_push($values, $v);
96 return array_shift($values);
97 });
98
99 $expected = [
100 "c" => 3,
101 "d" => 4
102 ];
103
104 $res = $this->m->getPrimitiveRepresentation($data);
105
106 $this->assertEquals($expected, $res);
107 $this->assertEquals(["a", "b"], $keys);
108 $this->assertEquals([1, 2], $values);
109 }
110
111 public function testFailsOnKeyFailure(): void
112 {
113 $data = ["a" => 1];
114
115 $this->v
116 ->method("getPrimitiveRepresentation")
117 ->willReturn(1);
118
119 $this->k
120 ->method("getPrimitiveRepresentation")
121 ->willReturn(fn() => yield "FAILURE");
122
123 $res = $this->m->getPrimitiveRepresentation($data);
124
125 $this->assertInstanceOf(\Closure::class, $res);
126 $errors = iterator_to_array($res());
127 $this->assertCount(1, $errors);
128 $this->assertTrue(is_string($errors[0]));
129 $this->assertTrue(str_contains($errors[0], "FAILURE"));
130 }
131
132 public function testFailsOnValueFailure(): void
133 {
134 $data = ["a" => 1];
135
136 $this->v
137 ->method("getPrimitiveRepresentation")
138 ->willReturn(fn() => yield "FAILURE");
139
140 $this->k
141 ->method("getPrimitiveRepresentation")
142 ->willReturn("a");
143
144 $res = $this->m->getPrimitiveRepresentation($data);
145
146 $this->assertInstanceOf(\Closure::class, $res);
147 $errors = iterator_to_array($res());
148 $this->assertCount(1, $errors);
149 $this->assertTrue(is_string($errors[0]));
150 $this->assertTrue(str_contains($errors[0], "FAILURE"));
151 }
152}
Description $v
Definition: DMapTest.php:36
static obviousNoMatchProvider()
Definition: DMapTest.php:62
testForwardsToSubDescriptions()
Definition: DMapTest.php:76
testObviouslyNotMatching($data)
@dataProvider obviousNoMatchProvider
Definition: DMapTest.php:52
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.