Created
October 19, 2022 14:43
-
-
Save Tiriel/0178e3a42f312a792bb3ad52c787b35e to your computer and use it in GitHub Desktop.
Simple Smoke test for Symfony applications
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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