66use BlueM \Tree \Exception \InvalidParentException ;
77use BlueM \Tree \Exception \MissingNodeInvalidParentException ;
88use BlueM \Tree \Node ;
9- use BlueM \Tree \Serializer \ HierarchicalTreeJsonSerializer ;
9+ use BlueM \Tree \Options ;
1010use PHPUnit \Framework \Attributes \Test ;
1111use PHPUnit \Framework \Attributes \TestDox ;
1212use PHPUnit \Framework \Attributes \Ticket ;
1313use PHPUnit \Framework \TestCase ;
1414
1515class TreeTest extends TestCase
1616{
17- #[Test]
18- #[TestDox('Constructor args: An exception is thrown if a non scalar value should be used as root id ' )]
19- public function constructorArgsNonScalarRootId (): void
20- {
21- $ this ->expectException (\InvalidArgumentException::class);
22- $ this ->expectExceptionMessage ('Option “rootid” must be scalar or null ' );
23-
24- new Tree ([], ['rootId ' => []]);
25- }
26-
27- #[Test]
28- #[TestDox('Constructor args: An exception is thrown if a non string value should be used as id field name ' )]
29- public function constructorArgsNonStringIDFieldName (): void
30- {
31- $ this ->expectException (\InvalidArgumentException::class);
32- $ this ->expectExceptionMessage ('Option “id” must be a string ' );
33-
34- new Tree ([], ['id ' => 123 ]);
35- }
36-
37- #[Test]
38- #[TestDox('Constructor args: An exception is thrown if a non string value should be used as parent id field name ' )]
39- public function constructorArgsNonStringParentIDFieldName (): void
40- {
41- $ this ->expectException (\InvalidArgumentException::class);
42- $ this ->expectExceptionMessage ('Option “parent” must be a string ' );
43-
44- new Tree ([], ['parent ' => $ this ]);
45- }
46-
47- #[Test]
48- #[TestDox('Constructor args: An exception is thrown if a non object should be used as serializer ' )]
49- public function constructorArgsNonObjectSerializer (): void
50- {
51- $ this ->expectException (\InvalidArgumentException::class);
52- $ this ->expectExceptionMessage ('Option “jsonSerializer” must be an object ' );
53-
54- new Tree ([], ['jsonSerializer ' => 'not an object ' ]);
55- }
56-
57- #[Test]
58- #[TestDox('Constructor args: The serializer can be set to an object implementing Serializerinterface ' )]
59- public function constructorArgsValidSerializer (): void
60- {
61- $ serializer = new HierarchicalTreeJsonSerializer ();
62-
63- $ subject = new Tree ([], ['jsonSerializer ' => $ serializer ]);
64-
65- $ serializerProperty = new \ReflectionProperty ($ subject , 'jsonSerializer ' );
66-
67- static ::assertSame ($ serializer , $ serializerProperty ->getValue ($ subject ));
68- }
69-
7017 #[Test]
7118 #[TestDox('Constructor args: Root nodes’ parent ID can be defined as null ' )]
7219 public function constructorArgsNullAsRootNodeParentId (): void
@@ -78,7 +25,7 @@ public function constructorArgsNullAsRootNodeParentId(): void
7825 ['id ' => 4 , 'parent ' => null , 'name ' => 'Root ' ],
7926 ];
8027
81- $ tree = new Tree ($ data , [ ' rootId ' => null ] );
28+ $ tree = new Tree ($ data , new Options ( rootId: null ) );
8229
8330 $ nodes = $ tree ->getNodes ();
8431 static ::assertCount (4 , $ nodes );
@@ -100,7 +47,7 @@ public function constructorArgsNullAsRootNodeIdPlusNodeWithId0(): void
10047 ['id ' => 3 , 'parent ' => 0 , 'name ' => 'Grandchild ' ],
10148 ];
10249
103- $ tree = new Tree ($ data , [ ' rootId ' => null ] );
50+ $ tree = new Tree ($ data , new Options ( rootId: null ) );
10451
10552 $ nodes = $ tree ->getNodes ();
10653 static ::assertCount (3 , $ nodes );
@@ -118,7 +65,7 @@ public function constructorArgsChangeFieldNames(): void
11865 {
11966 $ data = self ::dummyDataWithStringKeys ('id_node ' , 'id_parent ' );
12067
121- $ tree = new Tree ($ data , [ ' rootId ' => '' , ' id ' => 'id_node ' , ' parent ' => 'id_parent ' ] );
68+ $ tree = new Tree ($ data , new Options ( rootId: '' , idFieldName: 'id_node ' , parentIdFieldName: 'id_parent ' ) );
12269
12370 $ nodes = $ tree ->getRootNodes ();
12471
@@ -223,9 +170,7 @@ public function aCustomBuildWarningCallbackCanBeSpecifiedWhichIsCalledWithNodeAn
223170 ['id ' => 1 , 'parent ' => 0 ],
224171 ['id ' => 2 , 'parent ' => '' ],
225172 ],
226- [
227- 'buildwarningcallback ' => $ buildwarningcallback ,
228- ]
173+ new Options (buildWarningCallback: $ buildwarningcallback ),
229174 );
230175
231176 static ::assertSame (1 , $ invocationCount );
@@ -284,7 +229,7 @@ public function nodeGetByIntId(): void
284229 public function nodeGetByStringId (): void
285230 {
286231 $ data = self ::dummyDataWithStringKeys ();
287- $ tree = new Tree ($ data , [ ' rootId ' => '' ] );
232+ $ tree = new Tree ($ data , new Options ( rootId: '' ) );
288233 $ node = $ tree ->getNodeById ('library ' );
289234 static ::assertEquals ('library ' , $ node ->getId ());
290235 }
@@ -378,16 +323,14 @@ public function anExceptionIsThrownWhenAnInvalidParentIdIsReferenced(): void
378323 public function anExceptionIsThrownIfTheBuildWarningCallbackOptionIsNotACallable (): void
379324 {
380325 $ this ->expectException (\InvalidArgumentException::class);
381- $ this ->expectExceptionMessage ('Option “ buildWarningCallback” must be a callable ' );
326+ $ this ->expectExceptionMessage ('$ buildWarningCallback must be a callable ' );
382327
383328 new Tree (
384329 [
385330 ['id ' => 1 , 'parent ' => 0 ],
386331 ['id ' => 2 , 'parent ' => '' ],
387332 ],
388- [
389- 'buildwarningcallback ' => 'Must be a callable ' ,
390- ]
333+ new Options (buildWarningCallback: 'Will cause exception ' ),
391334 );
392335 }
393336
0 commit comments