Skip to content

Commit f1d08ed

Browse files
committed
[Tests] Streamline CompiledUrlGenerator tests
1 parent 5b5b866 commit f1d08ed

File tree

1 file changed

+33
-22
lines changed

1 file changed

+33
-22
lines changed

Tests/Generator/Dumper/CompiledUrlGeneratorDumperTest.php

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,16 @@ public function testDumpWithSimpleLocalizedRoutes()
123123

124124
public function testDumpWithRouteNotFoundLocalizedRoutes()
125125
{
126-
$this->expectException(RouteNotFoundException::class);
127-
$this->expectExceptionMessage('Unable to generate a URL for the named route "test" as such route does not exist.');
128126
$this->routeCollection->add('test.en', (new Route('/testing/is/fun'))->setDefault('_locale', 'en')->setDefault('_canonical_route', 'test')->setRequirement('_locale', 'en'));
129127

130128
$code = $this->generatorDumper->dump();
131129
file_put_contents($this->testTmpFilepath, $code);
132130

133131
$projectUrlGenerator = new CompiledUrlGenerator(require $this->testTmpFilepath, new RequestContext('/app.php'), null, 'pl_PL');
132+
133+
$this->expectException(RouteNotFoundException::class);
134+
$this->expectExceptionMessage('Unable to generate a URL for the named route "test" as such route does not exist.');
135+
134136
$projectUrlGenerator->generate('test');
135137
}
136138

@@ -183,22 +185,25 @@ public function testDumpWithTooManyRoutes()
183185

184186
public function testDumpWithoutRoutes()
185187
{
186-
$this->expectException(\InvalidArgumentException::class);
187188
file_put_contents($this->testTmpFilepath, $this->generatorDumper->dump());
188189

189190
$projectUrlGenerator = new CompiledUrlGenerator(require $this->testTmpFilepath, new RequestContext('/app.php'));
190191

192+
$this->expectException(\InvalidArgumentException::class);
193+
191194
$projectUrlGenerator->generate('Test', []);
192195
}
193196

194197
public function testGenerateNonExistingRoute()
195198
{
196-
$this->expectException(RouteNotFoundException::class);
197199
$this->routeCollection->add('Test', new Route('/test'));
198200

199201
file_put_contents($this->testTmpFilepath, $this->generatorDumper->dump());
200202

201203
$projectUrlGenerator = new CompiledUrlGenerator(require $this->testTmpFilepath, new RequestContext());
204+
205+
$this->expectException(RouteNotFoundException::class);
206+
202207
$projectUrlGenerator->generate('NonExisting', []);
203208
}
204209

@@ -287,66 +292,72 @@ public function testAliases()
287292

288293
public function testTargetAliasNotExisting()
289294
{
290-
$this->expectException(RouteNotFoundException::class);
291-
292-
$this->routeCollection->addAlias('a', 'not-existing');
295+
$this->routeCollection->add('not-existing', new Route('/not-existing'));
296+
$this->routeCollection->addAlias('alias', 'not-existing');
293297

294298
file_put_contents($this->testTmpFilepath, $this->generatorDumper->dump());
295299

296-
$compiledUrlGenerator = new CompiledUrlGenerator(require $this->testTmpFilepath, new RequestContext());
300+
$compiledRoutes = require $this->testTmpFilepath;
301+
unset($compiledRoutes['alias']);
297302

303+
$this->expectException(RouteNotFoundException::class);
304+
305+
$compiledUrlGenerator = new CompiledUrlGenerator($compiledRoutes, new RequestContext());
298306
$compiledUrlGenerator->generate('a');
299307
}
300308

301309
public function testTargetAliasWithNamePrefixNotExisting()
302310
{
303-
$this->expectException(RouteNotFoundException::class);
304-
305311
$subCollection = new RouteCollection();
306-
$subCollection->addAlias('a', 'not-existing');
312+
$subCollection->add('not-existing', new Route('/not-existing'));
313+
$subCollection->addAlias('alias', 'not-existing');
307314
$subCollection->addNamePrefix('sub_');
308315

309316
$this->routeCollection->addCollection($subCollection);
310317

311318
file_put_contents($this->testTmpFilepath, $this->generatorDumper->dump());
312319

313-
$compiledUrlGenerator = new CompiledUrlGenerator(require $this->testTmpFilepath, new RequestContext());
320+
$compiledRoutes = require $this->testTmpFilepath;
321+
unset($compiledRoutes['sub_alias']);
314322

315-
$compiledUrlGenerator->generate('sub_a');
323+
$this->expectException(RouteNotFoundException::class);
324+
325+
$compiledUrlGenerator = new CompiledUrlGenerator($compiledRoutes, new RequestContext());
326+
$compiledUrlGenerator->generate('sub_alias');
316327
}
317328

318329
public function testCircularReferenceShouldThrowAnException()
319330
{
320-
$this->expectException(RouteCircularReferenceException::class);
321-
$this->expectExceptionMessage('Circular reference detected for route "b", path: "b -> a -> b".');
322-
323331
$this->routeCollection->addAlias('a', 'b');
324332
$this->routeCollection->addAlias('b', 'a');
325333

334+
$this->expectException(RouteCircularReferenceException::class);
335+
$this->expectExceptionMessage('Circular reference detected for route "b", path: "b -> a -> b".');
336+
326337
$this->generatorDumper->dump();
327338
}
328339

329340
public function testDeepCircularReferenceShouldThrowAnException()
330341
{
331-
$this->expectException(RouteCircularReferenceException::class);
332-
$this->expectExceptionMessage('Circular reference detected for route "b", path: "b -> c -> b".');
333-
334342
$this->routeCollection->addAlias('a', 'b');
335343
$this->routeCollection->addAlias('b', 'c');
336344
$this->routeCollection->addAlias('c', 'b');
337345

346+
$this->expectException(RouteCircularReferenceException::class);
347+
$this->expectExceptionMessage('Circular reference detected for route "b", path: "b -> c -> b".');
348+
338349
$this->generatorDumper->dump();
339350
}
340351

341352
public function testIndirectCircularReferenceShouldThrowAnException()
342353
{
343-
$this->expectException(RouteCircularReferenceException::class);
344-
$this->expectExceptionMessage('Circular reference detected for route "b", path: "b -> c -> a -> b".');
345-
346354
$this->routeCollection->addAlias('a', 'b');
347355
$this->routeCollection->addAlias('b', 'c');
348356
$this->routeCollection->addAlias('c', 'a');
349357

358+
$this->expectException(RouteCircularReferenceException::class);
359+
$this->expectExceptionMessage('Circular reference detected for route "b", path: "b -> c -> a -> b".');
360+
350361
$this->generatorDumper->dump();
351362
}
352363

0 commit comments

Comments
 (0)