Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add deprecation when UrlGeneratorInterface is missing in events #318

Merged
merged 1 commit into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Event/SitemapAddUrlEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ public function __construct(string $route, array $options, UrlGeneratorInterface
$this->route = $route;
$this->options = $options;
$this->urlGenerator = $urlGenerator;
if ($urlGenerator === null) {
@trigger_error(
'Not injecting the $urlGenerator in ' . __CLASS__ . ' is deprecated and will be required in 4.0.',
\E_USER_DEPRECATED
);
}
}

/**
Expand Down
6 changes: 6 additions & 0 deletions src/Event/SitemapPopulateEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ public function __construct(
$this->urlContainer = $urlContainer;
$this->section = $section;
$this->urlGenerator = $urlGenerator;
if ($urlGenerator === null) {
@trigger_error(
'Not injecting the $urlGenerator in ' . __CLASS__ . ' is deprecated and will be required in 4.0.',
\E_USER_DEPRECATED
);
}
}

/**
Expand Down
46 changes: 17 additions & 29 deletions tests/Unit/EventListener/RouteAnnotationEventListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ class RouteAnnotationEventListenerTest extends TestCase
*/
public function testPopulateSitemap(?string $section, array $routes, array $urls): void
{
$urlContainer = new InMemoryUrlContainer();
$event = new SitemapPopulateEvent($urlContainer, $section);
$dispatcher = new EventDispatcher();
$this->dispatch($dispatcher, $event, $routes);
$urlContainer = $this->dispatch($section, $routes);

// ensure that all expected section were created but not more than expected
self::assertEquals(\array_keys($urls), $urlContainer->getSections());
Expand Down Expand Up @@ -63,36 +60,18 @@ public function testPopulateSitemap(?string $section, array $routes, array $urls
*/
public function testEventListenerCanPreventUrlFromBeingAddedToSitemap(?string $section, array $routes): void
{
$dispatcher = new EventDispatcher();
$dispatcher->addListener(
SitemapAddUrlEvent::NAME,
function (SitemapAddUrlEvent $event): void {
$event->preventRegistration();
}
);

$urlContainer = new InMemoryUrlContainer();
$event = new SitemapPopulateEvent($urlContainer, $section);

$this->dispatch($dispatcher, $event, $routes);
$urlContainer = $this->dispatch($section, $routes, function (SitemapAddUrlEvent $event): void {
$event->preventRegistration();
});

self::assertEmpty($urlContainer->getSections());
}

public function testEventListenerCanSetUrl(): void
{
$dispatcher = new EventDispatcher();
$dispatcher->addListener(
SitemapAddUrlEvent::NAME,
function (SitemapAddUrlEvent $event): void {
$event->setUrl(new UrlConcrete('http://localhost/redirect'));
}
);

$urlContainer = new InMemoryUrlContainer();
$event = new SitemapPopulateEvent($urlContainer, null);

$this->dispatch($dispatcher, $event, [['home', '/', true]]);
$urlContainer = $this->dispatch(null, [['home', '/', true]], function (SitemapAddUrlEvent $event): void {
$event->setUrl(new UrlConcrete('http://localhost/redirect'));
});

$urlset = $urlContainer->getUrlset('default');
self::assertCount(1, $urlset);
Expand Down Expand Up @@ -132,8 +111,13 @@ public function routes(): \Generator
];
}

private function dispatch(EventDispatcher $dispatcher, SitemapPopulateEvent $event, array $routes): void
private function dispatch(?string $section, array $routes, ?\Closure $listener = null): InMemoryUrlContainer
{
$dispatcher = new EventDispatcher();
if ($listener !== null) {
$dispatcher->addListener(SitemapAddUrlEvent::NAME, $listener);
}

$router = new Router(
new ClosureLoader(),
static function () use ($routes): RouteCollection {
Expand All @@ -147,8 +131,12 @@ static function () use ($routes): RouteCollection {
['resource_type' => 'closure']
);

$urlContainer = new InMemoryUrlContainer();
$dispatcher->addSubscriber(new RouteAnnotationEventListener($router, $dispatcher, 'default'));
$event = new SitemapPopulateEvent($urlContainer, $section, $router);
$dispatcher->dispatch($event, SitemapPopulateEvent::class);

return $urlContainer;
}

private function findUrl(array $urlset, string $loc): ?UrlConcrete
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private function dispatch(array $listenerOptions, string $route, array $options
$dispatcher = new EventDispatcher();
$dispatcher->addSubscriber(new StaticRoutesAlternateEventListener($this->router, $listenerOptions));

$event = new SitemapAddUrlEvent($route, $options);
$event = new SitemapAddUrlEvent($route, $options, $this->router);
$dispatcher->dispatch($event, SitemapAddUrlEvent::class);

return $event;
Expand Down