Skip to content

Commit b307d60

Browse files
dbulsmith77
authored andcommitted
fix format handling. fix #154
1 parent c7ab6d7 commit b307d60

File tree

3 files changed

+90
-15
lines changed

3 files changed

+90
-15
lines changed

Doctrine/Phpcr/RouteProvider.php

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,15 @@ public function getRouteCollectionForRequest(Request $request)
5656
return $collection;
5757
}
5858

59+
$format = preg_match('/.+\.([a-z]+)$/i', $url, $matches) ? $matches[1] : false;
60+
5961
try {
6062
$routes = $this->getObjectManager()->findMany($this->className, $candidates);
6163
// filter for valid route objects
6264
foreach ($routes as $key => $route) {
6365
if ($route instanceof SymfonyRoute) {
64-
if (preg_match('/.+\.([a-z]+)$/i', $url, $matches)) {
65-
if ($route->getDefault('_format') === $matches[1]) {
66-
continue;
67-
}
68-
69-
$route->setDefault('_format', $matches[1]);
66+
if ($format && null === $route->getDefault('_format')) {
67+
$route->setDefault('_format', $format);
7068
}
7169
$collection->add($key, $route);
7270
}
@@ -87,13 +85,13 @@ protected function getCandidates($url)
8785
$candidates = array();
8886
if ('/' !== $url) {
8987
if (preg_match('/(.+)\.[a-z]+$/i', $url, $matches)) {
90-
$candidates[] = $this->idPrefix.$url;
88+
$candidates[] = $this->idPrefix . $url;
9189
$url = $matches[1];
9290
}
9391

9492
$part = $url;
9593
while (false !== ($pos = strrpos($part, '/'))) {
96-
$candidates[] = $this->idPrefix.$part;
94+
$candidates[] = $this->idPrefix . $part;
9795
$part = substr($url, 0, $pos);
9896
}
9997
}
@@ -121,9 +119,20 @@ public function getRouteByName($name, $parameters = array())
121119
return $route;
122120
}
123121

122+
/**
123+
* {@inheritDoc}
124+
*/
124125
public function getRoutesByNames($names, $parameters = array())
125126
{
126-
return $this->getObjectManager()->findMany($this->className, $names);
127+
$collection = $this->getObjectManager()->findMany($this->className, $names);
128+
foreach($collection as $key => $document) {
129+
if (!$document instanceof SymfonyRoute) {
130+
// we follow the logic of DocumentManager::findMany and do not throw an exception
131+
unset($collection[$key]);
132+
}
133+
}
134+
135+
return $collection;
127136
}
128137

129138
}

Tests/Functional/BaseTestCase.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,29 @@
22

33
namespace Symfony\Cmf\Bundle\RoutingBundle\Tests\Functional;
44

5+
use Doctrine\ODM\PHPCR\DocumentManager;
6+
use PHPCR\Util\PathHelper;
57
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\Route;
6-
78
use Symfony\Cmf\Component\Testing\Functional\BaseTestCase as ComponentBaseTestCase;
8-
use PHPCR\Util\PathHelper;
99
use Symfony\Cmf\Component\Testing\Document\Content;
1010

1111
class BaseTestCase extends ComponentBaseTestCase
1212
{
13+
/**
14+
* @return DocumentManager
15+
*/
1316
protected function getDm()
1417
{
1518
$dm = $this->db('PHPCR')->getOm();
1619

1720
return $dm;
1821
}
1922

23+
/**
24+
* @param string $path
25+
*
26+
* @return Route
27+
*/
2028
protected function createRoute($path)
2129
{
2230
$parentPath = PathHelper::getParentPath($path);
@@ -30,6 +38,11 @@ protected function createRoute($path)
3038
return $route;
3139
}
3240

41+
/**
42+
* @param string $path
43+
*
44+
* @return Content
45+
*/
3346
protected function createContent($path)
3447
{
3548
$content = new Content;

Tests/Functional/Doctrine/Phpcr/RouteProviderTest.php

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ public function setUp()
2525
$this->repository = $this->getContainer()->get('cmf_routing.route_provider');
2626
}
2727

28-
public function testGetRouteCollectionForRequest()
28+
private function buildRoutes()
2929
{
30-
$route = new Route;
3130
$root = $this->getDm()->find(null, self::ROUTE_ROOT);
3231

32+
$route = new Route;
3333
$route->setPosition($root, 'testroute');
34+
$route->setDefault('_format', 'html');
3435
$this->getDm()->persist($route);
3536

3637
// smuggle a non-route thing into the repository
@@ -41,24 +42,76 @@ public function testGetRouteCollectionForRequest()
4142

4243
$childroute = new Route;
4344
$childroute->setPosition($noroute, 'child');
45+
$childroute->setDefault('_format', 'json');
4446
$this->getDm()->persist($childroute);
4547

4648
$this->getDm()->flush();
47-
4849
$this->getDm()->clear();
50+
}
51+
52+
public function testGetRouteCollectionForRequest()
53+
{
54+
$this->buildRoutes();
4955

5056
$routes = $this->repository->getRouteCollectionForRequest(Request::create('/testroute/noroute/child'));
5157
$this->assertCount(3, $routes);
5258
$this->assertContainsOnlyInstancesOf('Symfony\\Cmf\\Component\\Routing\\RouteObjectInterface', $routes);
59+
60+
$routes = $routes->all();
61+
list($key, $child) = each($routes);
62+
$this->assertEquals(self::ROUTE_ROOT . '/testroute/noroute/child', $key);
63+
$this->assertEquals('json', $child->getDefault('_format'));
64+
list($key, $testroute) = each($routes);
65+
$this->assertEquals(self::ROUTE_ROOT . '/testroute', $key);
66+
$this->assertEquals('html', $testroute->getDefault('_format'));
67+
list($key, $root) = each($routes);
68+
$this->assertEquals(self::ROUTE_ROOT, $key);
69+
$this->assertNull($root->getDefault('_format'));
5370
}
5471

55-
public function testFindNophpcrUrl()
72+
public function testGetRouteCollectionForRequestFormat()
73+
{
74+
$this->buildRoutes();
75+
76+
$routes = $this->repository->getRouteCollectionForRequest(Request::create('/testroute/noroute/child.html'));
77+
$this->assertCount(3, $routes);
78+
$this->assertContainsOnlyInstancesOf('Symfony\\Cmf\\Component\\Routing\\RouteObjectInterface', $routes);
79+
80+
$routes = $routes->all();
81+
list($key, $child) = each($routes);
82+
$this->assertEquals(self::ROUTE_ROOT . '/testroute/noroute/child', $key);
83+
$this->assertEquals('json', $child->getDefault('_format'));
84+
list($key, $testroute) = each($routes);
85+
$this->assertEquals(self::ROUTE_ROOT . '/testroute', $key);
86+
$this->assertEquals('html', $testroute->getDefault('_format'));
87+
list($key, $root) = each($routes);
88+
$this->assertEquals(self::ROUTE_ROOT, $key);
89+
$this->assertEquals('html', $root->getDefault('_format'));
90+
}
91+
92+
public function testGetRouteCollectionForRequestNophpcrUrl()
5693
{
5794
$collection = $this->repository->getRouteCollectionForRequest(Request::create(':///'));
5895
$this->assertInstanceOf('Symfony\\Component\\Routing\\RouteCollection', $collection);
5996
$this->assertCount(0, $collection);
6097
}
6198

99+
public function testGetRoutesByNames()
100+
{
101+
$this->buildRoutes();
102+
103+
$routeNames = array(
104+
self::ROUTE_ROOT . '/testroute/noroute/child',
105+
self::ROUTE_ROOT . '/testroute/noroute',
106+
self::ROUTE_ROOT . '/testroute'
107+
);
108+
109+
$routes = $this->repository->getRoutesByNames($routeNames);
110+
$this->assertCount(2, $routes);
111+
$this->assertContainsOnlyInstancesOf('Symfony\\Cmf\\Component\\Routing\\RouteObjectInterface', $routes);
112+
}
113+
114+
62115
public function testSetPrefix()
63116
{
64117
$this->repository->setPrefix(self::ROUTE_ROOT);

0 commit comments

Comments
 (0)