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

Commit 6433b6a

Browse files
committed
Added auto-increment conflict resolver
1 parent 1184bb8 commit 6433b6a

File tree

4 files changed

+113
-0
lines changed

4 files changed

+113
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\ConflictResolver;
4+
5+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\ConflictResolverInterface;
6+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\UrlContext;
7+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\Adapter\AdapterInterface;
8+
9+
/**
10+
* This conflict resolver will generate candidate URLs by appending
11+
* a number to the URL. It will keep incrementing this number until
12+
* the URL does not exist.
13+
*
14+
* @author Daniel Leech <[email protected]>
15+
*/
16+
class AutoIncrementConflictResolver implements ConflictResolverInterface
17+
{
18+
protected $adapter;
19+
protected $inc;
20+
21+
/**
22+
* @param AdapterInterface $adapter
23+
*/
24+
public function __construct(AdapterInterface $adapter)
25+
{
26+
$this->adapter = $adapter;
27+
}
28+
29+
/**
30+
* {@inheritDoc}
31+
*/
32+
public function resolveConflict(UrlContext $urlContext)
33+
{
34+
$this->inc = 0;
35+
36+
$url = $urlContext->getUrl();
37+
$candidateUrl = $this->incrementUrl($url);
38+
39+
while ($route = $this->adapter->findRouteForUrl($candidateUrl)) {
40+
$candidateUrl = $this->incrementUrl($url);
41+
}
42+
43+
return $candidateUrl;
44+
}
45+
46+
protected function incrementUrl($url)
47+
{
48+
return sprintf('%s-%s', $url, ++$this->inc);
49+
}
50+
}

AutoRoute/ConflictResolver/ThrowExceptionConflictResolver.php

Whitespace-only changes.

Tests/Functional/EventListener/AutoRouteListenerTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,4 +397,8 @@ public function testParentClassMapping()
397397

398398
$this->assertCount(1, $routes);
399399
}
400+
401+
public function testConflictResolver()
402+
{
403+
}
400404
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Unit\AutoRoute\ConflictResolver;
4+
5+
use Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\Unit\BaseTestCase;
6+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\ConflictResolver\AutoIncrementConflictResolver;
7+
8+
class AutoIncrementConflictResolverTest extends BaseTestCase
9+
{
10+
protected $adapter;
11+
12+
public function setUp()
13+
{
14+
parent::setUp();
15+
16+
$this->adapter = $this->prophesize('Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\Adapter\AdapterInterface');
17+
18+
$this->conflictResolver = new AutoIncrementConflictResolver($this->adapter->reveal());
19+
$this->urlContext = $this->prophesize('Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\UrlContext');
20+
}
21+
22+
public function provideResolveConflict()
23+
{
24+
return array(
25+
array(
26+
'/foobar/bar',
27+
array(
28+
'/foobar/bar-1',
29+
),
30+
'/foobar/bar-2'
31+
),
32+
array(
33+
'/foobar/bar',
34+
array(
35+
'/foobar/bar-1',
36+
'/foobar/bar-2',
37+
'/foobar/bar-4',
38+
),
39+
'/foobar/bar-3'
40+
)
41+
);
42+
}
43+
44+
/**
45+
* @dataProvider provideResolveConflict
46+
*/
47+
public function testResolveConflict($url, $existingRoutes, $expectedResult)
48+
{
49+
$this->urlContext->getUrl()->willReturn($url);
50+
51+
foreach ($existingRoutes as $existingRoute) {
52+
$this->adapter->findRouteForUrl($existingRoute)->willReturn(new \stdClass);
53+
}
54+
$this->adapter->findRouteForUrl($expectedResult)->willReturn(null);
55+
56+
$url = $this->conflictResolver->resolveConflict($this->urlContext->reveal());
57+
$this->assertEquals($expectedResult, $url);
58+
}
59+
}

0 commit comments

Comments
 (0)