-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented by-type and by-template route-generator (#14)
* immplemented by-type and by-template route-generator * fixed testcases to fit new sulu * added tests for custom-route-generators * changed config from service-id to alias * changed alias of route_generators * changed alias of route_generators
- Loading branch information
1 parent
6c9938f
commit cfebfb9
Showing
13 changed files
with
364 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Sulu. | ||
* | ||
* (c) MASSIVE ART WebServices GmbH | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Sulu\Bundle\ArticleBundle\Exception; | ||
|
||
/** | ||
* Will be thrown when the requested schema is not configured. | ||
*/ | ||
class RouteSchemaNotFoundException extends \Exception | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $requested; | ||
|
||
/** | ||
* @var string[] | ||
*/ | ||
private $available; | ||
|
||
/** | ||
* @param string $requested | ||
* @param string[] $available | ||
*/ | ||
public function __construct($requested, array $available) | ||
{ | ||
parent::__construct( | ||
sprintf('Route-schema for "%s" not configured. Available: ["%s"]', $requested, implode('","', $available)) | ||
); | ||
|
||
$this->requested = $requested; | ||
$this->available = $available; | ||
} | ||
|
||
/** | ||
* Returns requested. | ||
* | ||
* @return string | ||
*/ | ||
public function getRequested() | ||
{ | ||
return $this->requested; | ||
} | ||
|
||
/** | ||
* Returns requested. | ||
* | ||
* @return string[] | ||
*/ | ||
public function getAvailable() | ||
{ | ||
return $this->available; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Sulu. | ||
* | ||
* (c) MASSIVE ART WebServices GmbH | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Sulu\Bundle\ArticleBundle\Routing; | ||
|
||
use Sulu\Bundle\ArticleBundle\Exception\RouteSchemaNotFoundException; | ||
use Sulu\Bundle\ArticleBundle\Metadata\ArticleTypeTrait; | ||
use Sulu\Bundle\RouteBundle\Generator\RouteGeneratorInterface; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
/** | ||
* Generate route for articles by type. | ||
*/ | ||
class ArticleRouteGeneratorByTemplate implements RouteGeneratorInterface | ||
{ | ||
use ArticleTypeTrait; | ||
|
||
/** | ||
* @var RouteGeneratorInterface | ||
*/ | ||
private $routeGenerator; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function __construct(RouteGeneratorInterface $routeGenerator) | ||
{ | ||
$this->routeGenerator = $routeGenerator; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function generate($entity, array $options) | ||
{ | ||
$template = $entity->getStructureType(); | ||
|
||
if (!array_key_exists($template, $options)) { | ||
throw new RouteSchemaNotFoundException($template, array_keys($options)); | ||
} | ||
|
||
return $this->routeGenerator->generate($entity, ['route_schema' => $options[$template]]); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getOptionsResolver(array $options) | ||
{ | ||
return (new OptionsResolver())->setDefined(array_keys($options)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Sulu. | ||
* | ||
* (c) MASSIVE ART WebServices GmbH | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Sulu\Bundle\ArticleBundle\Routing; | ||
|
||
use Sulu\Bundle\ArticleBundle\Exception\RouteSchemaNotFoundException; | ||
use Sulu\Bundle\ArticleBundle\Metadata\ArticleTypeTrait; | ||
use Sulu\Bundle\RouteBundle\Generator\RouteGeneratorInterface; | ||
use Sulu\Component\Content\Metadata\Factory\StructureMetadataFactoryInterface; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
/** | ||
* Generate route for articles by type. | ||
*/ | ||
class ArticleRouteGeneratorByType implements RouteGeneratorInterface | ||
{ | ||
use ArticleTypeTrait; | ||
|
||
/** | ||
* @var RouteGeneratorInterface | ||
*/ | ||
private $routeGenerator; | ||
|
||
/** | ||
* @var StructureMetadataFactoryInterface | ||
*/ | ||
private $structureMetadataFactory; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function __construct( | ||
RouteGeneratorInterface $routeGenerator, | ||
StructureMetadataFactoryInterface $structureMetadataFactory | ||
) { | ||
$this->routeGenerator = $routeGenerator; | ||
$this->structureMetadataFactory = $structureMetadataFactory; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function generate($entity, array $options) | ||
{ | ||
$type = $this->getType( | ||
$this->structureMetadataFactory->getStructureMetadata('article', $entity->getStructureType()) | ||
); | ||
|
||
if (!array_key_exists($type, $options)) { | ||
throw new RouteSchemaNotFoundException($type, array_keys($options)); | ||
} | ||
|
||
return $this->routeGenerator->generate($entity, ['route_schema' => $options[$type]]); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getOptionsResolver(array $options) | ||
{ | ||
return (new OptionsResolver())->setDefined(array_keys($options)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
Tests/Unit/Routing/ArticleRouteGeneratorByTemplateTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Sulu. | ||
* | ||
* (c) MASSIVE ART WebServices GmbH | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Sulu\Bundle\ArticleBundle\Tests\Unit\Routing; | ||
|
||
use Sulu\Bundle\ArticleBundle\Exception\RouteSchemaNotFoundException; | ||
use Sulu\Bundle\ArticleBundle\Routing\ArticleRouteGeneratorByTemplate; | ||
use Sulu\Bundle\RouteBundle\Generator\RouteGeneratorInterface; | ||
use Sulu\Component\Content\Document\Behavior\StructureBehavior; | ||
|
||
class ArticleRouteGeneratorByTemplateTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var RouteGeneratorInterface | ||
*/ | ||
private $generatorBySchema; | ||
|
||
/** | ||
* @var RouteGeneratorInterface | ||
*/ | ||
private $generatorByTemplate; | ||
|
||
private $config = ['test1' => '/test1/{entity.getTitle()', 'test2' => '/test2/{entity.getTitle()']; | ||
|
||
public function setUp() | ||
{ | ||
$this->generatorBySchema = $this->prophesize(RouteGeneratorInterface::class); | ||
$this->generatorByTemplate = new ArticleRouteGeneratorByTemplate($this->generatorBySchema->reveal()); | ||
} | ||
|
||
public function testGenerate() | ||
{ | ||
$entity = $this->prophesize(StructureBehavior::class); | ||
$entity->getStructureType()->willReturn('test1'); | ||
|
||
$this->generatorByTemplate->generate($entity->reveal(), $this->config); | ||
|
||
$this->generatorBySchema->generate($entity->reveal(), ['route_schema' => '/test1/{entity.getTitle()']) | ||
->shouldBeCalled(); | ||
} | ||
|
||
public function testGenerateNotConfigured() | ||
{ | ||
$this->setExpectedException(RouteSchemaNotFoundException::class); | ||
|
||
$entity = $this->prophesize(StructureBehavior::class); | ||
$entity->getStructureType()->willReturn('test3'); | ||
|
||
$this->generatorByTemplate->generate($entity->reveal(), $this->config); | ||
} | ||
} |
Oops, something went wrong.