Skip to content

Commit

Permalink
Use Symfony's router instead of UrlAlias router (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
pspanja authored Dec 30, 2022
1 parent 1d42f3a commit f81760f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 22 deletions.
6 changes: 5 additions & 1 deletion src/bundle/Resources/config/fieldtype_services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ services:

Ibexa\FieldTypeRichText\RichText\Converter\Link:
class: Ibexa\FieldTypeRichText\RichText\Converter\Link
arguments: ['@ibexa.api.service.location', '@ibexa.api.service.content', '@Ibexa\Bundle\Core\Routing\UrlAliasRouter', '@?logger']
arguments:
- '@ibexa.api.service.location'
- '@ibexa.api.service.content'
- '@router'
- '@?logger'
tags:
- {name: ibexa.field_type.richtext.converter.output.xhtml5, priority: 0}

Expand Down
17 changes: 11 additions & 6 deletions src/lib/RichText/Converter/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Ibexa\Contracts\FieldTypeRichText\RichText\Converter;
use Ibexa\Core\MVC\Symfony\Routing\UrlAliasRouter;
use Psr\Log\LoggerInterface;
use Symfony\Component\Routing\RouterInterface;

class Link implements Converter
{
Expand All @@ -32,20 +33,24 @@ class Link implements Converter
protected $contentService;

/**
* @var \Ibexa\Core\MVC\Symfony\Routing\UrlAliasRouter
* @var \Symfony\Component\Routing\RouterInterface
*/
protected $urlAliasRouter;
protected $router;

/**
* @var \Psr\Log\LoggerInterface
*/
protected $logger;

public function __construct(LocationService $locationService, ContentService $contentService, UrlAliasRouter $urlAliasRouter, LoggerInterface $logger = null)
{
public function __construct(
LocationService $locationService,
ContentService $contentService,
RouterInterface $router,
LoggerInterface $logger = null
) {
$this->locationService = $locationService;
$this->contentService = $contentService;
$this->urlAliasRouter = $urlAliasRouter;
$this->router = $router;
$this->logger = $logger;
}

Expand Down Expand Up @@ -137,7 +142,7 @@ public function convert(DOMDocument $document)

private function generateUrlAliasForLocation(Location $location, string $fragment): string
{
$urlAlias = $this->urlAliasRouter->generate(
$urlAlias = $this->router->generate(
UrlAliasRouter::URL_ALIAS_ROUTE_NAME,
['location' => $location]
);
Expand Down
31 changes: 16 additions & 15 deletions tests/lib/RichText/Converter/LinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Ibexa\FieldTypeRichText\RichText\Converter\Link;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Symfony\Component\Routing\RouterInterface;

/**
* Tests the Link converter
Expand All @@ -45,9 +46,9 @@ protected function getMockLocationService()
/**
* @return \PHPUnit\Framework\MockObject\MockObject
*/
protected function getMockUrlAliasRouter()
protected function getMockRouter()
{
return $this->createMock(UrlAliasRouter::class);
return $this->createMock(RouterInterface::class);
}

/**
Expand Down Expand Up @@ -119,18 +120,18 @@ public function testLink($input, $output)

$contentService = $this->getMockContentService();
$locationService = $this->getMockLocationService();
$urlAliasRouter = $this->getMockUrlAliasRouter();
$router = $this->getMockRouter();

$contentService->expects($this->never())
->method($this->anything());

$locationService->expects($this->never())
->method($this->anything());

$urlAliasRouter->expects($this->never())
$router->expects($this->never())
->method($this->anything());

$converter = new Link($locationService, $contentService, $urlAliasRouter);
$converter = new Link($locationService, $contentService, $router);

$outputDocument = $converter->convert($inputDocument);

Expand Down Expand Up @@ -215,7 +216,7 @@ public function testConvertLocationLink($input, $output, $locationId, $urlResolv

$contentService = $this->getMockContentService();
$locationService = $this->getMockLocationService();
$urlAliasRouter = $this->getMockUrlAliasRouter();
$router = $this->getMockRouter();

$location = $this->createMock(APILocation::class);

Expand All @@ -224,12 +225,12 @@ public function testConvertLocationLink($input, $output, $locationId, $urlResolv
->with($this->equalTo($locationId))
->willReturn($location);

$urlAliasRouter->expects($this->once())
$router->expects($this->once())
->method('generate')
->with(UrlAliasRouter::URL_ALIAS_ROUTE_NAME, ['location' => $location])
->willReturn($urlResolved);

$converter = new Link($locationService, $contentService, $urlAliasRouter);
$converter = new Link($locationService, $contentService, $router);

$outputDocument = $converter->convert($inputDocument);

Expand Down Expand Up @@ -320,7 +321,7 @@ public function testConvertBadLocationLink($input, $output, $locationId, $except

$contentService = $this->getMockContentService();
$locationService = $this->getMockLocationService();
$urlAliasRouter = $this->getMockUrlAliasRouter();
$router = $this->getMockRouter();

$logger = $this->createMock(LoggerInterface::class);

Expand All @@ -333,7 +334,7 @@ public function testConvertBadLocationLink($input, $output, $locationId, $except
->with($this->equalTo($locationId))
->will($this->throwException($exception));

$converter = new Link($locationService, $contentService, $urlAliasRouter, $logger);
$converter = new Link($locationService, $contentService, $router, $logger);

$outputDocument = $converter->convert($inputDocument);

Expand Down Expand Up @@ -419,7 +420,7 @@ public function testConvertContentLink($input, $output, $contentId, $urlResolved

$contentService = $this->getMockContentService();
$locationService = $this->getMockLocationService();
$urlAliasRouter = $this->getMockUrlAliasRouter();
$router = $this->getMockRouter();

$contentInfo = $this->createMock(APIContentInfo::class);
$location = $this->createMock(APILocation::class);
Expand All @@ -439,12 +440,12 @@ public function testConvertContentLink($input, $output, $contentId, $urlResolved
->with($this->equalTo($locationId))
->willReturn($location);

$urlAliasRouter->expects($this->once())
$router->expects($this->once())
->method('generate')
->with(UrlAliasRouter::URL_ALIAS_ROUTE_NAME, ['location' => $location])
->willReturn($urlResolved);

$converter = new Link($locationService, $contentService, $urlAliasRouter);
$converter = new Link($locationService, $contentService, $router);

$outputDocument = $converter->convert($inputDocument);

Expand Down Expand Up @@ -535,7 +536,7 @@ public function testConvertBadContentLink($input, $output, $contentId, $exceptio

$contentService = $this->getMockContentService();
$locationService = $this->getMockLocationService();
$urlAliasRouter = $this->getMockUrlAliasRouter();
$router = $this->getMockRouter();

$logger = $this->createMock(LoggerInterface::class);

Expand All @@ -548,7 +549,7 @@ public function testConvertBadContentLink($input, $output, $contentId, $exceptio
->with($this->equalTo($contentId))
->will($this->throwException($exception));

$converter = new Link($locationService, $contentService, $urlAliasRouter, $logger);
$converter = new Link($locationService, $contentService, $router, $logger);

$outputDocument = $converter->convert($inputDocument);

Expand Down

0 comments on commit f81760f

Please sign in to comment.