Skip to content
This repository has been archived by the owner on Sep 16, 2021. It is now read-only.

Commit

Permalink
Refactored tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dantleech committed Jun 2, 2016
1 parent 10f0b7e commit 318a42e
Show file tree
Hide file tree
Showing 7 changed files with 358 additions and 281 deletions.
50 changes: 41 additions & 9 deletions Repository/AbstractPhpcrRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use Puli\Repository\Api\ResourceRepository;
use Puli\Repository\Api\UnsupportedLanguageException;
use Puli\Repository\Resource\Collection\ArrayResourceCollection;
use Webmozart\Assert\Assert;
use Webmozart\PathUtil\Path;
use Puli\Repository\AbstractRepository;
use Symfony\Cmf\Component\Resource\Repository\Api\EditableRepository;
Expand Down Expand Up @@ -78,23 +77,48 @@ public function find($query, $language = 'glob')
public function remove($query, $language = 'glob')
{
$this->failUnlessGlob($language);
Assert::notEq('', trim($query, '/'), 'The root directory cannot be deleted.');
$nodes = $this->finder->find($this->resolvePath($query));

// delegate remove nodes to the implementation
$this->removeNodes($nodes);
if (0 === count($nodes)) {
return 0;
}

try {
// delegate remove nodes to the implementation
$this->removeNodes($nodes);
} catch (\Exception $e) {
throw new \RuntimeException(sprintf(
'Error encountered when removing resource(s) using query "%s"',
$query
), null, $e);
}

return count($nodes);
}

/**
* {@inheritdoc}
*/
public function move($sourceQuery, $targetPath, $language = 'glob')
public function move($query, $targetPath, $language = 'glob')
{
$this->failUnlessGlob($language);
Assert::notEq('', trim($sourceQuery, '/'), 'The root directory cannot be moved.');
$nodes = $this->finder->find($this->resolvePath($query));

$this->moveNodes($nodes, $sourceQuery, $targetPath);
if (0 === count($nodes)) {
return 0;
}

try {
// delegate moving to the implementation
$this->moveNodes($nodes, $query, $targetPath);
} catch (\Exception $e) {
throw new \RuntimeException(sprintf(
'Error encountered when moving resource(s) using query "%s"',
$query
), null, $e);
}

return count($nodes);
}

/**
Expand All @@ -105,6 +129,14 @@ public function clear()
throw new \BadMethodCallException('Clear not supported');
}

/**
* {@inheritdoc}
*/
public function add($path, $resource)
{
throw new \BadMethodCallException('Add not supported');
}

/**
* Return the path with the basePath prefix
* if it has been set.
Expand Down Expand Up @@ -152,12 +184,12 @@ abstract protected function buildCollection(array $nodes);
*
* @param NodeInterface[]
*/
abstract protected function removeNodes($nodes);
abstract protected function removeNodes(array $nodes);

/**
* Move the given nodes.
*
* @param NodeInterface[]
*/
abstract protected function moveNodes($nodes, $sourceQuery, $targetPath);
abstract protected function moveNodes(array $nodes, $query, $targetPath);
}
7 changes: 6 additions & 1 deletion Repository/Api/EditableRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,17 @@
interface EditableRepository extends EditableRepository
{
/**
* Move all resources found by $sourceQuery to the target (parent) path.
* Move all resources and their subgraphs found by $sourceQuery to the
* target (parent) path and returns the number of nodes that have been
* *explicitly* moved (i.e. the number of resources found by the query, NOT
* the total number of nodes affected).
*
* @param string $sourceQuery
* @param string $targetPath
* @param string $language
*
* @return int
*
* @throws InvalidArgumentException If the sourceQuery is invalid.
* @throws UnsupportedLanguageException If the language is not supported.
*/
Expand Down
10 changes: 7 additions & 3 deletions Repository/PhpcrOdmRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Puli\Repository\Api\ResourceNotFoundException;
use Puli\Repository\Resource\Collection\ArrayResourceCollection;
use Symfony\Cmf\Component\Resource\Repository\Resource\PhpcrOdmResource;
use PHPCR\NodeInterface;

class PhpcrOdmRepository extends AbstractPhpcrRepository
{
Expand Down Expand Up @@ -124,7 +125,7 @@ protected function buildCollection(array $documents)
/**
* {@inheritdoc}
*/
protected function removeNodes($nodes)
protected function removeNodes(array $nodes)
{
foreach ($nodes as $node) {
$document = $this->getDocumentForNode($node);
Expand All @@ -134,13 +135,16 @@ protected function removeNodes($nodes)
$this->getManager()->flush();
}

protected function moveNodes($nodes, $sourceQuery, $targetPath)
/**
* {@inheritdoc}
*/
protected function moveNodes(array $nodes, $sourceQuery, $targetPath)
{
$this->doMoveNodes($nodes, $sourceQuery, $targetPath);
$this->getManager()->flush();
}

private function doMoveNodes($nodes, $sourceQuery, $targetPath)
private function doMoveNodes(array $nodes, $sourceQuery, $targetPath)
{
if (1 === count($nodes) && current($nodes)->getPath() === $sourceQuery) {
$document = $this->getDocumentForNode(current($nodes));
Expand Down
9 changes: 6 additions & 3 deletions Repository/PhpcrRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ public function get($path)
return $resource;
}

/**
* {@inheritdoc}
*/
public function listChildren($path)
{
$resource = $this->get($path);
Expand Down Expand Up @@ -123,7 +126,7 @@ protected function buildCollection(array $nodes)
/**
* {@inheritdoc}
*/
protected function removeNodes($nodes)
protected function removeNodes(array $nodes)
{
foreach ($nodes as $node) {
$node->remove();
Expand All @@ -135,13 +138,13 @@ protected function removeNodes($nodes)
/**
* {@inheritdoc}
*/
protected function moveNodes($nodes, $sourceQuery, $targetPath)
protected function moveNodes(array $nodes, $sourceQuery, $targetPath)
{
$this->doMoveNodes($nodes, $sourceQuery, $targetPath);
$this->session->save();
}

private function doMoveNodes($nodes, $sourceQuery, $targetPath)
private function doMoveNodes(array $nodes, $sourceQuery, $targetPath)
{
if (count($nodes) === 1 && current($nodes)->getPath() === $sourceQuery) {
return $this->session->move(current($nodes)->getPath(), $targetPath);
Expand Down
Loading

0 comments on commit 318a42e

Please sign in to comment.