Skip to content

Commit

Permalink
feat: format yml files, move views to the resources folder, move stor…
Browse files Browse the repository at this point in the history
…age to base path, implement swagger service tests;
  • Loading branch information
DenTray committed Dec 19, 2022
1 parent b00e0ac commit 2d360db
Show file tree
Hide file tree
Showing 29 changed files with 543 additions and 46 deletions.
42 changes: 20 additions & 22 deletions .github/workflows/laravel.yml
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
name: Laravel
name: run-tests-with-coverage

on:
push:
branches: [ "master" ]
branches: ["master"]
pull_request:
branches: [ "master" ]
branches: ["master"]

jobs:
laravel-tests:

tests:
runs-on: ubuntu-latest

steps:
- uses: shivammathur/setup-php@15c43e89cdef867065b0213be354c2841860869e
with:
php-version: '7.3'
- uses: actions/checkout@v3
- name: Copy .env
run: php -r "file_exists('.env') || copy('.env.example', '.env');"
- name: Install Dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
- name: Execute tests (Unit and Feature tests) via PHPUnit
run: vendor/bin/phpunit --coverage-clover build/logs/clover.xml
- name: Upload coverage results to Coveralls
env:
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
composer global require php-coveralls/php-coveralls
php-coveralls --coverage_clover=build/logs/clover.xml -v
- uses: shivammathur/setup-php@v2
with:
php-version: '7.1'
- uses: actions/checkout@v3
- name: Copy .env
run: php -r "file_exists('.env') || copy('.env.example', '.env');"
- name: Install Dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
- name: Execute tests (Unit and Feature tests) via PHPUnit
run: vendor/bin/phpunit --coverage-clover build/logs/clover.xml
- name: Upload coverage results to Coveralls
env:
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
composer global require php-coveralls/php-coveralls
php-coveralls --coverage_clover=build/logs/clover.xml -v
2 changes: 1 addition & 1 deletion config/auto-doc.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
|
| You can use your custom documentation view
*/
'description' => 'swagger-description',
'description' => 'auto-doc::swagger-description',
'version' => '0.0.0',
'title' => 'Name of Your Application',
'termsOfService' => '',
Expand Down
17 changes: 8 additions & 9 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
version: '3'

services:

apache:
image: ronasit/php-nginx-dev:7.3
working_dir: /app
ports:
- 80:80
- 443:443
volumes:
- ./:/app
nginx:
image: ronasit/php-nginx-dev:7.3
working_dir: /app
ports:
- 80:80
- 443:443
volumes:
- ./:/app
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions src/AutoDocServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function boot()
$this->mergeConfigFrom(__DIR__ . '/../config/auto-doc.php', 'auto-doc');

$this->publishes([
__DIR__ . '/Views/swagger-description.blade.php' => resource_path('views/swagger-description.blade.php'),
__DIR__ . '/../resources/views/swagger-description.blade.php' => resource_path('views/swagger-description.blade.php'),
], 'view');

if (!$this->app->routesAreCached()) {
Expand All @@ -23,7 +23,7 @@ public function boot()
PushDocumentationCommand::class
]);

$this->loadViewsFrom(__DIR__ . '/Views', 'auto-doc');
$this->loadViewsFrom(__DIR__ . '/../resources/views', 'auto-doc');
}

public function register()
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion tests/LocalDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function setUp(): void
parent::setUp();

$this->tmpData = $this->getJsonFixture('tmp_data');
$this->productionFilePath = __DIR__ . '/storage/documentation.json';
$this->productionFilePath = __DIR__ . '/../storage/documentation.json';

config(['auto-doc.drivers.local.production_path' => $this->productionFilePath]);

Expand Down
5 changes: 4 additions & 1 deletion tests/RemoteDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
namespace RonasIT\Support\Tests;

use RonasIT\Support\AutoDoc\Drivers\RemoteDriver;
use RonasIT\Support\Tests\Support\Traits\MockTrait;
use Illuminate\Contracts\Filesystem\FileNotFoundException;

class RemoteDriverTest extends TestCase
{
use MockTrait;

protected $tmpData;
protected $removeDriverClass;
protected $tmpDocumentationFilePath;
Expand All @@ -16,7 +19,7 @@ public function setUp(): void
parent::setUp();

$this->tmpData = $this->getJsonFixture('tmp_data');
$this->tmpDocumentationFilePath = __DIR__ . '/storage/temp_documentation.json';
$this->tmpDocumentationFilePath = __DIR__ . '/../storage/temp_documentation.json';

$this->removeDriverClass = new RemoteDriver();
}
Expand Down
132 changes: 132 additions & 0 deletions tests/SwaggerServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php

namespace RonasIT\Support\Tests;

use Symfony\Component\HttpFoundation\Response;
use RonasIT\Support\AutoDoc\Services\SwaggerService;
use RonasIT\Support\AutoDoc\Exceptions\LegacyConfigException;
use RonasIT\Support\Tests\Support\Traits\SwaggerServiceMockTrait;
use RonasIT\Support\AutoDoc\Exceptions\InvalidDriverClassException;
use RonasIT\Support\AutoDoc\Exceptions\SwaggerDriverClassNotFoundException;

class SwaggerServiceTest extends TestCase
{
use SwaggerServiceMockTrait;

public function testConstructorInvalidConfigVersion()
{
config(['auto-doc.config_version' => '1.0']);

$this->expectException(LegacyConfigException::class);

app(SwaggerService::class);
}

public function testConstructorEmptyConfigVersion()
{
config(['auto-doc.config_version' => null]);

$this->expectException(LegacyConfigException::class);

app(SwaggerService::class);
}

public function testConstructorDriverClassNotExists()
{
config(['auto-doc.drivers.local.class' => 'NotExistsClass']);

$this->expectException(SwaggerDriverClassNotFoundException::class);

app(SwaggerService::class);
}

public function testConstructorDriverClassNotImplementsInterface()
{
config(['auto-doc.drivers.local.class' => TestCase::class]);

$this->expectException(InvalidDriverClassException::class);

app(SwaggerService::class);
}

public function testAddData()
{
$this->mockDriverSaveTmpData($this->getJsonFixture('tmp_data_search_roles_request'));

$service = app(SwaggerService::class);

$request = $this->generateRequest('get', 'users/roles', [
'with' => ['users']
], [], [
'Content-type' => 'application/json'
]);

$response = new Response($this->getFixture('example_success_roles_response.json'), 200, [
'Content-type' => 'application/json',
'authorization' => 'Bearer some_token'
]);

$service->addData($request, $response);
}

public function testAddDataWithJWTSecurity()
{
config(['auto-doc.security' => 'jwt']);

$this->mockDriverSaveTmpData($this->getJsonFixture('tmp_data_search_roles_request_jwt_security'));

$service = app(SwaggerService::class);

$request = $this->generateRequest('get', 'users/roles', [
'with' => ['users']
]);

$response = new Response($this->getFixture('example_success_roles_response.json'), 200, [
'Content-type' => 'application/json',
'authorization' => 'Bearer some_token'
]);

$service->addData($request, $response);
}

public function testAddDataWithLaravelSecurity()
{
config(['auto-doc.security' => 'laravel']);

$this->mockDriverSaveTmpData($this->getJsonFixture('tmp_data_search_roles_request_laravel_security'));

$service = app(SwaggerService::class);

$request = $this->generateRequest('get', 'users/roles', [
'with' => ['users']
]);

$response = new Response($this->getFixture('example_success_roles_response.json'), 200, [
'Content-type' => 'application/json',
'authorization' => 'Bearer some_token'
]);

$service->addData($request, $response);
}

public function testAddDataWithPathParameters()
{
$this->mockDriverSaveTmpData($this->getJsonFixture('tmp_data_get_user_request'));

$service = app(SwaggerService::class);

$request = $this->generateRequest('get', 'users/{id}/assign-role/{role-id}', [
'with' => ['role'],
'with_likes_count' => true
], [
'id' => 1,
'role-id' => 5
]);

$response = new Response($this->getFixture('example_success_user_response.json'), 200, [
'Content-type' => 'application/json'
]);

$service->addData($request, $response);
}
}
41 changes: 31 additions & 10 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@

namespace RonasIT\Support\Tests;

use Illuminate\Http\Request;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Facades\Route;
use Orchestra\Testbench\TestCase as BaseTest;
use RonasIT\Support\AutoDoc\AutoDocServiceProvider;
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;

class TestCase extends BaseTest
{
public function tearDown(): void
{
parent::tearDown();

$this->clearDirectory(__DIR__ . '/storage', ['.gitignore']);
$this->clearDirectory(__DIR__ . '/../storage', ['.gitignore']);
}

protected function getPackageProviders($app): array
Expand All @@ -24,15 +27,7 @@ protected function getPackageProviders($app): array

protected function defineEnvironment($app)
{
$app->useStoragePath(__DIR__ . '/storage');
}

protected function mockCLass($className, $methods = [])
{
return $this
->getMockBuilder($className)
->onlyMethods($methods)
->getMock();
$app->setBasePath(__DIR__ . '/..');
}

protected function getJsonFixture($name)
Expand Down Expand Up @@ -64,4 +59,30 @@ protected function clearDirectory($dirPath, $exceptPaths = [])
}
}
}

protected function generateRequest($type, $uri, $data = [], $pathParams = [], $headers = []): Request
{
$realUri = $uri;

foreach ($pathParams as $pathParam => $value) {
$realUri = str_replace($pathParam, $value, $uri);
}

$symfonyRequest = SymfonyRequest::create(
$this->prepareUrlForRequest($realUri),
strtoupper($type),
$data,
[],
[],
$this->transformHeadersToServerVars($headers)
);

$request = Request::createFromBase($symfonyRequest);

$request->setRouteResolver(function () use ($uri) {
return Route::get($uri);
});

return $request;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[
{
"id": 1,
"name": "admin",
"users": [
{
"id": 1,
"name": "admin"
}
]
},
{
"id": 2,
"name": "client",
"users": [
{
"id": 2,
"name": "first_client"
},
{
"id": 3,
"name": "second_client"
}
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"id": 2,
"name": "first_client",
"likes_count": 23,
"role": {
"id": 2,
"name": "client"
}
}
Loading

0 comments on commit 2d360db

Please sign in to comment.