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

Commit b7b5ac8

Browse files
committed
Make sure that the content_name parent is updated
When the content_path route has changed - Fixes #52
1 parent 6f34059 commit b7b5ac8

File tree

6 files changed

+40
-12
lines changed

6 files changed

+40
-12
lines changed

AutoRoute/PathProvider/ContentDateTimeProvider.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ public function providePath(RouteStack $routeStack)
4747
$date = $object->$method();
4848

4949
if (!$date instanceof \DateTime) {
50-
throw new \RuntimeException(sprintf('Method %s:%s must return an instance of DateTime.'));
50+
throw new \RuntimeException(sprintf('Method %s:%s must return an instance of DateTime.',
51+
get_class($object),
52+
$method
53+
));
5154
}
5255

5356
$string = $date->format($this->dateFormat);

AutoRoute/RouteMaker/AutoRouteMaker.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ public function make(RouteStack $routeStack)
4040

4141
if (!$autoRoute) {
4242
$autoRoute = new AutoRoute;
43-
$autoRoute->setParent($context->getTopRoute());
4443
$autoRoute->setContent($content);
4544
}
4645

4746
$autoRoute->setName($routeStack->getPath());
47+
$autoRoute->setParent($context->getTopRoute());
4848

4949
$routeStack->addRoute($autoRoute);
5050
}

Doctrine/Phpcr/AutoRouteListener.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public function onFlush(ManagerEventArgs $args)
4848
$scheduledUpdates = $uow->getScheduledUpdates();
4949
$updates = array_merge($scheduledInserts, $scheduledUpdates);
5050

51+
$autoRoute = null;
5152
foreach ($updates as $document) {
5253
if ($this->getArm()->isAutoRouteable($document)) {
5354
$contexts = $this->getArm()->updateAutoRouteForDocument($document);
@@ -58,6 +59,7 @@ public function onFlush(ManagerEventArgs $args)
5859
foreach ($context->getRoutes() as $route) {
5960

6061
if ($route instanceof AutoRoute) {
62+
$autoRoute = $route;
6163
$routeParent = $route->getParent();
6264
$id = spl_object_hash($routeParent).$route->getName();
6365
} else {
@@ -71,14 +73,18 @@ public function onFlush(ManagerEventArgs $args)
7173

7274
$dm->persist($route);
7375
$persistedRoutes[$id] = true;
76+
}
77+
78+
$uow->computeChangeSets();
7479

75-
// this was originally computeSingleDocumentChangeset
76-
// however this caused problems in a real usecase
77-
// (functional tests were fine)
78-
//
79-
// this is probably not very efficient, but it works
80-
$uow->computeChangeSets();
80+
// For some reason the AutoRoute is not updated even though
81+
// it is persisted above. Re-persisting and recomputing the
82+
// changesets makes this work.
83+
if (null !== $autoRoute) {
84+
$dm->persist($autoRoute);
8185
}
86+
87+
$uow->computeChangeSets();
8288
}
8389
}
8490
}

Tests/Functional/Command/RefreshCommandTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ protected function createBlog($withPosts = false)
3535
$post->title = 'This is a post title';
3636
$post->body = 'Test Body';
3737
$post->blog = $blog;
38+
$post->date = new \DateTime('2013/03/21');
3839
$this->getDm()->persist($post);
3940
}
4041

Tests/Functional/EventListener/AutoRouteListenerTest.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ protected function createBlog($withPosts = false)
3232
$post = new Post;
3333
$post->title = 'This is a post title';
3434
$post->blog = $blog;
35+
$post->date = new \DateTime('2013/03/21');
3536
$this->getDm()->persist($post);
3637
}
3738

@@ -158,15 +159,27 @@ public function testUpdatePost()
158159

159160
// make sure auto-route references content
160161
$post = $this->getDm()->find(null, '/test/test-blog/This is a post title');
161-
$post->title = "This is different";
162+
$post->title = 'This is different';
163+
164+
// test for issue #52
165+
$post->date = new \DateTime('2014-01-25');
166+
162167
$this->getDm()->persist($post);
163168
$this->getDm()->flush();
164169

165170
$routes = $this->getDm()->getReferrers($post);
166171

167172
$this->assertCount(1, $routes);
168-
$this->assertInstanceOf('Symfony\Cmf\Bundle\RoutingAutoBundle\Model\AutoRoute', $routes[0]);
169-
$this->assertEquals('this-is-different', $routes[0]->getName());
173+
$route = $routes[0];
174+
175+
$this->assertInstanceOf('Symfony\Cmf\Bundle\RoutingAutoBundle\Model\AutoRoute', $route);
176+
$this->assertEquals('this-is-different', $route->getName());
177+
178+
$node = $this->getDm()->getNodeForDocument($route);
179+
$this->assertEquals(
180+
'/test/auto-route/blog/unit-testing-blog/2014/01/25/this-is-different',
181+
$node->getPath()
182+
);
170183
}
171184

172185
public function provideMultilangArticle()

Tests/Resources/Document/Post.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ class Post
4848
*/
4949
public $body;
5050

51+
/**
52+
* @PHPCR\Date(nullable=true)
53+
*/
54+
public $date;
55+
5156
public function getTitle()
5257
{
5358
return $this->title;
@@ -60,6 +65,6 @@ public function getBlog()
6065

6166
public function getDate()
6267
{
63-
return new \DateTime('2013/03/21');
68+
return $this->date;;
6469
}
6570
}

0 commit comments

Comments
 (0)