Skip to content

Instantly share code, notes, and snippets.

@Tiriel
Created October 19, 2022 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tiriel/0178e3a42f312a792bb3ad52c787b35e to your computer and use it in GitHub Desktop.
Save Tiriel/0178e3a42f312a792bb3ad52c787b35e to your computer and use it in GitHub Desktop.
Simple Smoke test for Symfony applications
<?php
/**
* Automatically smoke test all your static routes, and all dynamic routes with default values.
* Adapted and simplified from https://github.com/Pierstoval/SmokeTesting
* On an idea from Ismail1432 https://github.com/ismail1432 on Twitter:
* https://twitter.com/SmaineDev/status/1559542937696043008
*/
namespace App\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouterInterface;
class DefaultControllerTest extends WebTestCase
{
private static KernelBrowser $client;
public static function setUpBeforeClass(): void
{
static::$client = static::createClient();
}
/**
* @dataProvider providePublicUrlsAndStatusCodes
*/
public function testPublicUrlIsNotServerError(string $method, string $url): void
{
static::$client->request($method, $url);
$this->assertLessThan(500, static::$client->getResponse()->getStatusCode());
}
public function providePublicUrlsAndStatusCodes(): \Generator
{
$router = static::getContainer()->get(RouterInterface::class);
$collection = $router->getRouteCollection();
static::ensureKernelShutdown();
foreach ($collection as $routeName => $route) {
/** @var Route $route */
$variables = $route->compile()->getVariables();
if (count(array_diff($variables, $route->getDefaults())) > 0) {
continue;
}
if ([] === $methods = $route->getMethods()) {
$methods[] = 'GET';
}
foreach ($methods as $method) {
$path = $router->generate($routeName);
yield "$method $path" => [$method, $path];
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment