Skip to content

Commit 335fa86

Browse files
committed
Merge pull request #64 from dantleech/command-tests
[WIP] Refactored node dump command, added Node* command tests
2 parents 7d5c46b + 12ecd4e commit 335fa86

26 files changed

+647
-34
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,4 @@ constistent behaviour.
8787

8888
Move tests about the query converter from phpcr-api-tests tests/06_Query/QOM to
8989
the tests folder. How to do the tests without a QOM factory implementation?
90+

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

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Symfony\Component\Console\Input\InputArgument;
2626
use Symfony\Component\Console\Input\InputInterface;
2727
use Symfony\Component\Console\Output\OutputInterface;
28+
use PHPCR\RepositoryInterface;
2829

2930
/**
3031
* A command to create a workspace in the PHPCR repository
@@ -62,9 +63,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
6263
$name = $input->getArgument('name');
6364

6465
$workspace = $session->getWorkspace();
66+
$repo = $session->getRepository();
6567

66-
if (! $session->getRepository()->getDescriptor(\PHPCR\RepositoryInterface::OPTION_WORKSPACE_MANAGEMENT_SUPPORTED)) {
67-
$output->writeln('<error>Your PHPCR implementation does not support workspace management. Please refer to the documentation of your PHPCR implementation to learn how to create a workspace.</error>');
68+
if (!$repo->getDescriptor(RepositoryInterface::OPTION_WORKSPACE_MANAGEMENT_SUPPORTED)) {
69+
$output->writeln(
70+
'<error>Your PHPCR implementation does not support '.
71+
'workspace management. Please refer to the documentation '.
72+
'of your PHPCR implementation to learn how to create a workspace.</error>'
73+
);
6874

6975
return 1;
7076
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,10 @@ protected function configure()
6666
*/
6767
protected function execute(InputInterface $input, OutputInterface $output)
6868
{
69-
/** @var $session \PHPCR\SessionInterface */
7069
$session = $this->getHelper('phpcr')->getSession();
71-
if (! $session->getRepository()->getDescriptor(RepositoryInterface::OPTION_XML_EXPORT_SUPPORTED)) {
70+
$repo = $session->getRepository();
71+
72+
if (!$repo->getDescriptor(RepositoryInterface::OPTION_XML_EXPORT_SUPPORTED)) {
7273
$output->writeln('<error>This repository does not support xml export</error>');
7374

7475
return 1;

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,22 @@ protected function configure()
6767
*/
6868
protected function execute(InputInterface $input, OutputInterface $output)
6969
{
70-
/** @var $session \PHPCR\SessionInterface */
70+
$filename = $input->getArgument('filename');
71+
$parentPath = $input->getOption('parentpath');
7172
$session = $this->getHelper('phpcr')->getSession();
72-
if (! $session->getRepository()->getDescriptor(RepositoryInterface::OPTION_XML_IMPORT_SUPPORTED)) {
73+
$repo = $session->getRepository();
74+
75+
if (!$repo->getDescriptor(RepositoryInterface::OPTION_XML_IMPORT_SUPPORTED)) {
7376
$output->writeln('<error>This repository does not support xml import</error>');
7477

7578
return 1;
7679
}
7780

78-
$parentpath = $input->getOption('parentpath');
79-
$session->importXml($parentpath, $input->getArgument('filename'), ImportUUIDBehaviorInterface::IMPORT_UUID_CREATE_NEW);
81+
$session->importXml(
82+
$parentPath,
83+
$filename,
84+
ImportUUIDBehaviorInterface::IMPORT_UUID_CREATE_NEW
85+
);
8086
$session->save();
8187

8288
return 0;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
8383
}
8484

8585
$output->writeln(sprintf('<info>Purging workspace:</info> %s', $workspaceName));
86+
87+
// Using the static NodeHelper is bad for testing as we cannot mock it.
8688
NodeHelper::purgeWorkspace($session);
8789
$session->save();
8890

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,26 @@ protected function configure()
5757
protected function execute(InputInterface $input, OutputInterface $output)
5858
{
5959
$sql = $input->getArgument('query');
60-
61-
$session = $this->getHelper('phpcr')->getSession();
62-
$qm = $session->getWorkspace()->getQueryManager();
6360
$language = strtoupper($input->getOption('language'));
6461
$limit = $input->getOption('limit');
6562
$offset = $input->getOption('offset');
63+
64+
$session = $this->getHelper('phpcr')->getSession();
65+
$qm = $session->getWorkspace()->getQueryManager();
66+
6667
if (!defined('\PHPCR\Query\QueryInterface::'.$language)) {
67-
throw new \RuntimeException("Query language '\\PHPCR\\Query\\QueryInterface::$language' not defined.");
68+
throw new \RuntimeException(sprintf(
69+
"Query language '\\PHPCR\\Query\\QueryInterface::%s' not defined.",
70+
$language
71+
));
6872
}
6973

7074
$query = $qm->createQuery($sql, constant('\PHPCR\Query\QueryInterface::'.$language));
75+
7176
if ($limit) {
7277
$query->setLimit($limit);
7378
}
79+
7480
if ($offset) {
7581
$query->setOffset($offset);
7682
}
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: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
require_once(__DIR__.'/Stubb/MockNode.php');
12+
13+
abstract class BaseCommandTest extends \PHPUnit_Framework_TestCase
14+
{
15+
public function setUp()
16+
{
17+
$this->session = $this->getMock('PHPCR\SessionInterface');
18+
$this->workspace = $this->getMock('PHPCR\WorkspaceInterface');
19+
$this->repository = $this->getMock('PHPCR\RepositoryInterface');
20+
21+
$this->node1 = $this->getMock('PHPCR\Tests\Util\Console\Command\Stubb\MockNode');
22+
23+
$this->dumperHelper = $this->getMockBuilder(
24+
'PHPCR\Util\Console\Helper\PhpcrConsoleDumperHelper'
25+
)->disableOriginalConstructor()->getMock();
26+
27+
$this->helperSet = new HelperSet(array(
28+
'session' => new PhpcrHelper($this->session),
29+
'phpcr_console_dumper' => $this->dumperHelper,
30+
));
31+
32+
$this->session->expects($this->any())
33+
->method('getWorkspace')
34+
->will($this->returnValue($this->workspace));
35+
36+
$this->workspace->expects($this->any())
37+
->method('getName')
38+
->will($this->returnValue('test'));
39+
40+
$this->application = new Application();
41+
$this->application->setHelperSet($this->helperSet);
42+
}
43+
44+
public function executeCommand($name, $args)
45+
{
46+
$command = $this->application->find($name);
47+
$commandTester = new CommandTester($command);
48+
$args = $args = array_merge(array(
49+
'command' => $command->getName(),
50+
), $args);
51+
$commandTester->execute($args);
52+
53+
54+
55+
return $commandTester;
56+
}
57+
}

0 commit comments

Comments
 (0)