ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
DynamicInputDataIteratorTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
26 use LogicException;
28 
32 class DynamicInputDataIteratorTest extends TestCase
33 {
34  public function testValidityWithEmptyData(): void
35  {
36  $iterator = new DynamicInputDataIterator(
37  $this->getTestInputData([]),
38  'test_name_1'
39  );
40 
41  $this->assertFalse($iterator->valid());
42  $this->assertNull($iterator->key());
43  $this->assertNull($iterator->current());
44  }
45 
46  public function testValidityWithData(): void
47  {
48  $iterator = new DynamicInputDataIterator(
49  $this->getTestInputData([
50  'test_input_1' => [
51  [
52  'test_value_1'
53  ]
54  ]
55  ]),
56  'test_input_1'
57  );
58 
59  $this->assertTrue($iterator->valid());
60  $this->assertNotNull($iterator->key());
61  $this->assertNotNull($iterator->current());
62 
63  $iterator->next();
64 
65  $this->assertFalse($iterator->valid());
66  $this->assertNull($iterator->key());
67  $this->assertNull($iterator->current());
68  }
69 
70  public function testCurrentValue(): void
71  {
72  $test_value = 'val1';
73  $parent_input_name = 'parent_input';
74  $dynamic_input_name = 'dynamic_input';
75  $fake_post_array = [
76  $parent_input_name => [
77  $dynamic_input_name => [
78  $test_value,
79  ]
80  ]
81  ];
82 
83  $iterator = new DynamicInputDataIterator(
84  $this->getTestInputData($fake_post_array),
85  $parent_input_name
86  );
87 
88  $current = $iterator->current();
89  $this->assertInstanceOf(
90  InputData::class,
91  $current
92  );
93 
94  $rendered_dynamic_input_name = "{$parent_input_name}[$dynamic_input_name][]";
95  $this->assertEquals(
96  $test_value,
97  $current->getOr($rendered_dynamic_input_name, null)
98  );
99  }
100 
101  protected function getTestInputData(array $data): InputData
102  {
103  return new class ($data) implements InputData {
104  protected array $data;
105 
106  public function __construct(array $data)
107  {
108  $this->data = $data;
109  }
110 
111  public function get($name)
112  {
113  if (!isset($this->data[$name])) {
114  throw new LogicException();
115  }
116 
117  return $this->data[$name];
118  }
119 
120  public function getOr($name, $default)
121  {
122  return $this->data[$name] ?? $default;
123  }
124 
125  public function has($name): bool
126  {
127  return array_key_exists($name, $this->data);
128  }
129  };
130  }
131 }
Describes how Input-Elements want to interact with posted data.
Definition: InputData.php:29
has(string $class_name)
if($format !==null) $name
Definition: metadata.php:247
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct()
Constructor setup ILIAS global object public.
Definition: class.ilias.php:62