Skip to content

Added option to control the uuid-behavior #184

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions src/PHPCR/Util/Console/Command/WorkspaceImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
*/
class WorkspaceImportCommand extends BaseCommand
{
const UUID_BEHAVIOR = [
'new' => ImportUUIDBehaviorInterface::IMPORT_UUID_CREATE_NEW,
'remove' => ImportUUIDBehaviorInterface::IMPORT_UUID_COLLISION_REMOVE_EXISTING,
'replace' => ImportUUIDBehaviorInterface::IMPORT_UUID_COLLISION_REPLACE_EXISTING,
'throw' => ImportUUIDBehaviorInterface::IMPORT_UUID_COLLISION_THROW,
];

/**
* {@inheritdoc}
*/
Expand All @@ -28,6 +35,7 @@ protected function configure()
->setName('phpcr:workspace:import')
->addArgument('filename', null, 'The xml file to import')
->addOption('parentpath', 'p', InputOption::VALUE_OPTIONAL, 'Repository path to the parent where to import the file contents', '/')
->addOption('uuid-behavior', null, InputOption::VALUE_REQUIRED, 'Behavior to handle uuid on import', 'new')
->setDescription('Import xml data into the repository, either in JCR system view format or arbitrary xml')
->setHelp(<<<'EOF'
The <info>import</info> command uses the PHPCR SessionInterface::importXml method
Expand All @@ -38,6 +46,15 @@ protected function configure()

If the <info>parentpath</info> option is set, the document is imported to that
path. Otherwise the document is imported at the repository root.

The optional <info>uuid-behavior</info> option describes how uuids should be
handled. Following options are available:

* <info>new</info> recreate the uuid foreach node
* <info>remove</info> remove the node from imported dataset on uuid collision
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just fyi: remove does remove the existing node on collision, not the one being imported, according to the jcr specification. i changed the doc accordingly.

* <info>replace</info> replace the node in the database on uuid collision
* <info>throw</info> throw exception on uuid collision

EOF
);
}
Expand All @@ -58,11 +75,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
return 1;
}

$session->importXML(
$parentPath,
$filename,
ImportUUIDBehaviorInterface::IMPORT_UUID_CREATE_NEW
);
$uuidBehavior = $input->getOption('uuid-behavior');
if (!array_key_exists($uuidBehavior, self::UUID_BEHAVIOR)) {
$output->writeln(sprintf('<error>UUID-Behavior "%s" is not supported</error>', $uuidBehavior));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we please add another line saying 'Supported behaviors are %s', implode(', ', self::UUID_BEHAVIOR)

$output->writeln(sprintf('Supported behaviors are %s', implode(', ', array_keys(self::UUID_BEHAVIOR))));

return 1;
}

$session->importXML($parentPath, $filename, self::UUID_BEHAVIOR[$uuidBehavior]);
$session->save();

$output->writeln(sprintf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPCR\Tests\Util\Console\Command;

use PHPCR\ImportUUIDBehaviorInterface;
use PHPCR\RepositoryInterface;
use PHPCR\Util\Console\Command\WorkspaceImportCommand;

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

public function testNodeTypeList()
public function testImport()
{
$this->session->expects($this->once())
->method('getRepository')
Expand All @@ -26,12 +27,36 @@ public function testNodeTypeList()
->will($this->returnValue(true));

$this->session->expects($this->once())
->method('importXml');
->method('importXml')
->with('/', 'test_import.xml', ImportUUIDBehaviorInterface::IMPORT_UUID_CREATE_NEW);

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

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

public function testImportUuidBehaviorThrow()
{
$this->session->expects($this->once())
->method('getRepository')
->will($this->returnValue($this->repository));

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

$this->session->expects($this->once())
->method('importXml')
->with('/', 'test_import.xml', ImportUUIDBehaviorInterface::IMPORT_UUID_COLLISION_THROW);

$ct = $this->executeCommand('phpcr:workspace:import', [
'filename' => 'test_import.xml',
'--uuid-behavior' => 'throw',
]);

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