Skip to content

Commit 87529f6

Browse files
OskarStarknicolas-grekas
authored andcommitted
Use createMock() and use import instead of FQCN
1 parent f5a0765 commit 87529f6

12 files changed

+62
-43
lines changed

Tests/Generator/Dumper/CompiledUrlGeneratorDumperTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Routing\Tests\Generator\Dumper;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Routing\Exception\RouteNotFoundException;
1516
use Symfony\Component\Routing\Generator\CompiledUrlGenerator;
1617
use Symfony\Component\Routing\Generator\Dumper\CompiledUrlGeneratorDumper;
1718
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
@@ -118,7 +119,7 @@ public function testDumpWithSimpleLocalizedRoutes()
118119

119120
public function testDumpWithRouteNotFoundLocalizedRoutes()
120121
{
121-
$this->expectException(\Symfony\Component\Routing\Exception\RouteNotFoundException::class);
122+
$this->expectException(RouteNotFoundException::class);
122123
$this->expectExceptionMessage('Unable to generate a URL for the named route "test" as such route does not exist.');
123124
$this->routeCollection->add('test.en', (new Route('/testing/is/fun'))->setDefault('_locale', 'en')->setDefault('_canonical_route', 'test')->setRequirement('_locale', 'en'));
124125

@@ -188,7 +189,7 @@ public function testDumpWithoutRoutes()
188189

189190
public function testGenerateNonExistingRoute()
190191
{
191-
$this->expectException(\Symfony\Component\Routing\Exception\RouteNotFoundException::class);
192+
$this->expectException(RouteNotFoundException::class);
192193
$this->routeCollection->add('Test', new Route('/test'));
193194

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

Tests/Generator/Dumper/PhpGeneratorDumperTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Routing\Tests\Generator\Dumper;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Routing\Exception\RouteNotFoundException;
1516
use Symfony\Component\Routing\Generator\Dumper\PhpGeneratorDumper;
1617
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
1718
use Symfony\Component\Routing\RequestContext;
@@ -124,7 +125,7 @@ public function testDumpWithSimpleLocalizedRoutes()
124125

125126
public function testDumpWithRouteNotFoundLocalizedRoutes()
126127
{
127-
$this->expectException(\Symfony\Component\Routing\Exception\RouteNotFoundException::class);
128+
$this->expectException(RouteNotFoundException::class);
128129
$this->expectExceptionMessage('Unable to generate a URL for the named route "test" as such route does not exist.');
129130
$this->routeCollection->add('test.en', (new Route('/testing/is/fun'))->setDefault('_locale', 'en')->setDefault('_canonical_route', 'test')->setRequirement('_locale', 'en'));
130131

@@ -204,7 +205,7 @@ public function testDumpWithoutRoutes()
204205

205206
public function testGenerateNonExistingRoute()
206207
{
207-
$this->expectException(\Symfony\Component\Routing\Exception\RouteNotFoundException::class);
208+
$this->expectException(RouteNotFoundException::class);
208209
$this->routeCollection->add('Test', new Route('/test'));
209210

210211
file_put_contents($this->testTmpFilepath, $this->generatorDumper->dump(['class' => 'NonExistingRoutesUrlGenerator']));

Tests/Generator/UrlGeneratorTest.php

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
namespace Symfony\Component\Routing\Tests\Generator;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Psr\Log\LoggerInterface;
16+
use Symfony\Component\Routing\Exception\InvalidParameterException;
17+
use Symfony\Component\Routing\Exception\MissingMandatoryParametersException;
18+
use Symfony\Component\Routing\Exception\RouteNotFoundException;
1519
use Symfony\Component\Routing\Generator\UrlGenerator;
1620
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
1721
use Symfony\Component\Routing\RequestContext;
@@ -78,7 +82,7 @@ public function testRelativeUrlWithNullParameter()
7882

7983
public function testRelativeUrlWithNullParameterButNotOptional()
8084
{
81-
$this->expectException(\Symfony\Component\Routing\Exception\InvalidParameterException::class);
85+
$this->expectException(InvalidParameterException::class);
8286
$routes = $this->getRoutes('test', new Route('/testing/{foo}/bar', ['foo' => null]));
8387
// This must raise an exception because the default requirement for "foo" is "[^/]+" which is not met with these params.
8488
// Generating path "/testing//bar" would be wrong as matching this route would fail.
@@ -264,14 +268,14 @@ public function testDumpWithLocalizedRoutesPreserveTheGoodLocaleInTheUrl()
264268

265269
public function testGenerateWithoutRoutes()
266270
{
267-
$this->expectException(\Symfony\Component\Routing\Exception\RouteNotFoundException::class);
271+
$this->expectException(RouteNotFoundException::class);
268272
$routes = $this->getRoutes('foo', new Route('/testing/{foo}'));
269273
$this->getGenerator($routes)->generate('test', [], UrlGeneratorInterface::ABSOLUTE_URL);
270274
}
271275

272276
public function testGenerateWithInvalidLocale()
273277
{
274-
$this->expectException(\Symfony\Component\Routing\Exception\RouteNotFoundException::class);
278+
$this->expectException(RouteNotFoundException::class);
275279
$routes = new RouteCollection();
276280

277281
$route = new Route('');
@@ -293,21 +297,21 @@ public function testGenerateWithInvalidLocale()
293297

294298
public function testGenerateForRouteWithoutMandatoryParameter()
295299
{
296-
$this->expectException(\Symfony\Component\Routing\Exception\MissingMandatoryParametersException::class);
300+
$this->expectException(MissingMandatoryParametersException::class);
297301
$routes = $this->getRoutes('test', new Route('/testing/{foo}'));
298302
$this->getGenerator($routes)->generate('test', [], UrlGeneratorInterface::ABSOLUTE_URL);
299303
}
300304

301305
public function testGenerateForRouteWithInvalidOptionalParameter()
302306
{
303-
$this->expectException(\Symfony\Component\Routing\Exception\InvalidParameterException::class);
307+
$this->expectException(InvalidParameterException::class);
304308
$routes = $this->getRoutes('test', new Route('/testing/{foo}', ['foo' => '1'], ['foo' => 'd+']));
305309
$this->getGenerator($routes)->generate('test', ['foo' => 'bar'], UrlGeneratorInterface::ABSOLUTE_URL);
306310
}
307311

308312
public function testGenerateForRouteWithInvalidParameter()
309313
{
310-
$this->expectException(\Symfony\Component\Routing\Exception\InvalidParameterException::class);
314+
$this->expectException(InvalidParameterException::class);
311315
$routes = $this->getRoutes('test', new Route('/testing/{foo}', [], ['foo' => '1|2']));
312316
$this->getGenerator($routes)->generate('test', ['foo' => '0'], UrlGeneratorInterface::ABSOLUTE_URL);
313317
}
@@ -323,7 +327,7 @@ public function testGenerateForRouteWithInvalidOptionalParameterNonStrict()
323327
public function testGenerateForRouteWithInvalidOptionalParameterNonStrictWithLogger()
324328
{
325329
$routes = $this->getRoutes('test', new Route('/testing/{foo}', ['foo' => '1'], ['foo' => 'd+']));
326-
$logger = $this->getMockBuilder(\Psr\Log\LoggerInterface::class)->getMock();
330+
$logger = $this->createMock(LoggerInterface::class);
327331
$logger->expects($this->once())
328332
->method('error');
329333
$generator = $this->getGenerator($routes, [], $logger);
@@ -341,21 +345,21 @@ public function testGenerateForRouteWithInvalidParameterButDisabledRequirementsC
341345

342346
public function testGenerateForRouteWithInvalidMandatoryParameter()
343347
{
344-
$this->expectException(\Symfony\Component\Routing\Exception\InvalidParameterException::class);
348+
$this->expectException(InvalidParameterException::class);
345349
$routes = $this->getRoutes('test', new Route('/testing/{foo}', [], ['foo' => 'd+']));
346350
$this->getGenerator($routes)->generate('test', ['foo' => 'bar'], UrlGeneratorInterface::ABSOLUTE_URL);
347351
}
348352

349353
public function testGenerateForRouteWithInvalidUtf8Parameter()
350354
{
351-
$this->expectException(\Symfony\Component\Routing\Exception\InvalidParameterException::class);
355+
$this->expectException(InvalidParameterException::class);
352356
$routes = $this->getRoutes('test', new Route('/testing/{foo}', [], ['foo' => '\pL+'], ['utf8' => true]));
353357
$this->getGenerator($routes)->generate('test', ['foo' => 'abc123'], UrlGeneratorInterface::ABSOLUTE_URL);
354358
}
355359

356360
public function testRequiredParamAndEmptyPassed()
357361
{
358-
$this->expectException(\Symfony\Component\Routing\Exception\InvalidParameterException::class);
362+
$this->expectException(InvalidParameterException::class);
359363
$routes = $this->getRoutes('test', new Route('/{slug}', [], ['slug' => '.+']));
360364
$this->getGenerator($routes)->generate('test', ['slug' => '']);
361365
}
@@ -476,7 +480,7 @@ public function testAdjacentVariables()
476480

477481
// The default requirement for 'x' should not allow the separator '.' in this case because it would otherwise match everything
478482
// and following optional variables like _format could never match.
479-
$this->expectException(\Symfony\Component\Routing\Exception\InvalidParameterException::class);
483+
$this->expectException(InvalidParameterException::class);
480484
$generator->generate('test', ['x' => 'do.t', 'y' => '123', 'z' => 'bar', '_format' => 'xml']);
481485
}
482486

@@ -517,7 +521,7 @@ public function testImportantVariable()
517521

518522
public function testImportantVariableWithNoDefault()
519523
{
520-
$this->expectException(\Symfony\Component\Routing\Exception\MissingMandatoryParametersException::class);
524+
$this->expectException(MissingMandatoryParametersException::class);
521525
$routes = $this->getRoutes('test', new Route('/{page}.{!_format}'));
522526
$generator = $this->getGenerator($routes);
523527

@@ -526,14 +530,14 @@ public function testImportantVariableWithNoDefault()
526530

527531
public function testDefaultRequirementOfVariableDisallowsSlash()
528532
{
529-
$this->expectException(\Symfony\Component\Routing\Exception\InvalidParameterException::class);
533+
$this->expectException(InvalidParameterException::class);
530534
$routes = $this->getRoutes('test', new Route('/{page}.{_format}'));
531535
$this->getGenerator($routes)->generate('test', ['page' => 'index', '_format' => 'sl/ash']);
532536
}
533537

534538
public function testDefaultRequirementOfVariableDisallowsNextSeparator()
535539
{
536-
$this->expectException(\Symfony\Component\Routing\Exception\InvalidParameterException::class);
540+
$this->expectException(InvalidParameterException::class);
537541
$routes = $this->getRoutes('test', new Route('/{page}.{_format}'));
538542
$this->getGenerator($routes)->generate('test', ['page' => 'do.t', '_format' => 'html']);
539543
}
@@ -561,21 +565,21 @@ public function testWithHostSameAsContextAndAbsolute()
561565

562566
public function testUrlWithInvalidParameterInHost()
563567
{
564-
$this->expectException(\Symfony\Component\Routing\Exception\InvalidParameterException::class);
568+
$this->expectException(InvalidParameterException::class);
565569
$routes = $this->getRoutes('test', new Route('/', [], ['foo' => 'bar'], [], '{foo}.example.com'));
566570
$this->getGenerator($routes)->generate('test', ['foo' => 'baz'], UrlGeneratorInterface::ABSOLUTE_PATH);
567571
}
568572

569573
public function testUrlWithInvalidParameterInHostWhenParamHasADefaultValue()
570574
{
571-
$this->expectException(\Symfony\Component\Routing\Exception\InvalidParameterException::class);
575+
$this->expectException(InvalidParameterException::class);
572576
$routes = $this->getRoutes('test', new Route('/', ['foo' => 'bar'], ['foo' => 'bar'], [], '{foo}.example.com'));
573577
$this->getGenerator($routes)->generate('test', ['foo' => 'baz'], UrlGeneratorInterface::ABSOLUTE_PATH);
574578
}
575579

576580
public function testUrlWithInvalidParameterEqualsDefaultValueInHost()
577581
{
578-
$this->expectException(\Symfony\Component\Routing\Exception\InvalidParameterException::class);
582+
$this->expectException(InvalidParameterException::class);
579583
$routes = $this->getRoutes('test', new Route('/', ['foo' => 'baz'], ['foo' => 'bar'], [], '{foo}.example.com'));
580584
$this->getGenerator($routes)->generate('test', ['foo' => 'baz'], UrlGeneratorInterface::ABSOLUTE_PATH);
581585
}

Tests/Loader/AbstractAnnotationLoaderTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Routing\Tests\Loader;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Routing\Loader\AnnotationClassLoader;
1516

1617
abstract class AbstractAnnotationLoaderTest extends TestCase
1718
{
@@ -25,7 +26,7 @@ public function getReader()
2526

2627
public function getClassLoader($reader)
2728
{
28-
return $this->getMockBuilder(\Symfony\Component\Routing\Loader\AnnotationClassLoader::class)
29+
return $this->getMockBuilder(AnnotationClassLoader::class)
2930
->setConstructorArgs([$reader])
3031
->getMockForAbstractClass()
3132
;

Tests/Loader/PhpFileLoaderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class PhpFileLoaderTest extends TestCase
2222
{
2323
public function testSupports()
2424
{
25-
$loader = new PhpFileLoader($this->getMockBuilder(FileLocator::class)->getMock());
25+
$loader = new PhpFileLoader($this->createMock(FileLocator::class));
2626

2727
$this->assertTrue($loader->supports('foo.php'), '->supports() returns true if the resource is loadable');
2828
$this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');

Tests/Loader/XmlFileLoaderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class XmlFileLoaderTest extends TestCase
2323
{
2424
public function testSupports()
2525
{
26-
$loader = new XmlFileLoader($this->getMockBuilder(FileLocator::class)->getMock());
26+
$loader = new XmlFileLoader($this->createMock(FileLocator::class));
2727

2828
$this->assertTrue($loader->supports('foo.xml'), '->supports() returns true if the resource is loadable');
2929
$this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');

Tests/Loader/YamlFileLoaderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class YamlFileLoaderTest extends TestCase
2222
{
2323
public function testSupports()
2424
{
25-
$loader = new YamlFileLoader($this->getMockBuilder(FileLocator::class)->getMock());
25+
$loader = new YamlFileLoader($this->createMock(FileLocator::class));
2626

2727
$this->assertTrue($loader->supports('foo.yml'), '->supports() returns true if the resource is loadable');
2828
$this->assertTrue($loader->supports('foo.yaml'), '->supports() returns true if the resource is loadable');

Tests/Matcher/RedirectableUrlMatcherTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Routing\Tests\Matcher;
1313

14+
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
15+
use Symfony\Component\Routing\Matcher\RedirectableUrlMatcher;
1416
use Symfony\Component\Routing\RequestContext;
1517
use Symfony\Component\Routing\Route;
1618
use Symfony\Component\Routing\RouteCollection;
@@ -39,7 +41,7 @@ public function testExtraTrailingSlash()
3941

4042
public function testRedirectWhenNoSlashForNonSafeMethod()
4143
{
42-
$this->expectException(\Symfony\Component\Routing\Exception\ResourceNotFoundException::class);
44+
$this->expectException(ResourceNotFoundException::class);
4345
$coll = new RouteCollection();
4446
$coll->add('foo', new Route('/foo/'));
4547

@@ -209,6 +211,6 @@ public function testTrailingRequirementWithDefault_A()
209211

210212
protected function getUrlMatcher(RouteCollection $routes, RequestContext $context = null)
211213
{
212-
return $this->getMockForAbstractClass(\Symfony\Component\Routing\Matcher\RedirectableUrlMatcher::class, [$routes, $context ?: new RequestContext()]);
214+
return $this->getMockForAbstractClass(RedirectableUrlMatcher::class, [$routes, $context ?: new RequestContext()]);
213215
}
214216
}

Tests/Matcher/UrlMatcherTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
16+
use Symfony\Component\Routing\Exception\NoConfigurationException;
1617
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
1718
use Symfony\Component\Routing\Matcher\UrlMatcher;
1819
use Symfony\Component\Routing\RequestContext;
@@ -687,7 +688,7 @@ public function testHostIsCaseInsensitive()
687688

688689
public function testNoConfiguration()
689690
{
690-
$this->expectException(\Symfony\Component\Routing\Exception\NoConfigurationException::class);
691+
$this->expectException(NoConfigurationException::class);
691692
$coll = new RouteCollection();
692693

693694
$matcher = $this->getUrlMatcher($coll);

Tests/RouteCollectionBuilderTest.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Config\FileLocator;
16+
use Symfony\Component\Config\Loader\LoaderInterface;
17+
use Symfony\Component\Config\Loader\LoaderResolverInterface;
1618
use Symfony\Component\Config\Resource\FileResource;
1719
use Symfony\Component\Routing\Loader\YamlFileLoader;
1820
use Symfony\Component\Routing\Route;
@@ -23,8 +25,8 @@ class RouteCollectionBuilderTest extends TestCase
2325
{
2426
public function testImport()
2527
{
26-
$resolvedLoader = $this->getMockBuilder(\Symfony\Component\Config\Loader\LoaderInterface::class)->getMock();
27-
$resolver = $this->getMockBuilder(\Symfony\Component\Config\Loader\LoaderResolverInterface::class)->getMock();
28+
$resolvedLoader = $this->createMock(LoaderInterface::class);
29+
$resolver = $this->createMock(LoaderResolverInterface::class);
2830
$resolver->expects($this->once())
2931
->method('resolve')
3032
->with('admin_routing.yml', 'yaml')
@@ -41,7 +43,7 @@ public function testImport()
4143
->with('admin_routing.yml', 'yaml')
4244
->willReturn($expectedCollection);
4345

44-
$loader = $this->getMockBuilder(\Symfony\Component\Config\Loader\LoaderInterface::class)->getMock();
46+
$loader = $this->createMock(LoaderInterface::class);
4547
$loader->expects($this->any())
4648
->method('getResolver')
4749
->willReturn($resolver);
@@ -101,7 +103,7 @@ public function testFlushOrdering()
101103
$importedCollection->add('imported_route1', new Route('/imported/foo1'));
102104
$importedCollection->add('imported_route2', new Route('/imported/foo2'));
103105

104-
$loader = $this->getMockBuilder(\Symfony\Component\Config\Loader\LoaderInterface::class)->getMock();
106+
$loader = $this->createMock(LoaderInterface::class);
105107
// make this loader able to do the import - keeps mocking simple
106108
$loader->expects($this->any())
107109
->method('supports')
@@ -264,7 +266,7 @@ public function providePrefixTests()
264266

265267
public function testFlushSetsPrefixedWithMultipleLevels()
266268
{
267-
$loader = $this->getMockBuilder(\Symfony\Component\Config\Loader\LoaderInterface::class)->getMock();
269+
$loader = $this->createMock(LoaderInterface::class);
268270
$routes = new RouteCollectionBuilder($loader);
269271

270272
$routes->add('homepage', 'MainController::homepageAction', 'homepage');
@@ -342,7 +344,7 @@ public function testAddsThePrefixOnlyOnceWhenLoadingMultipleCollections()
342344
$secondCollection = new RouteCollection();
343345
$secondCollection->add('b', new Route('/b'));
344346

345-
$loader = $this->getMockBuilder(\Symfony\Component\Config\Loader\LoaderInterface::class)->getMock();
347+
$loader = $this->createMock(LoaderInterface::class);
346348
$loader->expects($this->any())
347349
->method('supports')
348350
->willReturn(true);

Tests/RouteTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Routing\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Routing\CompiledRoute;
1516
use Symfony\Component\Routing\Route;
1617
use Symfony\Component\Routing\Tests\Fixtures\CustomCompiledRoute;
1718
use Symfony\Component\Routing\Tests\Fixtures\CustomRouteCompiler;
@@ -188,7 +189,7 @@ public function testCondition()
188189
public function testCompile()
189190
{
190191
$route = new Route('/{foo}');
191-
$this->assertInstanceOf(\Symfony\Component\Routing\CompiledRoute::class, $compiled = $route->compile(), '->compile() returns a compiled route');
192+
$this->assertInstanceOf(CompiledRoute::class, $compiled = $route->compile(), '->compile() returns a compiled route');
192193
$this->assertSame($compiled, $route->compile(), '->compile() only compiled the route once if unchanged');
193194
$route->setRequirement('foo', '.*');
194195
$this->assertNotSame($compiled, $route->compile(), '->compile() recompiles if the route was modified');

0 commit comments

Comments
 (0)