Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add tests for loading PSR-4 classes from PHP files
  • Loading branch information
derrabus committed Oct 26, 2022
commit 416639c953696c639a5ff43f9612e2fa3213544c
14 changes: 14 additions & 0 deletions src/Symfony/Component/Routing/Tests/Fixtures/class-attributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Symfony\Component\Routing\Loader\Configurator;

use Symfony\Component\Routing\Tests\Fixtures\Psr4Controllers\MyController;

return function (RoutingConfigurator $routes): void {
$routes
->import(
resource: MyController::class,
type: 'attribute',
)
->prefix('/my-prefix');
};
15 changes: 15 additions & 0 deletions src/Symfony/Component/Routing/Tests/Fixtures/psr4-attributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Symfony\Component\Routing\Loader\Configurator;

return function (RoutingConfigurator $routes): void {
$routes
->import(
resource: [
'path' => './Psr4Controllers',
'namespace' => 'Symfony\Component\Routing\Tests\Fixtures\Psr4Controllers',
],
type: 'attribute',
)
->prefix('/my-prefix');
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Symfony\Component\Routing\Loader\Configurator;

return function (RoutingConfigurator $routes): void {
$routes->import('psr4-controllers-redirection/psr4-attributes.php');
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Symfony\Component\Routing\Loader\Configurator;

return function (RoutingConfigurator $routes): void {
$routes
->import(
resource: [
'path' => '../Psr4Controllers',
'namespace' => 'Symfony\Component\Routing\Tests\Fixtures\Psr4Controllers',
],
type: 'attribute',
)
->prefix('/my-prefix');
};
51 changes: 51 additions & 0 deletions src/Symfony/Component/Routing/Tests/Loader/PhpFileLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Loader\LoaderResolver;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Routing\Loader\AnnotationClassLoader;
use Symfony\Component\Routing\Loader\PhpFileLoader;
use Symfony\Component\Routing\Loader\Psr4DirectoryLoader;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Tests\Fixtures\Psr4Controllers\MyController;

class PhpFileLoaderTest extends TestCase
{
Expand Down Expand Up @@ -297,4 +301,51 @@ public function testImportingAliases()

$this->assertEquals($expectedRoutes('php'), $routes);
}

/**
* @dataProvider providePsr4ConfigFiles
*/
public function testImportAttributesWithPsr4Prefix(string $configFile)
{
$locator = new FileLocator(\dirname(__DIR__).'/Fixtures');
new LoaderResolver([
$loader = new PhpFileLoader($locator),
new Psr4DirectoryLoader($locator),
new class() extends AnnotationClassLoader {
protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, object $annot)
{
$route->setDefault('_controller', $class->getName().'::'.$method->getName());
}
},
]);

$route = $loader->load($configFile)->get('my_route');
$this->assertSame('/my-prefix/my/route', $route->getPath());
$this->assertSame(MyController::class.'::__invoke', $route->getDefault('_controller'));
}

public function providePsr4ConfigFiles(): array
{
return [
['psr4-attributes.php'],
['psr4-controllers-redirection.php'],
];
}

public function testImportAttributesFromClass()
{
new LoaderResolver([
$loader = new PhpFileLoader(new FileLocator(\dirname(__DIR__).'/Fixtures')),
new class() extends AnnotationClassLoader {
protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, object $annot)
{
$route->setDefault('_controller', $class->getName().'::'.$method->getName());
}
},
]);

$route = $loader->load('class-attributes.php')->get('my_route');
$this->assertSame('/my-prefix/my/route', $route->getPath());
$this->assertSame(MyController::class.'::__invoke', $route->getDefault('_controller'));
}
}