diff --git a/bin/phpcr b/bin/phpcr index 3e29861..2d1c1c8 100755 --- a/bin/phpcr +++ b/bin/phpcr @@ -47,6 +47,7 @@ $cli->addCommands(array( new \PHPCR\Util\Console\Command\NodeTypeListCommand(), new \PHPCR\Util\Console\Command\NodeTypeRegisterCommand(), new \PHPCR\Util\Console\Command\WorkspaceCreateCommand(), + new \PHPCR\Util\Console\Command\WorkspaceDeleteCommand(), new \PHPCR\Util\Console\Command\WorkspaceExportCommand(), new \PHPCR\Util\Console\Command\WorkspaceImportCommand(), new \PHPCR\Util\Console\Command\WorkspacePurgeCommand(), diff --git a/src/PHPCR/Util/Console/Command/WorkspaceCreateCommand.php b/src/PHPCR/Util/Console/Command/WorkspaceCreateCommand.php index 881d9e9..3322ca5 100644 --- a/src/PHPCR/Util/Console/Command/WorkspaceCreateCommand.php +++ b/src/PHPCR/Util/Console/Command/WorkspaceCreateCommand.php @@ -21,11 +21,12 @@ namespace PHPCR\Util\Console\Command; +use PHPCR\RepositoryInterface; +use PHPCR\SessionInterface; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -use PHPCR\RepositoryInterface; /** * A command to create a workspace in the PHPCR repository @@ -47,7 +48,7 @@ protected function configure() ->setHelp(<<workspace:create command creates a workspace with the specified name. It will fail if a workspace with that name already exists or if the repository implementation -does not support this operation. +does not support the workspace creation operation. EOT ) ; @@ -58,9 +59,10 @@ protected function configure() */ protected function execute(InputInterface $input, OutputInterface $output) { + /** @var $session SessionInterface */ $session = $this->getHelper('phpcr')->getSession(); - $name = $input->getArgument('name'); + $workspaceName = $input->getArgument('name'); $workspace = $session->getWorkspace(); $repo = $session->getRepository(); @@ -75,9 +77,9 @@ protected function execute(InputInterface $input, OutputInterface $output) return 1; } - $workspace->createWorkspace($name); + $workspace->createWorkspace($workspaceName); - $output->writeln("Created workspace '$name'."); + $output->writeln("Created workspace '$workspaceName'."); return 0; } diff --git a/src/PHPCR/Util/Console/Command/WorkspaceDeleteCommand.php b/src/PHPCR/Util/Console/Command/WorkspaceDeleteCommand.php new file mode 100644 index 0000000..78f5281 --- /dev/null +++ b/src/PHPCR/Util/Console/Command/WorkspaceDeleteCommand.php @@ -0,0 +1,108 @@ + + */ +class WorkspaceDeleteCommand extends Command +{ + /** + * {@inheritDoc} + */ + protected function configure() + { + $this + ->setName('phpcr:workspace:delete') + ->addArgument('name', InputArgument::REQUIRED, 'Name of the workspace to delete') + ->addOption('force', null, InputOption::VALUE_NONE, 'Use to bypass the confirmation dialog') + ->setDescription('Delete a workspace from the configured repository') + ->setHelp(<<workspace:delete command deletes the workspace with the specified name if it +exists. If the workspace with that name does not yet exist, the command will not fail. +However, if the workspace does exist but the repository implementation does not support +the delete operation, the command will fail. +EOT + ) + ; + } + + /** + * {@inheritDoc} + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + /** @var $session SessionInterface */ + $session = $this->getHelper('phpcr')->getSession(); + + $workspaceName = $input->getArgument('name'); + + $workspace = $session->getWorkspace(); + $repo = $session->getRepository(); + + if (! in_array($workspaceName, $workspace->getAccessibleWorkspaceNames())) { + $output->writeln("Workspace '$workspaceName' does not exist."); + return 0; + } + + if (!$repo->getDescriptor(RepositoryInterface::OPTION_WORKSPACE_MANAGEMENT_SUPPORTED)) { + $output->writeln( + 'Your PHPCR implementation does not support '. + 'workspace management. Please refer to the documentation '. + 'of your PHPCR implementation to learn how to remove a workspace.' + ); + + return 1; + } + + $force = $input->getOption('force'); + if (!$force) { + $dialog = new DialogHelper(); + $force = $dialog->askConfirmation($output, sprintf( + 'Are you sure you want to delete workspace "%s" Y/N ?', + $workspaceName + ), false); + } + if (!$force) { + $output->writeln('Aborted'); + + return 1; + } + + $workspace->deleteWorkspace($workspaceName); + + $output->writeln("Deleted workspace '$workspaceName'."); + + return 0; + } +} diff --git a/tests/PHPCR/Tests/Util/Console/Command/BaseCommandTest.php b/tests/PHPCR/Tests/Util/Console/Command/BaseCommandTest.php index 543f407..5b645d5 100644 --- a/tests/PHPCR/Tests/Util/Console/Command/BaseCommandTest.php +++ b/tests/PHPCR/Tests/Util/Console/Command/BaseCommandTest.php @@ -2,16 +2,33 @@ namespace PHPCR\Tests\Util\Console\Command; +use PHPCR\RepositoryInterface; use Symfony\Component\Console\Application; -use PHPCR\Util\Console\Command\NodeDumpCommand; use Symfony\Component\Console\Tester\CommandTester; use Symfony\Component\Console\Helper\HelperSet; + +use PHPCR\SessionInterface; +use PHPCR\WorkspaceInterface; +use PHPCR\Util\Console\Helper\PhpcrConsoleDumperHelper; use PHPCR\Util\Console\Helper\PhpcrHelper; require_once(__DIR__.'/Stubs/MockNode.php'); abstract class BaseCommandTest extends \PHPUnit_Framework_TestCase { + /** @var SessionInterface|\PHPUnit_Framework_MockObject_MockObject */ + protected $session; + /** @var WorkspaceInterface|\PHPUnit_Framework_MockObject_MockObject */ + protected $workspace; + /** @var RepositoryInterface|\PHPUnit_Framework_MockObject_MockObject */ + protected $repository; + /** @var PhpcrConsoleDumperHelper|\PHPUnit_Framework_MockObject_MockObject */ + protected $dumperHelper; + /** @var HelperSet */ + protected $helperSet; + /** @var Application */ + protected $application; + public function setUp() { $this->session = $this->getMock('PHPCR\SessionInterface'); @@ -41,16 +58,23 @@ public function setUp() $this->application->setHelperSet($this->helperSet); } - public function executeCommand($name, $args) + /** + * Build and execute the command tester. + * + * @param string $name command name + * @param array $args command arguments + * @param int $status expected return status + * + * @return CommandTester + */ + public function executeCommand($name, $args, $status = 0) { $command = $this->application->find($name); $commandTester = new CommandTester($command); $args = $args = array_merge(array( 'command' => $command->getName(), ), $args); - $commandTester->execute($args); - - + $this->assertEquals(0, $commandTester->execute($args)); return $commandTester; } diff --git a/tests/PHPCR/Tests/Util/Console/Command/WorkspaceCreateCommandTest.php b/tests/PHPCR/Tests/Util/Console/Command/WorkspaceCreateCommandTest.php index bce234d..0a00efa 100644 --- a/tests/PHPCR/Tests/Util/Console/Command/WorkspaceCreateCommandTest.php +++ b/tests/PHPCR/Tests/Util/Console/Command/WorkspaceCreateCommandTest.php @@ -2,6 +2,7 @@ namespace PHPCR\Tests\Util\Console\Command; +use PHPCR\RepositoryException; use Symfony\Component\Console\Application; use PHPCR\Util\Console\Command\WorkspaceCreateCommand; use PHPCR\RepositoryInterface; @@ -14,23 +15,58 @@ public function setUp() $this->application->add(new WorkspaceCreateCommand()); } - public function testNodeTypeList() + public function testCreate() { $this->session->expects($this->once()) ->method('getWorkspace') - ->will($this->returnValue($this->workspace)); + ->will($this->returnValue($this->workspace)) + ; + $this->session->expects($this->once()) + ->method('getRepository') + ->will($this->returnValue($this->repository)) + ; + $this->repository->expects($this->once()) + ->method('getDescriptor') + ->with(RepositoryInterface::OPTION_WORKSPACE_MANAGEMENT_SUPPORTED) + ->will($this->returnValue(true)) + ; $this->workspace->expects($this->once()) ->method('createWorkspace') - ->with('test_workspace'); + ->with('test_workspace') + ; + + $this->executeCommand('phpcr:workspace:create', array( + 'name' => 'test_workspace' + )); + } + + /** + * The real console catches this exception. + * + * @expectedException \PHPCR\RepositoryException + * @expectedExceptionMessage Workspace exists + */ + public function testCreateExisting() + { + $this->session->expects($this->once()) + ->method('getWorkspace') + ->will($this->returnValue($this->workspace)) + ; $this->session->expects($this->once()) ->method('getRepository') ->will($this->returnValue($this->repository)); $this->repository->expects($this->once()) ->method('getDescriptor') ->with(RepositoryInterface::OPTION_WORKSPACE_MANAGEMENT_SUPPORTED) - ->will($this->returnValue(true)); + ->will($this->returnValue(true)) + ; + $this->workspace->expects($this->once()) + ->method('createWorkspace') + ->with('test_workspace') + ->will($this->throwException(new RepositoryException('Workspace exists'))) + ; - $ct = $this->executeCommand('phpcr:workspace:create', array( + $this->executeCommand('phpcr:workspace:create', array( 'name' => 'test_workspace' )); } diff --git a/tests/PHPCR/Tests/Util/Console/Command/WorkspaceDeleteCommandTest.php b/tests/PHPCR/Tests/Util/Console/Command/WorkspaceDeleteCommandTest.php new file mode 100644 index 0000000..36751c5 --- /dev/null +++ b/tests/PHPCR/Tests/Util/Console/Command/WorkspaceDeleteCommandTest.php @@ -0,0 +1,68 @@ +application->add(new WorkspaceDeleteCommand()); + } + + public function testDelete() + { + $this->session->expects($this->once()) + ->method('getWorkspace') + ->will($this->returnValue($this->workspace)) + ; + $this->workspace->expects($this->once()) + ->method('getAccessibleWorkspaceNames') + ->will($this->returnValue(array('default', 'test_workspace', 'other'))) + ; + $this->session->expects($this->once()) + ->method('getRepository') + ->will($this->returnValue($this->repository)) + ; + $this->repository->expects($this->once()) + ->method('getDescriptor') + ->with(RepositoryInterface::OPTION_WORKSPACE_MANAGEMENT_SUPPORTED) + ->will($this->returnValue(true)) + ; + $this->workspace->expects($this->once()) + ->method('deleteWorkspace') + ->with('test_workspace') + ; + + $ct = $this->executeCommand('phpcr:workspace:delete', array( + 'name' => 'test_workspace', + '--force' => 'true', + )); + + $this->assertContains("Deleted workspace 'test_workspace'.", $ct->getDisplay()); + } + + public function testDeleteNonexistent() + { + $this->session->expects($this->once()) + ->method('getWorkspace') + ->will($this->returnValue($this->workspace)) + ; + $this->workspace->expects($this->once()) + ->method('getAccessibleWorkspaceNames') + ->will($this->returnValue(array('default', 'other'))) + ; + + + $ct = $this->executeCommand('phpcr:workspace:delete', array( + 'name' => 'test_workspace', + '--force' => 'true', + )); + + $this->assertContains("Workspace 'test_workspace' does not exist.", $ct->getDisplay()); + } +}