$provider * @psalm-param class-string $expectedResponse */ #[DataProvider('providerForTestCreateResponse')] public function testCreateResponse(string $provider, string $expectedResponse): void { $this->skipIfNotAvailable($provider); $responseFactory = new ResponseFactory(new $provider()); $response = $responseFactory->createResponse(StatusCodeInterface::STATUS_NOT_FOUND, 'Not Found'); $this->assertSame(StatusCodeInterface::STATUS_NOT_FOUND, $response->getStatusCode()); $this->assertSame('Not Found', $response->getReasonPhrase()); $actual = (new ReflectionProperty(Response::class, 'response'))->getValue($response); $this->assertInstanceOf($expectedResponse, $actual); } /** @psalm-return iterable, class-string}> */ public static function providerForTestCreateResponse(): iterable { yield 'slim/psr7' => [SlimResponseFactory::class, \Slim\Psr7\Response::class]; yield 'laminas/laminas-diactoros' => [LaminasResponseFactory::class, \Laminas\Diactoros\Response::class]; yield 'nyholm/psr7' => [Psr17Factory::class, \Nyholm\Psr7\Response::class]; yield 'guzzlehttp/psr7' => [HttpFactory::class, \GuzzleHttp\Psr7\Response::class]; yield 'httpsoft/http-message' => [HttpSoftResponseFactory::class, \HttpSoft\Message\Response::class]; } /** @psalm-param class-string $provider */ #[DataProvider('providerForTestCreate')] #[BackupStaticProperties(true)] public function testCreate(string $provider): void { $this->skipIfNotAvailable($provider); (new ReflectionProperty(ResponseFactory::class, 'providers'))->setValue(null, [$provider]); $responseFactory = ResponseFactory::create(); $actual = (new ReflectionProperty(ResponseFactory::class, 'responseFactory'))->getValue($responseFactory); $this->assertInstanceOf($provider, $actual); } /** @psalm-return iterable}> */ public static function providerForTestCreate(): iterable { yield 'slim/psr7' => [SlimResponseFactory::class]; yield 'laminas/laminas-diactoros' => [LaminasResponseFactory::class]; yield 'nyholm/psr7' => [Psr17Factory::class]; yield 'guzzlehttp/psr7' => [HttpFactory::class]; yield 'httpsoft/http-message' => [HttpSoftResponseFactory::class]; } #[BackupStaticProperties(true)] public function testCreateWithoutProvider(): void { (new ReflectionProperty(ResponseFactory::class, 'providers'))->setValue(null, ['InvalidResponseFactoryClass']); $this->expectException(RuntimeException::class); $this->expectExceptionMessage('No HTTP response factories found.'); ResponseFactory::create(); } /** @psalm-param class-string $provider */ private function skipIfNotAvailable(string $provider): void { if (class_exists($provider)) { return; } // This can happen when testing without the development packages. $this->markTestSkipped($provider . ' is not available.'); } }