databasePath = sys_get_temp_dir() . '/todo-test-' . uniqid() . '.sqlite'; putenv('DATABASE_PATH=' . $this->databasePath); $_ENV['DATABASE_PATH'] = $this->databasePath; $pdo = new PDO('sqlite:' . $this->databasePath); $pdo->exec('PRAGMA foreign_keys = ON'); foreach (glob(dirname(__DIR__) . '/migrations/*.sql') ?: [] as $migration) { $pdo->exec((string) file_get_contents($migration)); } $this->app = require dirname(__DIR__) . '/src/bootstrap.php'; } protected function tearDown(): void { @unlink($this->databasePath); putenv('DATABASE_PATH'); unset($_ENV['DATABASE_PATH']); } /** * @param array|null $body * @param array $headers */ protected function request( string $method, string $path, ?array $body = null, array $headers = [], ): ResponseInterface { $request = (new ServerRequestFactory())->createServerRequest($method, $path); foreach ($headers as $name => $value) { $request = $request->withHeader($name, $value); } if ($body !== null) { $request = $request->withParsedBody($body)->withHeader('Content-Type', 'application/json'); } return $this->app->handle($request); } /** * Register a fresh user and return an `Authorization` header for them. * * @return array */ protected function authHeader(string $email = 'user@example.com'): array { $token = $this->decode( $this->request('POST', '/api/auth/register', ['email' => $email, 'password' => 'password123']), )['token']; return ['Authorization' => 'Bearer ' . $token]; } /** * @return array */ protected function decode(ResponseInterface $response): array { return (array) json_decode((string) $response->getBody(), true, 512, JSON_THROW_ON_ERROR); } }