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

Commit

Permalink
Allow PuliResources
Browse files Browse the repository at this point in the history
  • Loading branch information
dantleech committed May 25, 2016
1 parent 624f650 commit b11a77c
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 65 deletions.
45 changes: 35 additions & 10 deletions Description/Description.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,34 @@

namespace Symfony\Cmf\Component\Resource\Description;

use Puli\Repository\Api\Resource\PuliResource;

/**
* Descriptive metadata for resources.
*/
class Description
{
/**
* @var array
*/
private $descriptors = [];
private $payloadType;

public function __construct($payloadType)
/**
* @var PuliResource
*/
private $resource;

/**
* @param PuliResource $resource
*/
public function __construct(PuliResource $resource)
{
$this->payloadType = $payloadType;
$this->resource = $resource;
}

/**
* Return the descriptors value for the given key.
*
* The key should be one of the constants defined in this class.
*
* @param string $key
*
* @return mixed
Expand All @@ -34,9 +47,10 @@ public function get($key)
{
if (!isset($this->descriptors[$key])) {
throw new \InvalidArgumentException(sprintf(
'Descriptor "%s" not supported for payload type "%s". Supported descriptors: "%s"',
'Descriptor "%s" not supported for resource "%s" of class "%s". Supported descriptors: "%s"',
$key,
$this->payloadType,
$this->resource->getPath(),
get_class($this->resource),
implode('", "', array_keys($this->descriptors))
));
}
Expand All @@ -47,11 +61,12 @@ public function get($key)
/**
* Set value for descriptors key.
*
* Note that:
*
* - It is possible to overwrite existing keys.
*
* - To help ensure interoperability, where possible, the key should be the
* value of one of the appropriate constants defined in the MetadescriptorsKey
* class.
* - Where possible the key should be the value of one of the constants
* defined in the Descriptor class.
*
* @param string $key
* @param mixed $value
Expand All @@ -60,4 +75,14 @@ public function set($key, $value)
{
$this->descriptors[$key] = $value;
}

/**
* Return the resource for which this is the description.
*
* @return PuliResource
*/
public function getResource()
{
return $this->resource;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,18 @@

namespace Symfony\Cmf\Component\Resource\Description;

use Symfony\Cmf\Component\Resource\Repository\Resource\CmfResource;
use Puli\Repository\Api\Resource\PuliResource;

interface DescriptionEnricherInterface
interface DescriptionEnhancerInterface
{
/**
* Enrich the payload description.
*
* @param Description $description
* @param object $payload
*
* @return Description
*/
public function enrich(Description $description, $payload);
public function enhance(Description $description);

/**
* Return true if the provider supports the given type.
Expand All @@ -32,5 +31,5 @@ public function enrich(Description $description, $payload);
*
* @return bool
*/
public function supports(CmfResource $resource);
public function supports(PuliResource $resource);
}
28 changes: 15 additions & 13 deletions Description/DescriptionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,39 @@

namespace Symfony\Cmf\Component\Resource\Description;

use Symfony\Cmf\Component\Resource\Repository\Resource\CmfResource;
use Puli\Repository\Api\Resource\PuliResource;
use Symfony\Cmf\Component\Resource\Description\DescriptionEnhancerInterface;

class DescriptionFactory
{
private $enrichers = [];
/**
* @var DescriptionEnhancerInterface[]
*/
private $enhancers = [];

/**
* @param array $enrichers
* @param array $enhancers
*/
public function __construct(array $enrichers)
public function __construct(array $enhancers)
{
$this->enrichers = $enrichers;
$this->enhancers = $enhancers;
}

/**
* Return a description of the given (CMF) Resource.
*
* @param CmfResource $resource
* @param PuliResource $resource
*/
public function getPayloadDescriptionFor(CmfResource $resource)
public function getPayloadDescriptionFor(PuliResource $resource)
{
$type = $resource->getPayloadType();
$payload = $resource->getPayload();
$description = new Description($type);
$description = new Description($resource);

foreach ($this->enrichers as $enricher) {
if (false === $enricher->supports($resource)) {
foreach ($this->enhancers as $enhancer) {
if (false === $enhancer->supports($resource)) {
continue;
}

$enricher->enrich($description, $payload);
$enhancer->enhance($description);
}

return $description;
Expand Down
8 changes: 4 additions & 4 deletions Description/Descriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
namespace Symfony\Cmf\Component\Resource\Description;

/**
* Class containing standard description keys which should be used when appropriate
* by enrichers in order to provide a level of interoperability.
* Class containing standard descriptors which should be used when appropriate
* by enhancers in order to provide a level of interoperability.
*/
final class Descriptor
{
Expand All @@ -33,7 +33,7 @@ final class Descriptor
const PAYLOAD_TITLE = 'title';

/**
* Keys to be used to store link values for HTML.
* Descriptors for HTML links.
*/
const LINK_EDIT_HTML = 'link.edit.html';
const LINK_CREATE_HTML = 'link.create.html';
Expand All @@ -42,7 +42,7 @@ final class Descriptor
const LINK_SHOW_HTML = 'link.show.html';

/**
* Keys to be used to store link values for REST.
* Descriptors for REST links.
*/
const LINK_EDIT_REST = 'link.edit.rest';
const LINK_CREATE_REST = 'link.create.rest';
Expand Down
57 changes: 26 additions & 31 deletions Tests/Unit/Description/DescriptionFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,51 +12,46 @@
namespace Symfony\Cmf\Component\Resource\Tests\Unit\Repository;

use Symfony\Cmf\Component\Resource\Description\Description;
use Symfony\Cmf\Component\Resource\Description\DescriptionEnricherInterface;
use Symfony\Cmf\Component\Resource\Description\DescriptionEnhancerInterface;
use Prophecy\Argument;
use Symfony\Cmf\Component\Resource\Description\DescriptionFactory;
use Symfony\Cmf\Component\Resource\Repository\Resource\CmfResource;
use Puli\Repository\Api\Resource\PuliResource;

class DescriptionFactoryTest extends \PHPUnit_Framework_TestCase
{
private $factory;
private $enricher1;
private $enricher2;
private $payload;
private $enhanceer1;
private $enhanceer2;
private $resource;

public function setUp()
{
$this->enricher1 = $this->prophesize(DescriptionEnricherInterface::class);
$this->enricher2 = $this->prophesize(DescriptionEnricherInterface::class);
$this->resource = $this->prophesize(CmfResource::class);

$this->payload = new \stdClass();
$this->resource->getPayload()->willReturn($this->payload);
$this->resource->getPayloadType()->willReturn('payload-type');
$this->enhanceer1 = $this->prophesize(DescriptionEnhancerInterface::class);
$this->enhanceer2 = $this->prophesize(DescriptionEnhancerInterface::class);
$this->resource = $this->prophesize(PuliResource::class);
}

/**
* It should return an enriched description.
* It should return an enhanceed description.
*/
public function testGetPayloadDescription()
public function testGetResourceDescription()
{
$this->enricher1->enrich(Argument::type(Description::class), $this->payload)
$this->enhanceer1->enhance(Argument::type(Description::class))
->will(function ($args) {
$description = $args[0];
$description->set('foobar', 'barfoo');
});
$this->enricher1->supports($this->resource->reveal())->willReturn(true);
$this->enricher2->enrich(Argument::type(Description::class), $this->payload)
$this->enhanceer1->supports($this->resource->reveal())->willReturn(true);
$this->enhanceer2->enhance(Argument::type(Description::class))
->will(function ($args) {
$description = $args[0];
$description->set('barfoo', 'foobar');
});
$this->enricher2->supports($this->resource->reveal())->willReturn(true);
$this->enhanceer2->supports($this->resource->reveal())->willReturn(true);

$description = $this->createFactory([
$this->enricher1->reveal(),
$this->enricher2->reveal(),
$this->enhanceer1->reveal(),
$this->enhanceer2->reveal(),
])->getPayloadDescriptionFor($this->resource->reveal());

$this->assertInstanceOf(Description::class, $description);
Expand All @@ -65,33 +60,33 @@ public function testGetPayloadDescription()
}

/**
* It should ignore providers that do not support the payload type.
* It should ignore providers that do not support the resource.
*/
public function testIgnoreNonSupporters()
{
$this->enricher1->enrich(Argument::cetera())->shouldNotBeCalled();
$this->enricher1->supports($this->resource->reveal())->willReturn(false);
$this->enhanceer1->enhance(Argument::cetera())->shouldNotBeCalled();
$this->enhanceer1->supports($this->resource->reveal())->willReturn(false);

$this->enricher2->enrich(Argument::cetera())->shouldBeCalled();
$this->enricher2->supports($this->resource->reveal())->willReturn(true);
$this->enhanceer2->enhance(Argument::cetera())->shouldBeCalled();
$this->enhanceer2->supports($this->resource->reveal())->willReturn(true);

$this->createFactory([
$this->enricher1->reveal(),
$this->enricher2->reveal(),
$this->enhanceer1->reveal(),
$this->enhanceer2->reveal(),
])->getPayloadDescriptionFor($this->resource->reveal());
}

/**
* It should work when no enrichers are given.
* It should work when no enhancers are given.
*/
public function testNoEnrichers()
public function testNoEnhancers()
{
$description = $this->createFactory([])->getPayloadDescriptionFor($this->resource->reveal());
$this->assertInstanceOf(Description::class, $description);
}

private function createFactory(array $enrichers)
private function createFactory(array $enhancers)
{
return new DescriptionFactory($enrichers);
return new DescriptionFactory($enhancers);
}
}
20 changes: 18 additions & 2 deletions Tests/Unit/Description/DescriptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Cmf\Component\Resource\Description\Description;
use Symfony\Cmf\Component\Resource\Description\Descriptor;
use Puli\Repository\Api\Resource\PuliResource;

class DescriptionTest extends \PHPUnit_Framework_TestCase
{
Expand All @@ -21,9 +22,15 @@ class DescriptionTest extends \PHPUnit_Framework_TestCase
*/
private $description;

/**
* @var PuliResource
*/
private $resource;

public function setUp()
{
$this->description = new Description('some-type');
$this->resource = $this->prophesize(PuliResource::class);
$this->description = new Description($this->resource->reveal());
}

/**
Expand All @@ -42,12 +49,21 @@ public function testGetSet()
* It should throw an exception when requesting an unsupported descriptor.
*
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Descriptor "not there" not supported for payload type "some-type". Supported descriptors: "foo", "bar"
* @expectedExceptionMessage Supported descriptors: "foo", "bar"
*/
public function testGetUnsupported()
{
$this->description->set('foo', 'bar');
$this->description->set('bar', 'foo');
$this->description->get('not there');
}

/**
* It should return the resource that it describes.
*/
public function testGetResource()
{
$resource = $this->description->getResource();
$this->assertSame($this->resource->reveal(), $resource);
}
}

0 comments on commit b11a77c

Please sign in to comment.