Skip to content

Commit f81760f

Browse files
authored
Use Symfony's router instead of UrlAlias router (#62)
1 parent 1d42f3a commit f81760f

File tree

3 files changed

+32
-22
lines changed

3 files changed

+32
-22
lines changed

src/bundle/Resources/config/fieldtype_services.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ services:
4646

4747
Ibexa\FieldTypeRichText\RichText\Converter\Link:
4848
class: Ibexa\FieldTypeRichText\RichText\Converter\Link
49-
arguments: ['@ibexa.api.service.location', '@ibexa.api.service.content', '@Ibexa\Bundle\Core\Routing\UrlAliasRouter', '@?logger']
49+
arguments:
50+
- '@ibexa.api.service.location'
51+
- '@ibexa.api.service.content'
52+
- '@router'
53+
- '@?logger'
5054
tags:
5155
- {name: ibexa.field_type.richtext.converter.output.xhtml5, priority: 0}
5256

src/lib/RichText/Converter/Link.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Ibexa\Contracts\FieldTypeRichText\RichText\Converter;
1919
use Ibexa\Core\MVC\Symfony\Routing\UrlAliasRouter;
2020
use Psr\Log\LoggerInterface;
21+
use Symfony\Component\Routing\RouterInterface;
2122

2223
class Link implements Converter
2324
{
@@ -32,20 +33,24 @@ class Link implements Converter
3233
protected $contentService;
3334

3435
/**
35-
* @var \Ibexa\Core\MVC\Symfony\Routing\UrlAliasRouter
36+
* @var \Symfony\Component\Routing\RouterInterface
3637
*/
37-
protected $urlAliasRouter;
38+
protected $router;
3839

3940
/**
4041
* @var \Psr\Log\LoggerInterface
4142
*/
4243
protected $logger;
4344

44-
public function __construct(LocationService $locationService, ContentService $contentService, UrlAliasRouter $urlAliasRouter, LoggerInterface $logger = null)
45-
{
45+
public function __construct(
46+
LocationService $locationService,
47+
ContentService $contentService,
48+
RouterInterface $router,
49+
LoggerInterface $logger = null
50+
) {
4651
$this->locationService = $locationService;
4752
$this->contentService = $contentService;
48-
$this->urlAliasRouter = $urlAliasRouter;
53+
$this->router = $router;
4954
$this->logger = $logger;
5055
}
5156

@@ -137,7 +142,7 @@ public function convert(DOMDocument $document)
137142

138143
private function generateUrlAliasForLocation(Location $location, string $fragment): string
139144
{
140-
$urlAlias = $this->urlAliasRouter->generate(
145+
$urlAlias = $this->router->generate(
141146
UrlAliasRouter::URL_ALIAS_ROUTE_NAME,
142147
['location' => $location]
143148
);

tests/lib/RichText/Converter/LinkTest.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Ibexa\FieldTypeRichText\RichText\Converter\Link;
2020
use PHPUnit\Framework\TestCase;
2121
use Psr\Log\LoggerInterface;
22+
use Symfony\Component\Routing\RouterInterface;
2223

2324
/**
2425
* Tests the Link converter
@@ -45,9 +46,9 @@ protected function getMockLocationService()
4546
/**
4647
* @return \PHPUnit\Framework\MockObject\MockObject
4748
*/
48-
protected function getMockUrlAliasRouter()
49+
protected function getMockRouter()
4950
{
50-
return $this->createMock(UrlAliasRouter::class);
51+
return $this->createMock(RouterInterface::class);
5152
}
5253

5354
/**
@@ -119,18 +120,18 @@ public function testLink($input, $output)
119120

120121
$contentService = $this->getMockContentService();
121122
$locationService = $this->getMockLocationService();
122-
$urlAliasRouter = $this->getMockUrlAliasRouter();
123+
$router = $this->getMockRouter();
123124

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

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

130-
$urlAliasRouter->expects($this->never())
131+
$router->expects($this->never())
131132
->method($this->anything());
132133

133-
$converter = new Link($locationService, $contentService, $urlAliasRouter);
134+
$converter = new Link($locationService, $contentService, $router);
134135

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

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

216217
$contentService = $this->getMockContentService();
217218
$locationService = $this->getMockLocationService();
218-
$urlAliasRouter = $this->getMockUrlAliasRouter();
219+
$router = $this->getMockRouter();
219220

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

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

227-
$urlAliasRouter->expects($this->once())
228+
$router->expects($this->once())
228229
->method('generate')
229230
->with(UrlAliasRouter::URL_ALIAS_ROUTE_NAME, ['location' => $location])
230231
->willReturn($urlResolved);
231232

232-
$converter = new Link($locationService, $contentService, $urlAliasRouter);
233+
$converter = new Link($locationService, $contentService, $router);
233234

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

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

321322
$contentService = $this->getMockContentService();
322323
$locationService = $this->getMockLocationService();
323-
$urlAliasRouter = $this->getMockUrlAliasRouter();
324+
$router = $this->getMockRouter();
324325

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

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

336-
$converter = new Link($locationService, $contentService, $urlAliasRouter, $logger);
337+
$converter = new Link($locationService, $contentService, $router, $logger);
337338

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

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

420421
$contentService = $this->getMockContentService();
421422
$locationService = $this->getMockLocationService();
422-
$urlAliasRouter = $this->getMockUrlAliasRouter();
423+
$router = $this->getMockRouter();
423424

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

442-
$urlAliasRouter->expects($this->once())
443+
$router->expects($this->once())
443444
->method('generate')
444445
->with(UrlAliasRouter::URL_ALIAS_ROUTE_NAME, ['location' => $location])
445446
->willReturn($urlResolved);
446447

447-
$converter = new Link($locationService, $contentService, $urlAliasRouter);
448+
$converter = new Link($locationService, $contentService, $router);
448449

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

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

536537
$contentService = $this->getMockContentService();
537538
$locationService = $this->getMockLocationService();
538-
$urlAliasRouter = $this->getMockUrlAliasRouter();
539+
$router = $this->getMockRouter();
539540

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

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

551-
$converter = new Link($locationService, $contentService, $urlAliasRouter, $logger);
552+
$converter = new Link($locationService, $contentService, $router, $logger);
552553

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

0 commit comments

Comments
 (0)