Skip to content

Commit

Permalink
Merge pull request #187 from phpcr/php72
Browse files Browse the repository at this point in the history
avoid `each` calls, test register single node type method
  • Loading branch information
dbu authored Nov 21, 2017
2 parents 10e9217 + ad15b65 commit 9d958ba
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 15 deletions.
4 changes: 2 additions & 2 deletions tests/NodeTypeDiscovery/NodeDefinitionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ public function setUp()
$defs = self::$file->getChildNodeDefinitions();
$this->assertInternalType('array', $defs);
$this->assertCount(1, $defs);
list($key, $this->content) = each($defs);
$this->content = current($defs);
$this->assertInstanceOf(NodeDefinitionInterface::class, $this->content);
$this->assertEquals('jcr:content', $this->content->getName());

$defs = self::$folder->getChildNodeDefinitions();
$this->assertInternalType('array', $defs);
$this->assertCount(1, $defs);
list($key, $this->hierarchyNodeDef) = each($defs);
$this->hierarchyNodeDef = next($defs);
$this->assertInstanceOf(NodeDefinitionInterface::class, $this->hierarchyNodeDef);
$this->assertEquals('*', $this->hierarchyNodeDef->getName());
} catch (Exception $e) {
Expand Down
2 changes: 1 addition & 1 deletion tests/NodeTypeDiscovery/NodeTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public function testGetChildNodeDefinitions()
$children = self::$file->getChildNodeDefinitions();
$this->assertInternalType('array', $children);
$this->assertCount(1, $children);
list($key, $child) = each($children);
$child = current($children);
$this->assertInstanceOf(NodeDefinitionInterface::class, $child);
$this->assertEquals('jcr:content', $child->getName());
// the rest is tested in NodeDefinitionTest
Expand Down
9 changes: 5 additions & 4 deletions tests/NodeTypeManagement/NodeTypeBaseCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ protected function assertTypes($types)
$this->assertCount(2, $types, 'Wrong number of nodes registered');

// apitest
list($name, $type) = each($types);
$type = current($types);
$name = key($types);
$this->assertEquals('phpcr:apitest', $name);
$this->assertInstanceOf(NodeTypeDefinitionInterface::class, $type);
/* @var $type NodeTypeDefinitionInterface */
Expand All @@ -108,7 +109,8 @@ protected function assertTypes($types)
$this->assertTrue($props[0]->isMultiple());

// test
list($name, $type) = each($types);
$type = next($types);
$name = key($types);
$this->assertEquals('phpcr:test', $name);
$this->assertInstanceOf(NodeTypeDefinitionInterface::class, $type);
/* @var $type NodeTypeDefinitionInterface */
Expand Down Expand Up @@ -164,8 +166,7 @@ public function testPrimaryItem()
$root = $this->session->getRootNode();

if ($root->hasNode('test_node')) {
$node = $root->getNode('test_node');
$node->remove();
$root->getNode('test_node')->remove();
$this->session->save();
}

Expand Down
40 changes: 40 additions & 0 deletions tests/NodeTypeManagement/NodeTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,53 @@

namespace PHPCR\Tests\NodeTypeManagement;

use PHPCR\NodeType\NodeTypeDefinitionInterface;
use PHPCR\PropertyType;

/**
* Covering jcr-2.8.3 spec $19.
*/
class NodeTypeTest extends NodeTypeBaseCase
{
public function testRegisterNodeType()
{
$ns = $this->workspace->getNamespaceRegistry();
$ns->registerNamespace('phpcr', 'http://www.doctrine-project.org/projects/phpcr_odm');

$ntm = $this->workspace->getNodeTypeManager();

$apitest = $ntm->createNodeTypeTemplate();
$apitest->setName('phpcr:apitest');
$apitest->setMixin(true);

$class = $ntm->createPropertyDefinitionTemplate();
$class->setName('phpcr:class');
$class->setRequiredType(PropertyType::STRING);
$class->setMultiple(true);
$apitest->getPropertyDefinitionTemplates()->append($class);

$type = $ntm->registerNodeType($apitest, true);

$this->assertApitestType($type);

$session = $this->renewSession();
$ntm = $session->getWorkspace()->getNodeTypeManager();

$this->assertTrue($ntm->hasNodeType('phpcr:apitest'));
$this->assertApitestType($ntm->getNodeType('phpcr:apitest'));
}

private function assertApitestType($type)
{
$this->assertInstanceOf(NodeTypeDefinitionInterface::class, $type);
/* @var $type NodeTypeDefinitionInterface */
$this->assertEquals('phpcr:apitest', $type->getName());
$props = $type->getDeclaredPropertyDefinitions();
$this->assertCount(1, $props, 'Wrong number of properties in phpcr:apitest');
$this->assertEquals('phpcr:class', $props[0]->getName());
$this->assertTrue($props[0]->isMultiple());
}

protected function registerNodeTypes($allowUpdate)
{
$ns = $this->workspace->getNamespaceRegistry();
Expand Down
3 changes: 2 additions & 1 deletion tests/PhpcrUtils/CndParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ protected function assertExampleCnd($res)
// get first node type
reset($res['nodeTypes']);
/** @var $def NodeTypeDefinitionInterface */
list($name, $def) = each($res['nodeTypes']);
$def = current($res['nodeTypes']);
$name = key($res['nodeTypes']);

$this->assertEquals('ns:NodeType', $name);
$this->assertInstanceOf(NodeTypeTemplateInterface::class, $def);
Expand Down
8 changes: 4 additions & 4 deletions tests/Reading/SessionReadMethodsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,10 +305,10 @@ public function testGetNodesByIdentifier()
]);

$this->assertCount(2, $nodes);
list($key, $node) = each($nodes);
$node = current($nodes);
$this->assertInstanceOf(NodeInterface::class, $node);
$this->assertEquals('/tests_general_base/idExample', $node->getPath());
list($key, $node) = each($nodes);
$node = next($nodes);
$this->assertInstanceOf(NodeInterface::class, $node);
$this->assertEquals('/tests_general_base/idExample/jcr:content/weakreference_target', $node->getPath());
}
Expand All @@ -322,10 +322,10 @@ public function testGetNodesByIdentifierTraversable()
]));

$this->assertCount(2, $nodes);
list($key, $node) = each($nodes);
$node = current($nodes);
$this->assertInstanceOf(NodeInterface::class, $node);
$this->assertEquals('/tests_general_base/idExample', $node->getPath());
list($key, $node) = each($nodes);
$node = next($nodes);
$this->assertInstanceOf(NodeInterface::class, $node);
$this->assertEquals('/tests_general_base/idExample/jcr:content/weakreference_target', $node->getPath());
}
Expand Down
8 changes: 5 additions & 3 deletions tests/SameNameSiblings/DeleteMethodsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,12 @@ public function testDeleteManyNodes()
$parent = $this->session->getNode($parentPath);
$this->assertCount(count($childrenAtEnd), $parent->getNodes());

$childValue = -1;
foreach ($parent->getNodes() as $node) {
$child = each($childrenAtEnd);
$this->assertEquals($parentPath.'/'.$child['key'], $node->getPath());
$this->assertEquals($child['value'], $node->getProperty('childNumber')->getValue());
$childValue = (-1 === $childValue) ? current($childrenAtEnd) : next($childrenAtEnd);
$childKey = key($childrenAtEnd);
$this->assertEquals($parentPath.'/'.$childKey, $node->getPath());
$this->assertEquals($childValue, $node->getProperty('childNumber')->getValue());
}
}

Expand Down

0 comments on commit 9d958ba

Please sign in to comment.