Skip to content

Commit 2d360db

Browse files
committed
feat: format yml files, move views to the resources folder, move storage to base path, implement swagger service tests;
1 parent b00e0ac commit 2d360db

29 files changed

+543
-46
lines changed

.github/workflows/laravel.yml

+20-22
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,28 @@
1-
name: Laravel
1+
name: run-tests-with-coverage
22

33
on:
44
push:
5-
branches: [ "master" ]
5+
branches: ["master"]
66
pull_request:
7-
branches: [ "master" ]
7+
branches: ["master"]
88

99
jobs:
10-
laravel-tests:
11-
10+
tests:
1211
runs-on: ubuntu-latest
13-
1412
steps:
15-
- uses: shivammathur/setup-php@15c43e89cdef867065b0213be354c2841860869e
16-
with:
17-
php-version: '7.3'
18-
- uses: actions/checkout@v3
19-
- name: Copy .env
20-
run: php -r "file_exists('.env') || copy('.env.example', '.env');"
21-
- name: Install Dependencies
22-
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
23-
- name: Execute tests (Unit and Feature tests) via PHPUnit
24-
run: vendor/bin/phpunit --coverage-clover build/logs/clover.xml
25-
- name: Upload coverage results to Coveralls
26-
env:
27-
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
28-
run: |
29-
composer global require php-coveralls/php-coveralls
30-
php-coveralls --coverage_clover=build/logs/clover.xml -v
13+
- uses: shivammathur/setup-php@v2
14+
with:
15+
php-version: '7.1'
16+
- uses: actions/checkout@v3
17+
- name: Copy .env
18+
run: php -r "file_exists('.env') || copy('.env.example', '.env');"
19+
- name: Install Dependencies
20+
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
21+
- name: Execute tests (Unit and Feature tests) via PHPUnit
22+
run: vendor/bin/phpunit --coverage-clover build/logs/clover.xml
23+
- name: Upload coverage results to Coveralls
24+
env:
25+
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
26+
run: |
27+
composer global require php-coveralls/php-coveralls
28+
php-coveralls --coverage_clover=build/logs/clover.xml -v

config/auto-doc.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
|
3232
| You can use your custom documentation view
3333
*/
34-
'description' => 'swagger-description',
34+
'description' => 'auto-doc::swagger-description',
3535
'version' => '0.0.0',
3636
'title' => 'Name of Your Application',
3737
'termsOfService' => '',

docker-compose.yml

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
version: '3'
22

33
services:
4-
5-
apache:
6-
image: ronasit/php-nginx-dev:7.3
7-
working_dir: /app
8-
ports:
9-
- 80:80
10-
- 443:443
11-
volumes:
12-
- ./:/app
4+
nginx:
5+
image: ronasit/php-nginx-dev:7.3
6+
working_dir: /app
7+
ports:
8+
- 80:80
9+
- 443:443
10+
volumes:
11+
- ./:/app

src/AutoDocServiceProvider.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public function boot()
1212
$this->mergeConfigFrom(__DIR__ . '/../config/auto-doc.php', 'auto-doc');
1313

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

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

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

2929
public function register()
File renamed without changes.

tests/LocalDriverTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function setUp(): void
1717
parent::setUp();
1818

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

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

tests/RemoteDriverTest.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
namespace RonasIT\Support\Tests;
44

55
use RonasIT\Support\AutoDoc\Drivers\RemoteDriver;
6+
use RonasIT\Support\Tests\Support\Traits\MockTrait;
67
use Illuminate\Contracts\Filesystem\FileNotFoundException;
78

89
class RemoteDriverTest extends TestCase
910
{
11+
use MockTrait;
12+
1013
protected $tmpData;
1114
protected $removeDriverClass;
1215
protected $tmpDocumentationFilePath;
@@ -16,7 +19,7 @@ public function setUp(): void
1619
parent::setUp();
1720

1821
$this->tmpData = $this->getJsonFixture('tmp_data');
19-
$this->tmpDocumentationFilePath = __DIR__ . '/storage/temp_documentation.json';
22+
$this->tmpDocumentationFilePath = __DIR__ . '/../storage/temp_documentation.json';
2023

2124
$this->removeDriverClass = new RemoteDriver();
2225
}

tests/SwaggerServiceTest.php

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
3+
namespace RonasIT\Support\Tests;
4+
5+
use Symfony\Component\HttpFoundation\Response;
6+
use RonasIT\Support\AutoDoc\Services\SwaggerService;
7+
use RonasIT\Support\AutoDoc\Exceptions\LegacyConfigException;
8+
use RonasIT\Support\Tests\Support\Traits\SwaggerServiceMockTrait;
9+
use RonasIT\Support\AutoDoc\Exceptions\InvalidDriverClassException;
10+
use RonasIT\Support\AutoDoc\Exceptions\SwaggerDriverClassNotFoundException;
11+
12+
class SwaggerServiceTest extends TestCase
13+
{
14+
use SwaggerServiceMockTrait;
15+
16+
public function testConstructorInvalidConfigVersion()
17+
{
18+
config(['auto-doc.config_version' => '1.0']);
19+
20+
$this->expectException(LegacyConfigException::class);
21+
22+
app(SwaggerService::class);
23+
}
24+
25+
public function testConstructorEmptyConfigVersion()
26+
{
27+
config(['auto-doc.config_version' => null]);
28+
29+
$this->expectException(LegacyConfigException::class);
30+
31+
app(SwaggerService::class);
32+
}
33+
34+
public function testConstructorDriverClassNotExists()
35+
{
36+
config(['auto-doc.drivers.local.class' => 'NotExistsClass']);
37+
38+
$this->expectException(SwaggerDriverClassNotFoundException::class);
39+
40+
app(SwaggerService::class);
41+
}
42+
43+
public function testConstructorDriverClassNotImplementsInterface()
44+
{
45+
config(['auto-doc.drivers.local.class' => TestCase::class]);
46+
47+
$this->expectException(InvalidDriverClassException::class);
48+
49+
app(SwaggerService::class);
50+
}
51+
52+
public function testAddData()
53+
{
54+
$this->mockDriverSaveTmpData($this->getJsonFixture('tmp_data_search_roles_request'));
55+
56+
$service = app(SwaggerService::class);
57+
58+
$request = $this->generateRequest('get', 'users/roles', [
59+
'with' => ['users']
60+
], [], [
61+
'Content-type' => 'application/json'
62+
]);
63+
64+
$response = new Response($this->getFixture('example_success_roles_response.json'), 200, [
65+
'Content-type' => 'application/json',
66+
'authorization' => 'Bearer some_token'
67+
]);
68+
69+
$service->addData($request, $response);
70+
}
71+
72+
public function testAddDataWithJWTSecurity()
73+
{
74+
config(['auto-doc.security' => 'jwt']);
75+
76+
$this->mockDriverSaveTmpData($this->getJsonFixture('tmp_data_search_roles_request_jwt_security'));
77+
78+
$service = app(SwaggerService::class);
79+
80+
$request = $this->generateRequest('get', 'users/roles', [
81+
'with' => ['users']
82+
]);
83+
84+
$response = new Response($this->getFixture('example_success_roles_response.json'), 200, [
85+
'Content-type' => 'application/json',
86+
'authorization' => 'Bearer some_token'
87+
]);
88+
89+
$service->addData($request, $response);
90+
}
91+
92+
public function testAddDataWithLaravelSecurity()
93+
{
94+
config(['auto-doc.security' => 'laravel']);
95+
96+
$this->mockDriverSaveTmpData($this->getJsonFixture('tmp_data_search_roles_request_laravel_security'));
97+
98+
$service = app(SwaggerService::class);
99+
100+
$request = $this->generateRequest('get', 'users/roles', [
101+
'with' => ['users']
102+
]);
103+
104+
$response = new Response($this->getFixture('example_success_roles_response.json'), 200, [
105+
'Content-type' => 'application/json',
106+
'authorization' => 'Bearer some_token'
107+
]);
108+
109+
$service->addData($request, $response);
110+
}
111+
112+
public function testAddDataWithPathParameters()
113+
{
114+
$this->mockDriverSaveTmpData($this->getJsonFixture('tmp_data_get_user_request'));
115+
116+
$service = app(SwaggerService::class);
117+
118+
$request = $this->generateRequest('get', 'users/{id}/assign-role/{role-id}', [
119+
'with' => ['role'],
120+
'with_likes_count' => true
121+
], [
122+
'id' => 1,
123+
'role-id' => 5
124+
]);
125+
126+
$response = new Response($this->getFixture('example_success_user_response.json'), 200, [
127+
'Content-type' => 'application/json'
128+
]);
129+
130+
$service->addData($request, $response);
131+
}
132+
}

tests/TestCase.php

+31-10
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@
22

33
namespace RonasIT\Support\Tests;
44

5+
use Illuminate\Http\Request;
56
use Illuminate\Filesystem\Filesystem;
7+
use Illuminate\Support\Facades\Route;
68
use Orchestra\Testbench\TestCase as BaseTest;
79
use RonasIT\Support\AutoDoc\AutoDocServiceProvider;
10+
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
811

912
class TestCase extends BaseTest
1013
{
1114
public function tearDown(): void
1215
{
1316
parent::tearDown();
1417

15-
$this->clearDirectory(__DIR__ . '/storage', ['.gitignore']);
18+
$this->clearDirectory(__DIR__ . '/../storage', ['.gitignore']);
1619
}
1720

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

2528
protected function defineEnvironment($app)
2629
{
27-
$app->useStoragePath(__DIR__ . '/storage');
28-
}
29-
30-
protected function mockCLass($className, $methods = [])
31-
{
32-
return $this
33-
->getMockBuilder($className)
34-
->onlyMethods($methods)
35-
->getMock();
30+
$app->setBasePath(__DIR__ . '/..');
3631
}
3732

3833
protected function getJsonFixture($name)
@@ -64,4 +59,30 @@ protected function clearDirectory($dirPath, $exceptPaths = [])
6459
}
6560
}
6661
}
62+
63+
protected function generateRequest($type, $uri, $data = [], $pathParams = [], $headers = []): Request
64+
{
65+
$realUri = $uri;
66+
67+
foreach ($pathParams as $pathParam => $value) {
68+
$realUri = str_replace($pathParam, $value, $uri);
69+
}
70+
71+
$symfonyRequest = SymfonyRequest::create(
72+
$this->prepareUrlForRequest($realUri),
73+
strtoupper($type),
74+
$data,
75+
[],
76+
[],
77+
$this->transformHeadersToServerVars($headers)
78+
);
79+
80+
$request = Request::createFromBase($symfonyRequest);
81+
82+
$request->setRouteResolver(function () use ($uri) {
83+
return Route::get($uri);
84+
});
85+
86+
return $request;
87+
}
6788
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[
2+
{
3+
"id": 1,
4+
"name": "admin",
5+
"users": [
6+
{
7+
"id": 1,
8+
"name": "admin"
9+
}
10+
]
11+
},
12+
{
13+
"id": 2,
14+
"name": "client",
15+
"users": [
16+
{
17+
"id": 2,
18+
"name": "first_client"
19+
},
20+
{
21+
"id": 3,
22+
"name": "second_client"
23+
}
24+
]
25+
}
26+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"id": 2,
3+
"name": "first_client",
4+
"likes_count": 23,
5+
"role": {
6+
"id": 2,
7+
"name": "client"
8+
}
9+
}

0 commit comments

Comments
 (0)