Skip to content

Commit f50eebb

Browse files
committed
add workspace:delete command and do some cleanups on testing. fix #62
1 parent d1edda0 commit f50eebb

File tree

6 files changed

+254
-15
lines changed

6 files changed

+254
-15
lines changed

bin/phpcr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ $cli->addCommands(array(
4747
new \PHPCR\Util\Console\Command\NodeTypeListCommand(),
4848
new \PHPCR\Util\Console\Command\NodeTypeRegisterCommand(),
4949
new \PHPCR\Util\Console\Command\WorkspaceCreateCommand(),
50+
new \PHPCR\Util\Console\Command\WorkspaceDeleteCommand(),
5051
new \PHPCR\Util\Console\Command\WorkspaceExportCommand(),
5152
new \PHPCR\Util\Console\Command\WorkspaceImportCommand(),
5253
new \PHPCR\Util\Console\Command\WorkspacePurgeCommand(),

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@
2121

2222
namespace PHPCR\Util\Console\Command;
2323

24+
use PHPCR\RepositoryInterface;
25+
use PHPCR\SessionInterface;
2426
use Symfony\Component\Console\Command\Command;
2527
use Symfony\Component\Console\Input\InputArgument;
2628
use Symfony\Component\Console\Input\InputInterface;
2729
use Symfony\Component\Console\Output\OutputInterface;
28-
use PHPCR\RepositoryInterface;
2930

3031
/**
3132
* A command to create a workspace in the PHPCR repository
@@ -47,7 +48,7 @@ protected function configure()
4748
->setHelp(<<<EOT
4849
The <info>workspace:create</info> command creates a workspace with the specified name.
4950
It will fail if a workspace with that name already exists or if the repository implementation
50-
does not support this operation.
51+
does not support the workspace creation operation.
5152
EOT
5253
)
5354
;
@@ -58,9 +59,10 @@ protected function configure()
5859
*/
5960
protected function execute(InputInterface $input, OutputInterface $output)
6061
{
62+
/** @var $session SessionInterface */
6163
$session = $this->getHelper('phpcr')->getSession();
6264

63-
$name = $input->getArgument('name');
65+
$workspaceName = $input->getArgument('name');
6466

6567
$workspace = $session->getWorkspace();
6668
$repo = $session->getRepository();
@@ -75,9 +77,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
7577
return 1;
7678
}
7779

78-
$workspace->createWorkspace($name);
80+
$workspace->createWorkspace($workspaceName);
7981

80-
$output->writeln("Created workspace '$name'.");
82+
$output->writeln("Created workspace '$workspaceName'.");
8183

8284
return 0;
8385
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the PHPCR Utils
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache Software License 2.0
19+
* @link http://phpcr.github.com/
20+
*/
21+
22+
namespace PHPCR\Util\Console\Command;
23+
24+
use PHPCR\RepositoryInterface;
25+
use PHPCR\SessionInterface;
26+
use Symfony\Component\Console\Command\Command;
27+
use Symfony\Component\Console\Helper\DialogHelper;
28+
use Symfony\Component\Console\Input\InputArgument;
29+
use Symfony\Component\Console\Input\InputInterface;
30+
use Symfony\Component\Console\Input\InputOption;
31+
use Symfony\Component\Console\Output\OutputInterface;
32+
33+
/**
34+
* A command to delete a workspace in the PHPCR repository
35+
*
36+
* @author David Buchmann <mail@davidbu.ch>
37+
*/
38+
class WorkspaceDeleteCommand extends Command
39+
{
40+
/**
41+
* {@inheritDoc}
42+
*/
43+
protected function configure()
44+
{
45+
$this
46+
->setName('phpcr:workspace:delete')
47+
->addArgument('name', InputArgument::REQUIRED, 'Name of the workspace to delete')
48+
->addOption('force', null, InputOption::VALUE_NONE, 'Use to bypass the confirmation dialog')
49+
->setDescription('Delete a workspace from the configured repository')
50+
->setHelp(<<<EOT
51+
The <info>workspace:delete</info> command deletes the workspace with the specified name if it
52+
exists. If the workspace with that name does not yet exist, the command will not fail.
53+
However, if the workspace does exist but the repository implementation does not support
54+
the delete operation, the command will fail.
55+
EOT
56+
)
57+
;
58+
}
59+
60+
/**
61+
* {@inheritDoc}
62+
*/
63+
protected function execute(InputInterface $input, OutputInterface $output)
64+
{
65+
/** @var $session SessionInterface */
66+
$session = $this->getHelper('phpcr')->getSession();
67+
68+
$workspaceName = $input->getArgument('name');
69+
70+
$workspace = $session->getWorkspace();
71+
$repo = $session->getRepository();
72+
73+
if (! in_array($workspaceName, $workspace->getAccessibleWorkspaceNames())) {
74+
$output->writeln("Workspace '$workspaceName' does not exist.");
75+
return 0;
76+
}
77+
78+
if (!$repo->getDescriptor(RepositoryInterface::OPTION_WORKSPACE_MANAGEMENT_SUPPORTED)) {
79+
$output->writeln(
80+
'<error>Your PHPCR implementation does not support '.
81+
'workspace management. Please refer to the documentation '.
82+
'of your PHPCR implementation to learn how to remove a workspace.</error>'
83+
);
84+
85+
return 1;
86+
}
87+
88+
$force = $input->getOption('force');
89+
if (!$force) {
90+
$dialog = new DialogHelper();
91+
$force = $dialog->askConfirmation($output, sprintf(
92+
'<question>Are you sure you want to delete workspace "%s" Y/N ?</question>',
93+
$workspaceName
94+
), false);
95+
}
96+
if (!$force) {
97+
$output->writeln('<error>Aborted</error>');
98+
99+
return 1;
100+
}
101+
102+
$workspace->deleteWorkspace($workspaceName);
103+
104+
$output->writeln("Deleted workspace '$workspaceName'.");
105+
106+
return 0;
107+
}
108+
}

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

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,33 @@
22

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

5+
use PHPCR\RepositoryInterface;
56
use Symfony\Component\Console\Application;
6-
use PHPCR\Util\Console\Command\NodeDumpCommand;
77
use Symfony\Component\Console\Tester\CommandTester;
88
use Symfony\Component\Console\Helper\HelperSet;
9+
10+
use PHPCR\SessionInterface;
11+
use PHPCR\WorkspaceInterface;
12+
use PHPCR\Util\Console\Helper\PhpcrConsoleDumperHelper;
913
use PHPCR\Util\Console\Helper\PhpcrHelper;
1014

1115
require_once(__DIR__.'/Stubs/MockNode.php');
1216

1317
abstract class BaseCommandTest extends \PHPUnit_Framework_TestCase
1418
{
19+
/** @var SessionInterface|\PHPUnit_Framework_MockObject_MockObject */
20+
protected $session;
21+
/** @var WorkspaceInterface|\PHPUnit_Framework_MockObject_MockObject */
22+
protected $workspace;
23+
/** @var RepositoryInterface|\PHPUnit_Framework_MockObject_MockObject */
24+
protected $repository;
25+
/** @var PhpcrConsoleDumperHelper|\PHPUnit_Framework_MockObject_MockObject */
26+
protected $dumperHelper;
27+
/** @var HelperSet */
28+
protected $helperSet;
29+
/** @var Application */
30+
protected $application;
31+
1532
public function setUp()
1633
{
1734
$this->session = $this->getMock('PHPCR\SessionInterface');
@@ -41,16 +58,23 @@ public function setUp()
4158
$this->application->setHelperSet($this->helperSet);
4259
}
4360

44-
public function executeCommand($name, $args)
61+
/**
62+
* Build and execute the command tester.
63+
*
64+
* @param string $name command name
65+
* @param array $args command arguments
66+
* @param int $status expected return status
67+
*
68+
* @return CommandTester
69+
*/
70+
public function executeCommand($name, $args, $status = 0)
4571
{
4672
$command = $this->application->find($name);
4773
$commandTester = new CommandTester($command);
4874
$args = $args = array_merge(array(
4975
'command' => $command->getName(),
5076
), $args);
51-
$commandTester->execute($args);
52-
53-
77+
$this->assertEquals(0, $commandTester->execute($args));
5478

5579
return $commandTester;
5680
}

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

Lines changed: 41 additions & 5 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\RepositoryException;
56
use Symfony\Component\Console\Application;
67
use PHPCR\Util\Console\Command\WorkspaceCreateCommand;
78
use PHPCR\RepositoryInterface;
@@ -14,23 +15,58 @@ public function setUp()
1415
$this->application->add(new WorkspaceCreateCommand());
1516
}
1617

17-
public function testNodeTypeList()
18+
public function testCreate()
1819
{
1920
$this->session->expects($this->once())
2021
->method('getWorkspace')
21-
->will($this->returnValue($this->workspace));
22+
->will($this->returnValue($this->workspace))
23+
;
24+
$this->session->expects($this->once())
25+
->method('getRepository')
26+
->will($this->returnValue($this->repository))
27+
;
28+
$this->repository->expects($this->once())
29+
->method('getDescriptor')
30+
->with(RepositoryInterface::OPTION_WORKSPACE_MANAGEMENT_SUPPORTED)
31+
->will($this->returnValue(true))
32+
;
2233
$this->workspace->expects($this->once())
2334
->method('createWorkspace')
24-
->with('test_workspace');
35+
->with('test_workspace')
36+
;
37+
38+
$this->executeCommand('phpcr:workspace:create', array(
39+
'name' => 'test_workspace'
40+
));
41+
}
42+
43+
/**
44+
* The real console catches this exception.
45+
*
46+
* @expectedException \PHPCR\RepositoryException
47+
* @expectedExceptionMessage Workspace exists
48+
*/
49+
public function testCreateExisting()
50+
{
51+
$this->session->expects($this->once())
52+
->method('getWorkspace')
53+
->will($this->returnValue($this->workspace))
54+
;
2555
$this->session->expects($this->once())
2656
->method('getRepository')
2757
->will($this->returnValue($this->repository));
2858
$this->repository->expects($this->once())
2959
->method('getDescriptor')
3060
->with(RepositoryInterface::OPTION_WORKSPACE_MANAGEMENT_SUPPORTED)
31-
->will($this->returnValue(true));
61+
->will($this->returnValue(true))
62+
;
63+
$this->workspace->expects($this->once())
64+
->method('createWorkspace')
65+
->with('test_workspace')
66+
->will($this->throwException(new RepositoryException('Workspace exists')))
67+
;
3268

33-
$ct = $this->executeCommand('phpcr:workspace:create', array(
69+
$this->executeCommand('phpcr:workspace:create', array(
3470
'name' => 'test_workspace'
3571
));
3672
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace PHPCR\Tests\Util\Console\Command;
4+
5+
use Symfony\Component\Console\Application;
6+
use PHPCR\Util\Console\Command\WorkspaceDeleteCommand;
7+
use PHPCR\RepositoryInterface;
8+
9+
class WorkspaceDeleteCommandTest extends BaseCommandTest
10+
{
11+
public function setUp()
12+
{
13+
parent::setUp();
14+
$this->application->add(new WorkspaceDeleteCommand());
15+
}
16+
17+
public function testDelete()
18+
{
19+
$this->session->expects($this->once())
20+
->method('getWorkspace')
21+
->will($this->returnValue($this->workspace))
22+
;
23+
$this->workspace->expects($this->once())
24+
->method('getAccessibleWorkspaceNames')
25+
->will($this->returnValue(array('default', 'test_workspace', 'other')))
26+
;
27+
$this->session->expects($this->once())
28+
->method('getRepository')
29+
->will($this->returnValue($this->repository))
30+
;
31+
$this->repository->expects($this->once())
32+
->method('getDescriptor')
33+
->with(RepositoryInterface::OPTION_WORKSPACE_MANAGEMENT_SUPPORTED)
34+
->will($this->returnValue(true))
35+
;
36+
$this->workspace->expects($this->once())
37+
->method('deleteWorkspace')
38+
->with('test_workspace')
39+
;
40+
41+
$ct = $this->executeCommand('phpcr:workspace:delete', array(
42+
'name' => 'test_workspace',
43+
'--force' => 'true',
44+
));
45+
46+
$this->assertContains("Deleted workspace 'test_workspace'.", $ct->getDisplay());
47+
}
48+
49+
public function testDeleteNonexistent()
50+
{
51+
$this->session->expects($this->once())
52+
->method('getWorkspace')
53+
->will($this->returnValue($this->workspace))
54+
;
55+
$this->workspace->expects($this->once())
56+
->method('getAccessibleWorkspaceNames')
57+
->will($this->returnValue(array('default', 'other')))
58+
;
59+
60+
61+
$ct = $this->executeCommand('phpcr:workspace:delete', array(
62+
'name' => 'test_workspace',
63+
'--force' => 'true',
64+
));
65+
66+
$this->assertContains("Workspace 'test_workspace' does not exist.", $ct->getDisplay());
67+
}
68+
}

0 commit comments

Comments
 (0)