From 4cc0c79e6d453cf7f8a045f66de23bc823ca6512 Mon Sep 17 00:00:00 2001 From: Darius Matulionis Date: Tue, 3 Sep 2024 09:19:53 +0300 Subject: [PATCH] update tests --- tests/RoutesTest.php | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/tests/RoutesTest.php b/tests/RoutesTest.php index 61f383f..ebcf7c0 100644 --- a/tests/RoutesTest.php +++ b/tests/RoutesTest.php @@ -10,8 +10,10 @@ use L5Swagger\Http\Controllers\SwaggerController; use L5Swagger\Http\Middleware\Config; use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\TestDox; use PHPUnit\Framework\MockObject\Exception; +use PHPUnit\Framework\MockObject\MockObject; /** * @covers \L5Swagger\Http\Controllers\SwaggerController @@ -179,12 +181,15 @@ public function testUserCanAccessDocumentationInterface(): void /** * @throws L5SwaggerException */ - public function testUserCanAccessDocumentationInterfaceAndConfigureProxy(): void - { + #[DataProvider('provideProxies')] + public function testUserCanAccessDocumentationInterfaceAndConfigureProxy( + mixed $proxy, + array $expectedProxies + ): void { config(['l5-swagger' => [ 'default' => 'default', 'documentations' => config('l5-swagger.documentations'), - 'defaults' => array_merge(config('l5-swagger.defaults'), ['proxy' => ['foo', 'bar']]), + 'defaults' => array_merge(config('l5-swagger.defaults'), ['proxy' => $proxy]), ]]); $config = $this->configFactory->documentationConfig(); @@ -195,18 +200,14 @@ public function testUserCanAccessDocumentationInterfaceAndConfigureProxy(): void ->assertSee(route('l5-swagger.default.oauth2_callback')) ->isOk(); - $this->assertSame(['foo', 'bar'], Request::getTrustedProxies()); - - config(['l5-swagger' => [ - 'default' => 'default', - 'documentations' => config('l5-swagger.documentations'), - 'defaults' => array_merge(config('l5-swagger.defaults'), ['proxy' => 'baz']), - ]]); - - $this->get($config['routes']['api']) - ->isOk(); + $this->assertSame($expectedProxies, Request::getTrustedProxies()); + } - $this->assertSame(['baz'], Request::getTrustedProxies()); + public static function provideProxies(): \Generator + { + yield 'proxies array' => ['proxy' => ['foo', 'bar'], 'expectedProxies' => ['foo', 'bar']]; + yield 'proxy as string' => ['proxy' => 'baz', 'expectedProxies' => ['baz']]; + yield 'proxy is null' => ['proxy' => null, 'expectedProxies' => []]; } /** @@ -273,13 +274,15 @@ public function testItWillReturn404WhenDocGenerationFails(): void ]]); $mockGenerator = $this->mockGenerator(); - $mockGenerator->expects($this->once())->method('generateDocs')->willThrowException(new \Exception); + $mockGenerator->expects($this->once())->method('generateDocs')->willThrowException(new L5SwaggerException()); $this->get($jsonUrl)->assertNotFound(); } /** * @throws Exception + * + * @return Generator&MockObject */ private function mockGenerator(): Generator {