From 6ecf1e6543a2a97a9062bbfb1083a70d0b9092eb Mon Sep 17 00:00:00 2001 From: Darius Matulionis Date: Wed, 13 Mar 2024 16:49:32 +0200 Subject: [PATCH 01/27] WIP --- composer.json | 3 ++- tests/TestCase.php | 30 ++++++------------------------ 2 files changed, 8 insertions(+), 25 deletions(-) diff --git a/composer.json b/composer.json index 41c53fc..e383687 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,8 @@ "phpunit/phpunit": "^11.0 || ^10.0 || ^9.5", "mockery/mockery": "1.*", "orchestra/testbench": "^9.0 || ^8.0 || 7.* || ^6.15 || 5.*", - "php-coveralls/php-coveralls": "^2.0" + "php-coveralls/php-coveralls": "^2.0", + "rector/rector": "^1.0" }, "autoload": { "psr-4": { diff --git a/tests/TestCase.php b/tests/TestCase.php index 6625405..6bf3cf7 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -9,44 +9,26 @@ use L5Swagger\Generator; use L5Swagger\L5SwaggerServiceProvider; use Orchestra\Testbench\TestCase as OrchestraTestCase; +use PHPUnit\Framework\Attributes\Before; use PHPUnit\Framework\MockObject\MockObject; use ReflectionObject; class TestCase extends OrchestraTestCase { - /** - * @var ConfigFactory - */ - protected $configFactory; + protected ConfigFactory $configFactory; - /** - * @var array - */ - protected $defaultConfig; + protected array $defaultConfig; - /** - * @var Generator - */ - protected $generator; + protected Generator $generator; - /** - * @var MockObject|Filesystem - */ - protected $fileSystem; + protected Filesystem|MockObject $fileSystem; - /** - * @before - * - * @return void - */ + #[Before] public function setUpFileSystem(): void { $this->fileSystem = $this->createMock(Filesystem::class); } - /** - * @return void - */ public function setUp(): void { parent::setUp(); From 00905258435800d9964dc6d457134510bf9e87e6 Mon Sep 17 00:00:00 2001 From: Darius Matulionis Date: Wed, 13 Mar 2024 16:49:41 +0200 Subject: [PATCH 02/27] WIP --- rector.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 rector.php diff --git a/rector.php b/rector.php new file mode 100644 index 0000000..09f0b6f --- /dev/null +++ b/rector.php @@ -0,0 +1,19 @@ +withRules([ +// \Rector\DeadCode\Rector\Property\RemoveUselessVarTagRector::class, + \Rector\PHPUnit\AnnotationsToAttributes\Rector\Class_\AnnotationWithValueToAttributeRector::class, + \Rector\PHPUnit\AnnotationsToAttributes\Rector\ClassMethod\DataProviderAnnotationToAttributeRector::class, + \Rector\PHPUnit\AnnotationsToAttributes\Rector\ClassMethod\TestWithAnnotationToAttributeRector::class, + \Rector\PHPUnit\AnnotationsToAttributes\Rector\Class_\CoversAnnotationWithValueToAttributeRector::class + ]) + // here we can define, what prepared sets of rules will be applied + ->withPreparedSets( + deadCode: true, + codeQuality: true + ); From c0df3792dc7490aafa476007f9968f3c23496241 Mon Sep 17 00:00:00 2001 From: Darius Matulionis Date: Wed, 13 Mar 2024 17:26:04 +0200 Subject: [PATCH 03/27] WIP --- composer.json | 3 +- rector.php | 19 ----- tests/ConfigFactoryTest.php | 122 ++++++++++++++---------------- tests/ConsoleTest.php | 11 +-- tests/GeneratorTest.php | 50 +++++------- tests/HelpersTest.php | 3 +- tests/RoutesTest.php | 51 +++++-------- tests/SecurityDefinitionsTest.php | 13 +--- tests/TestCase.php | 10 --- 9 files changed, 98 insertions(+), 184 deletions(-) delete mode 100644 rector.php diff --git a/composer.json b/composer.json index e383687..41c53fc 100644 --- a/composer.json +++ b/composer.json @@ -31,8 +31,7 @@ "phpunit/phpunit": "^11.0 || ^10.0 || ^9.5", "mockery/mockery": "1.*", "orchestra/testbench": "^9.0 || ^8.0 || 7.* || ^6.15 || 5.*", - "php-coveralls/php-coveralls": "^2.0", - "rector/rector": "^1.0" + "php-coveralls/php-coveralls": "^2.0" }, "autoload": { "psr-4": { diff --git a/rector.php b/rector.php deleted file mode 100644 index 09f0b6f..0000000 --- a/rector.php +++ /dev/null @@ -1,19 +0,0 @@ -withRules([ -// \Rector\DeadCode\Rector\Property\RemoveUselessVarTagRector::class, - \Rector\PHPUnit\AnnotationsToAttributes\Rector\Class_\AnnotationWithValueToAttributeRector::class, - \Rector\PHPUnit\AnnotationsToAttributes\Rector\ClassMethod\DataProviderAnnotationToAttributeRector::class, - \Rector\PHPUnit\AnnotationsToAttributes\Rector\ClassMethod\TestWithAnnotationToAttributeRector::class, - \Rector\PHPUnit\AnnotationsToAttributes\Rector\Class_\CoversAnnotationWithValueToAttributeRector::class - ]) - // here we can define, what prepared sets of rules will be applied - ->withPreparedSets( - deadCode: true, - codeQuality: true - ); diff --git a/tests/ConfigFactoryTest.php b/tests/ConfigFactoryTest.php index c6ded03..d06d85b 100644 --- a/tests/ConfigFactoryTest.php +++ b/tests/ConfigFactoryTest.php @@ -9,10 +9,7 @@ */ class ConfigFactoryTest extends TestCase { - /** - * @test - */ - public function itThrowsExceptionIfDocumentationConfigNotFound(): void + public function testItThrowsExceptionIfDocumentationConfigNotFound(): void { $config = config('l5-swagger'); unset($config['documentations']['default']); @@ -25,16 +22,12 @@ public function itThrowsExceptionIfDocumentationConfigNotFound(): void } /** - * @test * * @dataProvider configDataProvider * - * @param array $data - * @param array $assert - * * @throws L5SwaggerException */ - public function canMergeConfigurationDeep(array $data, array $assert): void + public function testCanMergeConfigurationDeep(array $data, array $assert): void { config(['l5-swagger' => array_merge( $data, @@ -58,72 +51,70 @@ public function canMergeConfigurationDeep(array $data, array $assert): void $this->assertArraySimilar($config, $assert); } - public static function configDataProvider(): array + public static function configDataProvider(): \Iterator { - return [ + yield [ [ - [ - 'default' => 'v2', - 'documentations' => [ - 'v2' => [ - 'api' => [ - 'title' => 'Api V2', - ], - 'paths' => [ - 'docs_json' => 'api-v2.json', - ], - 'proxy' => true, + 'default' => 'v2', + 'documentations' => [ + 'v2' => [ + 'api' => [ + 'title' => 'Api V2', ], + 'paths' => [ + 'docs_json' => 'api-v2.json', + ], + 'proxy' => true, ], ], - [ - 'api' => [ - 'title' => 'Api V2', - ], - 'routes' => [ - 'api' => 'api/documentation', - 'docs' => 'docs', - ], - 'paths' => [ - 'docs_json' => 'api-v2.json', - 'docs' => 'docs/', - 'docs_yaml' => 'docs.yaml', - ], - 'proxy' => true, + ], + [ + 'api' => [ + 'title' => 'Api V2', + ], + 'routes' => [ + 'api' => 'api/documentation', + 'docs' => 'docs', + ], + 'paths' => [ + 'docs_json' => 'api-v2.json', + 'docs' => 'docs/', + 'docs_yaml' => 'docs.yaml', ], + 'proxy' => true, ], + ]; + yield [ [ - [ - 'default' => 'v1', - 'documentations' => [ - 'v1' => [ - 'api' => [ - 'title' => 'Api V1', - ], - 'routes' => [ - 'api' => 'api/v1', - ], - 'paths' => [ - 'docs_json' => 'api-v1.json', - ], + 'default' => 'v1', + 'documentations' => [ + 'v1' => [ + 'api' => [ + 'title' => 'Api V1', + ], + 'routes' => [ + 'api' => 'api/v1', + ], + 'paths' => [ + 'docs_json' => 'api-v1.json', ], ], ], - [ - 'api' => [ - 'title' => 'Api V1', - ], - 'routes' => [ - 'api' => 'api/v1', - 'docs' => 'docs', - ], - 'paths' => [ - 'docs_json' => 'api-v1.json', - 'docs' => 'docs/', - 'docs_yaml' => 'docs.yaml', - ], - 'proxy' => false, + ], + [ + 'api' => [ + 'title' => 'Api V1', + ], + 'routes' => [ + 'api' => 'api/v1', + 'docs' => 'docs', ], + 'paths' => [ + 'docs_json' => 'api-v1.json', + 'docs' => 'docs/', + 'docs_yaml' => 'docs.yaml', + ], + 'proxy' => false, ], ]; } @@ -133,13 +124,10 @@ public static function configDataProvider(): array * * Both arrays must have the same indexes with identical values * without respect to key ordering - * - * @param array $expected - * @param array $array */ protected function assertArraySimilar(array $expected, array $array) { - $this->assertTrue(count(array_diff_key($array, $expected)) === 0); + $this->assertSame([], array_diff_key($array, $expected)); foreach ($expected as $key => $value) { if (is_array($value)) { diff --git a/tests/ConsoleTest.php b/tests/ConsoleTest.php index ba8dd7e..18b4bd5 100644 --- a/tests/ConsoleTest.php +++ b/tests/ConsoleTest.php @@ -13,16 +13,14 @@ class ConsoleTest extends TestCase { /** - * @test * * @dataProvider provideGenerateCommands * - * @param string $artisanCommand * * @throws L5SwaggerException * @throws FileNotFoundException */ - public function canGenerate(string $artisanCommand): void + public function testCanGenerate(string $artisanCommand): void { $fileSystem = new Filesystem(); @@ -38,9 +36,6 @@ public function canGenerate(string $artisanCommand): void $this->assertStringContainsString('L5 Swagger', $fileContent); } - /** - * @return iterable - */ public static function provideGenerateCommands(): iterable { yield 'default' => [ @@ -52,11 +47,9 @@ public static function provideGenerateCommands(): iterable } /** - * @test - * * @throws L5SwaggerException */ - public function canPublish(): void + public function testCanPublish(): void { $fileSystem = new Filesystem(); Artisan::call('vendor:publish', ['--provider' => 'L5Swagger\L5SwaggerServiceProvider']); diff --git a/tests/GeneratorTest.php b/tests/GeneratorTest.php index 3b4bcce..bacac4e 100644 --- a/tests/GeneratorTest.php +++ b/tests/GeneratorTest.php @@ -14,8 +14,7 @@ */ class GeneratorTest extends TestCase { - /** @test **/ - public function itThrowsExceptionIfDocumentationDirIsNotWritable(): void + public function testItThrowsExceptionIfDocumentationDirIsNotWritable(): void { $this->setAnnotationsPath(); @@ -41,8 +40,7 @@ public function itThrowsExceptionIfDocumentationDirIsNotWritable(): void $this->generator->generateDocs(); } - /** @test **/ - public function itWillCreateDocumentationDirIfItIsWritable(): void + public function testItWillCreateDocumentationDirIfItIsWritable(): void { $this->setAnnotationsPath(); @@ -72,8 +70,7 @@ public function itWillCreateDocumentationDirIfItIsWritable(): void $this->generator->generateDocs(); } - /** @test **/ - public function itThrowsExceptionIfDocumentationDirWasNotCreated(): void + public function testItThrowsExceptionIfDocumentationDirWasNotCreated(): void { $this->setAnnotationsPath(); @@ -104,15 +101,14 @@ public function itThrowsExceptionIfDocumentationDirWasNotCreated(): void $this->generator->generateDocs(); } - /** @test */ - public function canGenerateApiJsonFile(): void + public function testCanGenerateApiJsonFile(): void { $this->setAnnotationsPath(); $this->generator->generateDocs(); - $this->assertTrue(file_exists($this->jsonDocsFile())); - $this->assertTrue(file_exists($this->yamlDocsFile())); + $this->assertFileExists($this->jsonDocsFile()); + $this->assertFileExists($this->yamlDocsFile()); $this->get(route('l5-swagger.default.docs')) ->assertSee('L5 Swagger') @@ -133,8 +129,7 @@ public function canGenerateApiJsonFile(): void ->assertStatus(200); } - /** @test */ - public function canGenerateWithLegacyExcludedDirectories(): void + public function testCanGenerateWithLegacyExcludedDirectories(): void { $this->setAnnotationsPath(); @@ -152,7 +147,7 @@ public function canGenerateWithLegacyExcludedDirectories(): void $this->generator->generateDocs(); - $this->assertTrue(file_exists($this->jsonDocsFile())); + $this->assertFileExists($this->jsonDocsFile()); $this->get(route('l5-swagger.default.docs')) ->assertSee('L5 Swagger') @@ -163,8 +158,7 @@ public function canGenerateWithLegacyExcludedDirectories(): void ->assertStatus(200); } - /** @test */ - public function canGenerateWithScanOptions(): void + public function testCanGenerateWithScanOptions(): void { $cfg = config('l5-swagger.documentations.default'); @@ -191,7 +185,7 @@ public function canGenerateWithScanOptions(): void $this->generator->generateDocs(); - $this->assertTrue(file_exists($this->jsonDocsFile())); + $this->assertFileExists($this->jsonDocsFile()); $response = $this->get(route('l5-swagger.default.docs')); @@ -204,8 +198,7 @@ public function canGenerateWithScanOptions(): void ->assertStatus(200); } - /** @test */ - public function canGenerateApiJsonFileWithChangedBaseServer(): void + public function testCanGenerateApiJsonFileWithChangedBaseServer(): void { $this->setAnnotationsPath(); @@ -221,7 +214,7 @@ public function canGenerateApiJsonFileWithChangedBaseServer(): void $this->generator->generateDocs(); - $this->assertTrue(file_exists($this->jsonDocsFile())); + $this->assertFileExists($this->jsonDocsFile()); $this->get(route('l5-swagger.default.docs')) ->assertSee('https://test-server.url') @@ -238,8 +231,7 @@ public function canGenerateApiJsonFileWithChangedBaseServer(): void ->assertStatus(200); } - /** @test */ - public function canSetProxy(): void + public function testCanSetProxy(): void { $this->setAnnotationsPath(); @@ -257,17 +249,16 @@ public function canSetProxy(): void $this->get(route('l5-swagger.default.api')) ->assertStatus(200); - $this->assertEquals(Request::getTrustedProxies()[0], $proxy); + $this->assertSame(Request::getTrustedProxies()[0], $proxy); $this->get(route('l5-swagger.default.docs')) ->assertStatus(200); - $this->assertTrue(file_exists($this->jsonDocsFile())); - $this->assertTrue(file_exists($this->yamlDocsFile())); + $this->assertFileExists($this->jsonDocsFile()); + $this->assertFileExists($this->yamlDocsFile()); } - /** @test */ - public function canSetValidatorUrl(): void + public function testCanSetValidatorUrl(): void { $this->setAnnotationsPath(); @@ -294,12 +285,11 @@ public function canSetValidatorUrl(): void $this->get(route('l5-swagger.default.docs')) ->assertStatus(200); - $this->assertTrue(file_exists($this->jsonDocsFile())); - $this->assertTrue(file_exists($this->yamlDocsFile())); + $this->assertFileExists($this->jsonDocsFile()); + $this->assertFileExists($this->yamlDocsFile()); } - /** @test */ - public function canAppropriateYamlType(): void + public function testCanAppropriateYamlType(): void { $this->setAnnotationsPath(); diff --git a/tests/HelpersTest.php b/tests/HelpersTest.php index a410040..d7274ea 100644 --- a/tests/HelpersTest.php +++ b/tests/HelpersTest.php @@ -9,8 +9,7 @@ */ class HelpersTest extends TestCase { - /** @test */ - public function assetFunctionThrowsExceptionIfFileDoesNotExists(): void + public function testAssetFunctionThrowsExceptionIfFileDoesNotExists(): void { $this->expectException(L5SwaggerException::class); diff --git a/tests/RoutesTest.php b/tests/RoutesTest.php index 3ab3b10..0d61839 100644 --- a/tests/RoutesTest.php +++ b/tests/RoutesTest.php @@ -12,8 +12,7 @@ */ class RoutesTest extends TestCase { - /** @test */ - public function userCantAccessJsonFileIfItIsNotGenerated(): void + public function testUserCantAccessJsonFileIfItIsNotGenerated(): void { $jsonUrl = route('l5-swagger.default.docs'); @@ -21,8 +20,7 @@ public function userCantAccessJsonFileIfItIsNotGenerated(): void $this->assertTrue($response->isNotFound()); } - /** @test */ - public function userCanAccessJsonFileIfItIsGenerated(): void + public function testUserCanAccessJsonFileIfItIsGenerated(): void { $jsonUrl = route('l5-swagger.default.docs'); config(['l5-swagger' => [ @@ -39,8 +37,7 @@ public function userCanAccessJsonFileIfItIsGenerated(): void ->isOk(); } - /** @test */ - public function userCanAccessAndGenerateCustomJsonFile(): void + public function testUserCanAccessAndGenerateCustomJsonFile(): void { $customJsonFileName = 'docs.v1.json'; @@ -62,8 +59,7 @@ public function userCanAccessAndGenerateCustomJsonFile(): void ->isOk(); } - /** @test */ - public function userCanAccessAndGenerateYamlFile(): void + public function testUserCanAccessAndGenerateYamlFile(): void { $customYamlFileName = 'docs.yaml'; @@ -84,8 +80,7 @@ public function userCanAccessAndGenerateYamlFile(): void ->isOk(); } - /** @test */ - public function itCanAccessAndGenerateYamlFile(): void + public function testItCanAccessAndGenerateYamlFile(): void { $customYamlFileName = 'docs.yaml'; @@ -106,8 +101,7 @@ public function itCanAccessAndGenerateYamlFile(): void ->isOk(); } - /** @test */ - public function userCanAccessDocumentationFileWithoutExtensionIfItExists(): void + public function testUserCanAccessDocumentationFileWithoutExtensionIfItExists(): void { $customYamlFileName = 'docs-file-without-extension'; @@ -128,8 +122,7 @@ public function userCanAccessDocumentationFileWithoutExtensionIfItExists(): void ->isOk(); } - /** @test */ - public function itDoesNotThrowExceptionOnDocsFileWithoutExtension(): void + public function testItDoesNotThrowExceptionOnDocsFileWithoutExtension(): void { $fileWithoutExtension = 'docs'; @@ -143,11 +136,9 @@ public function itDoesNotThrowExceptionOnDocsFileWithoutExtension(): void } /** - * @test - * * @throws L5SwaggerException */ - public function userCanAccessDocumentationInterface(): void + public function testUserCanAccessDocumentationInterface(): void { $config = $this->configFactory->documentationConfig(); $jsonFile = $config['paths']['docs_json'] ?? 'api-docs.json'; @@ -159,19 +150,16 @@ public function userCanAccessDocumentationInterface(): void } /** - * @test - * * @throws L5SwaggerException */ - public function itCanServeAssets(): void + public function testItCanServeAssets(): void { $this->get(l5_swagger_asset('default', 'swagger-ui.css')) ->assertSee('.swagger-ui') ->isOk(); } - /** @test */ - public function itWillThrowExceptionForIncorrectAsset(): void + public function testItWillThrowExceptionForIncorrectAsset(): void { $this->expectException(L5SwaggerException::class); $this->expectExceptionMessage('(bad-swagger-ui.css) - this L5 Swagger asset is not allowed'); @@ -179,15 +167,13 @@ public function itWillThrowExceptionForIncorrectAsset(): void l5_swagger_asset('default', 'bad-swagger-ui.css'); } - /** @test */ - public function itHandleBadAssetRequest(): void + public function testItHandleBadAssetRequest(): void { $this->get(route('l5-swagger.default.asset', 'file.css')) ->assertNotFound(); } - /** @test */ - public function userCanAccessOauth2Redirect(): void + public function testUserCanAccessOauth2Redirect(): void { $this->get(route('l5-swagger.default.oauth2_callback')) ->assertSee('swaggerUIRedirectOauth2') @@ -195,16 +181,14 @@ public function userCanAccessOauth2Redirect(): void ->isOk(); } - /** @test */ - public function itWillReturn404ForIncorrectJsonFile(): void + public function testItWillReturn404ForIncorrectJsonFile(): void { $jsonUrl = route('l5-swagger.default.docs', 'invalid.json'); $this->get($jsonUrl)->assertNotFound(); } - /** @test */ - public function itWillNotAttemptDocGenerationWhenAlwaysGenerateIsDisabled(): void + public function testItWillNotAttemptDocGenerationWhenAlwaysGenerateIsDisabled(): void { $jsonUrl = route('l5-swagger.default.docs', 'unknown_file.json'); config(['l5-swagger' => [ @@ -219,8 +203,7 @@ public function itWillNotAttemptDocGenerationWhenAlwaysGenerateIsDisabled(): voi $this->get($jsonUrl)->assertNotFound(); } - /** @test */ - public function itWillReturn404WhenDocGenerationFails(): void + public function testItWillReturn404WhenDocGenerationFails(): void { $jsonUrl = route('l5-swagger.default.docs', 'docs'); config(['l5-swagger' => [ @@ -230,7 +213,7 @@ public function itWillReturn404WhenDocGenerationFails(): void ]]); $mockGenerator = $this->mockGenerator(); - $mockGenerator->expects($this->once())->method('generateDocs')->will($this->throwException(new \Exception)); + $mockGenerator->expects($this->once())->method('generateDocs')->willThrowException(new \Exception); $this->get($jsonUrl)->assertNotFound(); } @@ -242,7 +225,7 @@ private function mockGenerator() { $mockGenerator = $this->createMock(Generator::class); $mockGeneratorFactory = $this->createMock(GeneratorFactory::class); - $mockGeneratorFactory->expects($this->any())->method('make')->willReturn($mockGenerator); + $mockGeneratorFactory->method('make')->willReturn($mockGenerator); app()->extend(GeneratorFactory::class, function () use ($mockGeneratorFactory) { return $mockGeneratorFactory; }); diff --git a/tests/SecurityDefinitionsTest.php b/tests/SecurityDefinitionsTest.php index c788a62..7a7244e 100644 --- a/tests/SecurityDefinitionsTest.php +++ b/tests/SecurityDefinitionsTest.php @@ -11,11 +11,9 @@ class SecurityDefinitionsTest extends TestCase { /** - * @test - * * @throws L5SwaggerException */ - public function itWillNotAddEmptySecurityItems(): void + public function testItWillNotAddEmptySecurityItems(): void { $fileSystem = new Filesystem(); @@ -51,16 +49,12 @@ public function itWillNotAddEmptySecurityItems(): void } /** - * @test * * @dataProvider provideConfigAndSchemes * - * @param array $securitySchemes - * @param array $security - * * @throws L5SwaggerException */ - public function canGenerateApiJsonFileWithSecurityDefinition( + public function testCanGenerateApiJsonFileWithSecurityDefinition( array $securitySchemes, array $security ): void { @@ -95,9 +89,6 @@ public function canGenerateApiJsonFileWithSecurityDefinition( ->isOk(); } - /** - * @return iterable - */ public static function provideConfigAndSchemes(): iterable { $securitySchemes = [ diff --git a/tests/TestCase.php b/tests/TestCase.php index 6bf3cf7..951df80 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -65,7 +65,6 @@ public function tearDown(): void /** * @param Application $app - * @return array */ protected function getPackageProviders($app): array { @@ -99,7 +98,6 @@ protected function createYamlDocumentationFile(): void /** * Get path for json docs file. * - * @return string * * @throws L5SwaggerException */ @@ -119,7 +117,6 @@ protected function jsonDocsFile(): string /** * Get path for yaml docs file. * - * @return string * * @throws L5SwaggerException */ @@ -176,9 +173,6 @@ protected function makeGenerator(): void $this->generator = $this->app->make(Generator::class); } - /** - * @return void - */ protected function makeGeneratorWithMockedFileSystem(): void { $this->generator = $this->app->make(Generator::class); @@ -190,10 +184,6 @@ protected function makeGeneratorWithMockedFileSystem(): void $reflectionProperty->setValue($this->generator, $this->fileSystem); } - /** - * @param string $fileName - * @param string $type - */ protected function setCustomDocsFileName(string $fileName, string $type = 'json'): void { $cfg = config('l5-swagger.documentations.default'); From a3049354d5df3a345d4cbea277481b69c1987f4b Mon Sep 17 00:00:00 2001 From: Darius Matulionis Date: Wed, 13 Mar 2024 17:36:05 +0200 Subject: [PATCH 04/27] WIP --- tests/ConfigFactoryTest.php | 12 +++++------ tests/ConsoleTest.php | 11 ++++------ tests/GeneratorTest.php | 35 ++++++++++++++++++++++++++++--- tests/HelpersTest.php | 5 ++--- tests/RoutesTest.php | 25 +++++++++++++++++----- tests/SecurityDefinitionsTest.php | 16 +++++++------- tests/TestCase.php | 9 +++++--- 7 files changed, 76 insertions(+), 37 deletions(-) diff --git a/tests/ConfigFactoryTest.php b/tests/ConfigFactoryTest.php index d06d85b..ac32c40 100644 --- a/tests/ConfigFactoryTest.php +++ b/tests/ConfigFactoryTest.php @@ -3,10 +3,10 @@ namespace Tests; use L5Swagger\Exceptions\L5SwaggerException; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\TestDox; -/** - * @testdox Configuration factory - */ +#[TestDox('Configuration factory')] class ConfigFactoryTest extends TestCase { public function testItThrowsExceptionIfDocumentationConfigNotFound(): void @@ -22,11 +22,9 @@ public function testItThrowsExceptionIfDocumentationConfigNotFound(): void } /** - * - * @dataProvider configDataProvider - * * @throws L5SwaggerException */ + #[DataProvider('configDataProvider')] public function testCanMergeConfigurationDeep(array $data, array $assert): void { config(['l5-swagger' => array_merge( @@ -125,7 +123,7 @@ public static function configDataProvider(): \Iterator * Both arrays must have the same indexes with identical values * without respect to key ordering */ - protected function assertArraySimilar(array $expected, array $array) + protected function assertArraySimilar(array $expected, array $array): void { $this->assertSame([], array_diff_key($array, $expected)); diff --git a/tests/ConsoleTest.php b/tests/ConsoleTest.php index 18b4bd5..df3b275 100644 --- a/tests/ConsoleTest.php +++ b/tests/ConsoleTest.php @@ -6,20 +6,17 @@ use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Facades\Artisan; use L5Swagger\Exceptions\L5SwaggerException; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\TestDox; -/** - * @testdox Console commands - */ +#[TestDox('Console commands')] class ConsoleTest extends TestCase { /** - * - * @dataProvider provideGenerateCommands - * - * * @throws L5SwaggerException * @throws FileNotFoundException */ + #[DataProvider('provideGenerateCommands')] public function testCanGenerate(string $artisanCommand): void { $fileSystem = new Filesystem(); diff --git a/tests/GeneratorTest.php b/tests/GeneratorTest.php index bacac4e..6b4c8b5 100644 --- a/tests/GeneratorTest.php +++ b/tests/GeneratorTest.php @@ -6,14 +6,16 @@ use L5Swagger\Exceptions\L5SwaggerException; use OpenApi\Analysers\TokenAnalyser; use OpenApi\Processors\CleanUnmerged; +use PHPUnit\Framework\Attributes\TestDox; use Symfony\Component\Yaml\Parser; use Symfony\Component\Yaml\Yaml; -/** - * @testdox Generator - */ +#[TestDox('Generator')] class GeneratorTest extends TestCase { + /** + * @throws L5SwaggerException + */ public function testItThrowsExceptionIfDocumentationDirIsNotWritable(): void { $this->setAnnotationsPath(); @@ -40,6 +42,9 @@ public function testItThrowsExceptionIfDocumentationDirIsNotWritable(): void $this->generator->generateDocs(); } + /** + * @throws L5SwaggerException + */ public function testItWillCreateDocumentationDirIfItIsWritable(): void { $this->setAnnotationsPath(); @@ -70,6 +75,9 @@ public function testItWillCreateDocumentationDirIfItIsWritable(): void $this->generator->generateDocs(); } + /** + * @throws L5SwaggerException + */ public function testItThrowsExceptionIfDocumentationDirWasNotCreated(): void { $this->setAnnotationsPath(); @@ -101,6 +109,9 @@ public function testItThrowsExceptionIfDocumentationDirWasNotCreated(): void $this->generator->generateDocs(); } + /** + * @throws L5SwaggerException + */ public function testCanGenerateApiJsonFile(): void { $this->setAnnotationsPath(); @@ -129,6 +140,9 @@ public function testCanGenerateApiJsonFile(): void ->assertStatus(200); } + /** + * @throws L5SwaggerException + */ public function testCanGenerateWithLegacyExcludedDirectories(): void { $this->setAnnotationsPath(); @@ -158,6 +172,9 @@ public function testCanGenerateWithLegacyExcludedDirectories(): void ->assertStatus(200); } + /** + * @throws L5SwaggerException + */ public function testCanGenerateWithScanOptions(): void { $cfg = config('l5-swagger.documentations.default'); @@ -198,6 +215,9 @@ public function testCanGenerateWithScanOptions(): void ->assertStatus(200); } + /** + * @throws L5SwaggerException + */ public function testCanGenerateApiJsonFileWithChangedBaseServer(): void { $this->setAnnotationsPath(); @@ -231,6 +251,9 @@ public function testCanGenerateApiJsonFileWithChangedBaseServer(): void ->assertStatus(200); } + /** + * @throws L5SwaggerException + */ public function testCanSetProxy(): void { $this->setAnnotationsPath(); @@ -258,6 +281,9 @@ public function testCanSetProxy(): void $this->assertFileExists($this->yamlDocsFile()); } + /** + * @throws L5SwaggerException + */ public function testCanSetValidatorUrl(): void { $this->setAnnotationsPath(); @@ -289,6 +315,9 @@ public function testCanSetValidatorUrl(): void $this->assertFileExists($this->yamlDocsFile()); } + /** + * @throws L5SwaggerException + */ public function testCanAppropriateYamlType(): void { $this->setAnnotationsPath(); diff --git a/tests/HelpersTest.php b/tests/HelpersTest.php index d7274ea..9abfe4d 100644 --- a/tests/HelpersTest.php +++ b/tests/HelpersTest.php @@ -3,10 +3,9 @@ namespace Tests; use L5Swagger\Exceptions\L5SwaggerException; +use PHPUnit\Framework\Attributes\TestDox; -/** - * @testdox Helpers - */ +#[TestDox('Helpers')] class HelpersTest extends TestCase { public function testAssetFunctionThrowsExceptionIfFileDoesNotExists(): void diff --git a/tests/RoutesTest.php b/tests/RoutesTest.php index 0d61839..1e59055 100644 --- a/tests/RoutesTest.php +++ b/tests/RoutesTest.php @@ -5,11 +5,11 @@ use L5Swagger\Exceptions\L5SwaggerException; use L5Swagger\Generator; use L5Swagger\GeneratorFactory; +use PHPUnit\Framework\Attributes\TestDox; +use PHPUnit\Framework\MockObject\Exception; use PHPUnit\Framework\MockObject\MockObject; -/** - * @testdox Routes - */ +#[TestDox('Routes')] class RoutesTest extends TestCase { public function testUserCantAccessJsonFileIfItIsNotGenerated(): void @@ -20,6 +20,9 @@ public function testUserCantAccessJsonFileIfItIsNotGenerated(): void $this->assertTrue($response->isNotFound()); } + /** + * @throws L5SwaggerException + */ public function testUserCanAccessJsonFileIfItIsGenerated(): void { $jsonUrl = route('l5-swagger.default.docs'); @@ -37,6 +40,9 @@ public function testUserCanAccessJsonFileIfItIsGenerated(): void ->isOk(); } + /** + * @throws L5SwaggerException + */ public function testUserCanAccessAndGenerateCustomJsonFile(): void { $customJsonFileName = 'docs.v1.json'; @@ -59,6 +65,9 @@ public function testUserCanAccessAndGenerateCustomJsonFile(): void ->isOk(); } + /** + * @throws L5SwaggerException + */ public function testUserCanAccessAndGenerateYamlFile(): void { $customYamlFileName = 'docs.yaml'; @@ -101,6 +110,9 @@ public function testItCanAccessAndGenerateYamlFile(): void ->isOk(); } + /** + * @throws L5SwaggerException + */ public function testUserCanAccessDocumentationFileWithoutExtensionIfItExists(): void { $customYamlFileName = 'docs-file-without-extension'; @@ -122,6 +134,9 @@ public function testUserCanAccessDocumentationFileWithoutExtensionIfItExists(): ->isOk(); } + /** + * @throws L5SwaggerException + */ public function testItDoesNotThrowExceptionOnDocsFileWithoutExtension(): void { $fileWithoutExtension = 'docs'; @@ -219,9 +234,9 @@ public function testItWillReturn404WhenDocGenerationFails(): void } /** - * @return MockObject&Generator + * @throws Exception */ - private function mockGenerator() + private function mockGenerator(): Generator|MockObject { $mockGenerator = $this->createMock(Generator::class); $mockGeneratorFactory = $this->createMock(GeneratorFactory::class); diff --git a/tests/SecurityDefinitionsTest.php b/tests/SecurityDefinitionsTest.php index 7a7244e..26c745e 100644 --- a/tests/SecurityDefinitionsTest.php +++ b/tests/SecurityDefinitionsTest.php @@ -4,10 +4,10 @@ use Illuminate\Filesystem\Filesystem; use L5Swagger\Exceptions\L5SwaggerException; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\TestDox; -/** - * @testdox Security definition - */ +#[TestDox('Security definition')] class SecurityDefinitionsTest extends TestCase { /** @@ -49,11 +49,9 @@ public function testItWillNotAddEmptySecurityItems(): void } /** - * - * @dataProvider provideConfigAndSchemes - * * @throws L5SwaggerException */ + #[DataProvider('provideConfigAndSchemes')] public function testCanGenerateApiJsonFileWithSecurityDefinition( array $securitySchemes, array $security @@ -80,7 +78,7 @@ public function testCanGenerateApiJsonFileWithSecurityDefinition( $this->assertTrue($fileSystem->exists($this->jsonDocsFile())); $this->get(route('l5-swagger.default.docs')) - ->assertSee('new_api_key_securitye') + ->assertSee('new_api_key_security') ->assertSee('oauth2') // From annotations ->assertSee('read:projects') ->assertSee('read:oauth2') // From annotations @@ -92,7 +90,7 @@ public function testCanGenerateApiJsonFileWithSecurityDefinition( public static function provideConfigAndSchemes(): iterable { $securitySchemes = [ - 'new_api_key_securitye' => [ + 'new_api_key_security' => [ 'type' => 'apiKey', 'name' => 'api_key_name', 'in' => 'query', @@ -100,7 +98,7 @@ public static function provideConfigAndSchemes(): iterable ]; $security = [ - 'new_api_key_securitye' => [ + 'new_api_key_security' => [ 'read:projects', ], ]; diff --git a/tests/TestCase.php b/tests/TestCase.php index 951df80..3b259b6 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -10,6 +10,7 @@ use L5Swagger\L5SwaggerServiceProvider; use Orchestra\Testbench\TestCase as OrchestraTestCase; use PHPUnit\Framework\Attributes\Before; +use PHPUnit\Framework\MockObject\Exception; use PHPUnit\Framework\MockObject\MockObject; use ReflectionObject; @@ -23,6 +24,9 @@ class TestCase extends OrchestraTestCase protected Filesystem|MockObject $fileSystem; + /** + * @throws Exception + */ #[Before] public function setUpFileSystem(): void { @@ -64,7 +68,8 @@ public function tearDown(): void } /** - * @param Application $app + * @param Application $app + * @return string[] */ protected function getPackageProviders($app): array { @@ -98,7 +103,6 @@ protected function createYamlDocumentationFile(): void /** * Get path for json docs file. * - * * @throws L5SwaggerException */ protected function jsonDocsFile(): string @@ -117,7 +121,6 @@ protected function jsonDocsFile(): string /** * Get path for yaml docs file. * - * * @throws L5SwaggerException */ protected function yamlDocsFile(): string From 1760bfe79c3b47f562958f3e2af0e216d8c5d016 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Wed, 13 Mar 2024 15:37:40 +0000 Subject: [PATCH 05/27] Apply fixes from StyleCI --- tests/TestCase.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/TestCase.php b/tests/TestCase.php index 3b259b6..a130f95 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -68,7 +68,7 @@ public function tearDown(): void } /** - * @param Application $app + * @param Application $app * @return string[] */ protected function getPackageProviders($app): array From f9395535b7c93047884372afbc97a90a55c617fb Mon Sep 17 00:00:00 2001 From: Darius Matulionis Date: Wed, 13 Mar 2024 17:40:26 +0200 Subject: [PATCH 06/27] WIP --- tests/RoutesTest.php | 2 +- tests/TestCase.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/RoutesTest.php b/tests/RoutesTest.php index 1e59055..02d0c29 100644 --- a/tests/RoutesTest.php +++ b/tests/RoutesTest.php @@ -236,7 +236,7 @@ public function testItWillReturn404WhenDocGenerationFails(): void /** * @throws Exception */ - private function mockGenerator(): Generator|MockObject + private function mockGenerator(): Generator { $mockGenerator = $this->createMock(Generator::class); $mockGeneratorFactory = $this->createMock(GeneratorFactory::class); diff --git a/tests/TestCase.php b/tests/TestCase.php index a130f95..e86b2b5 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -22,7 +22,7 @@ class TestCase extends OrchestraTestCase protected Generator $generator; - protected Filesystem|MockObject $fileSystem; + protected Filesystem $fileSystem; /** * @throws Exception From 8d51ba9ebd131348f6e3ddcdd2ab270b2d2998ed Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Wed, 13 Mar 2024 15:40:35 +0000 Subject: [PATCH 07/27] Apply fixes from StyleCI --- tests/RoutesTest.php | 1 - tests/TestCase.php | 1 - 2 files changed, 2 deletions(-) diff --git a/tests/RoutesTest.php b/tests/RoutesTest.php index 02d0c29..a8319c6 100644 --- a/tests/RoutesTest.php +++ b/tests/RoutesTest.php @@ -7,7 +7,6 @@ use L5Swagger\GeneratorFactory; use PHPUnit\Framework\Attributes\TestDox; use PHPUnit\Framework\MockObject\Exception; -use PHPUnit\Framework\MockObject\MockObject; #[TestDox('Routes')] class RoutesTest extends TestCase diff --git a/tests/TestCase.php b/tests/TestCase.php index e86b2b5..d4a9ea5 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -11,7 +11,6 @@ use Orchestra\Testbench\TestCase as OrchestraTestCase; use PHPUnit\Framework\Attributes\Before; use PHPUnit\Framework\MockObject\Exception; -use PHPUnit\Framework\MockObject\MockObject; use ReflectionObject; class TestCase extends OrchestraTestCase From 4fff6f209ba9b61a19d409d097cd440a324057f2 Mon Sep 17 00:00:00 2001 From: Darius Matulionis Date: Fri, 9 Aug 2024 09:35:47 +0300 Subject: [PATCH 08/27] WIP --- tests/ConfigFactoryTest.php | 9 +++++---- tests/ConsoleTest.php | 1 + 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/tests/ConfigFactoryTest.php b/tests/ConfigFactoryTest.php index ac32c40..225ae37 100644 --- a/tests/ConfigFactoryTest.php +++ b/tests/ConfigFactoryTest.php @@ -23,6 +23,7 @@ public function testItThrowsExceptionIfDocumentationConfigNotFound(): void /** * @throws L5SwaggerException + * @dataProvider configDataProvider */ #[DataProvider('configDataProvider')] public function testCanMergeConfigurationDeep(array $data, array $assert): void @@ -52,7 +53,7 @@ public function testCanMergeConfigurationDeep(array $data, array $assert): void public static function configDataProvider(): \Iterator { yield [ - [ + 'data' => [ 'default' => 'v2', 'documentations' => [ 'v2' => [ @@ -66,7 +67,7 @@ public static function configDataProvider(): \Iterator ], ], ], - [ + 'assert' => [ 'api' => [ 'title' => 'Api V2', ], @@ -83,7 +84,7 @@ public static function configDataProvider(): \Iterator ], ]; yield [ - [ + 'data' => [ 'default' => 'v1', 'documentations' => [ 'v1' => [ @@ -99,7 +100,7 @@ public static function configDataProvider(): \Iterator ], ], ], - [ + 'assert' => [ 'api' => [ 'title' => 'Api V1', ], diff --git a/tests/ConsoleTest.php b/tests/ConsoleTest.php index df3b275..579e05f 100644 --- a/tests/ConsoleTest.php +++ b/tests/ConsoleTest.php @@ -15,6 +15,7 @@ class ConsoleTest extends TestCase /** * @throws L5SwaggerException * @throws FileNotFoundException + * @dataProvider provideGenerateCommands */ #[DataProvider('provideGenerateCommands')] public function testCanGenerate(string $artisanCommand): void From 4e0aa39521a9b029771cec0ca05a18de63543976 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Fri, 9 Aug 2024 06:35:55 +0000 Subject: [PATCH 09/27] Apply fixes from StyleCI --- tests/ConfigFactoryTest.php | 1 + tests/ConsoleTest.php | 1 + 2 files changed, 2 insertions(+) diff --git a/tests/ConfigFactoryTest.php b/tests/ConfigFactoryTest.php index 225ae37..8e26342 100644 --- a/tests/ConfigFactoryTest.php +++ b/tests/ConfigFactoryTest.php @@ -23,6 +23,7 @@ public function testItThrowsExceptionIfDocumentationConfigNotFound(): void /** * @throws L5SwaggerException + * * @dataProvider configDataProvider */ #[DataProvider('configDataProvider')] diff --git a/tests/ConsoleTest.php b/tests/ConsoleTest.php index 579e05f..9414a3e 100644 --- a/tests/ConsoleTest.php +++ b/tests/ConsoleTest.php @@ -15,6 +15,7 @@ class ConsoleTest extends TestCase /** * @throws L5SwaggerException * @throws FileNotFoundException + * * @dataProvider provideGenerateCommands */ #[DataProvider('provideGenerateCommands')] From 2371c75d8188975629f76849027be07b693a3157 Mon Sep 17 00:00:00 2001 From: Darius Matulionis Date: Fri, 9 Aug 2024 09:39:35 +0300 Subject: [PATCH 10/27] drop php7.4 support --- .github/workflows/test-config.yml | 2 +- .travis.yml | 54 ------------------------------- tests/ConfigFactoryTest.php | 1 - tests/ConsoleTest.php | 1 - 4 files changed, 1 insertion(+), 57 deletions(-) delete mode 100644 .travis.yml diff --git a/.github/workflows/test-config.yml b/.github/workflows/test-config.yml index e5f805b..b0ff953 100644 --- a/.github/workflows/test-config.yml +++ b/.github/workflows/test-config.yml @@ -13,7 +13,7 @@ jobs: strategy: fail-fast: true matrix: - php: [ '7.4', '8.0', '8.1', '8.2', '8.3' ] + php: [ '8.0', '8.1', '8.2', '8.3' ] l5-swagger-flags: ['latest', 'swagger-php-3', 'swagger-ui-3'] name: PHP ${{ matrix.php }} - ${{ matrix.l5-swagger-flags }} diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 641f526..0000000 --- a/.travis.yml +++ /dev/null @@ -1,54 +0,0 @@ -language: php -sudo: false -git: - depth: 1 -env: - global: - - DEFAULT_COMPOSER_FLAGS="--no-interaction --no-progress --optimize-autoloader" - - REPORT_TESTS_COVERAGE=0 - - CACHE_NAME=OPEN_API -matrix: - fast_finish: true - include: - - php: 7.2 - - php: 7.3 - - php: 7.4 - env: - - REPORT_TESTS_COVERAGE=1 - - XDEBUG_MODE=coverage - - php: 8.0 -cache: - directories: - - "$HOME/.composer/cache" -install: - - travis_retry composer update $DEFAULT_COMPOSER_FLAGS - - composer require 'zircote/swagger-php:3.*' - - composer info -D | sort -before_script: - - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter - - chmod +x ./cc-test-reporter - - ./cc-test-reporter before-build -script: - - pwd - - ls -al | grep $USER - - sudo chown -R $USER:$USER . - - sudo chmod -R g+rw . - - ls -al | grep $USER - - mkdir -p tests/storage/logs/test-reports - - mkdir -p vendor/orchestra/testbench-core/laravel/vendor/swagger-api - - mkdir -p vendor/orchestra/testbench-core/laravel/vendor/swagger-api/swagger-ui - - mkdir -p vendor/orchestra/testbench-core/laravel/vendor/swagger-api/swagger-ui/dist - - sudo chown -R $USER:$USER vendor/orchestra/testbench-core/laravel/vendor/swagger-api - - chmod -R 777 vendor/orchestra/testbench-core/laravel/vendor/swagger-api - - ls -al vendor/orchestra/testbench-core/laravel/vendor/swagger-api - - chmod -R 777 /home/travis/build/DarkaOnLine/L5-Swagger - - if [ $REPORT_TESTS_COVERAGE != 1 ]; then vendor/bin/phpunit --no-coverage; fi - - if [ $REPORT_TESTS_COVERAGE == 1 ]; then vendor/bin/phpunit; cp tests/storage/logs/test-reports/clover.xml clover.xml; fi - - ls -al vendor/orchestra/testbench-core/laravel/vendor/swagger-api/swagger-ui/dist -after_success: - - if [ $REPORT_TESTS_COVERAGE == 1 ] && [ $TRAVIS_PULL_REQUEST == "false" ]; then ./cc-test-reporter after-build -t clover --exit-code $TRAVIS_TEST_RESULT; fi - - if [ $REPORT_TESTS_COVERAGE == 1 ]; then php vendor/bin/php-coveralls -v; fi -notifications: - email: false - slack: - secure: ZymO41QYmcBtaqIcjKagifif5bH+vpUdZe9eN2Q27G2HF7ID0ZP2X6sCCUCVqdJfM3DhmEsNo8h/HiY86u/5zhf5YpZr7vt+CuYinIJ+nydGXWO5CU6nSotmafFgo3u+Yyjz6qIBhnOVZL8prYcbZsn0SIRvvTwT0qOCBGWIEohdxnu6DpC1BRKQ2RNgsGnO88N5+8FYGor1M4+UT38d/JMrgx8MGkW305ioCmW888NmVuNMheEh19wyFqp9FCQED2DsLxU3pi7A5w4knA5eIy89ULPOrNFUMq5LIBA5OdERrnNS6Yk5/kMmepH2A0tj9pFqBzxdFtThQtYYZ1UnxrotfE+YvU587FW3iXowpHAsaGeoLA3XkakCuZ6jpZhKwOWvslBcBt/6Irs0MPmj5EyaIOb8kAACij8nIfWQkQu/2Ik/AZ0L4lw36RRYNMdel8fMZAwcmK1bfh51dJqhTsf2e4iLa9xHCx7/Mib8EriDwRdYeO0GWSRk4cwTyIY2JjpkidgbDy2BHpiFxp9v/mNf6nJYYVG1Arnv/tK0t3WeILkOHy7tkWtGwFrjiRVQ7B2Hq+iBgda0reZSncK56GhmrMiHwO95QPe6hh3oz3lwcl7MoKGCyLASTeXTKNGhR5fds/6fG25bov8DDOaWXbQyeidJwO4UvLY6ksS3onc= diff --git a/tests/ConfigFactoryTest.php b/tests/ConfigFactoryTest.php index 225ae37..28ace5e 100644 --- a/tests/ConfigFactoryTest.php +++ b/tests/ConfigFactoryTest.php @@ -23,7 +23,6 @@ public function testItThrowsExceptionIfDocumentationConfigNotFound(): void /** * @throws L5SwaggerException - * @dataProvider configDataProvider */ #[DataProvider('configDataProvider')] public function testCanMergeConfigurationDeep(array $data, array $assert): void diff --git a/tests/ConsoleTest.php b/tests/ConsoleTest.php index 579e05f..df3b275 100644 --- a/tests/ConsoleTest.php +++ b/tests/ConsoleTest.php @@ -15,7 +15,6 @@ class ConsoleTest extends TestCase /** * @throws L5SwaggerException * @throws FileNotFoundException - * @dataProvider provideGenerateCommands */ #[DataProvider('provideGenerateCommands')] public function testCanGenerate(string $artisanCommand): void From f5eeae46546aecf8b9da69584e4d2b79d7db4520 Mon Sep 17 00:00:00 2001 From: Darius Matulionis Date: Fri, 9 Aug 2024 09:46:58 +0300 Subject: [PATCH 11/27] dependencies --- composer.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 41c53fc..ea4aca3 100644 --- a/composer.json +++ b/composer.json @@ -19,8 +19,8 @@ } ], "require": { - "php": "^7.2 || ^8.0", - "laravel/framework": "^11.0 || ^10.0 || ^9.0 || >=8.40.0 || ^7.0", + "php": "^8.0", + "laravel/framework": "^11.0 || ^10.0 || ^9.0", "zircote/swagger-php": "^3.2.0 || ^4.0.0", "swagger-api/swagger-ui": "^3.0 || >=4.1.3", "symfony/yaml": "^5.0 || ^6.0 || ^7.0", @@ -28,7 +28,7 @@ "doctrine/annotations": "^1.0 || ^2.0" }, "require-dev": { - "phpunit/phpunit": "^11.0 || ^10.0 || ^9.5", + "phpunit/phpunit": "^11.0 || ^10.0", "mockery/mockery": "1.*", "orchestra/testbench": "^9.0 || ^8.0 || 7.* || ^6.15 || 5.*", "php-coveralls/php-coveralls": "^2.0" From 33e73bfb64e6812e51a5f12bd9c166d5d98b68ae Mon Sep 17 00:00:00 2001 From: Darius Matulionis Date: Fri, 9 Aug 2024 09:49:19 +0300 Subject: [PATCH 12/27] dependencies --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index ea4aca3..45506c9 100644 --- a/composer.json +++ b/composer.json @@ -28,7 +28,7 @@ "doctrine/annotations": "^1.0 || ^2.0" }, "require-dev": { - "phpunit/phpunit": "^11.0 || ^10.0", + "phpunit/phpunit": "^11.0 || ^10.0 || ^9.0", "mockery/mockery": "1.*", "orchestra/testbench": "^9.0 || ^8.0 || 7.* || ^6.15 || 5.*", "php-coveralls/php-coveralls": "^2.0" From 6c6b546eed44085c8f74190c2bd45027f68e0042 Mon Sep 17 00:00:00 2001 From: Darius Matulionis Date: Fri, 9 Aug 2024 13:54:12 +0300 Subject: [PATCH 13/27] WIP --- .github/workflows/test-config.yml | 5 +++++ composer.json | 2 +- phpunit-php80.xml | 29 +++++++++++++++++++++++++++++ phpunit.xml | 5 +++++ tests/ConfigFactoryTest.php | 9 +++++++-- tests/ConsoleTest.php | 5 +++++ tests/GeneratorTest.php | 3 +++ tests/HelpersTest.php | 3 +++ tests/RoutesTest.php | 4 ++++ tests/SecurityDefinitionsTest.php | 5 +++++ tests/TestCase.php | 12 ++++++++++++ 11 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 phpunit-php80.xml diff --git a/.github/workflows/test-config.yml b/.github/workflows/test-config.yml index b0ff953..e2d36aa 100644 --- a/.github/workflows/test-config.yml +++ b/.github/workflows/test-config.yml @@ -66,8 +66,13 @@ jobs: ./cc-test-reporter before-build if: matrix.php == '8.3' && matrix.l5-swagger-flags == 'latest' + - name: Run PHP 8.0 test suite + run: vendor/bin/phpunit --testdox -c phpunit-php80.xml + if: matrix.php == '8.0' + - name: Run test suite run: composer run-script phpunit + if: matrix.php == '8.0' - name: Publish code coverage env: diff --git a/composer.json b/composer.json index 45506c9..ae59add 100644 --- a/composer.json +++ b/composer.json @@ -28,7 +28,7 @@ "doctrine/annotations": "^1.0 || ^2.0" }, "require-dev": { - "phpunit/phpunit": "^11.0 || ^10.0 || ^9.0", + "phpunit/phpunit": "^11.0 || ^10.0 || ^9.5", "mockery/mockery": "1.*", "orchestra/testbench": "^9.0 || ^8.0 || 7.* || ^6.15 || 5.*", "php-coveralls/php-coveralls": "^2.0" diff --git a/phpunit-php80.xml b/phpunit-php80.xml new file mode 100644 index 0000000..fd1b3f2 --- /dev/null +++ b/phpunit-php80.xml @@ -0,0 +1,29 @@ + + + + + tests + + + + + src + + + + + + + diff --git a/phpunit.xml b/phpunit.xml index 378379b..cb1f4fd 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -3,6 +3,11 @@ xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.0/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true" + forceCoversAnnotation="true" + beStrictAboutCoversAnnotation="true" + beStrictAboutOutputDuringTests="true" + beStrictAboutTodoAnnotatedTests="true" + convertDeprecationsToExceptions="true" stopOnFailure="false" cacheDirectory=".phpunit.cache" > diff --git a/tests/ConfigFactoryTest.php b/tests/ConfigFactoryTest.php index 28ace5e..42d33f1 100644 --- a/tests/ConfigFactoryTest.php +++ b/tests/ConfigFactoryTest.php @@ -6,6 +6,9 @@ use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\TestDox; +/** + * @covers \L5Swagger\ConfigFactory + */ #[TestDox('Configuration factory')] class ConfigFactoryTest extends TestCase { @@ -22,6 +25,8 @@ public function testItThrowsExceptionIfDocumentationConfigNotFound(): void } /** + * @dataProvider configDataProvider + * * @throws L5SwaggerException */ #[DataProvider('configDataProvider')] @@ -51,7 +56,7 @@ public function testCanMergeConfigurationDeep(array $data, array $assert): void public static function configDataProvider(): \Iterator { - yield [ + yield 'V1 configuration' => [ 'data' => [ 'default' => 'v2', 'documentations' => [ @@ -82,7 +87,7 @@ public static function configDataProvider(): \Iterator 'proxy' => true, ], ]; - yield [ + yield 'V2 configuration' => [ 'data' => [ 'default' => 'v1', 'documentations' => [ diff --git a/tests/ConsoleTest.php b/tests/ConsoleTest.php index df3b275..ce68974 100644 --- a/tests/ConsoleTest.php +++ b/tests/ConsoleTest.php @@ -9,10 +9,15 @@ use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\TestDox; +/** + * @covers \L5Swagger\Console\GenerateDocsCommand + */ #[TestDox('Console commands')] class ConsoleTest extends TestCase { /** + * @dataProvider provideGenerateCommands + * * @throws L5SwaggerException * @throws FileNotFoundException */ diff --git a/tests/GeneratorTest.php b/tests/GeneratorTest.php index 6b4c8b5..4afcd8b 100644 --- a/tests/GeneratorTest.php +++ b/tests/GeneratorTest.php @@ -10,6 +10,9 @@ use Symfony\Component\Yaml\Parser; use Symfony\Component\Yaml\Yaml; +/** + * @covers \L5Swagger\Generator + */ #[TestDox('Generator')] class GeneratorTest extends TestCase { diff --git a/tests/HelpersTest.php b/tests/HelpersTest.php index 9abfe4d..7030ebb 100644 --- a/tests/HelpersTest.php +++ b/tests/HelpersTest.php @@ -5,6 +5,9 @@ use L5Swagger\Exceptions\L5SwaggerException; use PHPUnit\Framework\Attributes\TestDox; +/** + * @covers L5SwaggerException + */ #[TestDox('Helpers')] class HelpersTest extends TestCase { diff --git a/tests/RoutesTest.php b/tests/RoutesTest.php index a8319c6..5febde6 100644 --- a/tests/RoutesTest.php +++ b/tests/RoutesTest.php @@ -8,6 +8,10 @@ use PHPUnit\Framework\Attributes\TestDox; use PHPUnit\Framework\MockObject\Exception; +/** + * @covers \L5Swagger\Http\Controllers\SwaggerController + * @covers \L5Swagger\Http\Controllers\SwaggerAssetController + */ #[TestDox('Routes')] class RoutesTest extends TestCase { diff --git a/tests/SecurityDefinitionsTest.php b/tests/SecurityDefinitionsTest.php index 26c745e..ebd17a7 100644 --- a/tests/SecurityDefinitionsTest.php +++ b/tests/SecurityDefinitionsTest.php @@ -7,6 +7,9 @@ use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\TestDox; +/** + * @covers \L5Swagger\SecurityDefinitions + */ #[TestDox('Security definition')] class SecurityDefinitionsTest extends TestCase { @@ -49,6 +52,8 @@ public function testItWillNotAddEmptySecurityItems(): void } /** + * @dataProvider provideConfigAndSchemes + * * @throws L5SwaggerException */ #[DataProvider('provideConfigAndSchemes')] diff --git a/tests/TestCase.php b/tests/TestCase.php index d4a9ea5..5a58a6d 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -11,19 +11,31 @@ use Orchestra\Testbench\TestCase as OrchestraTestCase; use PHPUnit\Framework\Attributes\Before; use PHPUnit\Framework\MockObject\Exception; +use PHPUnit\Framework\MockObject\MockObject; use ReflectionObject; class TestCase extends OrchestraTestCase { + /** + * @var ConfigFactory|MockObject + */ protected ConfigFactory $configFactory; protected array $defaultConfig; + /** + * @var Generator|MockObject + */ protected Generator $generator; + /** + * @var Filesystem|MockObject + */ protected Filesystem $fileSystem; /** + * @before + * * @throws Exception */ #[Before] From 556f1a2c96713183e1d63afd2219e5f9d1d662d7 Mon Sep 17 00:00:00 2001 From: Darius Matulionis Date: Fri, 9 Aug 2024 14:02:20 +0300 Subject: [PATCH 14/27] covers attributes --- phpunit.xml | 6 ++---- tests/ConfigFactoryTest.php | 3 +++ tests/ConsoleTest.php | 3 +++ tests/GeneratorTest.php | 3 +++ tests/HelpersTest.php | 3 +++ tests/RoutesTest.php | 5 +++++ tests/SecurityDefinitionsTest.php | 3 +++ 7 files changed, 22 insertions(+), 4 deletions(-) diff --git a/phpunit.xml b/phpunit.xml index cb1f4fd..6c8d556 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -3,11 +3,9 @@ xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.0/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true" - forceCoversAnnotation="true" - beStrictAboutCoversAnnotation="true" + requireCoverageMetadata="true" + beStrictAboutCoverageMetadata="true" beStrictAboutOutputDuringTests="true" - beStrictAboutTodoAnnotatedTests="true" - convertDeprecationsToExceptions="true" stopOnFailure="false" cacheDirectory=".phpunit.cache" > diff --git a/tests/ConfigFactoryTest.php b/tests/ConfigFactoryTest.php index 42d33f1..20afab6 100644 --- a/tests/ConfigFactoryTest.php +++ b/tests/ConfigFactoryTest.php @@ -2,7 +2,9 @@ namespace Tests; +use L5Swagger\ConfigFactory; use L5Swagger\Exceptions\L5SwaggerException; +use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\TestDox; @@ -10,6 +12,7 @@ * @covers \L5Swagger\ConfigFactory */ #[TestDox('Configuration factory')] +#[CoversClass(ConfigFactory::class)] class ConfigFactoryTest extends TestCase { public function testItThrowsExceptionIfDocumentationConfigNotFound(): void diff --git a/tests/ConsoleTest.php b/tests/ConsoleTest.php index ce68974..579058c 100644 --- a/tests/ConsoleTest.php +++ b/tests/ConsoleTest.php @@ -5,7 +5,9 @@ use Illuminate\Contracts\Filesystem\FileNotFoundException; use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Facades\Artisan; +use L5Swagger\Console\GenerateDocsCommand; use L5Swagger\Exceptions\L5SwaggerException; +use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\TestDox; @@ -13,6 +15,7 @@ * @covers \L5Swagger\Console\GenerateDocsCommand */ #[TestDox('Console commands')] +#[CoversClass(GenerateDocsCommand::class)] class ConsoleTest extends TestCase { /** diff --git a/tests/GeneratorTest.php b/tests/GeneratorTest.php index 4afcd8b..539b9f0 100644 --- a/tests/GeneratorTest.php +++ b/tests/GeneratorTest.php @@ -4,8 +4,10 @@ use Illuminate\Http\Request; use L5Swagger\Exceptions\L5SwaggerException; +use L5Swagger\Generator; use OpenApi\Analysers\TokenAnalyser; use OpenApi\Processors\CleanUnmerged; +use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\TestDox; use Symfony\Component\Yaml\Parser; use Symfony\Component\Yaml\Yaml; @@ -14,6 +16,7 @@ * @covers \L5Swagger\Generator */ #[TestDox('Generator')] +#[CoversClass(Generator::class)] class GeneratorTest extends TestCase { /** diff --git a/tests/HelpersTest.php b/tests/HelpersTest.php index 7030ebb..ff60582 100644 --- a/tests/HelpersTest.php +++ b/tests/HelpersTest.php @@ -3,12 +3,15 @@ namespace Tests; use L5Swagger\Exceptions\L5SwaggerException; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\CoversFunction; use PHPUnit\Framework\Attributes\TestDox; /** * @covers L5SwaggerException */ #[TestDox('Helpers')] +#[CoversFunction('l5_swagger_asset')] class HelpersTest extends TestCase { public function testAssetFunctionThrowsExceptionIfFileDoesNotExists(): void diff --git a/tests/RoutesTest.php b/tests/RoutesTest.php index 5febde6..fb8db8c 100644 --- a/tests/RoutesTest.php +++ b/tests/RoutesTest.php @@ -5,6 +5,9 @@ use L5Swagger\Exceptions\L5SwaggerException; use L5Swagger\Generator; use L5Swagger\GeneratorFactory; +use L5Swagger\Http\Controllers\SwaggerAssetController; +use L5Swagger\Http\Controllers\SwaggerController; +use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\TestDox; use PHPUnit\Framework\MockObject\Exception; @@ -13,6 +16,8 @@ * @covers \L5Swagger\Http\Controllers\SwaggerAssetController */ #[TestDox('Routes')] +#[CoversClass(SwaggerController::class)] +#[CoversClass(SwaggerAssetController::class)] class RoutesTest extends TestCase { public function testUserCantAccessJsonFileIfItIsNotGenerated(): void diff --git a/tests/SecurityDefinitionsTest.php b/tests/SecurityDefinitionsTest.php index ebd17a7..b7ffc59 100644 --- a/tests/SecurityDefinitionsTest.php +++ b/tests/SecurityDefinitionsTest.php @@ -4,6 +4,8 @@ use Illuminate\Filesystem\Filesystem; use L5Swagger\Exceptions\L5SwaggerException; +use L5Swagger\SecurityDefinitions; +use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\TestDox; @@ -11,6 +13,7 @@ * @covers \L5Swagger\SecurityDefinitions */ #[TestDox('Security definition')] +#[CoversClass(SecurityDefinitions::class)] class SecurityDefinitionsTest extends TestCase { /** From 8a8dbbaf7e79f37cccd8720ae413bcbb840a2d92 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Fri, 9 Aug 2024 11:02:28 +0000 Subject: [PATCH 15/27] Apply fixes from StyleCI --- tests/HelpersTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/HelpersTest.php b/tests/HelpersTest.php index ff60582..8349cf3 100644 --- a/tests/HelpersTest.php +++ b/tests/HelpersTest.php @@ -3,7 +3,6 @@ namespace Tests; use L5Swagger\Exceptions\L5SwaggerException; -use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\CoversFunction; use PHPUnit\Framework\Attributes\TestDox; From d1e0b9b71dc90145aeae733953de78bfb96ec525 Mon Sep 17 00:00:00 2001 From: Darius Matulionis Date: Fri, 9 Aug 2024 14:16:06 +0300 Subject: [PATCH 16/27] ci --- .github/workflows/test-config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test-config.yml b/.github/workflows/test-config.yml index e2d36aa..45d4854 100644 --- a/.github/workflows/test-config.yml +++ b/.github/workflows/test-config.yml @@ -72,7 +72,7 @@ jobs: - name: Run test suite run: composer run-script phpunit - if: matrix.php == '8.0' + if: matrix.php != '8.0' - name: Publish code coverage env: From 064e674c9bd8cb16e9634cabca5fdd2f43f9f872 Mon Sep 17 00:00:00 2001 From: Darius Matulionis Date: Fri, 9 Aug 2024 14:34:17 +0300 Subject: [PATCH 17/27] wip --- phpunit.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/phpunit.xml b/phpunit.xml index 6c8d556..dc9c59c 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -4,7 +4,6 @@ bootstrap="vendor/autoload.php" colors="true" requireCoverageMetadata="true" - beStrictAboutCoverageMetadata="true" beStrictAboutOutputDuringTests="true" stopOnFailure="false" cacheDirectory=".phpunit.cache" From 396d9d29a77c99dc388477eaec1c190c6abe404c Mon Sep 17 00:00:00 2001 From: Darius Matulionis Date: Wed, 28 Aug 2024 14:52:39 +0300 Subject: [PATCH 18/27] WIP --- phpunit.xml | 2 -- src/helpers.php | 4 ++-- tests/GeneratorTest.php | 4 ++++ tests/HelpersTest.php | 3 +++ tests/RoutesTest.php | 3 +++ 5 files changed, 12 insertions(+), 4 deletions(-) diff --git a/phpunit.xml b/phpunit.xml index dc9c59c..378379b 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -3,8 +3,6 @@ xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.0/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true" - requireCoverageMetadata="true" - beStrictAboutOutputDuringTests="true" stopOnFailure="false" cacheDirectory=".phpunit.cache" > diff --git a/src/helpers.php b/src/helpers.php index b574f60..424b3d3 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -14,7 +14,7 @@ */ function swagger_ui_dist_path(string $documentation, $asset = null) { - $allowed_files = [ + $allowedFiles = [ 'favicon-16x16.png', 'favicon-32x32.png', 'oauth2-redirect.html', @@ -37,7 +37,7 @@ function swagger_ui_dist_path(string $documentation, $asset = null) return realpath($path); } - if (! in_array($asset, $allowed_files)) { + if (! in_array($asset, $allowedFiles, true)) { throw new L5SwaggerException(sprintf('(%s) - this L5 Swagger asset is not allowed', $asset)); } diff --git a/tests/GeneratorTest.php b/tests/GeneratorTest.php index 539b9f0..f67a3d5 100644 --- a/tests/GeneratorTest.php +++ b/tests/GeneratorTest.php @@ -5,6 +5,8 @@ use Illuminate\Http\Request; use L5Swagger\Exceptions\L5SwaggerException; use L5Swagger\Generator; +use L5Swagger\GeneratorFactory; +use L5Swagger\L5SwaggerServiceProvider; use OpenApi\Analysers\TokenAnalyser; use OpenApi\Processors\CleanUnmerged; use PHPUnit\Framework\Attributes\CoversClass; @@ -16,7 +18,9 @@ * @covers \L5Swagger\Generator */ #[TestDox('Generator')] +#[CoversClass(GeneratorFactory::class)] #[CoversClass(Generator::class)] +#[CoversClass(L5SwaggerServiceProvider::class)] class GeneratorTest extends TestCase { /** diff --git a/tests/HelpersTest.php b/tests/HelpersTest.php index 8349cf3..2343d64 100644 --- a/tests/HelpersTest.php +++ b/tests/HelpersTest.php @@ -3,6 +3,7 @@ namespace Tests; use L5Swagger\Exceptions\L5SwaggerException; +use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\CoversFunction; use PHPUnit\Framework\Attributes\TestDox; @@ -10,7 +11,9 @@ * @covers L5SwaggerException */ #[TestDox('Helpers')] +#[CoversClass(L5SwaggerException::class)] #[CoversFunction('l5_swagger_asset')] +#[CoversFunction('swagger_ui_dist_path')] class HelpersTest extends TestCase { public function testAssetFunctionThrowsExceptionIfFileDoesNotExists(): void diff --git a/tests/RoutesTest.php b/tests/RoutesTest.php index fb8db8c..70ce789 100644 --- a/tests/RoutesTest.php +++ b/tests/RoutesTest.php @@ -7,6 +7,7 @@ use L5Swagger\GeneratorFactory; use L5Swagger\Http\Controllers\SwaggerAssetController; use L5Swagger\Http\Controllers\SwaggerController; +use L5Swagger\Http\Middleware\Config; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\TestDox; use PHPUnit\Framework\MockObject\Exception; @@ -18,6 +19,8 @@ #[TestDox('Routes')] #[CoversClass(SwaggerController::class)] #[CoversClass(SwaggerAssetController::class)] +#[CoversClass(Config::class)] +#[CoversClass(L5SwaggerException::class)] class RoutesTest extends TestCase { public function testUserCantAccessJsonFileIfItIsNotGenerated(): void From d9531ef96254fe09345789648c2420abae8aff69 Mon Sep 17 00:00:00 2001 From: Darius Matulionis Date: Fri, 30 Aug 2024 16:19:53 +0300 Subject: [PATCH 19/27] update tests --- phpunit.xml | 3 +++ tests/RoutesTest.php | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/phpunit.xml b/phpunit.xml index 378379b..07bcfb7 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -24,5 +24,8 @@ src/ + + src/routes.php + diff --git a/tests/RoutesTest.php b/tests/RoutesTest.php index 70ce789..61f383f 100644 --- a/tests/RoutesTest.php +++ b/tests/RoutesTest.php @@ -2,6 +2,7 @@ namespace Tests; +use Illuminate\Http\Request; use L5Swagger\Exceptions\L5SwaggerException; use L5Swagger\Generator; use L5Swagger\GeneratorFactory; @@ -175,6 +176,39 @@ public function testUserCanAccessDocumentationInterface(): void ->isOk(); } + /** + * @throws L5SwaggerException + */ + public function testUserCanAccessDocumentationInterfaceAndConfigureProxy(): void + { + config(['l5-swagger' => [ + 'default' => 'default', + 'documentations' => config('l5-swagger.documentations'), + 'defaults' => array_merge(config('l5-swagger.defaults'), ['proxy' => ['foo', 'bar']]), + ]]); + + $config = $this->configFactory->documentationConfig(); + $jsonFile = $config['paths']['docs_json'] ?? 'api-docs.json'; + + $this->get($config['routes']['api']) + ->assertSee(route('l5-swagger.default.docs', $jsonFile)) + ->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(['baz'], Request::getTrustedProxies()); + } + /** * @throws L5SwaggerException */ From 4cc0c79e6d453cf7f8a045f66de23bc823ca6512 Mon Sep 17 00:00:00 2001 From: Darius Matulionis Date: Tue, 3 Sep 2024 09:19:53 +0300 Subject: [PATCH 20/27] 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 { From 84ca74a4811d54c59c7f0f9481571e221d31146f Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Tue, 3 Sep 2024 06:20:01 +0000 Subject: [PATCH 21/27] Apply fixes from StyleCI --- tests/RoutesTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/RoutesTest.php b/tests/RoutesTest.php index ebcf7c0..6a79785 100644 --- a/tests/RoutesTest.php +++ b/tests/RoutesTest.php @@ -280,9 +280,9 @@ public function testItWillReturn404WhenDocGenerationFails(): void } /** - * @throws Exception - * * @return Generator&MockObject + * + * @throws Exception */ private function mockGenerator(): Generator { From 3a06766cdfecfd210470b57705858555f25de7ff Mon Sep 17 00:00:00 2001 From: Darius Matulionis Date: Tue, 3 Sep 2024 09:23:56 +0300 Subject: [PATCH 22/27] update tests --- tests/GeneratorTest.php | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/tests/GeneratorTest.php b/tests/GeneratorTest.php index 4d8982a..b7066d5 100644 --- a/tests/GeneratorTest.php +++ b/tests/GeneratorTest.php @@ -189,25 +189,16 @@ public function testCanGenerateWithScanOptions(): void { $cfg = config('l5-swagger.documentations.default'); - $cfg['scanOptions']['exclude'] = [ - __DIR__.'/storage/annotations/OpenApi/Clients', - ]; - + $cfg['scanOptions']['exclude'] = [__DIR__.'/storage/annotations/OpenApi/Clients']; $cfg['scanOptions']['pattern'] = 'L5SwaggerAnnotationsExample*.*'; $cfg['scanOptions']['analyser'] = new TokenAnalyser; $cfg['scanOptions']['open_api_spec_version'] = '3.1.0'; - $cfg['scanOptions']['processors'] = [ - new CleanUnmerged, - ]; - $cfg['scanOptions']['default_processors_configuration'] = [ - 'operationId' => ['hash' => false], - ]; + $cfg['scanOptions']['processors'] = [new CleanUnmerged]; + $cfg['scanOptions']['default_processors_configuration'] = ['operationId' => ['hash' => false]]; config(['l5-swagger' => [ 'default' => 'default', - 'documentations' => [ - 'default' => $cfg, - ], + 'documentations' => ['default' => $cfg], 'defaults' => config('l5-swagger.defaults'), ]]); From 2de2e7d3b188bd8745f7e17c625ac1cf2dd15b30 Mon Sep 17 00:00:00 2001 From: Darius Matulionis Date: Tue, 3 Sep 2024 09:25:53 +0300 Subject: [PATCH 23/27] update tests --- tests/RoutesTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/RoutesTest.php b/tests/RoutesTest.php index 6a79785..15a16c1 100644 --- a/tests/RoutesTest.php +++ b/tests/RoutesTest.php @@ -179,6 +179,8 @@ public function testUserCanAccessDocumentationInterface(): void } /** + * @dataProvider provideProxies + * * @throws L5SwaggerException */ #[DataProvider('provideProxies')] From 7f7fe71ea9b30ec09154932acb662acc32fce827 Mon Sep 17 00:00:00 2001 From: Darius Matulionis Date: Tue, 3 Sep 2024 09:28:59 +0300 Subject: [PATCH 24/27] update tests --- tests/RoutesTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/RoutesTest.php b/tests/RoutesTest.php index 15a16c1..515b24e 100644 --- a/tests/RoutesTest.php +++ b/tests/RoutesTest.php @@ -207,9 +207,9 @@ public function testUserCanAccessDocumentationInterfaceAndConfigureProxy( public static function provideProxies(): \Generator { + yield 'proxy is null' => ['proxy' => null, 'expectedProxies' => []]; yield 'proxies array' => ['proxy' => ['foo', 'bar'], 'expectedProxies' => ['foo', 'bar']]; yield 'proxy as string' => ['proxy' => 'baz', 'expectedProxies' => ['baz']]; - yield 'proxy is null' => ['proxy' => null, 'expectedProxies' => []]; } /** From b95fb31c16dd87c8e276b4d03f9543748831dd43 Mon Sep 17 00:00:00 2001 From: Darius Matulionis Date: Tue, 3 Sep 2024 09:31:40 +0300 Subject: [PATCH 25/27] update tests --- tests/RoutesTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/RoutesTest.php b/tests/RoutesTest.php index 515b24e..4bf5a80 100644 --- a/tests/RoutesTest.php +++ b/tests/RoutesTest.php @@ -207,7 +207,6 @@ public function testUserCanAccessDocumentationInterfaceAndConfigureProxy( public static function provideProxies(): \Generator { - yield 'proxy is null' => ['proxy' => null, 'expectedProxies' => []]; yield 'proxies array' => ['proxy' => ['foo', 'bar'], 'expectedProxies' => ['foo', 'bar']]; yield 'proxy as string' => ['proxy' => 'baz', 'expectedProxies' => ['baz']]; } From 7314605981a3d23938978867806f058ee05aa612 Mon Sep 17 00:00:00 2001 From: Darius Matulionis Date: Tue, 3 Sep 2024 12:27:54 +0300 Subject: [PATCH 26/27] update tests --- tests/HelpersTest.php | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/tests/HelpersTest.php b/tests/HelpersTest.php index 2343d64..7f5f066 100644 --- a/tests/HelpersTest.php +++ b/tests/HelpersTest.php @@ -2,6 +2,7 @@ namespace Tests; +use Illuminate\Support\Facades\Storage; use L5Swagger\Exceptions\L5SwaggerException; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\CoversFunction; @@ -16,10 +17,40 @@ #[CoversFunction('swagger_ui_dist_path')] class HelpersTest extends TestCase { - public function testAssetFunctionThrowsExceptionIfFileDoesNotExists(): void + public function testAssetFunctionReturnsRoute(): void + { + $path = l5_swagger_asset('default', 'swagger-ui.js'); + + $this->assertStringContainsString('http://localhost/docs/asset/swagger-ui.js?v=', $path); + } + + /** + * @throws L5SwaggerException + */ + public function testGeneratesBasePathForAssetsThrowsExceptionIfFileIsNotAllowed(): void { $this->expectException(L5SwaggerException::class); - l5_swagger_asset('default', 'asdasd'); + swagger_ui_dist_path('default', 'foo.bar'); + } + + /** + * @throws L5SwaggerException + */ + public function testGeneratesBasePathForAssets(): void + { + $path = swagger_ui_dist_path('default'); + + $this->assertStringContainsString('swagger-api/swagger-ui/dist', $path); + } + + /** + * @throws L5SwaggerException + */ + public function testGeneratesAssetPathForAssets(): void + { + $path = swagger_ui_dist_path('default', 'swagger-ui.js'); + + $this->assertStringContainsString('swagger-api/swagger-ui/dist/swagger-ui.js', $path); } } From 7018eaff08fe4c98724d7d711d9a95af627a85f0 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Tue, 3 Sep 2024 09:28:03 +0000 Subject: [PATCH 27/27] Apply fixes from StyleCI --- tests/HelpersTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/HelpersTest.php b/tests/HelpersTest.php index 7f5f066..103a25b 100644 --- a/tests/HelpersTest.php +++ b/tests/HelpersTest.php @@ -2,7 +2,6 @@ namespace Tests; -use Illuminate\Support\Facades\Storage; use L5Swagger\Exceptions\L5SwaggerException; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\CoversFunction;