ILIAS  trunk Revision v12.0_alpha-1540-g00f839d5fa1
DListTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
26
30use PHPUnit\Framework\TestCase;
31use PHPUnit\Framework\Attributes\DataProvider;
32
33class DListTest extends TestCase
34{
35 protected DList $l;
36 protected Description $v;
37
38 public function setUp(): void
39 {
40 $this->v = $this->createMock(Description::class);
41 $this->l = new DList(
42 $this->createMock(\ILIAS\Data\Text\SimpleDocumentMarkdown::class),
43 $this->v
44 );
45 }
46
47 #[DataProvider('obviousNoMatchProvider')]
48 public function testObviouslyNotMatching($data): void
49 {
50 $res = $this->l->getPrimitiveRepresentation($data);
51
52 $this->assertInstanceOf(\Closure::class, $res);
53 $errors = iterator_to_array($res());
54 $this->assertCount(1, $errors);
55 $this->assertTrue(is_string($errors[0]));
56 }
57
58 public static function obviousNoMatchProvider(): array
59 {
60 return [
61 [1], ["1"], [null], [true], [new \StdClass()], [new \DateTimeImmutable()]
62 ];
63 }
64
65 public function testEmptyMatches(): void
66 {
67 $res = $this->l->getPrimitiveRepresentation([]);
68
69 $this->assertEquals([], $res);
70 }
71
72 public function testForwardsToSubDescription(): void
73 {
74 $data = ["a","b"];
75
76 $values = ["c", "d"];
77 $this->v->expects($this->exactly(2))
78 ->method("getPrimitiveRepresentation")
79 ->willReturnCallback(function ($v) use (&$values) {
80 array_push($values, $v);
81 return array_shift($values);
82 });
83
84 $expected = ["c", "d"];
85
86 $res = $this->l->getPrimitiveRepresentation($data);
87
88 $this->assertEquals($expected, $res);
89 $this->assertEquals(["a", "b"], $values);
90 }
91
92 public function testFailsOnValueFailure(): void
93 {
94 $data = ["a"];
95
96 $this->v
97 ->method("getPrimitiveRepresentation")
98 ->willReturn(fn() => yield "FAILURE");
99
100 $res = $this->l->getPrimitiveRepresentation($data);
101
102 $this->assertInstanceOf(\Closure::class, $res);
103 $errors = iterator_to_array($res());
104 $this->assertCount(1, $errors);
105 $this->assertTrue(is_string($errors[0]));
106 $this->assertTrue(str_contains($errors[0], "FAILURE"));
107 }
108
109 public function testRenumbersEntrys(): void
110 {
111 $data = [2 => "a", 27 => "b"];
112
113 $this->v
114 ->method("getPrimitiveRepresentation")
115 ->willReturnCallback(fn($v) => $v);
116
117 $res = $this->l->getPrimitiveRepresentation($data);
118
119 $expected = ["a", "b"];
120
121 $this->assertEquals($expected, $res);
122 $this->assertNotEquals($data, $res);
123 }
124}
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.