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