ILIAS  trunk Revision v11.0_alpha-1731-gff9cd7e2bd3
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
LOMStructureInitiatorTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\MetaData\Structure;
22 
30 
31 class LOMStructureInitiatorTest extends TestCase
32 {
34  {
35  $initial_array = [
36  'name' => 'root',
37  'subs' => [
38  [
39  'name' => 'first',
40  'subs' => []
41  ],
42  [
43  'name' => 'second',
44  'subs' => [
45  [
46  'name' => 'sub second',
47  'subs' => []
48  ]
49  ]
50  ]
51  ]
52  ];
53 
54  $reader_factory = new class ($initial_array) extends NullReaderFactory {
55  public function __construct(protected array $initial_array)
56  {
57  }
58 
59  public function reader(): ReaderInterface
60  {
61  return new class ($this->initial_array) extends NullReader {
62  public function __construct(protected array $array)
63  {
64  }
65 
66  public function definition(): DefinitionInterface
67  {
68  return new class ($this->array['name']) extends NullDefinition {
69  public function __construct(protected string $name)
70  {
71  }
72 
73  public function name(): string
74  {
75  return $this->name;
76  }
77  };
78  }
79 
80  public function subDefinitions(): \Generator
81  {
82  foreach ($this->array['subs'] as $sub) {
83  yield new self($sub);
84  }
85  }
86  };
87  }
88  };
89 
90  /*
91  * The tree structure needs to be built from actual elements
92  * and not only their interfaces (see BaseElement::addSubElement,
93  * so the factory does not have an interface and needs to be used
94  * here as is.
95  */
96  return new LOMStructureInitiator(
97  $reader_factory,
98  new StructureFactory()
99  );
100  }
101 
102  public function testSet(): void
103  {
104  $root = $this->getLOMStructureInitiator()->set()->getRoot();
105  $this->assertSame(
106  'root',
107  $root->getDefinition()->name()
108  );
109  $subs = $root->getSubElements();
110  $first = $subs->current();
111  $subs->next();
112  $second = $subs->current();
113  $subs->next();
114  $third = $subs->current();
115 
116  $this->assertSame(
117  'first',
118  $first->getDefinition()->name()
119  );
120  $this->assertSame(
121  'second',
122  $second->getDefinition()->name()
123  );
124  $this->assertNull($third);
125 
126  $this->assertNull($first->getSubElements()->current());
127  $this->assertSame(
128  'sub second',
129  $second->getSubElements()->current()->getDefinition()->name()
130  );
131  }
132 }
__construct()
Constructor setup ILIAS global object public.
Definition: class.ilias.php:76