Skip to content

Instantly share code, notes, and snippets.

@scrummitch
Created November 1, 2023 05:55
Show Gist options
  • Save scrummitch/2db17100361a2bce064d64682ee0c7f7 to your computer and use it in GitHub Desktop.
Save scrummitch/2db17100361a2bce064d64682ee0c7f7 to your computer and use it in GitHub Desktop.
<?php
$routes = collect(app('router')->getRoutes())
->groupBy(fn (\Illuminate\Routing\Route $route) => $route->getPrefix())
->reject(fn ($route, $prefix) => in_array($prefix, ['_ignition', 'sanctum']));
$routes->put('web', $routes->pull(''));
$webRoutes = $routes
->get('web')
->groupBy(fn (\Illuminate\Routing\Route $route) => \Illuminate\Support\Str::before($route->getAction('controller'), '@'))
->keyBy(function (\Illuminate\Support\Collection $routes, $i) {
return \Illuminate\Support\Str::after($i, 'App\\Http\\Controllers\\');
})->filter(function ($value, $key) {
return $key != '';
});
$actionTemplate = <<<EOT
public function test%s()
{
\$this->%s('/%s')->assertStatus(200);
}
EOT;
$testClassTemplate = <<<EOT
<?php
namespace Tests\Feature;
use Tests\TestCase;
/**
* @group web
*/
class %s extends TestCase
{
%s
}
EOT;
$apiTestClassTemplate = <<<EOT
<?php
namespace Tests\Feature\Api;
use Tests\TestCase;
/**
* @group api
*/
class %s extends TestCase
{
%s
}
EOT;
foreach ($webRoutes as $key => $webRoutes) {
$actions = [];
$testName = \Illuminate\Support\Str::finish(\Illuminate\Support\Str::studly(str_replace('\\', ' ', $key)), 'Test');
foreach ($webRoutes as $route) {
foreach ($route->methods as $method) {
if (in_array($method, ['HEAD', 'OPTIONS'])) continue;
$actionMethod = $route->getActionMethod();
if ($method === 'GET') {
$actionMethod = $actionMethod.'Page';
}
$actions[] = sprintf($actionTemplate, \Illuminate\Support\Str::studly($actionMethod), strtolower($method), $route->uri());
}
}
$testClass = sprintf($testClassTemplate, $testName, implode("\n", $actions));
file_put_contents(base_path('tests/Feature/'.$testName.'.php'), $testClass);
}
$apiRoutes = $routes
->get('api')
->groupBy(fn (\Illuminate\Routing\Route $route) => \Illuminate\Support\Str::before($route->getAction('controller'), '@'))
->keyBy(function (\Illuminate\Support\Collection $routes, $i) {
return \Illuminate\Support\Str::after($i, 'App\\Http\\Controllers\\');
})->filter(function ($value, $key) {
return $key != '';
});
$apiActionTemplate = <<<EOT
public function test%s()
{
\$this->%sJson('/api/%s', [])
->assertStatus(200);
}
EOT;
foreach ($apiRoutes as $key => $apiRoutes) {
$actions = [];
$testName = \Illuminate\Support\Str::finish(\Illuminate\Support\Str::studly(str_replace('\\', ' ', $key)), 'Test');
foreach ($apiRoutes as $route) {
foreach ($route->methods as $method) {
if (in_array($method, ['HEAD', 'OPTIONS'])) continue;
$actionMethod = $route->getActionMethod();
if ($method === 'GET') {
$actionMethod = $actionMethod.'Page';
}
$actions[] = sprintf($apiActionTemplate, \Illuminate\Support\Str::studly($actionMethod), strtolower($method), $route->uri());
}
}
$testClass = sprintf($apiTestClassTemplate, $testName, implode("\n", $actions));
file_put_contents(base_path('tests/Feature/Api/'.$testName.'.php'), $testClass);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment