Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkaOnLine committed Mar 13, 2024
1 parent c0df379 commit a304935
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 37 deletions.
12 changes: 5 additions & 7 deletions tests/ConfigFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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));

Expand Down
11 changes: 4 additions & 7 deletions tests/ConsoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
35 changes: 32 additions & 3 deletions tests/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -40,6 +42,9 @@ public function testItThrowsExceptionIfDocumentationDirIsNotWritable(): void
$this->generator->generateDocs();
}

/**
* @throws L5SwaggerException
*/
public function testItWillCreateDocumentationDirIfItIsWritable(): void
{
$this->setAnnotationsPath();
Expand Down Expand Up @@ -70,6 +75,9 @@ public function testItWillCreateDocumentationDirIfItIsWritable(): void
$this->generator->generateDocs();
}

/**
* @throws L5SwaggerException
*/
public function testItThrowsExceptionIfDocumentationDirWasNotCreated(): void
{
$this->setAnnotationsPath();
Expand Down Expand Up @@ -101,6 +109,9 @@ public function testItThrowsExceptionIfDocumentationDirWasNotCreated(): void
$this->generator->generateDocs();
}

/**
* @throws L5SwaggerException
*/
public function testCanGenerateApiJsonFile(): void
{
$this->setAnnotationsPath();
Expand Down Expand Up @@ -129,6 +140,9 @@ public function testCanGenerateApiJsonFile(): void
->assertStatus(200);
}

/**
* @throws L5SwaggerException
*/
public function testCanGenerateWithLegacyExcludedDirectories(): void
{
$this->setAnnotationsPath();
Expand Down Expand Up @@ -158,6 +172,9 @@ public function testCanGenerateWithLegacyExcludedDirectories(): void
->assertStatus(200);
}

/**
* @throws L5SwaggerException
*/
public function testCanGenerateWithScanOptions(): void
{
$cfg = config('l5-swagger.documentations.default');
Expand Down Expand Up @@ -198,6 +215,9 @@ public function testCanGenerateWithScanOptions(): void
->assertStatus(200);
}

/**
* @throws L5SwaggerException
*/
public function testCanGenerateApiJsonFileWithChangedBaseServer(): void
{
$this->setAnnotationsPath();
Expand Down Expand Up @@ -231,6 +251,9 @@ public function testCanGenerateApiJsonFileWithChangedBaseServer(): void
->assertStatus(200);
}

/**
* @throws L5SwaggerException
*/
public function testCanSetProxy(): void
{
$this->setAnnotationsPath();
Expand Down Expand Up @@ -258,6 +281,9 @@ public function testCanSetProxy(): void
$this->assertFileExists($this->yamlDocsFile());
}

/**
* @throws L5SwaggerException
*/
public function testCanSetValidatorUrl(): void
{
$this->setAnnotationsPath();
Expand Down Expand Up @@ -289,6 +315,9 @@ public function testCanSetValidatorUrl(): void
$this->assertFileExists($this->yamlDocsFile());
}

/**
* @throws L5SwaggerException
*/
public function testCanAppropriateYamlType(): void
{
$this->setAnnotationsPath();
Expand Down
5 changes: 2 additions & 3 deletions tests/HelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 20 additions & 5 deletions tests/RoutesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,6 +20,9 @@ public function testUserCantAccessJsonFileIfItIsNotGenerated(): void
$this->assertTrue($response->isNotFound());
}

/**
* @throws L5SwaggerException
*/
public function testUserCanAccessJsonFileIfItIsGenerated(): void
{
$jsonUrl = route('l5-swagger.default.docs');
Expand All @@ -37,6 +40,9 @@ public function testUserCanAccessJsonFileIfItIsGenerated(): void
->isOk();
}

/**
* @throws L5SwaggerException
*/
public function testUserCanAccessAndGenerateCustomJsonFile(): void
{
$customJsonFileName = 'docs.v1.json';
Expand All @@ -59,6 +65,9 @@ public function testUserCanAccessAndGenerateCustomJsonFile(): void
->isOk();
}

/**
* @throws L5SwaggerException
*/
public function testUserCanAccessAndGenerateYamlFile(): void
{
$customYamlFileName = 'docs.yaml';
Expand Down Expand Up @@ -101,6 +110,9 @@ public function testItCanAccessAndGenerateYamlFile(): void
->isOk();
}

/**
* @throws L5SwaggerException
*/
public function testUserCanAccessDocumentationFileWithoutExtensionIfItExists(): void
{
$customYamlFileName = 'docs-file-without-extension';
Expand All @@ -122,6 +134,9 @@ public function testUserCanAccessDocumentationFileWithoutExtensionIfItExists():
->isOk();
}

/**
* @throws L5SwaggerException
*/
public function testItDoesNotThrowExceptionOnDocsFileWithoutExtension(): void
{
$fileWithoutExtension = 'docs';
Expand Down Expand Up @@ -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);
Expand Down
16 changes: 7 additions & 9 deletions tests/SecurityDefinitionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/**
Expand Down Expand Up @@ -49,11 +49,9 @@ public function testItWillNotAddEmptySecurityItems(): void
}

/**
*
* @dataProvider provideConfigAndSchemes
*
* @throws L5SwaggerException
*/
#[DataProvider('provideConfigAndSchemes')]
public function testCanGenerateApiJsonFileWithSecurityDefinition(
array $securitySchemes,
array $security
Expand All @@ -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
Expand All @@ -92,15 +90,15 @@ 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',
],
];

$security = [
'new_api_key_securitye' => [
'new_api_key_security' => [
'read:projects',
],
];
Expand Down
9 changes: 6 additions & 3 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -23,6 +24,9 @@ class TestCase extends OrchestraTestCase

protected Filesystem|MockObject $fileSystem;

/**
* @throws Exception
*/
#[Before]
public function setUpFileSystem(): void
{
Expand Down Expand Up @@ -64,7 +68,8 @@ public function tearDown(): void
}

/**
* @param Application $app
* @param Application $app
* @return string[]
*/
protected function getPackageProviders($app): array
{
Expand Down Expand Up @@ -98,7 +103,6 @@ protected function createYamlDocumentationFile(): void
/**
* Get path for json docs file.
*
*
* @throws L5SwaggerException
*/
protected function jsonDocsFile(): string
Expand All @@ -117,7 +121,6 @@ protected function jsonDocsFile(): string
/**
* Get path for yaml docs file.
*
*
* @throws L5SwaggerException
*/
protected function yamlDocsFile(): string
Expand Down

0 comments on commit a304935

Please sign in to comment.