149 lines
8.8 KiB
PHP
149 lines
8.8 KiB
PHP
<?php
|
|
|
|
namespace JumPHP\Ide\Tests;
|
|
|
|
use JumPHP\Ide\Symfony\ControllerCommands;
|
|
use JumPHP\Ide\Symfony\ControllerRoutes;
|
|
use JumPHP\Ide\Workspace\MemoryWorkspace;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
/** The controller panel under the editor: the routes of one controller read from its code (ide/ui/js/controller-panel.js). */
|
|
final class ControllerRoutesTest extends TestCase
|
|
{
|
|
private const FIXTURE = __DIR__ . '/fixtures/controllers/';
|
|
|
|
private static function post(): array
|
|
{
|
|
$code = (string)file_get_contents(self::FIXTURE . 'src/Controller/PostController.php');
|
|
$info = ControllerRoutes::analyze($code, 'src/Controller/PostController.php');
|
|
self::assertNotNull($info);
|
|
$byAction = [];
|
|
foreach ($info['routes'] as $r) {
|
|
$byAction[$r['action']] = $r;
|
|
}
|
|
return [$info, $byAction, $code];
|
|
}
|
|
|
|
public function testTheClassPrefixIsMergedAndOnlyActionsWithARouteAreListed(): void
|
|
{
|
|
[$info, $routes] = self::post();
|
|
self::assertSame('App\\Controller\\PostController', $info['class']);
|
|
self::assertSame(['path' => '/blog', 'name' => 'blog_'], $info['prefix']);
|
|
self::assertSame(['index', 'show', 'edit', 'create', 'search', 'archive'], array_keys($routes));
|
|
self::assertSame('/blog/', $routes['index']['path']);
|
|
self::assertSame('blog_index', $routes['index']['name']);
|
|
self::assertSame(['GET'], $routes['index']['methods']);
|
|
self::assertSame(['GET', 'POST'], $routes['edit']['methods']);
|
|
self::assertSame([], $routes['archive']['methods']);
|
|
}
|
|
|
|
public function testRequirementsDefaultsAndInlineRequirementsAreRead(): void
|
|
{
|
|
[, $routes] = self::post();
|
|
self::assertSame('/blog/{id<\d+>}', $routes['show']['path']);
|
|
self::assertSame('/blog/{id}', $routes['show']['url']);
|
|
self::assertSame(['id' => '\d+'], $routes['show']['requirements']);
|
|
self::assertSame(['slug' => '[a-z-]+'], $routes['edit']['requirements']);
|
|
self::assertSame(['page' => '1'], $routes['edit']['defaults']);
|
|
}
|
|
|
|
public function testRouteParametersAreMatchedWithTheMethodArguments(): void
|
|
{
|
|
[, $routes] = self::post();
|
|
$kinds = fn (string $a) => array_column($routes[$a]['params'], 'kind', 'name');
|
|
self::assertSame(['post' => 'entity', 'id' => 'route'], $kinds('show')); // the Post is fetched by {id}
|
|
self::assertFalse($routes['show']['params'][1]['unused']);
|
|
self::assertSame(['request' => 'request', 'post' => 'entity', 'slug' => 'route'], $kinds('edit'));
|
|
self::assertSame('MapEntity', $routes['edit']['params'][1]['via']);
|
|
self::assertSame('$post', $routes['edit']['params'][2]['via']);
|
|
self::assertSame(['input' => 'payload'], $kinds('create'));
|
|
self::assertSame(['filter' => 'query'], $kinds('search'));
|
|
// {year} goes to an int, {slug} has no argument at all
|
|
$archive = $routes['archive']['params'];
|
|
self::assertSame('route', $archive[0]['kind']);
|
|
self::assertTrue($archive[0]['placeholder']);
|
|
self::assertSame('slug', $archive[1]['name']);
|
|
self::assertTrue($archive[1]['unused']);
|
|
}
|
|
|
|
public function testIsGrantedOfTheClassAndOfTheActionAndAnInlineDenyAreCollected(): void
|
|
{
|
|
[, $routes] = self::post();
|
|
self::assertSame(['ROLE_USER'], array_column($routes['index']['grants'], 'attribute'));
|
|
self::assertSame(['ROLE_USER', 'POST_EDIT'], array_column($routes['edit']['grants'], 'attribute'));
|
|
self::assertSame('post', $routes['edit']['grants'][1]['subject']);
|
|
self::assertSame(['class', 'method'], array_column($routes['edit']['grants'], 'level'));
|
|
self::assertSame(['ROLE_USER', 'ROLE_ADMIN'], array_column($routes['search']['grants'], 'attribute'));
|
|
self::assertSame('call', $routes['search']['grants'][1]['level']);
|
|
}
|
|
|
|
public function testTemplatesRedirectsFormsAndJsonResponsesAreDetected(): void
|
|
{
|
|
[, $routes] = self::post();
|
|
self::assertSame(['post/index.html.twig'], array_column($routes['index']['templates'], 'name'));
|
|
self::assertSame(['post/missing.html.twig'], array_column($routes['edit']['templates'], 'name'));
|
|
self::assertSame(['blog_show'], array_column($routes['edit']['redirects'], 'route'));
|
|
self::assertSame(['App\\Form\\PostType'], array_column($routes['edit']['forms'], 'class'));
|
|
$json = array_values(array_filter($routes['create']['responses'], fn ($r) => $r['type'] === 'json'));
|
|
self::assertSame(['id', 'title'], $json[0]['keys']);
|
|
self::assertSame('json', $routes['search']['responses'][0]['type']);
|
|
self::assertSame('response', $routes['archive']['responses'][0]['type']);
|
|
}
|
|
|
|
public function testAFileThatIsNotAControllerIsIgnoredAndATypoIsReportedAsAnError(): void
|
|
{
|
|
self::assertNull(ControllerRoutes::analyze("<?php\nclass Foo { public function a() {} }", 'src/Service/Foo.php'));
|
|
self::assertNull(ControllerRoutes::analyze('x', 'README.md'));
|
|
$broken = ControllerRoutes::analyze("<?php\nclass AController {\n #[Route('/a')]\n public function a( {", 'src/Controller/AController.php');
|
|
self::assertTrue($broken['controller']);
|
|
self::assertNotEmpty($broken['error']);
|
|
$plain = ControllerRoutes::analyze("<?php\nnamespace App\\Controller;\nuse Symfony\\Bundle\\FrameworkBundle\\Controller\\AbstractController;\nclass Empty extends AbstractController {}", 'src/Other.php');
|
|
self::assertSame([], $plain['routes']);
|
|
}
|
|
|
|
public function testANewActionIsAddedAtTheEndWithItsUseLines(): void
|
|
{
|
|
$code = "<?php\n\nnamespace App\\Controller;\n\nuse Symfony\\Bundle\\FrameworkBundle\\Controller\\AbstractController;\n\nclass HomeController extends AbstractController\n{\n public function x(): void\n {\n }\n}\n";
|
|
$new = ControllerRoutes::addAction($code, 'showPost', '/show-post', ['GET'], 'app_show_post');
|
|
self::assertStringContainsString("use Symfony\\Component\\Routing\\Attribute\\Route;", $new);
|
|
self::assertStringContainsString("use Symfony\\Component\\HttpFoundation\\Response;", $new);
|
|
self::assertStringContainsString("#[Route('/show-post', name: 'app_show_post', methods: ['GET'])]\n public function showPost(): Response", $new);
|
|
self::assertSame('}', rtrim($new)[-1]);
|
|
$info = ControllerRoutes::analyze($new, 'src/Controller/HomeController.php');
|
|
self::assertSame(['app_show_post'], array_column($info['routes'], 'name'));
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
ControllerRoutes::addAction($new, 'showPost', '/again');
|
|
}
|
|
|
|
public function testTheCommandAddsTheSecurityRuleTheTemplateFilesAndTheSite(): void
|
|
{
|
|
$ws = new MemoryWorkspace([
|
|
'composer.json' => '{"require":{"symfony/framework-bundle":"*"},"autoload":{"psr-4":{"App\\\\":"src/"}}}',
|
|
'config/packages/security.yaml' => "security:\n firewalls:\n main:\n lazy: true\n access_control:\n - { path: '^/blog/api', roles: ROLE_ADMIN }\n - { path: '^/blog', roles: ROLE_USER }\n",
|
|
'src/Controller/PostController.php' => (string)file_get_contents(self::FIXTURE . 'src/Controller/PostController.php'),
|
|
'src/Form/PostType.php' => '<?php',
|
|
'templates/post/index.html.twig' => '',
|
|
]);
|
|
self::assertTrue(ControllerCommands::applies($ws));
|
|
$c = [];
|
|
ControllerCommands::register(function (string $n, callable $h) use (&$c) { $c[$n] = $h; }, $ws);
|
|
$r = $c['controller.routes'](['path' => 'src/Controller/PostController.php']);
|
|
$by = [];
|
|
foreach ($r['routes'] as $route) {
|
|
$by[$route['action']] = $route;
|
|
}
|
|
self::assertSame(1, $by['index']['access']['index']);
|
|
self::assertSame(['ROLE_USER'], $by['index']['access']['roles']);
|
|
self::assertSame(0, $by['create']['access']['index']);
|
|
self::assertSame('templates/post/index.html.twig', $by['index']['templates'][0]['file']);
|
|
self::assertNull($by['edit']['templates'][0]['file']);
|
|
self::assertSame('src/Form/PostType.php', $by['edit']['forms'][0]['file']);
|
|
self::assertSame('http://localhost:8000', $r['site']);
|
|
self::assertSame(['file' => 'src/Controller/PostController.php', 'line' => $by['show']['line']], $c['controller.route.find'](['name' => 'blog_show']));
|
|
// the unsaved text wins over the disk
|
|
self::assertFalse($c['controller.routes'](['path' => 'src/Service/X.php', 'text' => '<?php class X {}'])['controller']);
|
|
$add = $c['controller.addAction'](['path' => 'src/Controller/PostController.php', 'name' => 'fresh', 'route' => '/fresh', 'methods' => ['GET']]);
|
|
self::assertStringContainsString('public function fresh(): Response', $add['files'][0]['after']);
|
|
self::assertStringNotContainsString('function fresh', $add['files'][0]['before']);
|
|
}
|
|
}
|