44
55require_once (__DIR__ . '/../../inc/BaseCase.php ' );
66
7+ use PHPCR \NodeType \NodeTypeDefinitionInterface ;
8+ use PHPCR \NodeType \NodeTypeManagerInterface ;
79use PHPCR \PropertyType ;
810use PHPCR \NodeType \PropertyDefinitionTemplateInterface ;
911use PHPCR \Version \OnParentVersionAction ;
1012
11- use PHPCR \Util \CND \Helper \NodeTypeGenerator ;
12- use PHPCR \Util \CND \Reader \BufferReader ;
1313use PHPCR \Util \CND \Parser \CndParser ;
14- use PHPCR \Util \CND \Scanner \GenericScanner ;
15- use PHPCR \Util \CND \Scanner \Context \DefaultScannerContextWithoutSpacesAndComments ;
14+
1615
1716class CndParserTest extends \PHPCR \Test \BaseCase
1817{
19- function testGenerator ()
18+ /** @var CndParser */
19+ private $ cndParser ;
20+
21+ public function setUp ()
22+ {
23+ parent ::setUp ();
24+ $ this ->cndParser = new CndParser ($ this ->sharedFixture ['session ' ]->getWorkspace ()->getNodeTypeManager ());
25+ }
26+
27+ public function testParseNormal ()
2028 {
21- // the worst case example from http://jackrabbit.apache.org/node-type-notation.html
29+ $ res = $ this ->cndParser ->parseFile (__DIR__ . '/resources/cnd/example.cnd ' );
30+ $ this ->assertExampleCnd ($ res );
31+ }
32+
33+ public function testParseCompact ()
34+ {
35+ $ res = $ this ->cndParser ->parseFile (__DIR__ . '/resources/cnd/example.compact.cnd ' );
36+ $ this ->assertExampleCnd ($ res );
37+
38+ }
39+
40+ public function testParseVerbose ()
41+ {
42+ $ res = $ this ->cndParser ->parseFile (__DIR__ . '/resources/cnd/example.verbose.cnd ' );
43+ $ this ->assertExampleCnd ($ res );
44+ }
45+
46+
47+ public function testParseString ()
48+ {
49+ // the "worst case" example from http://jackrabbit.apache.org/node-type-notation.html
2250 $ cnd = <<<EOT
2351/** An example node type definition */
2452<ns ='http://namespace.com/ns'>
@@ -34,22 +62,130 @@ function testGenerator()
3462 mandatory autocreated protected VERSION
3563EOT ;
3664
37- //define('DEBUG', true);
3865
39- $ parser = new CndParser ($ this ->sharedFixture ['session ' ]->getWorkspace ()->getNodeTypeManager ());
66+ $ res = $ this ->cndParser ->parseString ($ cnd );
67+ $ this ->assertExampleCnd ($ res );
68+ }
69+
70+ /**
71+ * @expectedException \PHPCR\Util\CND\Exception\ParserException
72+ */
73+ public function testParseError ()
74+ {
75+ $ cnd = <<<EOT
76+ /** An example node type definition */
77+ <ns ='http://namespace.com/ns'>
78+ [ns:NodeType] > ns:ParentType1, ns:ParentType2
79+ orderable mixin
80+ - ex:property (STRING)
81+ = 'default1' , 'default2'
82+ mandatory invalid-string protected multiple
83+ VERSION
84+ < 'constraint1', 'constraint2'
85+ + ns:node (ns:reqType1, ns:reqType2)
86+ = ns:defaultType
87+ mandatory autocreated protected VERSION
88+ EOT ;
89+
90+ $ this ->cndParser ->parseString ($ cnd );
91+ }
92+
93+ /**
94+ * @expectedException \InvalidArgumentException
95+ */
96+ public function testErrorNoFile ()
97+ {
98+ $ this ->cndParser ->parseFile ('/not/found ' );
99+ }
100+
101+ /**
102+ * @expectedException \PHPCR\Util\CND\Exception\ScannerException
103+ */
104+ public function testScannerErrorComment ()
105+ {
106+ $ cnd = <<<EOT
107+ la /*
108+ EOT ;
109+
110+ $ this ->cndParser ->parseString ($ cnd );
111+ }
112+
113+ /**
114+ * @expectedException \PHPCR\Util\CND\Exception\ScannerException
115+ */
116+ public function testScannerErrorNewline ()
117+ {
118+ $ cnd = <<<EOT
119+ /** An example node type definition */
120+ <ns ='http://namespace.com/ns
121+ '>
122+ [ns:NodeType] > ns:ParentType1, ns:ParentType2
123+ orderable mixin
124+ - ex:property (STRING)
125+ EOT ;
126+
127+ $ this ->cndParser ->parseString ($ cnd );
128+ }
129+
130+ /**
131+ * Test the case where the parser did not parse correctly
132+ * the default values at the end of the parsed file.
133+ *
134+ * Assert no exception is thrown
135+ */
136+ public function testNoStopAtEofError ()
137+ {
138+ $ res = $ this ->cndParser ->parseFile (__DIR__ . '/resources/cnd/no-stop-at-eof.cnd ' );
139+
140+ $ this ->assertTrue (isset ($ res ['namespaces ' ]));
141+ $ this ->assertEquals (array ('phpcr ' => 'http://www.doctrine-project.org/projects/phpcr_odm ' ), $ res ['namespaces ' ]);
40142
41- $ res = $ parser ->parseString ($ cnd );
143+ $ this ->assertTrue (isset ($ res ['nodeTypes ' ]));
144+ }
145+
146+ public function testBigFile ()
147+ {
148+ //var_dump($this->sharedFixture['session']->getWorkspace()->getNodeTypeManager()->getNodeType('nt:file')->hasOrderableChildNodes());die;
149+ $ res = $ this ->cndParser ->parseFile (__DIR__ . '/resources/cnd/jackrabbit_nodetypes.cnd ' );
42150
43- $ def = reset ($ res ['nodeTypes ' ]);
151+ // some random sanity checks
152+ $ this ->assertTrue (isset ($ res ['nodeTypes ' ]));
44153
154+ $ def = $ res ['nodeTypes ' ];
155+ $ this ->assertTrue (isset ($ def ['nt:file ' ]));
156+ /** @var $parsed NodeTypeDefinitionInterface */
157+ $ parsed = $ def ['nt:file ' ];
158+ $ this ->assertEquals ('nt:file ' , $ parsed ->getName ());
159+ $ this ->assertTrue ($ parsed ->isQueryable ());
160+ $ this ->assertFalse ($ parsed ->isAbstract ());
161+ $ this ->assertFalse ($ parsed ->hasOrderableChildNodes ());
162+ $ this ->assertFalse ($ parsed ->isMixin ());
163+ }
164+
165+ /**
166+ * Check if $res matches the expected node type definition from the
167+ * "worst case" example.
168+ *
169+ * @param array $res namespaces and node types
170+ */
171+ protected function assertExampleCnd ($ res )
172+ {
173+ $ this ->assertTrue (isset ($ res ['namespaces ' ]));
45174 $ this ->assertEquals (array ('ns ' => 'http://namespace.com/ns ' ), $ res ['namespaces ' ]);
46175
176+ $ this ->assertTrue (isset ($ res ['nodeTypes ' ]));
177+ // get first node type
178+ reset ($ res ['nodeTypes ' ]);
179+ /** @var $def NodeTypeDefinitionInterface */
180+ list ($ name , $ def ) = each ($ res ['nodeTypes ' ]);
181+
182+ $ this ->assertEquals ('ns:NodeType ' , $ name );
47183 $ this ->assertInstanceOf ('\PHPCR\NodeType\NodeTypeTemplateInterface ' , $ def );
48184 $ this ->assertEquals ('ns:NodeType ' , $ def ->getName ());
49185 $ this ->assertEquals (array ('ns:ParentType1 ' , 'ns:ParentType2 ' ), $ def ->getDeclaredSuperTypeNames ());
50186 $ this ->assertTrue ($ def ->hasOrderableChildNodes ());
51187 $ this ->assertTrue ($ def ->isMixin ());
52- $ this ->assertFalse ($ def ->isQueryable ());
188+ $ this ->assertTrue ($ def ->isQueryable ());
53189 $ this ->assertFalse ($ def ->isAbstract ());
54190 $ this ->assertEquals (1 , count ($ def ->getPropertyDefinitionTemplates ()));
55191
@@ -70,12 +206,4 @@ function testGenerator()
70206 $ this ->assertTrue ($ prop ->isQueryOrderable ()); // True because there was no "noqueryorder" attribute
71207 }
72208
73- public function testBigFile ()
74- {
75- $ cnd = file_get_contents (__DIR__ . '/resources/jackrabbit_nodetypes.cnd ' );
76- $ parser = new CndParser ($ this ->sharedFixture ['session ' ]->getWorkspace ()->getNodeTypeManager ());
77-
78- $ res = $ parser ->parseString ($ cnd );
79- // TODO: compare with the types we get from the repository
80- }
81209}
0 commit comments