ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
DocumentTableTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
38 
39 require_once __DIR__ . '/../ContainerMock.php';
40 
41 class DocumentTableTest extends TestCase
42 {
43  use ContainerMock;
44 
45  public function testConstruct(): void
46  {
47  $this->assertInstanceOf(DocumentTable::class, new DocumentTable(
48  $this->fail(...),
49  $this->mock(DocumentRepository::class),
50  $this->mock(UI::class),
51  $this->mock(DocumentModal::class)
52  ));
53  }
54 
55  public function testColumns(): void
56  {
57  $ui = $this->mock(UI::class);
58  $ui->method('txt')->willReturnCallback(fn($s) => 'txt: ' . $s);
59 
60  $this->assertSame([
61  'order' => ['txt: tbl_docs_head_sorting', '', '5%'],
62  'title' => ['txt: tbl_docs_head_title', '', '25%'],
63  'created' => ['txt: tbl_docs_head_created'],
64  'change' => ['txt: tbl_docs_head_last_change'],
65  'criteria' => ['txt: tbl_docs_head_criteria'],
66  ], (new DocumentTable(
67  $this->fail(...),
68  $this->mock(DocumentRepository::class),
69  $ui,
70  $this->mock(DocumentModal::class)
71  ))->columns());
72  }
73 
74  public function testConfig(): void
75  {
76  $ui = $this->mock(UI::class);
77  $ui->method('txt')->willReturnCallback(fn($s) => 'txt: ' . $s);
78 
79  $config = $this->mock(TableConfig::class);
80  $config->expects(self::once())->method('setTitle')->with('txt: tbl_docs_title');
81  $config->expects(self::once())->method('setSelectableColumns')->with('created', 'change');
82 
83  $instance = new DocumentTable(
84  $this->fail(...),
85  $this->mock(DocumentRepository::class),
86  $ui,
87  $this->mock(DocumentModal::class)
88  );
89 
90  $instance->config($config);
91  }
92 
93  public function testRows(): void
94  {
95  $ui = $this->mock(UI::class);
96 
97  $select = $this->mock(TableSelection::class);
98 
99  $repository = $this->mockMethod(DocumentRepository::class, 'all', [], [
100  $this->mock(Document::class),
101  $this->mock(Document::class)
102  ]);
103 
104  $instance = new DocumentTable(
105  $this->fail(...),
106  $repository,
107  $ui,
108  $this->mock(DocumentModal::class),
109  $this->mock(...),
110  fn(DateTimeImmutable $d) => 'formatted date'
111  );
112 
113  $rows = $instance->rows($select);
114  $this->assertSame(2, count($rows));
115  foreach ($rows as $row) {
116  $this->assertSame([
117  'order',
118  'title',
119  'created',
120  'change',
121  'criteria',
122  ], array_keys($row));
123  }
124 
125  }
126 
127  public function testSelect(): void
128  {
129  $rows = [
130  $this->mock(Document::class),
131  $this->mock(Document::class)
132  ];
133 
134  $select = $this->mock(TableSelection::class);
135  $repository = $this->mockMethod(DocumentRepository::class, 'all', [], $rows);
136 
137  $instance = new DocumentTable(
138  $this->fail(...),
139  $repository,
140  $this->mock(UI::class),
141  $this->mock(DocumentModal::class)
142  );
143 
144  $this->assertSame($rows, $instance->select($select));
145  }
146 
147  public function testRow(): void
148  {
149  $instance = new DocumentTable(
150  $this->fail(...),
151  $this->mock(DocumentRepository::class),
152  $this->mock(UI::class),
153  $this->mock(DocumentModal::class),
154  $this->mock(...),
155  fn(DateTimeImmutable $date) => 'formatted date'
156  );
157 
158  $this->assertSame([
159  'order',
160  'title',
161  'created',
162  'change',
163  'criteria',
164  ], array_keys($instance->row($this->mock(Document::class), 1)));
165  }
166 
167  public function testShowEmptyCriteria(): void
168  {
169  $ui = $this->mock(UI::class);
170  $ui->method('txt')->willReturnCallback(fn($s) => 'txt: ' . $s);
171 
172  $instance = new DocumentTable(
173  $this->fail(...),
174  $this->mock(DocumentRepository::class),
175  $ui,
176  $this->mock(DocumentModal::class)
177  );
178 
179  $this->assertSame('txt: tbl_docs_cell_not_criterion', $instance->showCriteria(
180  $this->mockTree(Document::class, ['criteria' => []]),
181  $this->fail(...)
182  ));
183  }
184 
185  public function testShowCriteria(): void
186  {
187  $ui = $this->mock(UI::class);
188  $ui->method('txt')->willReturnCallback(fn($s) => 'txt: ' . $s);
189 
190  $instance = new DocumentTable(
191  $this->fail(...),
192  $this->mock(DocumentRepository::class),
193  $ui,
194  $this->mock(DocumentModal::class)
195  );
196 
197  $criteria = [
198  $this->mock(Criterion::class),
199  $this->mock(Criterion::class),
200  ];
201 
202  $this->assertSame(['a', 'b', 'a', 'b'], $instance->showCriteria(
203  $this->mockTree(Document::class, ['criteria' => $criteria]),
204  function (Criterion $criterion) use ($criteria) {
205  $this->assertTrue(in_array($criterion, $criteria, true));
206  return ['a', 'b'];
207  }
208  ));
209  }
210 
211  public function testShowCriterion(): void
212  {
213  $content = $this->mock(CriterionContent::class);
214  $legacy = $this->mock(Legacy::class);
215  $component = $this->mock(Component::class);
216 
217  $ui = $this->mockTree(UI::class, ['create' => $this->mockMethod(UIFactory::class, 'legacy', ['<br/>'], $legacy)]);
218 
219  $instance = new DocumentTable(
220  function (CriterionContent $c) use ($content, $component) {
221  $this->assertSame($content, $c);
222  return $component;
223  },
224  $this->mock(DocumentRepository::class),
225  $ui,
226  $this->mock(DocumentModal::class)
227  );
228 
229  $criterion = $this->mockTree(Criterion::class, ['content' => $content]);
230 
231  $this->assertSame([$component, $legacy], $instance->showCriterion($criterion));
232  }
233 
234  public function testCriterionName(): void
235  {
236  $content = $this->mock(CriterionContent::class);
237  $component = $this->mock(Component::class);
238 
239  $instance = new DocumentTable(
240  function (CriterionContent $c) use ($content, $component) {
241  $this->assertSame($content, $c);
242  return $component;
243  },
244  $this->mock(DocumentRepository::class),
245  $this->mock(UI::class),
246  $this->mock(DocumentModal::class)
247  );
248 
249  $this->assertSame(
250  $component,
251  $instance->criterionName($this->mockTree(Criterion::class, ['content' => $content]))
252  );
253  }
254 
255  public function testOrderInputGui(): void
256  {
257  $input = null;
258  $called = false;
259 
260  $create = function (string $class) use (&$called, &$input) {
261  if ($called) {
262  $this->fail('Called more than once');
263  }
264  $called = true;
265  $input = $this->mock($class);
266  return $input;
267  };
268 
269  $instance = new DocumentTable(
270  $this->fail(...),
271  $this->mock(DocumentRepository::class),
272  $this->mock(UI::class),
273  $this->mock(DocumentModal::class),
274  $create
275  );
276 
277  $result = $instance->orderInputGui($this->mock(Document::class), 1);
278  $this->assertSame($input, $result);
279  }
280 
281  public function testUi(): void
282  {
283  $ui = $this->mock(UI::class);
284  $instance = new DocumentTable(
285  $this->fail(...),
286  $this->mock(DocumentRepository::class),
287  $ui,
288  $this->mock(DocumentModal::class)
289  );
290 
291  $this->assertSame($ui, $instance->ui());
292  }
293 
294  public function testName(): void
295  {
296  $this->assertSame(DocumentTable::class, (new DocumentTable(
297  $this->fail(...),
298  $this->mock(DocumentRepository::class),
299  $this->mock(UI::class),
300  $this->mock(DocumentModal::class)
301  ))->name());
302  }
303 }
ILIAS DI UIServices $ui
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$c
Definition: deliver.php:9