ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
DynamicInputDataIteratorTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
25use PHPUnit\Framework\TestCase;
26use LogicException;
28
32class 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}
Other than the FormInputNameSource this name source is for inputs that can be dynamically added multi...
__construct()
Constructor setup ILIAS global object @access public.
Definition: class.ilias.php:76
Describes how Input-Elements want to interact with posted data.
Definition: InputData.php:30
has(string $class_name)