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 eeff6f3
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 255 deletions.
32 changes: 26 additions & 6 deletions Repository/AbstractPhpcrRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
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;
use Puli\Repository\Api\ResourceNotFoundException;
use DTL\Glob\GlobHelper;

/**
* Abstract repository for both PHPCR and PHPCR-ODM repositories.
Expand Down Expand Up @@ -78,23 +79,34 @@ 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));

if (0 === count($nodes)) {
return 0;
}

// delegate remove nodes to the implementation
$this->removeNodes($nodes);

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;
}

// delegate moving to the implementation
$this->moveNodes($nodes, $query, $targetPath);

return count($nodes);
}

/**
Expand All @@ -105,6 +117,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 @@ -159,5 +179,5 @@ abstract protected function removeNodes($nodes);
*
* @param NodeInterface[]
*/
abstract protected function moveNodes($nodes, $sourceQuery, $targetPath);
abstract protected function moveNodes($nodes, $query, $targetPath);
}
1 change: 1 addition & 0 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
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@

namespace Symfony\Cmf\Component\Resource\Tests\Unit\Repository;

use Puli\Repository\Resource\Collection\ArrayResourceCollection;
use Symfony\Cmf\Component\Resource\Tests\Fixtures\FalsyResource;

abstract class RepositoryTestCase extends \PHPUnit_Framework_TestCase
abstract class AbstractPhpcrRepositoryTestCase extends \PHPUnit_Framework_TestCase
{
protected $finder;
protected $session;
Expand Down Expand Up @@ -61,76 +58,33 @@ public function provideHasChildren()
);
}

public function provideAddInvalid()
{
return [
['', null, 'Target path "" must be absolute.'],
['/test', null, 'Expected an instance of', true],
['/test', new FalsyResource(), 'Expected an instance of', true],
['/test', new ArrayResourceCollection([new FalsyResource()]), 'Expected an instance of '],
];
}

public function provideRemoveInvalid()
{
return [
['/', 'The root directory cannot be deleted.'],
['', 'The target path "" is not absolute.'],
];
}

public function provideInvalidMove()
{
return [
['', ''],
['', '/'],
['/', ''],
['/', '/'],
];
}

/**
* @dataProvider provideRemoveInvalid
*/
public function testRemovePathAssertThrows($path, $expectedExceptionMessage, $language = 'glob')
{
$this->setExpectedException(\InvalidArgumentException::class, $expectedExceptionMessage);

$this->getRepository()->remove($path, $language);
}

/**
* @expectedException \Puli\Repository\Api\UnsupportedLanguageException
*/
public function testRemoveFailsOnNotSupportedGlob()
{
$this->getRepository()->remove('/test', 'some-other');
}

/**
* @expectedException \Puli\Repository\Api\UnsupportedLanguageException
* Clear is not supported.
*
* @expectedException \Exception
*/
public function testMoveFailsOnNotSupportedGlob()
public function testClearShouldThrow()
{
$this->getRepository()->move('/test', '/test', 'some-other');
$this->getRepository()->clear();
}

/**
* @dataProvider provideInvalidMove
*
* @expectedException \InvalidArgumentException
* When removing, it should return 0 as number of items if no nodes are found.
*/
public function testFailingMoveWillThrow($sourcePath, $targetPath, $language = 'glob')
public function testRemoveZeroNodesFound()
{
$this->getRepository()->move($sourcePath, $targetPath, $language);
$this->finder->find('/path/to/foo')->willReturn([]);
$numberOfNodes = $this->getRepository()->remove('/path/to/foo');
$this->assertEquals(0, $numberOfNodes);
}

/**
* @expectedException \Exception
*/
public function testClearShouldThrow()
public function testMoveZeroNodes()
{
$this->getRepository()->clear();
$this->finder->find('/path/to/foo')->willReturn([]);
$numberOfNodes = $this->getRepository()->move('/path/to/foo', '/bar/bar');
$this->assertEquals(0, $numberOfNodes);
}

abstract public function testGetNotExisting();
Expand All @@ -154,11 +108,13 @@ abstract protected function getRepository($path = null);
*/
abstract public function testListChildren($basePath, $requestedPath, $canonicalPath, $absPath);

abstract public function testFind();

abstract public function testGet($basePath, $requestedPath, $canonicalPath, $evaluatedPath);

abstract public function testGetVersion();
abstract public function testFind();

abstract public function testRemove();

abstract public function testMove();

abstract public function testMoveMultiple();
}
Loading

0 comments on commit eeff6f3

Please sign in to comment.