Skip to content

Commit 79084c3

Browse files
committed
more comprehensive tests of cnd parser
1 parent ba30e9f commit 79084c3

File tree

6 files changed

+218
-19
lines changed

6 files changed

+218
-19
lines changed

tests/PhpcrUtils/CndParserTest.php

Lines changed: 147 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,49 @@
44

55
require_once(__DIR__ . '/../../inc/BaseCase.php');
66

7+
use PHPCR\NodeType\NodeTypeDefinitionInterface;
8+
use PHPCR\NodeType\NodeTypeManagerInterface;
79
use PHPCR\PropertyType;
810
use PHPCR\NodeType\PropertyDefinitionTemplateInterface;
911
use PHPCR\Version\OnParentVersionAction;
1012

11-
use PHPCR\Util\CND\Helper\NodeTypeGenerator;
12-
use PHPCR\Util\CND\Reader\BufferReader;
1313
use PHPCR\Util\CND\Parser\CndParser;
14-
use PHPCR\Util\CND\Scanner\GenericScanner;
15-
use PHPCR\Util\CND\Scanner\Context\DefaultScannerContextWithoutSpacesAndComments;
14+
1615

1716
class 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
3563
EOT;
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
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* An example node type definition */
2+
<ns ='http://namespace.com/ns'>
3+
[ns:NodeType] > ns:ParentType1, ns:ParentType2
4+
orderable mixin
5+
- ex:property (STRING)
6+
= 'default1' , 'default2'
7+
mandatory autocreated protected multiple
8+
VERSION
9+
< 'constraint1', 'constraint2'
10+
+ ns:node (ns:reqType1, ns:reqType2)
11+
= ns:defaultType
12+
mandatory autocreated protected VERSION
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<ns= 'http://namespace.com/ns'>
2+
[ns:NodeType]>ns:ParentType1, ns:ParentType2 o m
3+
-ex:property(STRING)='default1','default2' m a p * VERSION
4+
<'constraint1', 'constraint2'
5+
+ns:node(ns:reqType1,ns:reqType2)=ns:defaultType
6+
m a p VERSION
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* An example node type definition */
2+
3+
// The namespace declaration
4+
<ns = 'http://namespace.com/ns'>
5+
6+
// Node type name
7+
[ns:NodeType]
8+
9+
// Supertypes
10+
> ns:ParentType1, ns:ParentType2
11+
12+
// This node type supports orderable child nodes
13+
orderable
14+
15+
// This is a mixin node type
16+
mixin
17+
18+
// Nodes of this node type have a property called 'ex:property' of type STRING
19+
- ex:property (STRING)
20+
21+
// The default values for this
22+
// (multi-value) property are...
23+
= 'default1', 'default2'
24+
25+
// and it is...
26+
mandatory autocreated protected
27+
28+
// and multi-valued
29+
multiple
30+
31+
// and has an on-parent-version setting of ...
32+
VERSION
33+
34+
// The constraint settings are...
35+
< 'constraint1', 'constraint2'
36+
37+
// Nodes of this node type have a child node called ns:node which must be of
38+
// at least the node types ns:reqType1 and ns:reqType2
39+
+ ns:node (ns:reqType1, ns:reqType2)
40+
41+
// and the default primary node type of the child node is...
42+
= ns:defaultType
43+
44+
// This child node is...
45+
mandatory autocreated protected
46+
47+
// and has an on-parent-version setting of ...
48+
VERSION
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<'phpcr'='http://www.doctrine-project.org/projects/phpcr_odm'>
2+
[phpcr:primary_item_test] > ?
3+
- phpcr:content (STRING)
4+
= ?
5+
< ?

0 commit comments

Comments
 (0)