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

Commit 44bebff

Browse files
committed
Integrated conflict resolvers
1 parent 63baf16 commit 44bebff

File tree

7 files changed

+75
-30
lines changed

7 files changed

+75
-30
lines changed

AutoRoute/AutoRouteManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function buildUrlContextCollection(UrlContextCollection $urlContextCollec
6161
$autoRoute = $existingRoute;
6262
} else {
6363
$url = $urlContext->getUrl();
64-
$url = $this->urlGenerator->resolveConflict($url);
64+
$url = $this->urlGenerator->resolveConflict($urlContext);
6565
$urlContext->setUrl($url);
6666
}
6767
}

AutoRoute/Mapping/ClassMetadata.php

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ class ClassMetadata extends MergeableClassMetadata
3232
protected $tokenProviders = array();
3333

3434
/**
35-
* @var null|array
35+
* @var array
3636
*/
37-
protected $conflictResolver;
37+
protected $conflictResolver = array('name' => 'throw_exception', 'options' => array());
3838

3939
/**
4040
* Defunct route handler, default to remove
@@ -122,17 +122,6 @@ public function getConflictResolver()
122122
return $this->conflictResolver;
123123
}
124124

125-
/**
126-
* Return true if a conflict resolver configuration
127-
* has been registered.
128-
*
129-
* @return boolean
130-
*/
131-
public function hasConflictResolver()
132-
{
133-
return null !== $this->conflictResolver;
134-
}
135-
136125
/**
137126
* Set the defunct route handler configuration.
138127
*
@@ -155,16 +144,6 @@ public function getDefunctRouteHandler()
155144
return $this->defunctRouteHandler;
156145
}
157146

158-
/**
159-
* Return true if a defunct route handler has been set.
160-
*
161-
* @return boolean
162-
*/
163-
public function hasDefunctRouteHandler()
164-
{
165-
return null !== $this->defunctRouteHandler;
166-
}
167-
168147
/**
169148
* Extend the metadata of the mapped class with given $name
170149
*

AutoRoute/UrlGenerator.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,15 @@ public function generateUrl(UrlContext $urlContext)
6666
*/
6767
public function resolveConflict(UrlContext $urlContext)
6868
{
69-
$realClassName = $this->driver->getRealClassName($urlContext->getSubjectObject());
70-
$metadata = $this->factory->getMetadataForClass($realClassName);
69+
$realClassName = $this->driver->getRealClassName(get_class($urlContext->getSubjectObject()));
70+
$metadata = $this->metadataFactory->getMetadataForClass($realClassName);
7171

72-
list ($name, $config) = $metadata->getConflictResolverConfig();
73-
$conflictResolver = $this->serviceRegistry->getConflictResolver($name, $config);
74-
$url = $conflictResolver->resolveConflict($url);
72+
$conflictResolverConfig = $metadata->getConflictResolver();
73+
$conflictResolver = $this->serviceRegistry->getConflictResolver(
74+
$conflictResolverConfig['name'],
75+
$conflictResolverConfig['options']
76+
);
77+
$url = $conflictResolver->resolveConflict($urlContext);
7578

7679
return $url;
7780
}

Tests/Functional/Command/RefreshCommandTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ protected function createBlog($withPosts = false)
3232

3333
if ($withPosts) {
3434
$post = new Post;
35+
$post->name = 'This is a post title';
3536
$post->title = 'This is a post title';
3637
$post->body = 'Test Body';
3738
$post->blog = $blog;

Tests/Functional/EventListener/AutoRouteListenerTest.php

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ protected function createBlog($withPosts = false)
3232

3333
if ($withPosts) {
3434
$post = new Post;
35+
$post->name = 'This is a post title';
3536
$post->title = 'This is a post title';
3637
$post->blog = $blog;
3738
$post->date = new \DateTime('2013/03/21');
@@ -398,7 +399,62 @@ public function testParentClassMapping()
398399
$this->assertCount(1, $routes);
399400
}
400401

401-
public function testConflictResolver()
402+
public function testConflictResolverAutoIncrement()
402403
{
404+
$this->createBlog();
405+
$blog = $this->getDm()->find(null, '/test/test-blog');
406+
407+
$post = new Post;
408+
$post->name = 'Post 1';
409+
$post->title = 'Same Title';
410+
$post->blog = $blog;
411+
$post->date = new \DateTime('2013/03/21');
412+
$this->getDm()->persist($post);
413+
$this->getDm()->flush();
414+
415+
$post = new Post;
416+
$post->name = 'Post 2';
417+
$post->title = 'Same Title';
418+
$post->blog = $blog;
419+
$post->date = new \DateTime('2013/03/21');
420+
$this->getDm()->persist($post);
421+
$this->getDm()->flush();
422+
423+
$post = new Post;
424+
$post->name = 'Post 3';
425+
$post->title = 'Same Title';
426+
$post->blog = $blog;
427+
$post->date = new \DateTime('2013/03/21');
428+
$this->getDm()->persist($post);
429+
$this->getDm()->flush();
430+
431+
$expectedRoutes = array(
432+
'/test/auto-route/blog/unit-testing-blog/2013/03/21/same-title',
433+
'/test/auto-route/blog/unit-testing-blog/2013/03/21/same-title-1',
434+
'/test/auto-route/blog/unit-testing-blog/2013/03/21/same-title-2',
435+
);
436+
437+
foreach ($expectedRoutes as $expectedRoute) {
438+
$route = $this->getDm()->find('Symfony\Cmf\Bundle\RoutingAutoBundle\Model\AutoRouteInterface', $expectedRoute);
439+
$this->assertNotNull($route);
440+
}
441+
}
442+
443+
/**
444+
* @expectedException Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\ConflictResolver\Exception\ExistingUrlException
445+
*/
446+
public function testConflictResolverDefaultThrowException()
447+
{
448+
$blog = new Blog;
449+
$blog->path = '/test/test-blog';
450+
$blog->title = 'Unit testing blog';
451+
$this->getDm()->persist($blog);
452+
$this->getDm()->flush();
453+
454+
$blog = new Blog;
455+
$blog->path = '/test/test-blog-the-second';
456+
$blog->title = 'Unit testing blog';
457+
$this->getDm()->persist($blog);
458+
$this->getDm()->flush();
403459
}
404460
}

Tests/Resources/Document/Post.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ class Post
4141
/**
4242
* @PHPCR\NodeName()
4343
*/
44+
public $name;
45+
46+
/**
47+
* @PHPCR\String()
48+
*/
4449
public $title;
4550

4651
/**

Tests/Resources/app/config/routing_auto.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Document\Blog:
55

66
Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Resources\Document\Post:
77
url_schema: /blog/{blog_title}/{post_date}/{post_title}
8+
conflict_resolver: [auto_increment, { }]
89
token_providers:
910
blog_title: [content_method, { method: getBlogTitle } ]
1011
post_date: [content_datetime, { method: getDate, date_format: Y/m/d, slugify: false } ]

0 commit comments

Comments
 (0)