14
14
use PHPUnit \Framework \TestCase ;
15
15
use Symfony \Component \Config \Definition \Builder \ArrayNodeDefinition ;
16
16
use Symfony \Component \Config \Definition \Builder \BooleanNodeDefinition ;
17
+ use Symfony \Component \Config \Definition \Builder \NodeDefinition ;
17
18
use Symfony \Component \Config \Definition \Builder \NodeFinder ;
18
19
use Symfony \Component \Config \Definition \Builder \ScalarNodeDefinition ;
19
- use Symfony \Component \Config \Definition \Builder \TreeBuilder ;
20
- use Symfony \Component \Config \Tests \Fixtures \Builder \NodeBuilder as CustomNodeBuilder ;
21
- use Symfony \Component \Config \Tests \Fixtures \Builder \VariableNodeDefinition ;
22
20
23
21
/**
24
22
* @author Jan Schädlich <jan.schaedlich@sensiolabs.de>
25
23
*/
26
24
class NodeFinderTest extends TestCase
27
25
{
28
- public function testOne ()
26
+ public function testItShouldFindRootNode ()
29
27
{
30
28
$ rootNode = new ScalarNodeDefinition ('root ' );
31
29
@@ -35,7 +33,34 @@ public function testOne()
35
33
$ this ->assertSame ('root ' , $ foundNode ->getName ());
36
34
}
37
35
38
- public function testTwo ()
36
+ public function testItShouldThrowExceptionIfNodeDoesNotExistInScalarRootNode ()
37
+ {
38
+ $ this ->expectException (\RuntimeException::class);
39
+ $ this ->expectExceptionMessage ('Node with name "child" does not exist in the given root node "root"! ' );
40
+
41
+ $ rootNode = new ScalarNodeDefinition ('root ' );
42
+
43
+ $ nodeFinder = new NodeFinder ();
44
+ $ nodeFinder ->find ('child ' , $ rootNode );
45
+ }
46
+
47
+ public function testItShouldThrowExceptionIfNodeDoesNotExistInArrayRootNode ()
48
+ {
49
+ $ this ->expectException (\RuntimeException::class);
50
+ $ this ->expectExceptionMessage ('Node with name "child" does not exist in the given root node "root"! ' );
51
+
52
+ $ rootNode = new ArrayNodeDefinition ('root ' );
53
+ $ rootNode
54
+ ->children ()
55
+ ->arrayNode ('social_media_channels ' )->end ()
56
+ ->end ()
57
+ ;
58
+
59
+ $ nodeFinder = new NodeFinder ();
60
+ $ nodeFinder ->find ('child ' , $ rootNode );
61
+ }
62
+
63
+ public function testItShouldHandleComplexConfigurationProbably ()
39
64
{
40
65
$ rootNode = new ArrayNodeDefinition ('root ' );
41
66
$ rootNode
@@ -58,22 +83,32 @@ public function testTwo()
58
83
$ nodeFinder = new NodeFinder ();
59
84
60
85
$ socialMediaChannelsNode = $ nodeFinder ->find ('social_media_channels ' , $ rootNode );
61
- $ this ->assertSame ('social_media_channels ' , $ socialMediaChannelsNode ->getName ());
62
- $ this ->assertInstanceOf (ArrayNodeDefinition::class, $ socialMediaChannelsNode );
63
-
64
86
$ socialMediaChannelsEnableNode = $ nodeFinder ->find ('social_media_channels.enable ' , $ rootNode );
65
- $ this ->assertSame ('enable ' , $ socialMediaChannelsEnableNode ->getName ());
66
- $ this ->assertInstanceOf (BooleanNodeDefinition::class, $ socialMediaChannelsEnableNode );
67
-
68
87
88
+ $ twitterNode = $ nodeFinder ->find ('social_media_channels.twitter ' , $ rootNode );
89
+ $ facebookNode = $ nodeFinder ->find ('social_media_channels.facebook ' , $ rootNode );
90
+ $ instagramNode = $ nodeFinder ->find ('social_media_channels.instagram ' , $ rootNode );
91
+ $ instagramEnableNode = $ nodeFinder ->find ('social_media_channels.instagram.enable ' , $ rootNode );
92
+ $ instagramAccountsNode = $ nodeFinder ->find ('social_media_channels.instagram.accounts ' , $ rootNode );
69
93
70
- $ this ->assertSame ( ' twitter ' , $ nodeFinder -> find ( 'social_media_channels.twitter ' , $ rootNode )-> getName () );
71
- $ this ->assertSame ( ' facebook ' , $ nodeFinder -> find ( ' social_media_channels.facebook ' , $ rootNode )-> getName () );
94
+ $ this ->assertNode ( $ socialMediaChannelsNode , 'social_media_channels ' , ArrayNodeDefinition::class );
95
+ $ this ->assertNode ( $ socialMediaChannelsEnableNode , ' enable ' , BooleanNodeDefinition::class );
72
96
73
- $ this ->assertSame ('instagram ' , $ nodeFinder ->find ('social_media_channels.instagram ' , $ rootNode )->getName ());
74
- $ this ->assertSame ('enable ' , $ nodeFinder ->find ('social_media_channels.instagram.enable ' , $ rootNode )->getName ());
75
- $ this ->assertSame ('accounts ' , $ nodeFinder ->find ('social_media_channels.instagram.accounts ' , $ rootNode )->getName ());
97
+ $ this ->assertNode ($ twitterNode , 'twitter ' , ArrayNodeDefinition::class);
98
+ $ this ->assertNode ($ facebookNode , 'facebook ' , ArrayNodeDefinition::class);
99
+ $ this ->assertNode ($ instagramNode , 'instagram ' , ArrayNodeDefinition::class);
100
+ $ this ->assertNode ($ instagramEnableNode , 'enable ' , BooleanNodeDefinition::class);
101
+ $ this ->assertNode ($ instagramAccountsNode , 'accounts ' , ArrayNodeDefinition::class);
76
102
}
77
103
78
-
104
+ /**
105
+ * @param NodeDefinition $actualNode
106
+ * @param string $expectedName
107
+ * @param string $expectedType
108
+ */
109
+ private function assertNode (NodeDefinition $ actualNode , $ expectedName , $ expectedType )
110
+ {
111
+ $ this ->assertSame ($ expectedName , $ actualNode ->getName ());
112
+ $ this ->assertInstanceOf ($ expectedType , $ actualNode );
113
+ }
79
114
}
0 commit comments