Skip to content

Backport of #185 #186

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

Merged
merged 6 commits into from
Feb 13, 2018
Merged
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
33 changes: 28 additions & 5 deletions src/PHPCR/Util/Console/Command/WorkspaceImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,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 @@ -29,6 +36,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, 'How to handle UUID collisions during the 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 @@ -39,6 +47,17 @@ 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. The following options are available:

* <info>new</info> recreate a new uuid for each imported node;
* <info>remove</info> on collision, remove the old node from the repository and
put the imported data in the tree;
* <info>replace</info> on collision, replace the existing node with the one being
imported. All children of the imported node also go to the new path;
* <info>throw</info> throw an exception on uuid collision.

EOF
)
;
Expand All @@ -60,11 +79,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));
$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,8 +2,9 @@

namespace PHPCR\Tests\Util\Console\Command;

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

class WorkspaceImportCommandTest extends BaseCommandTest
{
Expand All @@ -14,24 +15,48 @@ public function setUp()
$this->application->add(new WorkspaceImportCommand());
}

public function testNodeTypeList()
public function testImport()
{
$this->session->expects($this->once())
->method('getRepository')
->will($this->returnValue($this->repository));
$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');
$this->session->expects($this->once())->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());
}
}