Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
209 changes: 209 additions & 0 deletions tests/PhpcrUtils/CndParserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
<?php

namespace PHPCR\Tests\PhpcrUtils;

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

use PHPCR\NodeType\NodeTypeDefinitionInterface;
use PHPCR\NodeType\NodeTypeManagerInterface;
use PHPCR\PropertyType;
use PHPCR\NodeType\PropertyDefinitionTemplateInterface;
use PHPCR\Version\OnParentVersionAction;

use PHPCR\Util\CND\Parser\CndParser;


class CndParserTest extends \PHPCR\Test\BaseCase
{
/** @var CndParser */
private $cndParser;

public function setUp()
{
parent::setUp();
$this->cndParser = new CndParser($this->sharedFixture['session']->getWorkspace()->getNodeTypeManager());
}

public function testParseNormal()
{
$res = $this->cndParser->parseFile(__DIR__ . '/resources/cnd/example.cnd');
$this->assertExampleCnd($res);
}

public function testParseCompact()
{
$res = $this->cndParser->parseFile(__DIR__ . '/resources/cnd/example.compact.cnd');
$this->assertExampleCnd($res);

}

public function testParseVerbose()
{
$res = $this->cndParser->parseFile(__DIR__ . '/resources/cnd/example.verbose.cnd');
$this->assertExampleCnd($res);
}


public function testParseString()
{
// the "worst case" example from http://jackrabbit.apache.org/node-type-notation.html
$cnd = <<<EOT
/** An example node type definition */
<ns ='http://namespace.com/ns'>
[ns:NodeType] > ns:ParentType1, ns:ParentType2
orderable mixin
- ex:property (STRING)
= 'default1' , 'default2'
mandatory autocreated protected multiple
VERSION
< 'constraint1', 'constraint2'
+ ns:node (ns:reqType1, ns:reqType2)
= ns:defaultType
mandatory autocreated protected VERSION
EOT;


$res = $this->cndParser->parseString($cnd);
$this->assertExampleCnd($res);
}

/**
* @expectedException \PHPCR\Util\CND\Exception\ParserException
*/
public function testParseError()
{
$cnd = <<<EOT
/** An example node type definition */
<ns ='http://namespace.com/ns'>
[ns:NodeType] > ns:ParentType1, ns:ParentType2
orderable mixin
- ex:property (STRING)
= 'default1' , 'default2'
mandatory invalid-string protected multiple
VERSION
< 'constraint1', 'constraint2'
+ ns:node (ns:reqType1, ns:reqType2)
= ns:defaultType
mandatory autocreated protected VERSION
EOT;

$this->cndParser->parseString($cnd);
}

/**
* @expectedException \InvalidArgumentException
*/
public function testErrorNoFile()
{
$this->cndParser->parseFile('/not/found');
}

/**
* @expectedException \PHPCR\Util\CND\Exception\ScannerException
*/
public function testScannerErrorComment()
{
$cnd = <<<EOT
la /*
EOT;

$this->cndParser->parseString($cnd);
}

/**
* @expectedException \PHPCR\Util\CND\Exception\ScannerException
*/
public function testScannerErrorNewline()
{
$cnd = <<<EOT
/** An example node type definition */
<ns ='http://namespace.com/ns
'>
[ns:NodeType] > ns:ParentType1, ns:ParentType2
orderable mixin
- ex:property (STRING)
EOT;

$this->cndParser->parseString($cnd);
}

/**
* Test the case where the parser did not parse correctly
* the default values at the end of the parsed file.
*
* Assert no exception is thrown
*/
public function testNoStopAtEofError()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sixty-nine do you remember what exactly you wanted to test here?

{
$res = $this->cndParser->parseFile(__DIR__ . '/resources/cnd/no-stop-at-eof.cnd');

$this->assertTrue(isset($res['namespaces']));
$this->assertEquals(array('phpcr' => 'http://www.doctrine-project.org/projects/phpcr_odm'), $res['namespaces']);

$this->assertTrue(isset($res['nodeTypes']));
}

public function testBigFile()
{
//var_dump($this->sharedFixture['session']->getWorkspace()->getNodeTypeManager()->getNodeType('nt:file')->hasOrderableChildNodes());die;
$res = $this->cndParser->parseFile(__DIR__ . '/resources/cnd/jackrabbit_nodetypes.cnd');

// some random sanity checks
$this->assertTrue(isset($res['nodeTypes']));

$def = $res['nodeTypes'];
$this->assertTrue(isset($def['nt:file']));
/** @var $parsed NodeTypeDefinitionInterface */
$parsed = $def['nt:file'];
$this->assertEquals('nt:file', $parsed->getName());
$this->assertFalse($parsed->isAbstract());
$this->assertFalse($parsed->hasOrderableChildNodes());
$this->assertFalse($parsed->isMixin());
// queryable default is implementation specific
}

/**
* Check if $res matches the expected node type definition from the
* "worst case" example.
*
* @param array $res namespaces and node types
*/
protected function assertExampleCnd($res)
{
$this->assertTrue(isset($res['namespaces']));
$this->assertEquals(array('ns' => 'http://namespace.com/ns'), $res['namespaces']);

$this->assertTrue(isset($res['nodeTypes']));
// get first node type
reset($res['nodeTypes']);
/** @var $def NodeTypeDefinitionInterface */
list($name, $def) = each($res['nodeTypes']);

$this->assertEquals('ns:NodeType', $name);
$this->assertInstanceOf('\PHPCR\NodeType\NodeTypeTemplateInterface', $def);
$this->assertEquals('ns:NodeType', $def->getName());
$this->assertEquals(array('ns:ParentType1', 'ns:ParentType2'), $def->getDeclaredSuperTypeNames());
$this->assertTrue($def->hasOrderableChildNodes());
$this->assertTrue($def->isMixin());
// queryable default is implementation specific
$this->assertFalse($def->isAbstract());
$this->assertEquals(1, count($def->getPropertyDefinitionTemplates()));

/** @var $prop PropertyDefinitionTemplateInterface */
$prop = $def->getPropertyDefinitionTemplates()->getIterator()->current();

$this->assertEquals('ex:property', $prop->getName());
$this->assertEquals(PropertyType::STRING, $prop->getRequiredType());
$this->assertEquals(array('default1', 'default2'), $prop->getDefaultValues());
$this->assertEquals(array('constraint1', 'constraint2'), $prop->getValueConstraints());
$this->assertTrue($prop->isAutoCreated());
$this->assertTrue($prop->isMandatory());
$this->assertTrue($prop->isProtected());
$this->assertTrue($prop->isMultiple());
$this->assertEquals(OnParentVersionAction::VERSION, $prop->getOnParentVersion());
$this->assertEquals(array(), $prop->getAvailableQueryOperators());
$this->assertTrue($prop->isFullTextSearchable()); // True because there was no "nofulltext" attribute
$this->assertTrue($prop->isQueryOrderable()); // True because there was no "noqueryorder" attribute
}

}
12 changes: 12 additions & 0 deletions tests/PhpcrUtils/resources/cnd/example.cnd
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* An example node type definition */
<ns ='http://namespace.com/ns'>
[ns:NodeType] > ns:ParentType1, ns:ParentType2
orderable mixin
- ex:property (STRING)
= 'default1' , 'default2'
mandatory autocreated protected multiple
VERSION
< 'constraint1', 'constraint2'
+ ns:node (ns:reqType1, ns:reqType2)
= ns:defaultType
mandatory autocreated protected VERSION
6 changes: 6 additions & 0 deletions tests/PhpcrUtils/resources/cnd/example.compact.cnd
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<ns= 'http://namespace.com/ns'>
[ns:NodeType]>ns:ParentType1, ns:ParentType2 o m
-ex:property(STRING)='default1','default2' m a p * VERSION
<'constraint1', 'constraint2'
+ns:node(ns:reqType1,ns:reqType2)=ns:defaultType
m a p VERSION
48 changes: 48 additions & 0 deletions tests/PhpcrUtils/resources/cnd/example.verbose.cnd
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* An example node type definition */

// The namespace declaration
<ns = 'http://namespace.com/ns'>

// Node type name
[ns:NodeType]

// Supertypes
> ns:ParentType1, ns:ParentType2

// This node type supports orderable child nodes
orderable

// This is a mixin node type
mixin

// Nodes of this node type have a property called 'ex:property' of type STRING
- ex:property (STRING)

// The default values for this
// (multi-value) property are...
= 'default1', 'default2'

// and it is...
mandatory autocreated protected

// and multi-valued
multiple

// and has an on-parent-version setting of ...
VERSION

// The constraint settings are...
< 'constraint1', 'constraint2'

// Nodes of this node type have a child node called ns:node which must be of
// at least the node types ns:reqType1 and ns:reqType2
+ ns:node (ns:reqType1, ns:reqType2)

// and the default primary node type of the child node is...
= ns:defaultType

// This child node is...
mandatory autocreated protected

// and has an on-parent-version setting of ...
VERSION
Loading