Skip to content

Commit 3208e71

Browse files
committed
Refactored node dump command, added Node* command tests
- Still need todo workspace* command tests
1 parent 9d7c531 commit 3208e71

File tree

12 files changed

+408
-22
lines changed

12 files changed

+408
-22
lines changed

src/PHPCR/Util/Console/Command/NodeDumpCommand.php

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -96,43 +96,31 @@ public function setDumpMaxLineLength($length)
9696
protected function execute(InputInterface $input, OutputInterface $output)
9797
{
9898
$session = $this->getHelper('phpcr')->getSession();
99+
$dumperHelper = $this->getHelper('phpcr_console_dumper');
99100

100101
// node to dump
101102
$identifier = $input->getArgument('identifier');
102103

103104
// whether to dump node uuid
104-
$identifiers = $input->hasParameterOption('--identifiers');
105-
$nodeVisitor = new ConsoleDumperNodeVisitor($output, $identifiers);
105+
$options = array();
106+
$options['dump_uuids'] = $input->hasParameterOption('--identifiers');
107+
$options['ref_format'] = $input->getOption('ref-format');
108+
$options['show_props'] = $input->hasParameterOption('--props');
109+
$options['show_sys_nodes'] = $input->hasParameterOption('--sys-nodes');
106110

107-
$propVisitor = null;
108-
$refFormat = $input->getOption('ref-format');
109-
110-
if (null !== $refFormat && !in_array($refFormat, array('uuid', 'path'))) {
111+
if (null !== $options['ref_format']&& !in_array($options['ref_format'], array('uuid', 'path'))) {
111112
throw new \Exception('The ref-format option must be set to either "path" or "uuid"');
112113
}
113114

114-
if ($input->hasParameterOption('--props')) {
115-
$options = array();
116-
if ($refFormat) {
117-
$options['ref_format'] = $refFormat;
118-
}
119-
$propVisitor = new ConsoleDumperPropertyVisitor($output, $options);
120-
}
121-
122-
$walker = new TreeWalker($nodeVisitor, $propVisitor);
123-
124-
if (!$input->hasParameterOption('--sys_nodes')) {
125-
$filter = new SystemNodeFilter();
126-
$walker->addNodeFilter($filter);
127-
$walker->addPropertyFilter($filter);
128-
}
115+
$walker = $dumperHelper->getTreeWalker($output, $options);
129116

130117
try {
131118
if (UUIDHelper::isUUID($identifier)) {
132119
$node = $session->getNodeByIdentifier($identifier);
133120
} else {
134121
$node = $session->getNode($identifier);
135122
}
123+
136124
$walker->traverse($node, $input->getOption('depth'));
137125
} catch (RepositoryException $e) {
138126
if ($e instanceof PathNotFoundException || $e instanceof ItemNotFoundException) {

src/PHPCR/Util/Console/Command/NodeRemoveCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
8888
// try to remove system nodes.
8989
throw new \InvalidArgumentException(
9090
'Can not delete root node (path "/"), please use the '.
91-
'workspace:purge command instead to purge the whole workspace.'
91+
'workspace:purge command instead to purge the whole workspace.'
9292
);
9393
}
9494

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the PHPCR Utils
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache Software License 2.0
19+
* @link http://phpcr.github.com/
20+
*/
21+
22+
namespace PHPCR\Util\Console\Helper;
23+
24+
use Symfony\Component\Console\Helper\Helper;
25+
use Symfony\Component\Console\Output\OutputInterface;
26+
use PHPCR\Util\Console\Helper\TreeDumper\ConsoleDumperNodeVisitor;
27+
use PHPCR\Util\Console\Helper\TreeDumper\ConsoleDumperPropertyVisitor;
28+
use PHPCR\Util\TreeWalker;
29+
use PHPCR\Util\Console\Helper\TreeDumper\SystemNodeFilter;
30+
31+
/**
32+
* Helper class to make the session instance available to console commands
33+
*/
34+
class PhpcrConsoleDumperHelper extends Helper
35+
{
36+
public function getTreeWalker(OutputInterface $output, $options)
37+
{
38+
$options = array_merge(array(
39+
'dump_uuids' => false,
40+
'ref_format' => 'uuid',
41+
'show_props' => false,
42+
'show_sys_nodes' => false,
43+
), $options);
44+
45+
$propVisitor = null;
46+
$nodeVisitor = new ConsoleDumperNodeVisitor($output, $options['dump_uuids']);
47+
48+
if (true === $options['show_props']) {
49+
new ConsoleDumperPropertyVisitor($output, $options);
50+
}
51+
52+
$treeWalker = new TreeWalker($nodeVisitor, $propVisitor);
53+
if (false === $options['show_sys_nodes']) {
54+
$filter = new SystemNodeFilter();
55+
$treeWalker->addNodeFilter($filter);
56+
$treeWalker->addPropertyFilter($filter);
57+
}
58+
59+
return $treeWalker;
60+
}
61+
62+
/**
63+
* {@inheritDoc}
64+
*/
65+
public function getName()
66+
{
67+
return 'phpcr_console_dumper';
68+
}
69+
}
70+
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace PHPCR\Tests\Util\Console\Command;
4+
5+
use Symfony\Component\Console\Application;
6+
use PHPCR\Util\Console\Command\NodeDumpCommand;
7+
use Symfony\Component\Console\Tester\CommandTester;
8+
use Symfony\Component\Console\Helper\HelperSet;
9+
use PHPCR\Util\Console\Helper\PhpcrHelper;
10+
11+
abstract class BaseCommandTest extends \PHPUnit_Framework_TestCase
12+
{
13+
public function setUp()
14+
{
15+
$this->session = $this->getMock('PHPCR\SessionInterface');
16+
$this->dumperHelper = $this->getMockBuilder('PHPCR\Util\Console\Helper\PhpcrConsoleDumperHelper')
17+
->disableOriginalConstructor()
18+
->getMock();
19+
$this->helperSet = new HelperSet(array(
20+
'session' => new PhpcrHelper($this->session),
21+
'phpcr_console_dumper' => $this->dumperHelper,
22+
));
23+
24+
$this->application = new Application();
25+
$this->application->setHelperSet($this->helperSet);
26+
27+
for ($i = 1; $i <= 3; $i++) {
28+
$varName = 'node'.$i;
29+
$this->$varName = $this->getMockBuilder('Jackalope\Node')
30+
->disableOriginalConstructor()
31+
->getMock();
32+
if ($i > 1) {
33+
$this->$varName->expects($this->any())
34+
->method('getNodes')
35+
->will($this->returnValue(array()));
36+
}
37+
}
38+
39+
$this->workspace = $this->getMock('PHPCR\WorkspaceInterface');
40+
41+
$this->session->expects($this->any())
42+
->method('getWorkspace')
43+
->will($this->returnValue($this->workspace));
44+
45+
$this->workspace->expects($this->any())
46+
->method('getName')
47+
->will($this->returnValue('test'));
48+
}
49+
50+
public function executeCommand($name, $args)
51+
{
52+
$command = $this->application->find($name);
53+
$commandTester = new CommandTester($command);
54+
$args = $args = array_merge(array(
55+
'command' => $command->getName(),
56+
), $args);
57+
$commandTester->execute($args);
58+
59+
60+
61+
return $commandTester;
62+
}
63+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace PHPCR\Tests\Util\Console\Command;
4+
5+
use Symfony\Component\Console\Application;
6+
use PHPCR\Util\Console\Command\NodeDumpCommand;
7+
8+
class NodeDumpCommandTest extends BaseCommandTest
9+
{
10+
public function setUp()
11+
{
12+
parent::setUp();
13+
$this->treeWalker = $this->getMockBuilder(
14+
'PHPCR\Util\TreeWalker'
15+
)->disableOriginalConstructor()->getMock();
16+
}
17+
18+
public function testCommand()
19+
{
20+
$this->dumperHelper->expects($this->once())
21+
->method('getTreeWalker')
22+
->will($this->returnValue($this->treeWalker));
23+
$this->session->expects($this->once())
24+
->method('getNode')
25+
->will($this->returnValue($this->node1));
26+
$this->node1->expects($this->any())
27+
->method('getNodes')
28+
->will($this->returnValue(array(
29+
$this->node1,
30+
$this->node2,
31+
)));
32+
33+
$this->application->add(new NodeDumpCommand());
34+
35+
$ct = $this->executeCommand('phpcr:node:dump', array());
36+
}
37+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace PHPCR\Tests\Util\Console\Command;
4+
5+
use Symfony\Component\Console\Application;
6+
use PHPCR\Util\Console\Command\NodeMoveCommand;
7+
8+
class NodeMoveCommandTest extends BaseCommandTest
9+
{
10+
public function provideCommand()
11+
{
12+
return array(
13+
array(array('source' => '/foo', 'destination' => '/bar'))
14+
);
15+
}
16+
17+
/**
18+
* Note that this test isn't very conclusive. This should
19+
* be a functional test.
20+
*
21+
* @dataProvider provideCommand
22+
*/
23+
public function testCommand($args)
24+
{
25+
$this->session->expects($this->once())
26+
->method('move')
27+
->with($args['source'], $args['destination']);
28+
$this->session->expects($this->once())
29+
->method('save');
30+
31+
$this->application->add(new NodeMoveCommand());
32+
$ct = $this->executeCommand('phpcr:node:move', $args);
33+
}
34+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace PHPCR\Tests\Util\Console\Command;
4+
5+
use Symfony\Component\Console\Application;
6+
use PHPCR\Util\Console\Command\NodeRemoveCommand;
7+
8+
class NodeRemoveCommandTest extends BaseCommandTest
9+
{
10+
public function setUp()
11+
{
12+
parent::setUp();
13+
$this->application->add(new NodeRemoveCommand());
14+
}
15+
16+
public function testRemove()
17+
{
18+
$this->session->expects($this->once())
19+
->method('removeItem')
20+
->with('/cms');
21+
22+
$ct = $this->executeCommand('phpcr:node:remove', array(
23+
'--force' => true,
24+
'path' => '/cms',
25+
));
26+
}
27+
28+
/**
29+
* @expectedException \LogicException
30+
*/
31+
public function testRemoveRoot()
32+
{
33+
$ct = $this->executeCommand('phpcr:node:remove', array(
34+
'--force' => true,
35+
'path' => '/',
36+
));
37+
}
38+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace PHPCR\Tests\Util\Console\Command;
4+
5+
use Symfony\Component\Console\Application;
6+
use PHPCR\Util\Console\Command\NodeTouchCommand;
7+
8+
/**
9+
* Currently very minimal test for touch command
10+
*/
11+
class NodeTouchCommandTest extends BaseCommandTest
12+
{
13+
public function setUp()
14+
{
15+
parent::setUp();
16+
$this->application->add(new NodeTouchCommand());
17+
$this->nodeType = $this->getMock('PHPCR\NodeType\NodeTypeInterface');
18+
}
19+
20+
public function testTouch()
21+
{
22+
$test = $this;
23+
$this->session->expects($this->exactly(2))
24+
->method('getNode')
25+
->will($this->returnCallback(function ($path) use ($test) {
26+
switch ($path) {
27+
case '/':
28+
return $this->node1;
29+
case '/cms':
30+
return null;
31+
}
32+
}));
33+
34+
$this->node1->expects($this->once())
35+
->method('addNode');
36+
37+
$this->session->expects($this->once())
38+
->method('save');
39+
40+
$ct = $this->executeCommand('phpcr:node:touch', array(
41+
'path' => '/cms',
42+
));
43+
}
44+
}
45+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace PHPCR\Tests\Util\Console\Command;
4+
5+
use Symfony\Component\Console\Application;
6+
use PHPCR\Util\Console\Command\NodeTypeListCommand;
7+
8+
class NodeTypeListCommandTest extends BaseCommandTest
9+
{
10+
public function setUp()
11+
{
12+
parent::setUp();
13+
$this->application->add(new NodeTypeListCommand());
14+
$this->nodeTypeManager = $this->getMockBuilder(
15+
'Jackalope\NodeType\NodeTypeManager'
16+
)->disableOriginalConstructor()->getMock();
17+
}
18+
19+
public function testNodeTypeList()
20+
{
21+
$this->session->expects($this->once())
22+
->method('getWorkspace')
23+
->will($this->returnValue($this->workspace));
24+
$this->workspace->expects($this->once())
25+
->method('getNodeTypeManager')
26+
->will($this->returnValue($this->nodeTypeManager));
27+
$this->nodeTypeManager->expects($this->once())
28+
->method('getAllNodeTypes')
29+
->will($this->returnValue(array()));
30+
$ct = $this->executeCommand('phpcr:node-type:list', array(
31+
));
32+
}
33+
}
34+
35+

0 commit comments

Comments
 (0)