From 0a861cf70da5b7090d23413d0b294fac1e696d11 Mon Sep 17 00:00:00 2001 From: dantleech Date: Fri, 18 Dec 2015 11:32:42 +0100 Subject: [PATCH] Support for multiple routes --- .travis.yml | 4 +- Adapter/PhpcrOdmAdapter.php | 5 ++- Command/RefreshCommand.php | 2 +- Doctrine/Phpcr/AutoRouteListener.php | 2 +- Resources/config/auto_route.xml | 10 +++-- .../EventListener/AutoRouteListenerTest.php | 30 +++++++++++-- Tests/Resources/app/config/routing_auto.yml | 42 +++++++++++++++---- Tests/Unit/Adapter/PhpcrOdmAdapterTest.php | 36 ++++++++++++++++ composer.json | 6 +-- 9 files changed, 113 insertions(+), 24 deletions(-) diff --git a/.travis.yml b/.travis.yml index 14cdf6b..8f20762 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,6 @@ language: php php: - - 5.3 - - 5.4 - 5.5 - 5.6 - 7.0 @@ -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 diff --git a/Adapter/PhpcrOdmAdapter.php b/Adapter/PhpcrOdmAdapter.php index 19edaa1..f38289e 100644 --- a/Adapter/PhpcrOdmAdapter.php +++ b/Adapter/PhpcrOdmAdapter.php @@ -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) { @@ -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; } diff --git a/Command/RefreshCommand.php b/Command/RefreshCommand.php index bd94cd0..a50e348 100644 --- a/Command/RefreshCommand.php +++ b/Command/RefreshCommand.php @@ -25,7 +25,7 @@ public function configure() $this ->setName('cmf:routing:auto:refresh') ->setDescription('Refresh auto-routeable documents') - ->setHelp(<<setHelp(<<<'HERE' WARNING: Experimental! This command iterates over all Documents that are mapped by the auto diff --git a/Doctrine/Phpcr/AutoRouteListener.php b/Doctrine/Phpcr/AutoRouteListener.php index acfbaa8..404437f 100644 --- a/Doctrine/Phpcr/AutoRouteListener.php +++ b/Doctrine/Phpcr/AutoRouteListener.php @@ -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; } diff --git a/Resources/config/auto_route.xml b/Resources/config/auto_route.xml index d461909..df4790d 100644 --- a/Resources/config/auto_route.xml +++ b/Resources/config/auto_route.xml @@ -9,6 +9,7 @@ Symfony\Cmf\Component\RoutingAuto\AutoRouteManager Symfony\Cmf\Component\RoutingAuto\ServiceRegistry Symfony\Cmf\Component\RoutingAuto\UriGenerator + Symfony\Cmf\Component\RoutingAuto\UriContextCollectionBuilder Symfony\Cmf\Component\RoutingAuto\Mapping\Loader\YmlFileLoader Symfony\Cmf\Component\RoutingAuto\Mapping\Loader\XmlFileLoader @@ -37,13 +38,17 @@ + + + + + - @@ -84,8 +89,7 @@ + factory-method="getMetadataFactory" /> 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(); @@ -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() @@ -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', ), ), ); @@ -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()); @@ -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); diff --git a/Tests/Resources/app/config/routing_auto.yml b/Tests/Resources/app/config/routing_auto.yml index 164c82f..7d2e7e8 100644 --- a/Tests/Resources/app/config/routing_auto.yml +++ b/Tests/Resources/app/config/routing_auto.yml @@ -1,30 +1,52 @@ 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 } ] @@ -32,11 +54,15 @@ Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Document\SeoArticleMultilan # 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 } ] diff --git a/Tests/Unit/Adapter/PhpcrOdmAdapterTest.php b/Tests/Unit/Adapter/PhpcrOdmAdapterTest.php index 88ab52c..b8caaf8 100644 --- a/Tests/Unit/Adapter/PhpcrOdmAdapterTest.php +++ b/Tests/Unit/Adapter/PhpcrOdmAdapterTest.php @@ -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); @@ -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"\./ diff --git a/composer.json b/composer.json index addf104..2a01274 100644 --- a/composer.json +++ b/composer.json @@ -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.*", @@ -41,7 +41,7 @@ }, "extra": { "branch-alias": { - "dev-master": "1.1-dev" + "dev-master": "2.0-dev" } } }