Skip to content
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
1 change: 1 addition & 0 deletions bin/phpcr
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
12 changes: 7 additions & 5 deletions src/PHPCR/Util/Console/Command/WorkspaceCreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -47,7 +48,7 @@ protected function configure()
->setHelp(<<<EOT
The <info>workspace:create</info> 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
)
;
Expand All @@ -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();
Expand All @@ -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;
}
Expand Down
108 changes: 108 additions & 0 deletions src/PHPCR/Util/Console/Command/WorkspaceDeleteCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

/**
* This file is part of the PHPCR Utils
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache Software License 2.0
* @link http://phpcr.github.com/
*/

namespace PHPCR\Util\Console\Command;

use PHPCR\RepositoryInterface;
use PHPCR\SessionInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\DialogHelper;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
* A command to delete a workspace in the PHPCR repository
*
* @author David Buchmann <mail@davidbu.ch>
*/
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(<<<EOT
The <info>workspace:delete</info> 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(
'<error>Your PHPCR implementation does not support '.
'workspace management. Please refer to the documentation '.
'of your PHPCR implementation to learn how to remove a workspace.</error>'
);

return 1;
}

$force = $input->getOption('force');
if (!$force) {
$dialog = new DialogHelper();
$force = $dialog->askConfirmation($output, sprintf(
'<question>Are you sure you want to delete workspace "%s" Y/N ?</question>',
$workspaceName
), false);
}
if (!$force) {
$output->writeln('<error>Aborted</error>');

return 1;
}

$workspace->deleteWorkspace($workspaceName);

$output->writeln("Deleted workspace '$workspaceName'.");

return 0;
}
}
34 changes: 29 additions & 5 deletions tests/PHPCR/Tests/Util/Console/Command/BaseCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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;
}
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\RepositoryException;
use Symfony\Component\Console\Application;
use PHPCR\Util\Console\Command\WorkspaceCreateCommand;
use PHPCR\RepositoryInterface;
Expand All @@ -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'
));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace PHPCR\Tests\Util\Console\Command;

use Symfony\Component\Console\Application;
use PHPCR\Util\Console\Command\WorkspaceDeleteCommand;
use PHPCR\RepositoryInterface;

class WorkspaceDeleteCommandTest extends BaseCommandTest
{
public function setUp()
{
parent::setUp();
$this->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());
}
}