ILIAS  trunk Revision v12.0_alpha-1540-g00f839d5fa1
DObjectTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
26
32use PHPUnit\Framework\TestCase;
33use PHPUnit\Framework\Attributes\DataProvider;
34
35class DObjectTest extends TestCase
36{
37 #[DataProvider('simpleObjectsProvider')]
38 public function testSimpleObject(string $field, $object, $expected): void
39 {
40 $md = $this->createMock(\ILIAS\Data\Text\SimpleDocumentMarkdown::class);
41 $desc = new DObject(
42 $md,
43 new Field(
44 $field,
45 new DValue(
46 $this->createMock(\ILIAS\Data\Text\SimpleDocumentMarkdown::class),
47 ValueType::INT
48 )
49 )
50 );
51
52 $res = $desc->getPrimitiveRepresentation($object);
53
54 if (!is_null($expected)) {
55 $this->assertEquals($expected, $res);
56 } else {
57 $this->assertInstanceOf(\Closure::class, $res);
58 $errors = iterator_to_array($res());
59 $this->assertCount(1, $errors);
60 $this->assertTrue(is_string($errors[0]));
61 }
62 }
63
64 public static function simpleObjectsProvider(): array
65 {
66 $v1 = "value";
67 $o1_a = new class () {
68 public function getValue(): int
69 {
70 return 42;
71 }
72 };
73 $o1_b = new class () {
74 public $value = 42;
75 };
76 $e1 = new \StdClass();
77 $e1->value = 42;
78
79 $v2 = "some_value";
80 $o2_a = new class () {
81 public function getSomeValue(): int
82 {
83 return 23;
84 }
85 };
86 $o2_b = new class () {
87 public $some_value = 23;
88 };
89 $o2_c = new class () {
90 public $someValue = 23;
91 };
92 $e2 = new \StdClass();
93 $e2->some_value = 23;
94
95
96 return [
97 [$v1, $o1_a, $e1],
98 [$v1, $o1_b, $e1],
99 [$v2, $o2_a, $e2],
100 [$v2, $o2_b, $e2],
101 [$v2, $o2_c, $e2],
102 ];
103 }
104
105 #[DataProvider('fieldNamesProvider')]
106 public function testAllowedFieldNames(string $name, bool $is_allowed): void
107 {
108 if (!$is_allowed) {
109 $this->expectException(\InvalidArgumentException::class);
110 }
111
112 $field = new Field($name, $this->createMock(Description::class));
113 $this->assertEquals($name, $field->getName());
114 }
115
116 public static function fieldNamesProvider(): array
117 {
118 return [
119 ["someName", true],
120 ["some_name", true],
121 ["some", true],
122 ["some1", true],
123 ["1some", false],
124 ["some one", false]
125 ];
126 }
127
128 #[DataProvider('obviousNoMatchProvider')]
129 public function testObviouslyNotMatching($data): void
130 {
131 $desc = new DObject(
132 $this->createMock(\ILIAS\Data\Text\SimpleDocumentMarkdown::class)
133 );
134 $res = $desc->getPrimitiveRepresentation($data);
135
136 $this->assertInstanceOf(\Closure::class, $res);
137 $errors = iterator_to_array($res());
138 $this->assertCount(1, $errors);
139 $this->assertTrue(is_string($errors[0]));
140 }
141
142 public static function obviousNoMatchProvider(): array
143 {
144 return [
145 [1], ["1"], [null], [true], [[]]
146 ];
147 }
148}
testAllowedFieldNames(string $name, bool $is_allowed)
testSimpleObject(string $field, $object, $expected)
Definition: DObjectTest.php:38
This describes some datastructure in terms of standard data structures such as primitives,...
Definition: Description.php:33
$res
Definition: ltiservices.php:69
getValue()
Get the value that is displayed in the input client side.
Definition: Group.php:49
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.