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

Commit 6f34059

Browse files
committed
Merge pull request #57 from symfony-cmf/issue-56
Allow parent class mapping
2 parents d248395 + 221e6b3 commit 6f34059

File tree

8 files changed

+127
-14
lines changed

8 files changed

+127
-14
lines changed

AutoRoute/Factory.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,7 @@ public function getContentNameBuilderUnit($classFqn)
121121
*/
122122
public function hasMapping($classFqn)
123123
{
124-
// @todo: Do we need to support inheritance?
125-
return isset($this->mapping[$classFqn]);
124+
return null !== $this->getMapping($classFqn, false);
126125
}
127126

128127
/**
@@ -164,15 +163,21 @@ protected function generateBuilderUnit($config)
164163
return $builderUnit;
165164
}
166165

167-
protected function getMapping($classFqn)
166+
protected function getMapping($classFqn, $throw = true)
168167
{
169-
if (!isset($this->mapping[$classFqn])) {
170-
throw new Exception\ClassNotMappedException($classFqn);
171-
}
168+
$classFqns = class_parents($classFqn);
169+
$classFqns[] = $classFqn;
170+
$classFqns = array_reverse($classFqns);
172171

173-
$mapping = $this->mapping[$classFqn];
172+
foreach ($classFqns as $classFqn) {
173+
if (isset($this->mapping[$classFqn])) {
174+
return $this->mapping[$classFqn];
175+
}
176+
}
174177

175-
return $mapping;
178+
if ($throw) {
179+
throw new Exception\ClassNotMappedException($classFqn);
180+
}
176181
}
177182

178183
private function validateMapping($classFqn, $mapping)

AutoRoute/PathProvider/LocaleProvider.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ public function providePath(RouteStack $routeStack)
3232
$locale = $context->getLocale();
3333

3434
if (!$locale) {
35-
throw new \RuntimeException(
36-
'LocaleProvider requires that a locale is set on the BuilderContext'
37-
);
35+
throw new \RuntimeException(sprintf(
36+
'LocaleProvider requires that a locale is set on the BuilderContext for "%s"',
37+
get_class($context->getContent())
38+
));
3839
}
3940

4041
$routeStack->addPathElements(array($locale));

Tests/Functional/EventListener/AutoRouteListenerTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Document\Post;
1717
use Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Document\Article;
1818
use Symfony\Cmf\Bundle\RoutingAutoBundle\Model\AutoRoute;
19+
use Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Document\ConcreteContent;
1920

2021
class AutoRouteListenerTest extends BaseTestCase
2122
{
@@ -221,4 +222,22 @@ public function testMultilangArticle($data, $expectedPaths)
221222
$this->assertEquals('Hello everybody!', $content->title);
222223
}
223224
}
225+
226+
/**
227+
* Ensure that we can map parent classes: #56
228+
*/
229+
public function testParentClassMapping()
230+
{
231+
$content = new ConcreteContent();
232+
$content->path = '/test/content';
233+
$content->title = 'Hello';
234+
$this->getDm()->persist($content);
235+
$this->getDm()->flush();
236+
237+
$this->getDm()->refresh($content);
238+
239+
$routes = $content->routes;
240+
241+
$this->assertCount(1, $routes);
242+
}
224243
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony CMF package.
5+
*
6+
* (c) 2011-2013 Symfony CMF
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
13+
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Document;
14+
15+
use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCR;
16+
use Symfony\Cmf\Bundle\RoutingBundle\Mapping\Annotations as CMFRouting;
17+
18+
/**
19+
* @PHPCR\Document(referenceable=true)
20+
*/
21+
class AbstractContent
22+
{
23+
/**
24+
* @PHPCR\Id()
25+
*/
26+
public $path;
27+
28+
/**
29+
* @PHPCR\Referrers(
30+
* referringDocument="Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\Route",
31+
* referencedBy="content"
32+
* )
33+
*/
34+
public $routes;
35+
36+
/**
37+
* @PHPCR\String()
38+
*/
39+
public $title;
40+
41+
public function getTitle()
42+
{
43+
return $this->title;
44+
}
45+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony CMF package.
5+
*
6+
* (c) 2011-2013 Symfony CMF
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
13+
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Document;
14+
15+
use Doctrine\ODM\PHPCR\Mapping\Annotations as PHPCR;
16+
use Symfony\Cmf\Bundle\RoutingBundle\Mapping\Annotations as CMFRouting;
17+
18+
/**
19+
* @PHPCR\Document()
20+
*/
21+
class ConcreteContent extends AbstractContent
22+
{
23+
}

Tests/Resources/app/config/routingautoroute.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ cmf_routing_auto:
6464
exists_action: [ auto_increment, { pattern: -%d } ]
6565
not_exists_action: [ create ]
6666

67+
# Article document for multilang tests
6768
Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Document\Article:
6869

6970
content_path:
@@ -80,3 +81,17 @@ cmf_routing_auto:
8081
provider: [ content_method, { method: getTitle } ]
8182
exists_action: [ auto_increment, { pattern: -%d } ]
8283
not_exists_action: create
84+
85+
# AbstractContent for ensuring that parent class mapping works
86+
Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Document\AbstractContent:
87+
88+
content_path:
89+
path_units:
90+
base:
91+
provider: [ specified, { path: test/auto-route/articles } ]
92+
exists_action: use
93+
not_exists_action: create
94+
content_name:
95+
provider: [ content_method, { method: getTitle } ]
96+
exists_action: [ auto_increment, { pattern: -%d } ]
97+
not_exists_action: create

Tests/Unit/AutoRoute/FactoryTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function setUp()
6060
*/
6161
public function testClassNotMappedException()
6262
{
63-
$this->bucf->getRouteStackBuilderUnitChain('FooBar');
63+
$this->bucf->getRouteStackBuilderUnitChain('stdClass');
6464
}
6565

6666
public function provideTestGetChain()
@@ -130,7 +130,7 @@ public function testGetChain($config, $assertOptions)
130130
->with($assertOptions);
131131
}
132132

133-
$this->bucf->registerMapping('FooBar/Class', $config);
134-
$this->bucf->getRouteStackBuilderUnitChain('FooBar/Class');
133+
$this->bucf->registerMapping('stdClass', $config);
134+
$this->bucf->getRouteStackBuilderUnitChain('stdClass');
135135
}
136136
}

Tests/Unit/AutoRoute/PathProvider/LocaleProviderTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,17 @@ public function setUp()
3030

3131
/**
3232
* @expectedException RuntimeException
33+
* @expectedExceptionMessage LocaleProvider requires that a locale is set on the BuilderContext for "stdClass"
3334
*/
3435
public function testProvidePathNoLocale()
3536
{
3637
$this->routeStack->expects($this->once())
3738
->method('getContext')
3839
->will($this->returnValue($this->builderContext));
40+
41+
$this->builderContext->expects($this->once())
42+
->method('getContent')
43+
->will($this->returnValue(new \stdClass));
3944
$this->provider->providePath($this->routeStack);
4045
}
4146

0 commit comments

Comments
 (0)