Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb87d6f9

Browse files
committed
minor#48010 [Routing] Add tests for loading PSR-4 classes from PHP files (derrabus)
This PR was merged into the 6.2 branch.Discussion----------[Routing] Add tests for loading PSR-4 classes from PHP files| Q | A| ------------- | ---| Branch? | 6.2| Bug fix? | no| New feature? | no| Deprecations? | no| Tickets | N/A| License | MIT| Doc PR | N/AFollow-up to#47916,#47943This PR adds more tests, demonstrating how to trigger the new PSR-4 loader from a PHP config file.Commits-------416639c Add tests for loading PSR-4 classes from PHP files
2 parents44a6e66 +416639c commitb87d6f9

File tree

5 files changed

+102
-0
lines changed

5 files changed

+102
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespaceSymfony\Component\Routing\Loader\Configurator;
4+
5+
useSymfony\Component\Routing\Tests\Fixtures\Psr4Controllers\MyController;
6+
7+
returnfunction (RoutingConfigurator$routes):void {
8+
$routes
9+
->import(
10+
resource: MyController::class,
11+
type:'attribute',
12+
)
13+
->prefix('/my-prefix');
14+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespaceSymfony\Component\Routing\Loader\Configurator;
4+
5+
returnfunction (RoutingConfigurator$routes):void {
6+
$routes
7+
->import(
8+
resource: [
9+
'path' =>'./Psr4Controllers',
10+
'namespace' =>'Symfony\Component\Routing\Tests\Fixtures\Psr4Controllers',
11+
],
12+
type:'attribute',
13+
)
14+
->prefix('/my-prefix');
15+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespaceSymfony\Component\Routing\Loader\Configurator;
4+
5+
returnfunction (RoutingConfigurator$routes):void {
6+
$routes->import('psr4-controllers-redirection/psr4-attributes.php');
7+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespaceSymfony\Component\Routing\Loader\Configurator;
4+
5+
returnfunction (RoutingConfigurator$routes):void {
6+
$routes
7+
->import(
8+
resource: [
9+
'path' =>'../Psr4Controllers',
10+
'namespace' =>'Symfony\Component\Routing\Tests\Fixtures\Psr4Controllers',
11+
],
12+
type:'attribute',
13+
)
14+
->prefix('/my-prefix');
15+
};

‎src/Symfony/Component/Routing/Tests/Loader/PhpFileLoaderTest.php‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@
1313

1414
usePHPUnit\Framework\TestCase;
1515
useSymfony\Component\Config\FileLocator;
16+
useSymfony\Component\Config\Loader\LoaderResolver;
1617
useSymfony\Component\Config\Resource\FileResource;
18+
useSymfony\Component\Routing\Loader\AnnotationClassLoader;
1719
useSymfony\Component\Routing\Loader\PhpFileLoader;
20+
useSymfony\Component\Routing\Loader\Psr4DirectoryLoader;
1821
useSymfony\Component\Routing\Route;
1922
useSymfony\Component\Routing\RouteCollection;
23+
useSymfony\Component\Routing\Tests\Fixtures\Psr4Controllers\MyController;
2024

2125
class PhpFileLoaderTestextends TestCase
2226
{
@@ -297,4 +301,51 @@ public function testImportingAliases()
297301

298302
$this->assertEquals($expectedRoutes('php'),$routes);
299303
}
304+
305+
/**
306+
* @dataProvider providePsr4ConfigFiles
307+
*/
308+
publicfunctiontestImportAttributesWithPsr4Prefix(string$configFile)
309+
{
310+
$locator =newFileLocator(\dirname(__DIR__).'/Fixtures');
311+
newLoaderResolver([
312+
$loader =newPhpFileLoader($locator),
313+
newPsr4DirectoryLoader($locator),
314+
newclass()extends AnnotationClassLoader {
315+
protectedfunctionconfigureRoute(Route$route,\ReflectionClass$class,\ReflectionMethod$method,object$annot)
316+
{
317+
$route->setDefault('_controller',$class->getName().'::'.$method->getName());
318+
}
319+
},
320+
]);
321+
322+
$route =$loader->load($configFile)->get('my_route');
323+
$this->assertSame('/my-prefix/my/route',$route->getPath());
324+
$this->assertSame(MyController::class.'::__invoke',$route->getDefault('_controller'));
325+
}
326+
327+
publicfunctionprovidePsr4ConfigFiles():array
328+
{
329+
return [
330+
['psr4-attributes.php'],
331+
['psr4-controllers-redirection.php'],
332+
];
333+
}
334+
335+
publicfunctiontestImportAttributesFromClass()
336+
{
337+
newLoaderResolver([
338+
$loader =newPhpFileLoader(newFileLocator(\dirname(__DIR__).'/Fixtures')),
339+
newclass()extends AnnotationClassLoader {
340+
protectedfunctionconfigureRoute(Route$route,\ReflectionClass$class,\ReflectionMethod$method,object$annot)
341+
{
342+
$route->setDefault('_controller',$class->getName().'::'.$method->getName());
343+
}
344+
},
345+
]);
346+
347+
$route =$loader->load('class-attributes.php')->get('my_route');
348+
$this->assertSame('/my-prefix/my/route',$route->getPath());
349+
$this->assertSame(MyController::class.'::__invoke',$route->getDefault('_controller'));
350+
}
300351
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp