8000 minor #48010 [Routing] Add tests for loading PSR-4 classes from PHP f… · Nommyde/symfony@b87d6f9 · GitHub
[go: up one dir, main page]

Skip to content

Commit b87d6f9

Browse files
committed
minor symfony#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/A Follow-up to symfony#47916, symfony#47943 This 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 parents 44a6e66 + 416639c commit b87d6f9

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+
namespace Symfony\Component\Routing\Loader\Configurator;
4+
5+
use Symfony\Component\Routing\Tests\Fixtures\Psr4Controllers\MyController;
6+
7+
return function (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+
namespace Symfony\Component\Routing\Loader\Configurator;
4+
5+
return function (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+
namespace Symfony\Component\Routing\Loader\Configurator;
4+
5+
return function (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+
namespace Symfony\Component\Routing\Loader\Configurator;
4+
5+
return function (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
use Symfony\Component\Routing\Loader\PhpFileLoader;
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@
1313

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

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

298302
$this->assertEquals($expectedRoutes('php'), $routes);
299303
}
304+
305+
/**
306+
* @dataProvider providePsr4ConfigFiles
307+
*/
308+
public function testImportAttributesWithPsr4Prefix(string $configFile)
309+
{
310+
$locator = new FileLocator(\dirname(__DIR__).'/Fixtures');
311+
new LoaderResolver([
312+
$loader = new PhpFileLoader($locator),
313+
new Psr4DirectoryLoader($locator),
314+
new class() extends AnnotationClassLoader {
315+
protected function configureRoute(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+
public function providePsr4ConfigFiles(): array
328+
{
329+
return [
330+
['psr4-attributes.php'],
331+
['psr4-controllers-redirection.php'],
332+
];
333+
}
334+
335+
public function testImportAttributesFromClass()
336+
{
337+
new LoaderResolver([
338+
$loader = new PhpFileLoader(new FileLocator(\dirname(__DIR__).'/Fixtures')),
339+
new class() extends AnnotationClassLoader {
340+
protected function configureRoute(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)
0