feat: add the Handler and the handler.php stubUnverified
The Handler builds the Application once per Warm Process, retries a failed build on the next Invocation and turns escaping errors into a 500 Function Response. An integration test runs a MicroKernelTrait Kernel through the stub, which needs symfony/framework-bundle as a dev dependency.
d541338 parent: c5937dd modified
composer.json +2 -1 | @@ -19,7 +19,8 @@ | ||
| 19 | 19 | "friendsofphp/php-cs-fixer": "^3.95", |
| 20 | 20 | "phpstan/phpstan": "^2.3", |
| 21 | 21 | "phpstan/phpstan-phpunit": "^2.1", |
| 22 | - "phpunit/phpunit": "^13.4" | |
| 22 | + "phpunit/phpunit": "^13.4", | |
| 23 | + "symfony/framework-bundle": "^7.4 || ^8.0" | |
| 23 | 24 | }, |
| 24 | 25 | "autoload": { |
| 25 | 26 | "psr-4": { |
| @@ -19,7 +19,8 @@ | |||
| 19 | "friendsofphp/php-cs-fixer": "^3.95", | 19 | "friendsofphp/php-cs-fixer": "^3.95", |
| 20 | "phpstan/phpstan": "^2.3", | 20 | "phpstan/phpstan": "^2.3", |
| 21 | "phpstan/phpstan-phpunit": "^2.1", | 21 | "phpstan/phpstan-phpunit": "^2.1", |
| 22 | - "phpunit/phpunit": "^13.4" | 22 | + "phpunit/phpunit": "^13.4", |
| 23 | + "symfony/framework-bundle": "^7.4 || ^8.0" | ||
| 23 | }, | 24 | }, |
| 24 | "autoload": { | 25 | "autoload": { |
| 25 | "psr-4": { | 26 | "psr-4": { |
added
resources/handler.php +8 -0 | new file mode 100644 | ||
| @@ -0,0 +1,8 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +require __DIR__.'/vendor/autoload.php'; | |
| 4 | + | |
| 5 | +function handle(array $event, mixed $context): array | |
| 6 | +{ | |
| 7 | + return JDecool\ScalewayRuntime\Handler::handle($event, $context); | |
| 8 | +} | |
| new file mode 100644 | |||
| @@ -0,0 +1,8 @@ | |||
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +require __DIR__.'/vendor/autoload.php'; | ||
| 4 | + | ||
| 5 | +function handle(array $event, mixed $context): array | ||
| 6 | +{ | ||
| 7 | + return JDecool\ScalewayRuntime\Handler::handle($event, $context); | ||
| 8 | +} | ||
added
src/Handler.php +105 -0 | new file mode 100644 | ||
| @@ -0,0 +1,105 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +declare(strict_types=1); | |
| 4 | + | |
| 5 | +namespace JDecool\ScalewayRuntime; | |
| 6 | + | |
| 7 | +use Composer\InstalledVersions; | |
| 8 | +use JDecool\ScalewayRuntime\Runner\InvocationRunner; | |
| 9 | + | |
| 10 | +/** | |
| 11 | + * Backs the global handle() function Scaleway calls for each Invocation. | |
| 12 | + * | |
| 13 | + * The Application is built once per Warm Process and reused; when building it fails, | |
| 14 | + * the next Invocation tries again from scratch instead of keeping a half-booted Application. | |
| 15 | + * | |
| 16 | + * @phpstan-import-type FunctionResponse from HttpConverter | |
| 17 | + */ | |
| 18 | +final class Handler | |
| 19 | +{ | |
| 20 | + private static ?self $instance = null; | |
| 21 | + | |
| 22 | + private ?ScalewayRuntime $runtime = null; | |
| 23 | + private ?InvocationRunner $runner = null; | |
| 24 | + | |
| 25 | + /** | |
| 26 | + * @param string $composerDir the directory holding the application's composer.json | |
| 27 | + * @param resource $errorLog | |
| 28 | + */ | |
| 29 | + public function __construct( | |
| 30 | + private readonly string $composerDir, | |
| 31 | + private $errorLog, | |
| 32 | + ) { | |
| 33 | + } | |
| 34 | + | |
| 35 | + /** | |
| 36 | + * @param array<mixed> $event | |
| 37 | + * | |
| 38 | + * @return FunctionResponse | |
| 39 | + */ | |
| 40 | + public static function handle(array $event, mixed $context): array | |
| 41 | + { | |
| 42 | + if (null === self::$instance) { | |
| 43 | + $errorLog = fopen('php://stderr', 'w'); | |
| 44 | + \assert(false !== $errorLog); | |
| 45 | + self::$instance = new self((string) realpath(InstalledVersions::getRootPackage()['install_path']), $errorLog); | |
| 46 | + } | |
| 47 | + | |
| 48 | + return self::$instance->invoke($event, $context); | |
| 49 | + } | |
| 50 | + | |
| 51 | + /** | |
| 52 | + * @param array<mixed> $event | |
| 53 | + * | |
| 54 | + * @return FunctionResponse | |
| 55 | + */ | |
| 56 | + public function invoke(array $event, mixed $context): array | |
| 57 | + { | |
| 58 | + try { | |
| 59 | + $this->runtime ??= new ScalewayRuntime($this->runtimeOptions()); | |
| 60 | + $this->runner ??= $this->runtime->createInvocationRunner(); | |
| 61 | + | |
| 62 | + return $this->runner->invoke($event, $context); | |
| 63 | + } catch (\Throwable $e) { | |
| 64 | + fwrite($this->errorLog, 'Invocation failed: '.$e.\PHP_EOL); | |
| 65 | + | |
| 66 | + $debug = filter_var($_SERVER['APP_DEBUG'] ?? $_ENV['APP_DEBUG'] ?? false, \FILTER_VALIDATE_BOOL); | |
| 67 | + | |
| 68 | + return [ | |
| 69 | + 'statusCode' => 500, | |
| 70 | + 'headers' => ['Content-Type' => 'text/plain; charset=UTF-8'], | |
| 71 | + 'body' => $debug ? (string) $e : 'Internal Server Error', | |
| 72 | + ]; | |
| 73 | + } | |
| 74 | + } | |
| 75 | + | |
| 76 | + /** | |
| 77 | + * Gathers the options the same way vendor/autoload_runtime.php does: APP_RUNTIME_OPTIONS | |
| 78 | + * wins over composer.json extra.runtime, whose "project_dir" is relative to composer.json. | |
| 79 | + * | |
| 80 | + * @return array<mixed> | |
| 81 | + */ | |
| 82 | + private function runtimeOptions(): array | |
| 83 | + { | |
| 84 | + $extra = []; | |
| 85 | + $composerFile = $this->composerDir.'/composer.json'; | |
| 86 | + if (is_file($composerFile)) { | |
| 87 | + $composer = json_decode((string) file_get_contents($composerFile), true, 512, \JSON_THROW_ON_ERROR); | |
| 88 | + $runtime = \is_array($composer) && \is_array($composer['extra'] ?? null) ? $composer['extra']['runtime'] ?? null : null; | |
| 89 | + $extra = \is_array($runtime) ? $runtime : []; | |
| 90 | + } | |
| 91 | + | |
| 92 | + $projectDir = $this->composerDir.'/'.(\is_string($extra['project_dir'] ?? null) ? $extra['project_dir'] : ''); | |
| 93 | + unset($extra['class'], $extra['autoload_template'], $extra['project_dir']); | |
| 94 | + | |
| 95 | + $options = $_SERVER['APP_RUNTIME_OPTIONS'] ?? $_ENV['APP_RUNTIME_OPTIONS'] ?? []; | |
| 96 | + if (\is_string($options)) { | |
| 97 | + $options = json_decode($options, true, 512, \JSON_THROW_ON_ERROR); | |
| 98 | + } | |
| 99 | + if (!\is_array($options)) { | |
| 100 | + throw new \UnexpectedValueException('APP_RUNTIME_OPTIONS must be a JSON object.'); | |
| 101 | + } | |
| 102 | + | |
| 103 | + return $options + $extra + ['project_dir' => realpath($projectDir) ?: $projectDir]; | |
| 104 | + } | |
| 105 | +} | |
| new file mode 100644 | |||
| @@ -0,0 +1,105 @@ | |||
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +declare(strict_types=1); | ||
| 4 | + | ||
| 5 | +namespace JDecool\ScalewayRuntime; | ||
| 6 | + | ||
| 7 | +use Composer\InstalledVersions; | ||
| 8 | +use JDecool\ScalewayRuntime\Runner\InvocationRunner; | ||
| 9 | + | ||
| 10 | +/** | ||
| 11 | + * Backs the global handle() function Scaleway calls for each Invocation. | ||
| 12 | + * | ||
| 13 | + * The Application is built once per Warm Process and reused; when building it fails, | ||
| 14 | + * the next Invocation tries again from scratch instead of keeping a half-booted Application. | ||
| 15 | + * | ||
| 16 | + * @phpstan-import-type FunctionResponse from HttpConverter | ||
| 17 | + */ | ||
| 18 | +final class Handler | ||
| 19 | +{ | ||
| 20 | + private static ?self $instance = null; | ||
| 21 | + | ||
| 22 | + private ?ScalewayRuntime $runtime = null; | ||
| 23 | + private ?InvocationRunner $runner = null; | ||
| 24 | + | ||
| 25 | + /** | ||
| 26 | + * @param string $composerDir the directory holding the application's composer.json | ||
| 27 | + * @param resource $errorLog | ||
| 28 | + */ | ||
| 29 | + public function __construct( | ||
| 30 | + private readonly string $composerDir, | ||
| 31 | + private $errorLog, | ||
| 32 | + ) { | ||
| 33 | + } | ||
| 34 | + | ||
| 35 | + /** | ||
| 36 | + * @param array<mixed> $event | ||
| 37 | + * | ||
| 38 | + * @return FunctionResponse | ||
| 39 | + */ | ||
| 40 | + public static function handle(array $event, mixed $context): array | ||
| 41 | + { | ||
| 42 | + if (null === self::$instance) { | ||
| 43 | + $errorLog = fopen('php://stderr', 'w'); | ||
| 44 | + \assert(false !== $errorLog); | ||
| 45 | + self::$instance = new self((string) realpath(InstalledVersions::getRootPackage()['install_path']), $errorLog); | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + return self::$instance->invoke($event, $context); | ||
| 49 | + } | ||
| 50 | + | ||
| 51 | + /** | ||
| 52 | + * @param array<mixed> $event | ||
| 53 | + * | ||
| 54 | + * @return FunctionResponse | ||
| 55 | + */ | ||
| 56 | + public function invoke(array $event, mixed $context): array | ||
| 57 | + { | ||
| 58 | + try { | ||
| 59 | + $this->runtime ??= new ScalewayRuntime($this->runtimeOptions()); | ||
| 60 | + $this->runner ??= $this->runtime->createInvocationRunner(); | ||
| 61 | + | ||
| 62 | + return $this->runner->invoke($event, $context); | ||
| 63 | + } catch (\Throwable $e) { | ||
| 64 | + fwrite($this->errorLog, 'Invocation failed: '.$e.\PHP_EOL); | ||
| 65 | + | ||
| 66 | + $debug = filter_var($_SERVER['APP_DEBUG'] ?? $_ENV['APP_DEBUG'] ?? false, \FILTER_VALIDATE_BOOL); | ||
| 67 | + | ||
| 68 | + return [ | ||
| 69 | + 'statusCode' => 500, | ||
| 70 | + 'headers' => ['Content-Type' => 'text/plain; charset=UTF-8'], | ||
| 71 | + 'body' => $debug ? (string) $e : 'Internal Server Error', | ||
| 72 | + ]; | ||
| 73 | + } | ||
| 74 | + } | ||
| 75 | + | ||
| 76 | + /** | ||
| 77 | + * Gathers the options the same way vendor/autoload_runtime.php does: APP_RUNTIME_OPTIONS | ||
| 78 | + * wins over composer.json extra.runtime, whose "project_dir" is relative to composer.json. | ||
| 79 | + * | ||
| 80 | + * @return array<mixed> | ||
| 81 | + */ | ||
| 82 | + private function runtimeOptions(): array | ||
| 83 | + { | ||
| 84 | + $extra = []; | ||
| 85 | + $composerFile = $this->composerDir.'/composer.json'; | ||
| 86 | + if (is_file($composerFile)) { | ||
| 87 | + $composer = json_decode((string) file_get_contents($composerFile), true, 512, \JSON_THROW_ON_ERROR); | ||
| 88 | + $runtime = \is_array($composer) && \is_array($composer['extra'] ?? null) ? $composer['extra']['runtime'] ?? null : null; | ||
| 89 | + $extra = \is_array($runtime) ? $runtime : []; | ||
| 90 | + } | ||
| 91 | + | ||
| 92 | + $projectDir = $this->composerDir.'/'.(\is_string($extra['project_dir'] ?? null) ? $extra['project_dir'] : ''); | ||
| 93 | + unset($extra['class'], $extra['autoload_template'], $extra['project_dir']); | ||
| 94 | + | ||
| 95 | + $options = $_SERVER['APP_RUNTIME_OPTIONS'] ?? $_ENV['APP_RUNTIME_OPTIONS'] ?? []; | ||
| 96 | + if (\is_string($options)) { | ||
| 97 | + $options = json_decode($options, true, 512, \JSON_THROW_ON_ERROR); | ||
| 98 | + } | ||
| 99 | + if (!\is_array($options)) { | ||
| 100 | + throw new \UnexpectedValueException('APP_RUNTIME_OPTIONS must be a JSON object.'); | ||
| 101 | + } | ||
| 102 | + | ||
| 103 | + return $options + $extra + ['project_dir' => realpath($projectDir) ?: $projectDir]; | ||
| 104 | + } | ||
| 105 | +} | ||
added
tests/Fixtures/App/Counter.php +25 -0 | new file mode 100644 | ||
| @@ -0,0 +1,25 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +declare(strict_types=1); | |
| 4 | + | |
| 5 | +namespace JDecool\ScalewayRuntime\Tests\Fixtures\App; | |
| 6 | + | |
| 7 | +use Symfony\Contracts\Service\ResetInterface; | |
| 8 | + | |
| 9 | +/** | |
| 10 | + * Request-scoped state that leaks across Invocations unless the Kernel resets its services. | |
| 11 | + */ | |
| 12 | +final class Counter implements ResetInterface | |
| 13 | +{ | |
| 14 | + private int $count = 0; | |
| 15 | + | |
| 16 | + public function increment(): int | |
| 17 | + { | |
| 18 | + return ++$this->count; | |
| 19 | + } | |
| 20 | + | |
| 21 | + public function reset(): void | |
| 22 | + { | |
| 23 | + $this->count = 0; | |
| 24 | + } | |
| 25 | +} | |
| new file mode 100644 | |||
| @@ -0,0 +1,25 @@ | |||
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +declare(strict_types=1); | ||
| 4 | + | ||
| 5 | +namespace JDecool\ScalewayRuntime\Tests\Fixtures\App; | ||
| 6 | + | ||
| 7 | +use Symfony\Contracts\Service\ResetInterface; | ||
| 8 | + | ||
| 9 | +/** | ||
| 10 | + * Request-scoped state that leaks across Invocations unless the Kernel resets its services. | ||
| 11 | + */ | ||
| 12 | +final class Counter implements ResetInterface | ||
| 13 | +{ | ||
| 14 | + private int $count = 0; | ||
| 15 | + | ||
| 16 | + public function increment(): int | ||
| 17 | + { | ||
| 18 | + return ++$this->count; | ||
| 19 | + } | ||
| 20 | + | ||
| 21 | + public function reset(): void | ||
| 22 | + { | ||
| 23 | + $this->count = 0; | ||
| 24 | + } | ||
| 25 | +} | ||
added
tests/Fixtures/App/Kernel.php +65 -0 | new file mode 100644 | ||
| @@ -0,0 +1,65 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +declare(strict_types=1); | |
| 4 | + | |
| 5 | +namespace JDecool\ScalewayRuntime\Tests\Fixtures\App; | |
| 6 | + | |
| 7 | +use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait; | |
| 8 | +use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; | |
| 9 | +use Symfony\Component\HttpFoundation\JsonResponse; | |
| 10 | +use Symfony\Component\HttpFoundation\Request; | |
| 11 | +use Symfony\Component\HttpKernel\Kernel as BaseKernel; | |
| 12 | +use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator; | |
| 13 | + | |
| 14 | +final class Kernel extends BaseKernel | |
| 15 | +{ | |
| 16 | + use MicroKernelTrait; | |
| 17 | + | |
| 18 | + public static int $instances = 0; | |
| 19 | + | |
| 20 | + public function __construct(string $environment, bool $debug) | |
| 21 | + { | |
| 22 | + ++self::$instances; | |
| 23 | + parent::__construct($environment, $debug); | |
| 24 | + } | |
| 25 | + | |
| 26 | + // The fixture is copied out of this repository, whose composer.json the default lookup would find. | |
| 27 | + public function getProjectDir(): string | |
| 28 | + { | |
| 29 | + $projectDir = $_SERVER['APP_PROJECT_DIR'] ?? null; | |
| 30 | + \assert(\is_string($projectDir)); | |
| 31 | + | |
| 32 | + return $projectDir; | |
| 33 | + } | |
| 34 | + | |
| 35 | + public function state(Request $request): JsonResponse | |
| 36 | + { | |
| 37 | + $counter = $this->getContainer()->get(Counter::class); | |
| 38 | + \assert($counter instanceof Counter); | |
| 39 | + | |
| 40 | + return new JsonResponse([ | |
| 41 | + 'count' => $counter->increment(), | |
| 42 | + 'kernelInstances' => self::$instances, | |
| 43 | + 'pid' => getmypid(), | |
| 44 | + 'cacheDir' => $this->getCacheDir(), | |
| 45 | + 'buildDir' => $this->getBuildDir(), | |
| 46 | + 'secure' => $request->isSecure(), | |
| 47 | + 'clientIp' => $request->getClientIp(), | |
| 48 | + 'query' => $request->query->all(), | |
| 49 | + ]); | |
| 50 | + } | |
| 51 | + | |
| 52 | + private function configureContainer(ContainerConfigurator $container): void | |
| 53 | + { | |
| 54 | + $container->extension('framework', ['secret' => 'fixture', 'http_method_override' => false]); | |
| 55 | + $container->services() | |
| 56 | + ->set(Counter::class) | |
| 57 | + ->public() | |
| 58 | + ->tag('kernel.reset', ['method' => 'reset']); | |
| 59 | + } | |
| 60 | + | |
| 61 | + private function configureRoutes(RoutingConfigurator $routes): void | |
| 62 | + { | |
| 63 | + $routes->add('state', '/state')->controller('kernel::state'); | |
| 64 | + } | |
| 65 | +} | |
| new file mode 100644 | |||
| @@ -0,0 +1,65 @@ | |||
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +declare(strict_types=1); | ||
| 4 | + | ||
| 5 | +namespace JDecool\ScalewayRuntime\Tests\Fixtures\App; | ||
| 6 | + | ||
| 7 | +use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait; | ||
| 8 | +use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; | ||
| 9 | +use Symfony\Component\HttpFoundation\JsonResponse; | ||
| 10 | +use Symfony\Component\HttpFoundation\Request; | ||
| 11 | +use Symfony\Component\HttpKernel\Kernel as BaseKernel; | ||
| 12 | +use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator; | ||
| 13 | + | ||
| 14 | +final class Kernel extends BaseKernel | ||
| 15 | +{ | ||
| 16 | + use MicroKernelTrait; | ||
| 17 | + | ||
| 18 | + public static int $instances = 0; | ||
| 19 | + | ||
| 20 | + public function __construct(string $environment, bool $debug) | ||
| 21 | + { | ||
| 22 | + ++self::$instances; | ||
| 23 | + parent::__construct($environment, $debug); | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | + // The fixture is copied out of this repository, whose composer.json the default lookup would find. | ||
| 27 | + public function getProjectDir(): string | ||
| 28 | + { | ||
| 29 | + $projectDir = $_SERVER['APP_PROJECT_DIR'] ?? null; | ||
| 30 | + \assert(\is_string($projectDir)); | ||
| 31 | + | ||
| 32 | + return $projectDir; | ||
| 33 | + } | ||
| 34 | + | ||
| 35 | + public function state(Request $request): JsonResponse | ||
| 36 | + { | ||
| 37 | + $counter = $this->getContainer()->get(Counter::class); | ||
| 38 | + \assert($counter instanceof Counter); | ||
| 39 | + | ||
| 40 | + return new JsonResponse([ | ||
| 41 | + 'count' => $counter->increment(), | ||
| 42 | + 'kernelInstances' => self::$instances, | ||
| 43 | + 'pid' => getmypid(), | ||
| 44 | + 'cacheDir' => $this->getCacheDir(), | ||
| 45 | + 'buildDir' => $this->getBuildDir(), | ||
| 46 | + 'secure' => $request->isSecure(), | ||
| 47 | + 'clientIp' => $request->getClientIp(), | ||
| 48 | + 'query' => $request->query->all(), | ||
| 49 | + ]); | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + private function configureContainer(ContainerConfigurator $container): void | ||
| 53 | + { | ||
| 54 | + $container->extension('framework', ['secret' => 'fixture', 'http_method_override' => false]); | ||
| 55 | + $container->services() | ||
| 56 | + ->set(Counter::class) | ||
| 57 | + ->public() | ||
| 58 | + ->tag('kernel.reset', ['method' => 'reset']); | ||
| 59 | + } | ||
| 60 | + | ||
| 61 | + private function configureRoutes(RoutingConfigurator $routes): void | ||
| 62 | + { | ||
| 63 | + $routes->add('state', '/state')->controller('kernel::state'); | ||
| 64 | + } | ||
| 65 | +} | ||
added
tests/Fixtures/App/public/index.php +13 -0 | new file mode 100644 | ||
| @@ -0,0 +1,13 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +declare(strict_types=1); | |
| 4 | + | |
| 5 | +use JDecool\ScalewayRuntime\Tests\Fixtures\App\Kernel; | |
| 6 | + | |
| 7 | +require_once dirname(__DIR__).'/vendor/autoload_runtime.php'; | |
| 8 | + | |
| 9 | +return static function (array $context): Kernel { | |
| 10 | + assert(is_string($context['APP_ENV'])); | |
| 11 | + | |
| 12 | + return new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']); | |
| 13 | +}; | |
| new file mode 100644 | |||
| @@ -0,0 +1,13 @@ | |||
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +declare(strict_types=1); | ||
| 4 | + | ||
| 5 | +use JDecool\ScalewayRuntime\Tests\Fixtures\App\Kernel; | ||
| 6 | + | ||
| 7 | +require_once dirname(__DIR__).'/vendor/autoload_runtime.php'; | ||
| 8 | + | ||
| 9 | +return static function (array $context): Kernel { | ||
| 10 | + assert(is_string($context['APP_ENV'])); | ||
| 11 | + | ||
| 12 | + return new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']); | ||
| 13 | +}; | ||
added
tests/Fixtures/App/vendor/autoload_runtime.php +11 -0 | new file mode 100644 | ||
| @@ -0,0 +1,11 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +declare(strict_types=1); | |
| 4 | + | |
| 5 | +// Mirrors the first check of the vendor/autoload_runtime.php Symfony generates: the Handler already | |
| 6 | +// loaded vendor/autoload.php, so an unchanged Front Controller must return here and go no further. | |
| 7 | +if (true === (require_once __DIR__.'/autoload.php')) { | |
| 8 | + return; | |
| 9 | +} | |
| 10 | + | |
| 11 | +throw new LogicException('The Front Controller must not boot the Symfony Runtime on Scaleway.'); | |
| new file mode 100644 | |||
| @@ -0,0 +1,11 @@ | |||
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +declare(strict_types=1); | ||
| 4 | + | ||
| 5 | +// Mirrors the first check of the vendor/autoload_runtime.php Symfony generates: the Handler already | ||
| 6 | +// loaded vendor/autoload.php, so an unchanged Front Controller must return here and go no further. | ||
| 7 | +if (true === (require_once __DIR__.'/autoload.php')) { | ||
| 8 | + return; | ||
| 9 | +} | ||
| 10 | + | ||
| 11 | +throw new LogicException('The Front Controller must not boot the Symfony Runtime on Scaleway.'); | ||
added
tests/Fixtures/ComposerProject/composer.json +9 -0 | new file mode 100644 | ||
| @@ -0,0 +1,9 @@ | ||
| 1 | +{ | |
| 2 | + "extra": { | |
| 3 | + "runtime": { | |
| 4 | + "class": "Ignored\\Runtime", | |
| 5 | + "project_dir": "..", | |
| 6 | + "front_controller": "front-controllers/recording.php" | |
| 7 | + } | |
| 8 | + } | |
| 9 | +} | |
| new file mode 100644 | |||
| @@ -0,0 +1,9 @@ | |||
| 1 | +{ | ||
| 2 | + "extra": { | ||
| 3 | + "runtime": { | ||
| 4 | + "class": "Ignored\\Runtime", | ||
| 5 | + "project_dir": "..", | ||
| 6 | + "front_controller": "front-controllers/recording.php" | ||
| 7 | + } | ||
| 8 | + } | ||
| 9 | +} | ||
added
tests/HandlerTest.php +156 -0 | new file mode 100644 | ||
| @@ -0,0 +1,156 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +declare(strict_types=1); | |
| 4 | + | |
| 5 | +namespace JDecool\ScalewayRuntime\Tests; | |
| 6 | + | |
| 7 | +use JDecool\ScalewayRuntime\Handler; | |
| 8 | +use JDecool\ScalewayRuntime\Tests\Fixtures\RecordingKernel; | |
| 9 | +use PHPUnit\Framework\TestCase; | |
| 10 | +use Symfony\Component\HttpFoundation\Request; | |
| 11 | + | |
| 12 | +final class HandlerTest extends TestCase | |
| 13 | +{ | |
| 14 | + private const string FIXTURES = __DIR__.'/Fixtures'; | |
| 15 | + | |
| 16 | + /** @var array<mixed> */ | |
| 17 | + private array $server; | |
| 18 | + /** @var array<mixed> */ | |
| 19 | + private array $env; | |
| 20 | + private int $umask; | |
| 21 | + /** @var resource */ | |
| 22 | + private $errorLog; | |
| 23 | + | |
| 24 | + protected function setUp(): void | |
| 25 | + { | |
| 26 | + $this->server = $_SERVER; | |
| 27 | + $this->env = $_ENV; | |
| 28 | + // GenericRuntime resets the umask in debug mode. | |
| 29 | + $this->umask = umask(); | |
| 30 | + $errorLog = fopen('php://memory', 'w+'); | |
| 31 | + self::assertNotFalse($errorLog); | |
| 32 | + $this->errorLog = $errorLog; | |
| 33 | + RecordingKernel::reset(); | |
| 34 | + } | |
| 35 | + | |
| 36 | + protected function tearDown(): void | |
| 37 | + { | |
| 38 | + $_SERVER = $this->server; | |
| 39 | + $_ENV = $this->env; | |
| 40 | + umask($this->umask); | |
| 41 | + Request::setTrustedProxies([], 0); | |
| 42 | + } | |
| 43 | + | |
| 44 | + public function testApplicationIsBuiltOnceAndTerminatedAfterEachInvocation(): void | |
| 45 | + { | |
| 46 | + $handler = $this->handler(['front_controller' => 'front-controllers/recording.php']); | |
| 47 | + | |
| 48 | + self::assertSame('handled #1 /a', $handler->invoke(['path' => '/a'], [])['body']); | |
| 49 | + self::assertSame('handled #2 /b', $handler->invoke(['path' => '/b'], [])['body']); | |
| 50 | + self::assertSame(1, RecordingKernel::$builds); | |
| 51 | + self::assertSame(2, RecordingKernel::$last?->terminated); | |
| 52 | + } | |
| 53 | + | |
| 54 | + public function testFailedBuildIsRetriedOnNextInvocation(): void | |
| 55 | + { | |
| 56 | + RecordingKernel::$failingBuilds = 1; | |
| 57 | + $handler = $this->handler(['front_controller' => 'front-controllers/recording.php', 'debug' => false]); | |
| 58 | + | |
| 59 | + $first = $handler->invoke(['path' => '/'], []); | |
| 60 | + self::assertSame(500, $first['statusCode']); | |
| 61 | + self::assertStringContainsString('Build failed on purpose.', $this->errorLog()); | |
| 62 | + | |
| 63 | + self::assertSame('handled #1 /', $handler->invoke(['path' => '/'], [])['body']); | |
| 64 | + self::assertSame(2, RecordingKernel::$builds); | |
| 65 | + } | |
| 66 | + | |
| 67 | + public function testEscapedErrorIsGenericWithoutDebug(): void | |
| 68 | + { | |
| 69 | + $response = $this->handler(['front_controller' => 'front-controllers/recording.php', 'debug' => false]) | |
| 70 | + ->invoke(['path' => '/throw'], []); | |
| 71 | + | |
| 72 | + self::assertSame([ | |
| 73 | + 'statusCode' => 500, | |
| 74 | + 'headers' => ['Content-Type' => 'text/plain; charset=UTF-8'], | |
| 75 | + 'body' => 'Internal Server Error', | |
| 76 | + ], $response); | |
| 77 | + self::assertStringContainsString('Escaped on purpose.', $this->errorLog()); | |
| 78 | + } | |
| 79 | + | |
| 80 | + public function testEscapedErrorIsDetailedInDebug(): void | |
| 81 | + { | |
| 82 | + $response = $this->handler(['front_controller' => 'front-controllers/recording.php', 'debug' => true]) | |
| 83 | + ->invoke(['path' => '/throw'], []); | |
| 84 | + | |
| 85 | + self::assertSame(500, $response['statusCode']); | |
| 86 | + self::assertStringContainsString('LogicException: Escaped on purpose.', $response['body']); | |
| 87 | + } | |
| 88 | + | |
| 89 | + public function testApplicationKeepsServingAfterAnEscapedError(): void | |
| 90 | + { | |
| 91 | + $handler = $this->handler(['front_controller' => 'front-controllers/recording.php', 'debug' => false]); | |
| 92 | + | |
| 93 | + $handler->invoke(['path' => '/throw'], []); | |
| 94 | + | |
| 95 | + self::assertSame('handled #1 /', $handler->invoke(['path' => '/'], [])['body']); | |
| 96 | + self::assertSame(1, RecordingKernel::$builds); | |
| 97 | + } | |
| 98 | + | |
| 99 | + public function testUnsupportedApplicationTypeIsReported(): void | |
| 100 | + { | |
| 101 | + $this->handler(['front_controller' => 'front-controllers/unsupported.php', 'debug' => false])->invoke([], []); | |
| 102 | + | |
| 103 | + self::assertStringContainsString('cannot run Applications of type "stdClass"', $this->errorLog()); | |
| 104 | + } | |
| 105 | + | |
| 106 | + public function testFrontControllerMustReturnACallable(): void | |
| 107 | + { | |
| 108 | + $this->handler(['front_controller' => 'front-controllers/not-callable.php', 'debug' => false])->invoke([], []); | |
| 109 | + | |
| 110 | + self::assertStringContainsString('callable expected, "string" returned', $this->errorLog()); | |
| 111 | + } | |
| 112 | + | |
| 113 | + public function testOptionsComeFromComposerExtraRuntime(): void | |
| 114 | + { | |
| 115 | + $handler = $this->handler([], self::FIXTURES.'/ComposerProject'); | |
| 116 | + | |
| 117 | + self::assertSame('handled #1 /', $handler->invoke([], [])['body']); | |
| 118 | + } | |
| 119 | + | |
| 120 | + public function testRuntimeOptionsEnvVarWinsOverComposerExtraRuntime(): void | |
| 121 | + { | |
| 122 | + $handler = $this->handler(['front_controller' => 'front-controllers/unsupported.php', 'debug' => false], self::FIXTURES.'/ComposerProject'); | |
| 123 | + | |
| 124 | + self::assertSame(500, $handler->invoke([], [])['statusCode']); | |
| 125 | + self::assertStringContainsString('stdClass', $this->errorLog()); | |
| 126 | + } | |
| 127 | + | |
| 128 | + public function testRuntimeOptionsEnvVarIsDecodedFromJson(): void | |
| 129 | + { | |
| 130 | + $_SERVER['APP_RUNTIME_OPTIONS'] = json_encode([ | |
| 131 | + 'error_handler' => false, | |
| 132 | + 'project_dir' => self::FIXTURES, | |
| 133 | + 'front_controller' => 'front-controllers/recording.php', | |
| 134 | + ]); | |
| 135 | + | |
| 136 | + self::assertSame(200, new Handler(self::FIXTURES, $this->errorLog)->invoke([], [])['statusCode']); | |
| 137 | + } | |
| 138 | + | |
| 139 | + /** | |
| 140 | + * @param array<string, mixed> $options | |
| 141 | + */ | |
| 142 | + private function handler(array $options, string $composerDir = self::FIXTURES): Handler | |
| 143 | + { | |
| 144 | + // The Symfony error handler would take over PHPUnit's. | |
| 145 | + $_SERVER['APP_RUNTIME_OPTIONS'] = $options + ['error_handler' => false]; | |
| 146 | + | |
| 147 | + return new Handler($composerDir, $this->errorLog); | |
| 148 | + } | |
| 149 | + | |
| 150 | + private function errorLog(): string | |
| 151 | + { | |
| 152 | + rewind($this->errorLog); | |
| 153 | + | |
| 154 | + return (string) stream_get_contents($this->errorLog); | |
| 155 | + } | |
| 156 | +} | |
| new file mode 100644 | |||
| @@ -0,0 +1,156 @@ | |||
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +declare(strict_types=1); | ||
| 4 | + | ||
| 5 | +namespace JDecool\ScalewayRuntime\Tests; | ||
| 6 | + | ||
| 7 | +use JDecool\ScalewayRuntime\Handler; | ||
| 8 | +use JDecool\ScalewayRuntime\Tests\Fixtures\RecordingKernel; | ||
| 9 | +use PHPUnit\Framework\TestCase; | ||
| 10 | +use Symfony\Component\HttpFoundation\Request; | ||
| 11 | + | ||
| 12 | +final class HandlerTest extends TestCase | ||
| 13 | +{ | ||
| 14 | + private const string FIXTURES = __DIR__.'/Fixtures'; | ||
| 15 | + | ||
| 16 | + /** @var array<mixed> */ | ||
| 17 | + private array $server; | ||
| 18 | + /** @var array<mixed> */ | ||
| 19 | + private array $env; | ||
| 20 | + private int $umask; | ||
| 21 | + /** @var resource */ | ||
| 22 | + private $errorLog; | ||
| 23 | + | ||
| 24 | + protected function setUp(): void | ||
| 25 | + { | ||
| 26 | + $this->server = $_SERVER; | ||
| 27 | + $this->env = $_ENV; | ||
| 28 | + // GenericRuntime resets the umask in debug mode. | ||
| 29 | + $this->umask = umask(); | ||
| 30 | + $errorLog = fopen('php://memory', 'w+'); | ||
| 31 | + self::assertNotFalse($errorLog); | ||
| 32 | + $this->errorLog = $errorLog; | ||
| 33 | + RecordingKernel::reset(); | ||
| 34 | + } | ||
| 35 | + | ||
| 36 | + protected function tearDown(): void | ||
| 37 | + { | ||
| 38 | + $_SERVER = $this->server; | ||
| 39 | + $_ENV = $this->env; | ||
| 40 | + umask($this->umask); | ||
| 41 | + Request::setTrustedProxies([], 0); | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + public function testApplicationIsBuiltOnceAndTerminatedAfterEachInvocation(): void | ||
| 45 | + { | ||
| 46 | + $handler = $this->handler(['front_controller' => 'front-controllers/recording.php']); | ||
| 47 | + | ||
| 48 | + self::assertSame('handled #1 /a', $handler->invoke(['path' => '/a'], [])['body']); | ||
| 49 | + self::assertSame('handled #2 /b', $handler->invoke(['path' => '/b'], [])['body']); | ||
| 50 | + self::assertSame(1, RecordingKernel::$builds); | ||
| 51 | + self::assertSame(2, RecordingKernel::$last?->terminated); | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + public function testFailedBuildIsRetriedOnNextInvocation(): void | ||
| 55 | + { | ||
| 56 | + RecordingKernel::$failingBuilds = 1; | ||
| 57 | + $handler = $this->handler(['front_controller' => 'front-controllers/recording.php', 'debug' => false]); | ||
| 58 | + | ||
| 59 | + $first = $handler->invoke(['path' => '/'], []); | ||
| 60 | + self::assertSame(500, $first['statusCode']); | ||
| 61 | + self::assertStringContainsString('Build failed on purpose.', $this->errorLog()); | ||
| 62 | + | ||
| 63 | + self::assertSame('handled #1 /', $handler->invoke(['path' => '/'], [])['body']); | ||
| 64 | + self::assertSame(2, RecordingKernel::$builds); | ||
| 65 | + } | ||
| 66 | + | ||
| 67 | + public function testEscapedErrorIsGenericWithoutDebug(): void | ||
| 68 | + { | ||
| 69 | + $response = $this->handler(['front_controller' => 'front-controllers/recording.php', 'debug' => false]) | ||
| 70 | + ->invoke(['path' => '/throw'], []); | ||
| 71 | + | ||
| 72 | + self::assertSame([ | ||
| 73 | + 'statusCode' => 500, | ||
| 74 | + 'headers' => ['Content-Type' => 'text/plain; charset=UTF-8'], | ||
| 75 | + 'body' => 'Internal Server Error', | ||
| 76 | + ], $response); | ||
| 77 | + self::assertStringContainsString('Escaped on purpose.', $this->errorLog()); | ||
| 78 | + } | ||
| 79 | + | ||
| 80 | + public function testEscapedErrorIsDetailedInDebug(): void | ||
| 81 | + { | ||
| 82 | + $response = $this->handler(['front_controller' => 'front-controllers/recording.php', 'debug' => true]) | ||
| 83 | + ->invoke(['path' => '/throw'], []); | ||
| 84 | + | ||
| 85 | + self::assertSame(500, $response['statusCode']); | ||
| 86 | + self::assertStringContainsString('LogicException: Escaped on purpose.', $response['body']); | ||
| 87 | + } | ||
| 88 | + | ||
| 89 | + public function testApplicationKeepsServingAfterAnEscapedError(): void | ||
| 90 | + { | ||
| 91 | + $handler = $this->handler(['front_controller' => 'front-controllers/recording.php', 'debug' => false]); | ||
| 92 | + | ||
| 93 | + $handler->invoke(['path' => '/throw'], []); | ||
| 94 | + | ||
| 95 | + self::assertSame('handled #1 /', $handler->invoke(['path' => '/'], [])['body']); | ||
| 96 | + self::assertSame(1, RecordingKernel::$builds); | ||
| 97 | + } | ||
| 98 | + | ||
| 99 | + public function testUnsupportedApplicationTypeIsReported(): void | ||
| 100 | + { | ||
| 101 | + $this->handler(['front_controller' => 'front-controllers/unsupported.php', 'debug' => false])->invoke([], []); | ||
| 102 | + | ||
| 103 | + self::assertStringContainsString('cannot run Applications of type "stdClass"', $this->errorLog()); | ||
| 104 | + } | ||
| 105 | + | ||
| 106 | + public function testFrontControllerMustReturnACallable(): void | ||
| 107 | + { | ||
| 108 | + $this->handler(['front_controller' => 'front-controllers/not-callable.php', 'debug' => false])->invoke([], []); | ||
| 109 | + | ||
| 110 | + self::assertStringContainsString('callable expected, "string" returned', $this->errorLog()); | ||
| 111 | + } | ||
| 112 | + | ||
| 113 | + public function testOptionsComeFromComposerExtraRuntime(): void | ||
| 114 | + { | ||
| 115 | + $handler = $this->handler([], self::FIXTURES.'/ComposerProject'); | ||
| 116 | + | ||
| 117 | + self::assertSame('handled #1 /', $handler->invoke([], [])['body']); | ||
| 118 | + } | ||
| 119 | + | ||
| 120 | + public function testRuntimeOptionsEnvVarWinsOverComposerExtraRuntime(): void | ||
| 121 | + { | ||
| 122 | + $handler = $this->handler(['front_controller' => 'front-controllers/unsupported.php', 'debug' => false], self::FIXTURES.'/ComposerProject'); | ||
| 123 | + | ||
| 124 | + self::assertSame(500, $handler->invoke([], [])['statusCode']); | ||
| 125 | + self::assertStringContainsString('stdClass', $this->errorLog()); | ||
| 126 | + } | ||
| 127 | + | ||
| 128 | + public function testRuntimeOptionsEnvVarIsDecodedFromJson(): void | ||
| 129 | + { | ||
| 130 | + $_SERVER['APP_RUNTIME_OPTIONS'] = json_encode([ | ||
| 131 | + 'error_handler' => false, | ||
| 132 | + 'project_dir' => self::FIXTURES, | ||
| 133 | + 'front_controller' => 'front-controllers/recording.php', | ||
| 134 | + ]); | ||
| 135 | + | ||
| 136 | + self::assertSame(200, new Handler(self::FIXTURES, $this->errorLog)->invoke([], [])['statusCode']); | ||
| 137 | + } | ||
| 138 | + | ||
| 139 | + /** | ||
| 140 | + * @param array<string, mixed> $options | ||
| 141 | + */ | ||
| 142 | + private function handler(array $options, string $composerDir = self::FIXTURES): Handler | ||
| 143 | + { | ||
| 144 | + // The Symfony error handler would take over PHPUnit's. | ||
| 145 | + $_SERVER['APP_RUNTIME_OPTIONS'] = $options + ['error_handler' => false]; | ||
| 146 | + | ||
| 147 | + return new Handler($composerDir, $this->errorLog); | ||
| 148 | + } | ||
| 149 | + | ||
| 150 | + private function errorLog(): string | ||
| 151 | + { | ||
| 152 | + rewind($this->errorLog); | ||
| 153 | + | ||
| 154 | + return (string) stream_get_contents($this->errorLog); | ||
| 155 | + } | ||
| 156 | +} | ||
added
tests/IntegrationTest.php +141 -0 | new file mode 100644 | ||
| @@ -0,0 +1,141 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +declare(strict_types=1); | |
| 4 | + | |
| 5 | +namespace JDecool\ScalewayRuntime\Tests; | |
| 6 | + | |
| 7 | +use PHPUnit\Framework\TestCase; | |
| 8 | +use Symfony\Component\Filesystem\Filesystem; | |
| 9 | + | |
| 10 | +/** | |
| 11 | + * Runs a real MicroKernelTrait Kernel through the committed handler stub, in a separate PHP process | |
| 12 | + * that loads handler.php once and calls handle() per Event, as Scaleway does. | |
| 13 | + */ | |
| 14 | +final class IntegrationTest extends TestCase | |
| 15 | +{ | |
| 16 | + private Filesystem $filesystem; | |
| 17 | + private string $dir; | |
| 18 | + private string $projectDir; | |
| 19 | + | |
| 20 | + protected function setUp(): void | |
| 21 | + { | |
| 22 | + $this->filesystem = new Filesystem(); | |
| 23 | + $this->dir = sys_get_temp_dir().'/scaleway-runtime-'.bin2hex(random_bytes(4)); | |
| 24 | + $this->projectDir = $this->dir.'/project'; | |
| 25 | + | |
| 26 | + $this->filesystem->mirror(__DIR__.'/Fixtures/App', $this->projectDir); | |
| 27 | + $this->filesystem->copy(__DIR__.'/../resources/handler.php', $this->projectDir.'/handler.php'); | |
| 28 | + $this->filesystem->dumpFile( | |
| 29 | + $this->projectDir.'/vendor/autoload.php', | |
| 30 | + '<?php return require '.var_export(\dirname(__DIR__).'/vendor/autoload.php', true).';', | |
| 31 | + ); | |
| 32 | + $this->filesystem->mkdir($this->dir.'/tmp'); | |
| 33 | + } | |
| 34 | + | |
| 35 | + protected function tearDown(): void | |
| 36 | + { | |
| 37 | + $this->filesystem->chmod($this->dir, 0o777, 0o000, true); | |
| 38 | + $this->filesystem->remove($this->dir); | |
| 39 | + } | |
| 40 | + | |
| 41 | + public function testConsecutiveInvocationsReuseTheKernelAndResetServices(): void | |
| 42 | + { | |
| 43 | + [$first, $second] = $this->invoke([ | |
| 44 | + [ | |
| 45 | + 'path' => '/state', | |
| 46 | + 'method' => 'GET', | |
| 47 | + 'queryStringParameters' => ['page' => '2'], | |
| 48 | + 'headers' => ['Host' => 'internal.local', 'X-Forwarded-Proto' => 'https', 'X-Forwarded-For' => '203.0.113.7'], | |
| 49 | + 'body' => '', | |
| 50 | + 'isBase64Encoded' => false, | |
| 51 | + ], | |
| 52 | + ['path' => '/state', 'method' => 'GET', 'headers' => ['Host' => 'internal.local']], | |
| 53 | + ]); | |
| 54 | + | |
| 55 | + self::assertSame(200, $first['statusCode']); | |
| 56 | + self::assertSame(200, $second['statusCode']); | |
| 57 | + $first = $this->json($first); | |
| 58 | + $second = $this->json($second); | |
| 59 | + | |
| 60 | + self::assertSame(1, $first['count']); | |
| 61 | + self::assertSame(1, $second['count'], 'The Counter service must be reset between Invocations.'); | |
| 62 | + self::assertSame(1, $second['kernelInstances'], 'The Kernel must be reused across Invocations.'); | |
| 63 | + self::assertSame($first['pid'], $second['pid']); | |
| 64 | + self::assertTrue($first['secure']); | |
| 65 | + self::assertSame('203.0.113.7', $first['clientIp']); | |
| 66 | + self::assertSame(['page' => '2'], $first['query']); | |
| 67 | + self::assertSame($this->projectDir.'/var/cache/prod', $first['cacheDir']); | |
| 68 | + } | |
| 69 | + | |
| 70 | + public function testUnknownRouteIsHandledByTheKernel(): void | |
| 71 | + { | |
| 72 | + [$response] = $this->invoke([['path' => '/missing', 'method' => 'GET']]); | |
| 73 | + | |
| 74 | + self::assertSame(404, $response['statusCode']); | |
| 75 | + } | |
| 76 | + | |
| 77 | + public function testReadOnlyProjectUsesTheWarmedBuildDirAndATempCacheDir(): void | |
| 78 | + { | |
| 79 | + $this->invoke([['path' => '/state']]); | |
| 80 | + $this->filesystem->chmod($this->projectDir, 0o555, 0o000, true); | |
| 81 | + | |
| 82 | + [$response] = $this->invoke([['path' => '/state']]); | |
| 83 | + | |
| 84 | + self::assertSame(200, $response['statusCode']); | |
| 85 | + $state = $this->json($response); | |
| 86 | + self::assertSame($this->dir.'/tmp/cache/prod', $state['cacheDir']); | |
| 87 | + self::assertSame($this->projectDir.'/var/cache/prod', $state['buildDir']); | |
| 88 | + } | |
| 89 | + | |
| 90 | + /** | |
| 91 | + * @param list<array<string, mixed>> $events | |
| 92 | + * | |
| 93 | + * @return array<array<mixed>> | |
| 94 | + */ | |
| 95 | + private function invoke(array $events): array | |
| 96 | + { | |
| 97 | + $driver = 'require $argv[1]; echo json_encode(array_map(fn ($event) => handle($event, []), json_decode(stream_get_contents(STDIN), true)));'; | |
| 98 | + $process = proc_open( | |
| 99 | + [\PHP_BINARY, '-r', $driver, $this->projectDir.'/handler.php'], | |
| 100 | + [0 => ['pipe', 'r'], 1 => ['pipe', 'w'], 2 => ['pipe', 'w']], | |
| 101 | + $pipes, | |
| 102 | + null, | |
| 103 | + [ | |
| 104 | + 'APP_ENV' => 'prod', | |
| 105 | + 'APP_DEBUG' => '0', | |
| 106 | + 'APP_RUNTIME_OPTIONS' => json_encode(['project_dir' => $this->projectDir], \JSON_THROW_ON_ERROR), | |
| 107 | + 'TMPDIR' => $this->dir.'/tmp', | |
| 108 | + 'PATH' => getenv('PATH') ?: '', | |
| 109 | + ], | |
| 110 | + ); | |
| 111 | + self::assertIsResource($process); | |
| 112 | + fwrite($pipes[0], json_encode($events, \JSON_THROW_ON_ERROR)); | |
| 113 | + fclose($pipes[0]); | |
| 114 | + $output = (string) stream_get_contents($pipes[1]); | |
| 115 | + $errors = (string) stream_get_contents($pipes[2]); | |
| 116 | + $exitCode = proc_close($process); | |
| 117 | + | |
| 118 | + self::assertSame(0, $exitCode, $output.$errors); | |
| 119 | + $responses = json_decode($output, true, 512, \JSON_THROW_ON_ERROR); | |
| 120 | + self::assertIsArray($responses, $errors); | |
| 121 | + | |
| 122 | + return array_map(static function (mixed $response): array { | |
| 123 | + self::assertIsArray($response); | |
| 124 | + | |
| 125 | + return $response; | |
| 126 | + }, $responses); | |
| 127 | + } | |
| 128 | + | |
| 129 | + /** | |
| 130 | + * @return array<mixed> | |
| 131 | + */ | |
| 132 | + private function json(mixed $response): array | |
| 133 | + { | |
| 134 | + self::assertIsArray($response); | |
| 135 | + self::assertIsString($response['body']); | |
| 136 | + $data = json_decode($response['body'], true, 512, \JSON_THROW_ON_ERROR); | |
| 137 | + self::assertIsArray($data); | |
| 138 | + | |
| 139 | + return $data; | |
| 140 | + } | |
| 141 | +} | |
| new file mode 100644 | |||
| @@ -0,0 +1,141 @@ | |||
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +declare(strict_types=1); | ||
| 4 | + | ||
| 5 | +namespace JDecool\ScalewayRuntime\Tests; | ||
| 6 | + | ||
| 7 | +use PHPUnit\Framework\TestCase; | ||
| 8 | +use Symfony\Component\Filesystem\Filesystem; | ||
| 9 | + | ||
| 10 | +/** | ||
| 11 | + * Runs a real MicroKernelTrait Kernel through the committed handler stub, in a separate PHP process | ||
| 12 | + * that loads handler.php once and calls handle() per Event, as Scaleway does. | ||
| 13 | + */ | ||
| 14 | +final class IntegrationTest extends TestCase | ||
| 15 | +{ | ||
| 16 | + private Filesystem $filesystem; | ||
| 17 | + private string $dir; | ||
| 18 | + private string $projectDir; | ||
| 19 | + | ||
| 20 | + protected function setUp(): void | ||
| 21 | + { | ||
| 22 | + $this->filesystem = new Filesystem(); | ||
| 23 | + $this->dir = sys_get_temp_dir().'/scaleway-runtime-'.bin2hex(random_bytes(4)); | ||
| 24 | + $this->projectDir = $this->dir.'/project'; | ||
| 25 | + | ||
| 26 | + $this->filesystem->mirror(__DIR__.'/Fixtures/App', $this->projectDir); | ||
| 27 | + $this->filesystem->copy(__DIR__.'/../resources/handler.php', $this->projectDir.'/handler.php'); | ||
| 28 | + $this->filesystem->dumpFile( | ||
| 29 | + $this->projectDir.'/vendor/autoload.php', | ||
| 30 | + '<?php return require '.var_export(\dirname(__DIR__).'/vendor/autoload.php', true).';', | ||
| 31 | + ); | ||
| 32 | + $this->filesystem->mkdir($this->dir.'/tmp'); | ||
| 33 | + } | ||
| 34 | + | ||
| 35 | + protected function tearDown(): void | ||
| 36 | + { | ||
| 37 | + $this->filesystem->chmod($this->dir, 0o777, 0o000, true); | ||
| 38 | + $this->filesystem->remove($this->dir); | ||
| 39 | + } | ||
| 40 | + | ||
| 41 | + public function testConsecutiveInvocationsReuseTheKernelAndResetServices(): void | ||
| 42 | + { | ||
| 43 | + [$first, $second] = $this->invoke([ | ||
| 44 | + [ | ||
| 45 | + 'path' => '/state', | ||
| 46 | + 'method' => 'GET', | ||
| 47 | + 'queryStringParameters' => ['page' => '2'], | ||
| 48 | + 'headers' => ['Host' => 'internal.local', 'X-Forwarded-Proto' => 'https', 'X-Forwarded-For' => '203.0.113.7'], | ||
| 49 | + 'body' => '', | ||
| 50 | + 'isBase64Encoded' => false, | ||
| 51 | + ], | ||
| 52 | + ['path' => '/state', 'method' => 'GET', 'headers' => ['Host' => 'internal.local']], | ||
| 53 | + ]); | ||
| 54 | + | ||
| 55 | + self::assertSame(200, $first['statusCode']); | ||
| 56 | + self::assertSame(200, $second['statusCode']); | ||
| 57 | + $first = $this->json($first); | ||
| 58 | + $second = $this->json($second); | ||
| 59 | + | ||
| 60 | + self::assertSame(1, $first['count']); | ||
| 61 | + self::assertSame(1, $second['count'], 'The Counter service must be reset between Invocations.'); | ||
| 62 | + self::assertSame(1, $second['kernelInstances'], 'The Kernel must be reused across Invocations.'); | ||
| 63 | + self::assertSame($first['pid'], $second['pid']); | ||
| 64 | + self::assertTrue($first['secure']); | ||
| 65 | + self::assertSame('203.0.113.7', $first['clientIp']); | ||
| 66 | + self::assertSame(['page' => '2'], $first['query']); | ||
| 67 | + self::assertSame($this->projectDir.'/var/cache/prod', $first['cacheDir']); | ||
| 68 | + } | ||
| 69 | + | ||
| 70 | + public function testUnknownRouteIsHandledByTheKernel(): void | ||
| 71 | + { | ||
| 72 | + [$response] = $this->invoke([['path' => '/missing', 'method' => 'GET']]); | ||
| 73 | + | ||
| 74 | + self::assertSame(404, $response['statusCode']); | ||
| 75 | + } | ||
| 76 | + | ||
| 77 | + public function testReadOnlyProjectUsesTheWarmedBuildDirAndATempCacheDir(): void | ||
| 78 | + { | ||
| 79 | + $this->invoke([['path' => '/state']]); | ||
| 80 | + $this->filesystem->chmod($this->projectDir, 0o555, 0o000, true); | ||
| 81 | + | ||
| 82 | + [$response] = $this->invoke([['path' => '/state']]); | ||
| 83 | + | ||
| 84 | + self::assertSame(200, $response['statusCode']); | ||
| 85 | + $state = $this->json($response); | ||
| 86 | + self::assertSame($this->dir.'/tmp/cache/prod', $state['cacheDir']); | ||
| 87 | + self::assertSame($this->projectDir.'/var/cache/prod', $state['buildDir']); | ||
| 88 | + } | ||
| 89 | + | ||
| 90 | + /** | ||
| 91 | + * @param list<array<string, mixed>> $events | ||
| 92 | + * | ||
| 93 | + * @return array<array<mixed>> | ||
| 94 | + */ | ||
| 95 | + private function invoke(array $events): array | ||
| 96 | + { | ||
| 97 | + $driver = 'require $argv[1]; echo json_encode(array_map(fn ($event) => handle($event, []), json_decode(stream_get_contents(STDIN), true)));'; | ||
| 98 | + $process = proc_open( | ||
| 99 | + [\PHP_BINARY, '-r', $driver, $this->projectDir.'/handler.php'], | ||
| 100 | + [0 => ['pipe', 'r'], 1 => ['pipe', 'w'], 2 => ['pipe', 'w']], | ||
| 101 | + $pipes, | ||
| 102 | + null, | ||
| 103 | + [ | ||
| 104 | + 'APP_ENV' => 'prod', | ||
| 105 | + 'APP_DEBUG' => '0', | ||
| 106 | + 'APP_RUNTIME_OPTIONS' => json_encode(['project_dir' => $this->projectDir], \JSON_THROW_ON_ERROR), | ||
| 107 | + 'TMPDIR' => $this->dir.'/tmp', | ||
| 108 | + 'PATH' => getenv('PATH') ?: '', | ||
| 109 | + ], | ||
| 110 | + ); | ||
| 111 | + self::assertIsResource($process); | ||
| 112 | + fwrite($pipes[0], json_encode($events, \JSON_THROW_ON_ERROR)); | ||
| 113 | + fclose($pipes[0]); | ||
| 114 | + $output = (string) stream_get_contents($pipes[1]); | ||
| 115 | + $errors = (string) stream_get_contents($pipes[2]); | ||
| 116 | + $exitCode = proc_close($process); | ||
| 117 | + | ||
| 118 | + self::assertSame(0, $exitCode, $output.$errors); | ||
| 119 | + $responses = json_decode($output, true, 512, \JSON_THROW_ON_ERROR); | ||
| 120 | + self::assertIsArray($responses, $errors); | ||
| 121 | + | ||
| 122 | + return array_map(static function (mixed $response): array { | ||
| 123 | + self::assertIsArray($response); | ||
| 124 | + | ||
| 125 | + return $response; | ||
| 126 | + }, $responses); | ||
| 127 | + } | ||
| 128 | + | ||
| 129 | + /** | ||
| 130 | + * @return array<mixed> | ||
| 131 | + */ | ||
| 132 | + private function json(mixed $response): array | ||
| 133 | + { | ||
| 134 | + self::assertIsArray($response); | ||
| 135 | + self::assertIsString($response['body']); | ||
| 136 | + $data = json_decode($response['body'], true, 512, \JSON_THROW_ON_ERROR); | ||
| 137 | + self::assertIsArray($data); | ||
| 138 | + | ||
| 139 | + return $data; | ||
| 140 | + } | ||
| 141 | +} | ||