ILIAS  trunk Revision v11.0_alpha-2658-ge2404539063
DListTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
25 namespace ILIAS\Data\Description;
26 
31 
32 class DListTest extends TestCase
33 {
34  protected DList $l;
35  protected Description $v;
36 
37  public function setUp(): void
38  {
39  $this->v = $this->createMock(Description::class);
40  $this->l = new DList(
41  $this->createMock(\ILIAS\Data\Text\SimpleDocumentMarkdown::class),
42  $this->v
43  );
44  }
45 
49  public function testObviouslyNotMatching($data): void
50  {
51  $res = $this->l->getPrimitiveRepresentation($data);
52 
53  $this->assertInstanceOf(\Closure::class, $res);
54  $errors = iterator_to_array($res());
55  $this->assertCount(1, $errors);
56  $this->assertTrue(is_string($errors[0]));
57  }
58 
59  public static function obviousNoMatchProvider(): array
60  {
61  return [
62  [1], ["1"], [null], [true], [new \StdClass()], [new \DateTimeImmutable()]
63  ];
64  }
65 
66  public function testEmptyMatches(): void
67  {
68  $res = $this->l->getPrimitiveRepresentation([]);
69 
70  $this->assertEquals([], $res);
71  }
72 
73  public function testForwardsToSubDescription(): void
74  {
75  $data = ["a","b"];
76 
77  $values = ["c", "d"];
78  $this->v->expects($this->exactly(2))
79  ->method("getPrimitiveRepresentation")
80  ->willReturnCallback(function ($v) use (&$values) {
81  array_push($values, $v);
82  return array_shift($values);
83  });
84 
85  $expected = ["c", "d"];
86 
87  $res = $this->l->getPrimitiveRepresentation($data);
88 
89  $this->assertEquals($expected, $res);
90  $this->assertEquals(["a", "b"], $values);
91  }
92 
93  public function testFailsOnValueFailure(): void
94  {
95  $data = ["a"];
96 
97  $this->v
98  ->method("getPrimitiveRepresentation")
99  ->willReturn(fn() => yield "FAILURE");
100 
101  $res = $this->l->getPrimitiveRepresentation($data);
102 
103  $this->assertInstanceOf(\Closure::class, $res);
104  $errors = iterator_to_array($res());
105  $this->assertCount(1, $errors);
106  $this->assertTrue(is_string($errors[0]));
107  $this->assertTrue(str_contains($errors[0], "FAILURE"));
108  }
109 
110  public function testRenumbersEntrys(): void
111  {
112  $data = [2 => "a", 27 => "b"];
113 
114  $this->v
115  ->method("getPrimitiveRepresentation")
116  ->will($this->returnCallback(fn($v) => $v));
117 
118  $res = $this->l->getPrimitiveRepresentation($data);
119 
120  $expected = ["a", "b"];
121 
122  $this->assertEquals($expected, $res);
123  $this->assertNotEquals($data, $res);
124  }
125 }
$res
Definition: ltiservices.php:66
Interface Observer Contains several chained tasks and infos about them.
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
This describes some datastructure in terms of standard data structures such as primitives, lists, maps and objects and helpful (hopefully...) human readable texts.
Definition: Description.php:32
testObviouslyNotMatching($data)
obviousNoMatchProvider
Definition: DListTest.php:49