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

Commit

Permalink
Support for multiple routes
Browse files Browse the repository at this point in the history
  • Loading branch information
dantleech committed Feb 20, 2016
1 parent c1bb5a4 commit 0a861cf
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 24 deletions.
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
language: php

php:
- 5.3
- 5.4
- 5.5
- 5.6
- 7.0
Expand All @@ -19,7 +17,7 @@ env:

matrix:
include:
- php: 5.3
- php: 5.5
env: SYMFONY_VERSION=2.3.* COMPOSER_FLAGS="--prefer-lowest"
- php: 5.6
env: SYMFONY_VERSION=2.3.* SYMFONY_DEPRECATIONS_HELPER=weak
Expand Down
5 changes: 4 additions & 1 deletion Adapter/PhpcrOdmAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ public function createAutoRoute(UriContext $uriContext, $contentDocument, $autoR
$basePath
));
}

$segments = preg_split('#/#', $uriContext->getUri(), null, PREG_SPLIT_NO_EMPTY);
$headName = array_pop($segments);
foreach ($segments as $segment) {
Expand Down Expand Up @@ -165,6 +164,10 @@ public function createAutoRoute(UriContext $uriContext, $contentDocument, $autoR
$headRoute->setAutoRouteTag($autoRouteTag);
$headRoute->setType(AutoRouteInterface::TYPE_PRIMARY);

foreach ($uriContext->getDefaults() as $key => $value) {
$headRoute->setDefault($key, $value);
}

return $headRoute;
}

Expand Down
2 changes: 1 addition & 1 deletion Command/RefreshCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function configure()
$this
->setName('cmf:routing:auto:refresh')
->setDescription('Refresh auto-routeable documents')
->setHelp(<<<HERE
->setHelp(<<<'HERE'
WARNING: Experimental!
This command iterates over all Documents that are mapped by the auto
Expand Down
2 changes: 1 addition & 1 deletion Doctrine/Phpcr/AutoRouteListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public function endFlush(ManagerEventArgs $args)
private function isAutoRouteable($document)
{
try {
return (boolean) $this->getMetadataFactory()->getMetadataForClass(get_class($document));
return (bool) $this->getMetadataFactory()->getMetadataForClass(get_class($document));
} catch (ClassNotMappedException $e) {
return false;
}
Expand Down
10 changes: 7 additions & 3 deletions Resources/config/auto_route.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<parameter key="cmf_routing_auto.auto_route_manager.class">Symfony\Cmf\Component\RoutingAuto\AutoRouteManager</parameter>
<parameter key="cmf_routing_auto.service_registry.class">Symfony\Cmf\Component\RoutingAuto\ServiceRegistry</parameter>
<parameter key="cmf_routing_auto.uri_generator.class">Symfony\Cmf\Component\RoutingAuto\UriGenerator</parameter>
<parameter key="cmf_routing_auto.uri_context_collection_builder.class">Symfony\Cmf\Component\RoutingAuto\UriContextCollectionBuilder</parameter>

<parameter key="cmf_routing_auto.metadata.loader.yaml.class">Symfony\Cmf\Component\RoutingAuto\Mapping\Loader\YmlFileLoader</parameter>
<parameter key="cmf_routing_auto.metadata.loader.xml.class">Symfony\Cmf\Component\RoutingAuto\Mapping\Loader\XmlFileLoader</parameter>
Expand Down Expand Up @@ -37,13 +38,17 @@
<argument type="service" id="cmf_routing_auto.adapter" />
<argument type="service" id="cmf_routing_auto.uri_generator" />
<argument type="service" id="cmf_routing_auto.defunct_route_handler.delegating" />
<argument type="service" id="cmf_routing_auto.uri_context_collection_builder" />
</service>

<!-- URL Generator -->
<service id="cmf_routing_auto.uri_generator" class="%cmf_routing_auto.uri_generator.class%">
<argument type="service" id="cmf_routing_auto.service_registry" />
</service>

<service id="cmf_routing_auto.uri_context_collection_builder" class="%cmf_routing_auto.uri_context_collection_builder.class%">
<argument type="service" id="cmf_routing_auto.metadata.factory" />
<argument type="service" id="cmf_routing_auto.adapter" />
<argument type="service" id="cmf_routing_auto.service_registry" />
</service>

<!-- Service Registry -->
Expand Down Expand Up @@ -84,8 +89,7 @@
<service id="cmf_routing_auto.metadata.factory"
class="%cmf_routing_auto.metadata.factory.class%"
factory-service="cmf_routing_auto.metadata.factory.builder"
factory-method="getMetadataFactory"
/>
factory-method="getMetadataFactory" />

<!-- Controller -->
<service
Expand Down
30 changes: 26 additions & 4 deletions Tests/Functional/EventListener/AutoRouteListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ protected function createBlog($withPosts = false)
$this->getDm()->clear();
}

/**
* It should persist the blog document and create an auto route.
* It should set the defaults on the route.
*/
public function testPersistBlog()
{
$this->createBlog();
Expand All @@ -62,6 +66,11 @@ public function testPersistBlog()
$this->assertInstanceOf('Symfony\Cmf\Bundle\RoutingAutoBundle\Model\AutoRoute', $routes[0]);
$this->assertEquals('unit-testing-blog', $routes[0]->getName());
$this->assertEquals(PhpcrOdmAdapter::TAG_NO_MULTILANG, $routes[0]->getAutoRouteTag());
$this->assertEquals(array(
'_auto_route_tag' => 'no-multilang',
'type' => 'cmf_routing_auto.primary',
'_controller' => 'BlogController',
), $routes[0]->getDefaults());
}

public function provideTestUpdateBlog()
Expand Down Expand Up @@ -206,6 +215,16 @@ public function provideMultilangArticle()
'test/auto-route/articles/fr/bonjour-le-monde',
'test/auto-route/articles/de/gutentag',
'test/auto-route/articles/es/hola-todo-el-mundo',

'test/auto-route/articles/en/hello-everybody-edit',
'test/auto-route/articles/fr/bonjour-le-monde-edit',
'test/auto-route/articles/de/gutentag-edit',
'test/auto-route/articles/es/hola-todo-el-mundo-edit',

'test/auto-route/articles/en/hello-everybody-review',
'test/auto-route/articles/fr/bonjour-le-monde-review',
'test/auto-route/articles/de/gutentag-review',
'test/auto-route/articles/es/hola-todo-el-mundo-review',
),
),
);
Expand All @@ -231,11 +250,12 @@ public function testMultilangArticle($data, $expectedPaths)
$locales = array_keys($data);

foreach ($expectedPaths as $i => $expectedPath) {
$expectedLocale = $locales[$i];
$localeIndex = $i % count($locales);
$expectedLocale = $locales[$localeIndex];

$route = $this->getDm()->find(null, $expectedPath);

$this->assertNotNull($route);
$this->assertNotNull($route, 'Route: '.$expectedPath);
$this->assertInstanceOf('Symfony\Cmf\Bundle\RoutingAutoBundle\Model\AutoRoute', $route);
$this->assertEquals($expectedLocale, $route->getAutoRouteTag());

Expand Down Expand Up @@ -317,11 +337,13 @@ public function testUpdateMultilangArticle($data, $expectedPaths)

$article_de = $this->getDm()->findTranslation('Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Document\Article', '/test/article-1', 'de');
$routes = $this->getDm()->getReferrers($article_de);
$this->assertCount(count($data), $routes);

// Multiply the expected paths by 3 because Article has 3 routes defined.
$this->assertCount(count($data) * 3, $routes);

$this->getDm()->clear();

foreach ($expectedPaths as $i => $expectedPath) {
foreach ($expectedPaths as $expectedPath) {
$route = $this->getDm()->find(null, $expectedPath);

$this->assertNotNull($route);
Expand Down
42 changes: 34 additions & 8 deletions Tests/Resources/app/config/routing_auto.yml
Original file line number Diff line number Diff line change
@@ -1,42 +1,68 @@
Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Document\Blog:
uri_schema: /blog/{blog_title}
definitions:
frontend:
uri_schema: /blog/{blog_title}
defaults:
_controller: BlogController
token_providers:
blog_title: [content_method, { method: getTitle } ]

Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Document\Post:
uri_schema: /blog/{blog_title}/{post_date}/{post_title}
conflict_resolver: [auto_increment, { }]
definitions:
view:
uri_schema: /blog/{blog_title}/{post_date}/{post_title}
token_providers:
blog_title: [content_method, { method: getBlogTitle } ]
post_date: [content_datetime, { method: getDate, date_format: Y/m/d } ]
post_title: [content_method, { method: getTitle } ]
conflict_resolver: [auto_increment, { }]

Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Document\Article:
uri_schema: /articles/{article_locale}/{article_title}
definitions:
view:
uri_schema: /articles/{article_locale}/{article_title}
defaults:
_type: article_view
edit:
uri_schema: /articles/{article_locale}/{article_title}-edit
defaults:
_type: article_edit
review:
uri_schema: /articles/{article_locale}/{article_title}-review
defaults:
_type: article_review
token_providers:
article_title: [content_method, { method: getTitle } ]
article_locale: [content_locale, {} ]

Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Document\SeoArticle:
uri_schema: /seo-articles/{article_title}
definitions:
view:
uri_schema: /seo-articles/{article_title}
defunct_route_handler: [leave_redirect, {}]
token_providers:
article_title: [content_method, { method: getTitle } ]

Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Document\SeoArticleMultilang:
uri_schema: /seo-articles/{article_locale}/{article_title}
definitions:
view:
uri_schema: /seo-articles/{article_locale}/{article_title}
defunct_route_handler: [leave_redirect, {}]
token_providers:
article_title: [content_method, { method: getTitle } ]
article_locale: [content_locale, {} ]

# AbstractContent for ensuring that parent class mapping works
Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Document\AbstractContent:
uri_schema: /articles/{article_title}
definitions:
view:
uri_schema: /articles/{article_title}
token_providers:
article_title: [content_method, { method: getTitle } ]

Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Document\Page:
uri_schema: /{title}
definitions:
view:
uri_schema: /{title}
token_providers:
title: [content_method, { method: getTitle } ]
36 changes: 36 additions & 0 deletions Tests/Unit/Adapter/PhpcrOdmAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ public function testCreateAutoRoute($path, $expectedParentPath, $expectedName, $
}

$this->uriContext->getUri()->willReturn($path);
$this->uriContext->getDefaults()->willReturn(array());
$res = $this->adapter->createAutoRoute($this->uriContext->reveal(), $this->contentDocument, 'fr');
$this->assertNotNull($res);
$this->assertInstanceOf('Symfony\Cmf\Bundle\RoutingAutoBundle\Model\AutoRoute', $res);
Expand All @@ -126,6 +127,41 @@ public function testCreateAutoRoute($path, $expectedParentPath, $expectedName, $
$this->assertSame($this->contentDocument, $res->getContent());
}

/**
* It should set the route defaults on the head document.
*/
public function testCreateAutoRouteSetDefaults()
{
$this->dm->getPhpcrSession()->willReturn($this->phpcrSession);
$this->phpcrSession->getRootNode()->willReturn($this->phpcrRootNode);
$this->dm->find(null, $this->baseRoutePath)->willReturn($this->baseNode);

$this->dm->find(null, '/test/uri')
->willReturn($this->parentRoute);
$this->dm->find(null, '/test/uri/to')
->willReturn(null);

$this->uriContext->getUri()->willReturn('/uri/to');
$this->uriContext->getDefaults()->willReturn(array(
'one' => 'k1',
'two' => 'k2',
));

$res = $this->adapter->createAutoRoute($this->uriContext->reveal(), $this->contentDocument, 'fr');
$this->assertNotNull($res);
$this->assertInstanceOf('Symfony\Cmf\Bundle\RoutingAutoBundle\Model\AutoRoute', $res);
$this->assertEquals('to', $res->getName());
$this->assertEquals(array(
'_auto_route_tag' => 'fr',
'type' => 'cmf_routing_auto.primary',
'one' => 'k1',
'two' => 'k2',
), $res->getDefaults());

$this->assertSame($this->parentRoute, $res->getParent());
$this->assertSame($this->contentDocument, $res->getContent());
}

/**
* @expectedException \RuntimeException
* @expectedExceptionMessageRegExp /Failed to migrate existing.*? at "\/test\/generic" .*? It is an instance of "stdClass"\./
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": "^5.3.9|^7.0",
"symfony-cmf/routing-auto": "^1.1",
"php": "^5.5.0|^7.0",
"symfony-cmf/routing-auto": "^2.0@dev",
"symfony-cmf/routing-bundle": "^1.2.0",
"symfony-cmf/core-bundle": "^1.2",
"aferrandini/urlizer": "1.0.*",
Expand Down Expand Up @@ -41,7 +41,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.1-dev"
"dev-master": "2.0-dev"
}
}
}

0 comments on commit 0a861cf

Please sign in to comment.