|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Pierstoval\SmokeTesting\PhpUnitVersions; |
| 4 | + |
| 5 | +use Pierstoval\SmokeTesting\RoutesExtractor; |
| 6 | +use Symfony\Bundle\FrameworkBundle\KernelBrowser; |
| 7 | +use function sprintf; |
| 8 | +use Generator; |
| 9 | +use RuntimeException; |
| 10 | +use Symfony\Bundle\FrameworkBundle\Test\TestContainer; |
| 11 | +use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
| 12 | +use Symfony\Component\Routing\Route; |
| 13 | +use Symfony\Component\Routing\RouterInterface; |
| 14 | + |
| 15 | +abstract class PhpUnit12 extends WebTestCase |
| 16 | +{ |
| 17 | + protected function beforeRequest(KernelBrowser $client, string $routeName, string $routePath): void |
| 18 | + { |
| 19 | + // To be overriden by the end-user. |
| 20 | + } |
| 21 | + |
| 22 | + protected function afterRequest(KernelBrowser $client, string $routeName, string $routePath): void |
| 23 | + { |
| 24 | + // To be overriden by the end-user. |
| 25 | + } |
| 26 | + |
| 27 | + protected function afterAssertion(KernelBrowser $client, string $routeName, string $routePath): void |
| 28 | + { |
| 29 | + // To be overriden by the end-user. |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * @return Generator<string, Route> |
| 34 | + */ |
| 35 | + public static function provideRouteCollection(): Generator |
| 36 | + { |
| 37 | + if (!is_a(self::class, WebTestCase::class, true)) { |
| 38 | + throw new RuntimeException(sprintf('The "%s" trait trait can only be used in an instance of "%s"', self::class, WebTestCase::class)); |
| 39 | + } |
| 40 | + |
| 41 | + static::bootKernel(); |
| 42 | + |
| 43 | + /** @var TestContainer $container */ |
| 44 | + $container = static::getContainer(); |
| 45 | + |
| 46 | + /** @var RouterInterface $router */ |
| 47 | + $router = $container->get(RouterInterface::class); |
| 48 | + |
| 49 | + $routes = $router->getRouteCollection(); |
| 50 | + |
| 51 | + static::ensureKernelShutdown(); |
| 52 | + |
| 53 | + if (!$routes->count()) { |
| 54 | + throw new RuntimeException('No routes found in the application.'); |
| 55 | + } |
| 56 | + |
| 57 | + yield from RoutesExtractor::extractRoutesFromRouter($router); |
| 58 | + } |
| 59 | + |
| 60 | + public function testRoutesDoNotReturnInternalError(string $httpMethod, string $routeName, string $routePath): void |
| 61 | + { |
| 62 | + $client = static::createClient(); |
| 63 | + |
| 64 | + $this->beforeRequest($client, $routeName, $routePath); |
| 65 | + |
| 66 | + $client->request($httpMethod, $routePath); |
| 67 | + |
| 68 | + $this->afterRequest($client, $routeName, $routePath); |
| 69 | + |
| 70 | + $response = $client->getResponse(); |
| 71 | + static::assertLessThan( |
| 72 | + 500, |
| 73 | + $response->getStatusCode(), |
| 74 | + sprintf('Request "%s %s" for route "%s" returned an internal error.', $httpMethod, $routePath, $routeName), |
| 75 | + ); |
| 76 | + |
| 77 | + $this->afterAssertion($client, $routeName, $routePath); |
| 78 | + } |
| 79 | +} |
0 commit comments