1+ <?php
2+
3+ use Html \Delegator \HTMLDocumentDelegator ;
4+ use Html \Interface \ComponentBuilderInterface ;
5+ use Html \Service \ComponentBuilder ;
6+ use InvalidArgumentException ;
7+
8+ beforeEach (function () {
9+ $ this ->builder = new ComponentBuilder ();
10+ });
11+
12+ test ('implements interface ' , function () {
13+ expect ($ this ->builder )->toBeInstanceOf (ComponentBuilderInterface::class);
14+ });
15+
16+ test ('constructor ' , function () {
17+ expect ($ this ->builder )->toBeInstanceOf (ComponentBuilder::class);
18+ });
19+
20+ test ('buildComponent throws exception for multiple components ' , function () {
21+ $ document = HTMLDocumentDelegator::createEmpty ();
22+ $ data = [
23+ 'component1 ' => ['structure ' => []],
24+ 'component2 ' => ['structure ' => []],
25+ ];
26+
27+ expect (fn () => $ this ->builder ->buildComponent ($ document , $ data ))
28+ ->toThrow (InvalidArgumentException::class, 'Only one component per file is allowed. ' );
29+ });
30+
31+ test ('buildComponent throws exception when structure is missing ' , function () {
32+ $ document = HTMLDocumentDelegator::createEmpty ();
33+ $ data = [
34+ 'component1 ' => ['other ' => 'data ' ],
35+ ];
36+
37+ expect (fn () => $ this ->builder ->buildComponent ($ document , $ data ))
38+ ->toThrow (InvalidArgumentException::class, 'Component structure is required ' );
39+ });
40+
41+ test ('buildComponent with valid single component does not throw ' , function () {
42+ $ document = HTMLDocumentDelegator::createEmpty ();
43+
44+ $ data = [
45+ 'myComponent ' => [
46+ 'structure ' => [
47+ 'div ' => [
48+ 'class ' => 'container ' ,
49+ 'text ' => 'Hello World '
50+ ]
51+ ]
52+ ]
53+ ];
54+
55+ // This test verifies that the method doesn't throw exceptions with valid structure
56+ // The actual DOM building would require more complex setup with real element classes
57+ expect (fn () => $ this ->builder ->buildComponent ($ document , $ data ))
58+ ->toThrow (InvalidArgumentException::class, "Element class for tag 'div' not found. " );
59+ });
60+
61+ test ('buildComponent throws exception for unknown element ' , function () {
62+ $ document = HTMLDocumentDelegator::createEmpty ();
63+
64+ $ data = [
65+ 'component ' => [
66+ 'structure ' => [
67+ 'unknown-element ' => []
68+ ]
69+ ]
70+ ];
71+
72+ expect (fn () => $ this ->builder ->buildComponent ($ document , $ data ))
73+ ->toThrow (InvalidArgumentException::class, "Element class for tag 'unknown-element' not found. " );
74+ });
0 commit comments