Skip to content

Commit e437994

Browse files
added option to control the uuid-behavior
1 parent c41d985 commit e437994

File tree

2 files changed

+46
-10
lines changed

2 files changed

+46
-10
lines changed

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

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
namespace PHPCR\Util\Console\Command;
44

5+
use PHPCR\ImportUUIDBehaviorInterface;
6+
use PHPCR\RepositoryInterface;
57
use Symfony\Component\Console\Input\InputInterface;
68
use Symfony\Component\Console\Input\InputOption;
79
use Symfony\Component\Console\Output\OutputInterface;
8-
use PHPCR\RepositoryInterface;
9-
use PHPCR\ImportUUIDBehaviorInterface;
1010

1111
/**
1212
* Command to import a system or document view XML into the repository.
@@ -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, 'Behavior to handle uuid on 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
@@ -60,11 +68,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
6068
return 1;
6169
}
6270

63-
$session->importXML(
64-
$parentPath,
65-
$filename,
66-
ImportUUIDBehaviorInterface::IMPORT_UUID_CREATE_NEW
67-
);
71+
$uuidBehavior = $input->getOption('uuid-behavior');
72+
if (!array_key_exists($uuidBehavior, self::UUID_BEHAVIOR)) {
73+
$output->writeln(sprintf('<error>UUID-Behavior "%s" is not supported</error>', $uuidBehavior));
74+
75+
return 1;
76+
}
77+
78+
$session->importXML($parentPath, $filename, self::UUID_BEHAVIOR[$uuidBehavior]);
6879
$session->save();
6980

7081
$output->writeln(sprintf(

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

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

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

5+
use PHPCR\ImportUUIDBehaviorInterface;
56
use PHPCR\Util\Console\Command\WorkspaceImportCommand;
67
use PHPCR\RepositoryInterface;
78

@@ -14,7 +15,7 @@ public function setUp()
1415
$this->application->add(new WorkspaceImportCommand());
1516
}
1617

17-
public function testNodeTypeList()
18+
public function testImport()
1819
{
1920
$this->session->expects($this->once())
2021
->method('getRepository')
@@ -26,10 +27,34 @@ public function testNodeTypeList()
2627
->will($this->returnValue(true));
2728

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

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

3560
$this->assertContains('Successfully imported', $ct->getDisplay());

0 commit comments

Comments
 (0)