Skip to content

Commit 0f100e4

Browse files
authored
Merge pull request #186 from wachterjohannes/1.3
Backport of #185
2 parents c41d985 + d30f84f commit 0f100e4

File tree

2 files changed

+60
-12
lines changed

2 files changed

+60
-12
lines changed

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

+28-5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818
*/
1919
class WorkspaceImportCommand extends BaseCommand
2020
{
21+
const UUID_BEHAVIOR = [
22+
'new' => ImportUUIDBehaviorInterface::IMPORT_UUID_CREATE_NEW,
23+
'remove' => ImportUUIDBehaviorInterface::IMPORT_UUID_COLLISION_REMOVE_EXISTING,
24+
'replace' => ImportUUIDBehaviorInterface::IMPORT_UUID_COLLISION_REPLACE_EXISTING,
25+
'throw' => ImportUUIDBehaviorInterface::IMPORT_UUID_COLLISION_THROW,
26+
];
27+
2128
/**
2229
* {@inheritDoc}
2330
*/
@@ -29,6 +36,7 @@ protected function configure()
2936
->setName('phpcr:workspace:import')
3037
->addArgument('filename', null, 'The xml file to import')
3138
->addOption('parentpath', 'p', InputOption::VALUE_OPTIONAL, 'Repository path to the parent where to import the file contents', '/')
39+
->addOption('uuid-behavior', null, InputOption::VALUE_REQUIRED, 'How to handle UUID collisions during the import', 'new')
3240
->setDescription('Import xml data into the repository, either in JCR system view format or arbitrary xml')
3341
->setHelp(<<<EOF
3442
The <info>import</info> command uses the PHPCR SessionInterface::importXml method
@@ -39,6 +47,17 @@ protected function configure()
3947
4048
If the <info>parentpath</info> option is set, the document is imported to that
4149
path. Otherwise the document is imported at the repository root.
50+
51+
The optional <info>uuid-behavior</info> option describes how UUIDs should be
52+
handled. The following options are available:
53+
54+
* <info>new</info> recreate a new uuid for each imported node;
55+
* <info>remove</info> on collision, remove the old node from the repository and
56+
put the imported data in the tree;
57+
* <info>replace</info> on collision, replace the existing node with the one being
58+
imported. All children of the imported node also go to the new path;
59+
* <info>throw</info> throw an exception on uuid collision.
60+
4261
EOF
4362
)
4463
;
@@ -60,11 +79,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
6079
return 1;
6180
}
6281

63-
$session->importXML(
64-
$parentPath,
65-
$filename,
66-
ImportUUIDBehaviorInterface::IMPORT_UUID_CREATE_NEW
67-
);
82+
$uuidBehavior = $input->getOption('uuid-behavior');
83+
if (!array_key_exists($uuidBehavior, self::UUID_BEHAVIOR)) {
84+
$output->writeln(sprintf('<error>UUID-Behavior "%s" is not supported</error>', $uuidBehavior));
85+
$output->writeln(sprintf('Supported behaviors are %s', implode(', ', array_keys(self::UUID_BEHAVIOR))));
86+
87+
return 1;
88+
}
89+
90+
$session->importXML($parentPath, $filename, self::UUID_BEHAVIOR[$uuidBehavior]);
6891
$session->save();
6992

7093
$output->writeln(sprintf(

tests/PHPCR/Tests/Util/Console/Command/WorkspaceImportCommandTest.php

+32-7
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
namespace PHPCR\Tests\Util\Console\Command;
44

5-
use PHPCR\Util\Console\Command\WorkspaceImportCommand;
5+
use PHPCR\ImportUUIDBehaviorInterface;
66
use PHPCR\RepositoryInterface;
7+
use PHPCR\Util\Console\Command\WorkspaceImportCommand;
78

89
class WorkspaceImportCommandTest extends BaseCommandTest
910
{
@@ -14,24 +15,48 @@ public function setUp()
1415
$this->application->add(new WorkspaceImportCommand());
1516
}
1617

17-
public function testNodeTypeList()
18+
public function testImport()
1819
{
19-
$this->session->expects($this->once())
20-
->method('getRepository')
21-
->will($this->returnValue($this->repository));
20+
$this->session->expects($this->once())->method('getRepository')->will($this->returnValue($this->repository));
2221

2322
$this->repository->expects($this->once())
2423
->method('getDescriptor')
2524
->with(RepositoryInterface::OPTION_XML_IMPORT_SUPPORTED)
2625
->will($this->returnValue(true));
2726

28-
$this->session->expects($this->once())
29-
->method('importXml');
27+
$this->session->expects($this->once())->method('importXml')->with(
28+
'/',
29+
'test_import.xml',
30+
ImportUUIDBehaviorInterface::IMPORT_UUID_CREATE_NEW
31+
);
3032

3133
$ct = $this->executeCommand('phpcr:workspace:import', [
3234
'filename' => 'test_import.xml'
3335
]);
3436

3537
$this->assertContains('Successfully imported', $ct->getDisplay());
3638
}
39+
40+
public function testImportUuidBehaviorThrow()
41+
{
42+
$this->session->expects($this->once())->method('getRepository')->will($this->returnValue($this->repository));
43+
$this->repository->expects($this->once())->method('getDescriptor')->with(
44+
RepositoryInterface::OPTION_XML_IMPORT_SUPPORTED
45+
)->will($this->returnValue(true));
46+
$this->session->expects($this->once())->method('importXml')->with(
47+
'/',
48+
'test_import.xml',
49+
ImportUUIDBehaviorInterface::IMPORT_UUID_COLLISION_THROW
50+
);
51+
52+
$ct = $this->executeCommand(
53+
'phpcr:workspace:import',
54+
[
55+
'filename' => 'test_import.xml',
56+
'--uuid-behavior' => 'throw',
57+
]
58+
);
59+
60+
$this->assertContains('Successfully imported', $ct->getDisplay());
61+
}
3762
}

0 commit comments

Comments
 (0)