Skip to content

Commit eb3151a

Browse files
committed
Increase UT coverage
1 parent b092331 commit eb3151a

19 files changed

+198
-45
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
/tests export-ignore
1010
/.editorconfig export-ignore
1111
/.php_cs.dist export-ignore
12+
/_ide_helper.php export-ignore
1213
/psalm.xml export-ignore
1314
/psalm.xml.dist export-ignore
1415
/testbench.yaml export-ignore

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ vendor
1212
node_modules
1313
.php-cs-fixer.cache
1414
.DS_Store
15+
_ide_helper.php

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
"php": "^7.4|^8.0",
2020
"illuminate/contracts": "^8.37|^9.0",
2121
"kitloong/laravel-app-logger": "^1.0",
22-
"spatie/laravel-package-tools": "^1.4.3"
22+
"spatie/laravel-package-tools": "^1.4.3",
23+
"ext-json": "*"
2324
},
2425
"require-dev": {
26+
"barryvdh/laravel-ide-helper": "^2.12",
2527
"brianium/paratest": "^6.2",
2628
"friendsofphp/php-cs-fixer": "^3.5",
2729
"nunomaduro/collision": "^5.3|^6.0",
@@ -43,6 +45,7 @@
4345
},
4446
"scripts": {
4547
"psalm": "vendor/bin/psalm",
48+
"phpcs": "vendor/bin/phpcs",
4649
"test": "./vendor/bin/testbench package:test --parallel --no-coverage",
4750
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"
4851
},

phpcs.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<?xml version="1.0"?>
22
<ruleset name="PSR2">
33
<description>Standard Based on PSR2</description>
4+
<file>src</file>
5+
<file>tests</file>
6+
47
<exclude-pattern type="relative-root">tests/*</exclude-pattern>
58
<rule ref="PSR2"/>
69
<rule ref="Generic.Files.LineLength">

src/LaravelRequestDocs.php

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
class LaravelRequestDocs
1313
{
14-
public function getDocs()
14+
public function getDocs(): array
1515
{
1616
$docs = [];
1717
$excludePatterns = config('request-docs.hide_matching') ?? [];
@@ -68,20 +68,22 @@ public function getControllersInfo(): array
6868
$routes = collect(Route::getRoutes());
6969
$onlyRouteStartWith = config('request-docs.only_route_uri_start_with') ?? '';
7070

71+
/** @var \Illuminate\Routing\Route $route */
7172
foreach ($routes as $route) {
7273
if ($onlyRouteStartWith && !Str::startsWith($route->uri, $onlyRouteStartWith)) {
7374
continue;
7475
}
7576

7677
try {
7778
$actionControllerName = $route->action['controller'] ?? $route->action["0"];
78-
/// Show Pnly Controller Name
79+
/// Show Only Controller Name
7980
$controllerFullPath = explode('@', $actionControllerName)[0];
8081
$getStartWord = strrpos(explode('@', $actionControllerName)[0], '\\') + 1;
8182
$controllerName = substr($controllerFullPath, $getStartWord);
8283

8384
$method = explode('@', $actionControllerName)[1] ?? '__invoke';
8485
$httpMethod = $route->methods[0];
86+
8587
foreach ($controllersInfo as $controllerInfo) {
8688
if ($controllerInfo['uri'] == $route->uri && $controllerInfo['httpMethod'] == $httpMethod) {
8789
// is duplicate
@@ -113,16 +115,17 @@ public function getControllersInfo(): array
113115
return $controllersInfo;
114116
}
115117

116-
public function appendRequestRules(array $controllersInfo)
118+
public function appendRequestRules(array $controllersInfo): array
117119
{
118120
foreach ($controllersInfo as $index => $controllerInfo) {
119121
$controller = $controllerInfo['controller_full_path'];
120122
$method = $controllerInfo['method'];
121123
try {
122124
$reflectionMethod = new ReflectionMethod($controller, $method);
123125
} catch (Throwable $e) {
126+
// Skip to next if controller is not exists.
124127
if (config('request-docs.debug')) {
125-
throw $e;
128+
throw $e; // @codeCoverageIgnore
126129
}
127130
continue;
128131
}
@@ -187,16 +190,6 @@ public function lrdDocComment($docComment): string
187190
return $lrdComment;
188191
}
189192

190-
// get text between first and last tag
191-
private function getTextBetweenTags($docComment, $tag1, $tag2)
192-
{
193-
$docComment = trim($docComment);
194-
$start = strpos($docComment, $tag1);
195-
$end = strpos($docComment, $tag2);
196-
$text = substr($docComment, $start + strlen($tag1), $end - $start - strlen($tag1));
197-
return $text;
198-
}
199-
200193
public function flattenRules($mixedRules)
201194
{
202195
$rules = [];

src/LaravelRequestDocsFacade.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Support\Facades\Facade;
66

77
/**
8+
* @codeCoverageIgnore
89
* @see \Rakutentech\LaravelRequestDocs\LaravelRequestDocs
910
*/
1011
class LaravelRequestDocsFacade extends Facade

src/LaravelRequestDocsServiceProvider.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ public function configurePackage(Package $package): void
2222
->hasViews()
2323
->hasAssets()
2424
->hasCommand(LaravelRequestDocsCommand::class);
25+
}
26+
27+
public function packageBooted()
28+
{
29+
parent::packageBooted();
2530

2631
Route::get(config('request-docs.url'), [\Rakutentech\LaravelRequestDocs\Controllers\LaravelRequestDocsController::class, 'index'])
2732
->name('request-docs.index')

tests/ExampleTest.php

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Rakutentech\LaravelRequestDocs\Tests\Feature\Commands;
4+
5+
use Illuminate\Support\Facades\File;
6+
use Rakutentech\LaravelRequestDocs\Tests\TestCase;
7+
8+
class LaravelRequestDocsCommandTest extends TestCase
9+
{
10+
protected function tearDown(): void
11+
{
12+
File::deleteDirectory(config('request-docs.docs_path'));
13+
File::deleteDirectory(base_path('docs/request-docs/'));
14+
15+
parent::tearDown();
16+
}
17+
18+
public function testHandle()
19+
{
20+
$this->assertFalse(File::exists(config('request-docs.docs_path') . '/index.html'));
21+
$this->assertFalse(File::exists(config('request-docs.docs_path') . '/lrd-openapi.json'));
22+
23+
$this->artisan('lrd:generate')
24+
->assertExitCode(0);
25+
26+
$this->assertTrue(File::exists(config('request-docs.docs_path') . '/index.html'));
27+
$this->assertTrue(File::exists(config('request-docs.docs_path') . '/lrd-openapi.json'));
28+
29+
config('request-docs.docs_path');
30+
}
31+
32+
public function testWillCreateDirectory()
33+
{
34+
File::deleteDirectory(config('request-docs.docs_path'));
35+
$this->assertFalse(File::exists(config('request-docs.docs_path')));
36+
37+
$this->artisan('lrd:generate')
38+
->assertExitCode(0);
39+
40+
$this->assertTrue(File::exists(config('request-docs.docs_path')));
41+
}
42+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Rakutentech\LaravelRequestDocs\Tests\Feature\Controllers;
4+
5+
use Illuminate\Http\Response;
6+
use Illuminate\Support\Facades\Config;
7+
use Rakutentech\LaravelRequestDocs\Tests\TestCase;
8+
9+
class LaravelRequestDocsControllerTest extends TestCase
10+
{
11+
public function testIndex()
12+
{
13+
$this->get(config('request-docs.url'))
14+
->assertStatus(Response::HTTP_OK);
15+
}
16+
17+
public function testSortDocsByDefault()
18+
{
19+
Config::set('request-docs.sort_by', 'default');
20+
$this->get(config('request-docs.url'))
21+
->assertStatus(Response::HTTP_OK);
22+
}
23+
24+
public function testOpenAPI()
25+
{
26+
$this->get(config('request-docs.url') . '?openapi=true')
27+
->assertStatus(Response::HTTP_OK);
28+
}
29+
}

tests/LRDOpenApiTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
<?php
22

33
namespace Rakutentech\LaravelRequestDocs\Tests;
4-
use Route;
54

65
class LRDOpenApiTest extends TestCase
76
{
87
public function testDocsCount()
98
{
109
$docs = $this->lrd->getDocs();
1110
$openApi = $this->lrdToOpenApi->openApi($docs)->toArray();
12-
$routes = collect(Route::getRoutes());
1311

14-
$this->assertSame($routes->count(), count($docs));
12+
$this->assertSame($this->countRoutesWithLRDDoc(), count($docs));
1513

1614
$countRoutes = 0;
1715
foreach ($openApi["paths"] as $path) {
1816
$countRoutes += count(array_keys($path));
1917
}
2018

21-
$this->assertSame($routes->count(), $countRoutes);
19+
$this->assertSame($this->countRoutesWithLRDDoc(), $countRoutes);
2220
}
2321
}

tests/LRDTest.php

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
11
<?php
22

33
namespace Rakutentech\LaravelRequestDocs\Tests;
4-
use Route;
4+
5+
use Illuminate\Support\Facades\Config;
56

67
class LRDTest extends TestCase
78
{
8-
public function testDocsCount()
9+
public function testGetDocs()
910
{
1011
$docs = $this->lrd->getDocs();
11-
$routes = collect(Route::getRoutes());
1212

13-
$this->assertSame($routes->count(), count($docs));
13+
$this->assertSame($this->countRoutesWithLRDDoc(), count($docs));
14+
15+
$docSize = 9;
16+
$firstDoc = $docs[0];
17+
$this->assertCount($docSize, $firstDoc);
18+
$this->assertArrayHasKey('uri', $firstDoc);
19+
$this->assertArrayHasKey('methods', $firstDoc);
20+
$this->assertArrayHasKey('middlewares', $firstDoc);
21+
$this->assertArrayHasKey('controller', $firstDoc);
22+
$this->assertArrayHasKey('controller_full_path', $firstDoc);
23+
$this->assertArrayHasKey('method', $firstDoc);
24+
$this->assertArrayHasKey('httpMethod', $firstDoc);
25+
$this->assertArrayHasKey('rules', $firstDoc);
26+
$this->assertArrayHasKey('docBlock', $firstDoc);
1427
}
1528

1629
public function testDocsCanFetchAllMethods()
@@ -25,18 +38,12 @@ public function testDocsCanFetchAllMethods()
2538
$this->assertSame(['DELETE', 'GET', 'HEAD', 'POST', 'PUT'], $methods);
2639
}
2740

28-
public function testDocsCanFetchInfo()
41+
public function testOnlyRouteURIStartWith()
2942
{
43+
Config::set('request-docs.only_route_uri_start_with', 'welcome');
3044
$docs = $this->lrd->getDocs();
3145
foreach ($docs as $doc) {
32-
$this->assertNotEmpty($doc['rules']);
33-
$this->assertNotEmpty($doc['methods']);
34-
// $this->assertNotEmpty($doc['middlewares']); //todo: add middlewares to test
35-
$this->assertNotEmpty($doc['controller']);
36-
$this->assertNotEmpty($doc['controller_full_path']);
37-
$this->assertNotEmpty($doc['method']);
38-
$this->assertNotEmpty($doc['httpMethod']);
39-
$this->assertNotEmpty($doc['rules']);
46+
$this->assertStringStartsWith('welcome', $doc['uri']);
4047
}
4148
}
4249
}

tests/TestCase.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,17 @@ public function getEnvironmentSetUp($app)
3535
public function registerRoutes() {
3636
Route::get('/', [TestControllers\WelcomeController::class, 'index']);
3737
Route::get('welcome', [TestControllers\WelcomeController::class, 'index']);
38-
Route::post('welcome', [TestControllers\WelcomeController::class, 'store']);
38+
Route::post('welcome', [TestControllers\WelcomeController::class, 'store'])->middleware('auth:api');
3939
Route::put('welcome', [TestControllers\WelcomeController::class, 'edit']);
4040
Route::delete('welcome', [TestControllers\WelcomeController::class, 'destroy']);
41+
Route::get('single', TestControllers\SingleActionController::class);
42+
43+
// Expected to be skipped
44+
Route::get('telescope', [TestControllers\TelescopeController::class, 'index']);
45+
}
46+
47+
protected function countRoutesWithLRDDoc(): int
48+
{
49+
return count(Route::getRoutes()) - 2; // Exclude `telescope`, `request-docs`
4150
}
4251
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Rakutentech\LaravelRequestDocs\Tests\TestControllers;
4+
5+
/**
6+
* Single action controller with a single `__invoke` method
7+
*
8+
* @see https://laravel.com/docs/controllers#single-action-controllers
9+
*/
10+
class SingleActionController
11+
{
12+
public function __invoke()
13+
{
14+
return 1;
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Rakutentech\LaravelRequestDocs\Tests\TestControllers;
4+
5+
class TelescopeController
6+
{
7+
/**
8+
* For `config('request-docs.hide_matching')` test.
9+
*
10+
* @return int
11+
*/
12+
public function index()
13+
{
14+
return 1;
15+
}
16+
}

tests/TestControllers/WelcomeController.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,23 @@
99

1010
class WelcomeController
1111
{
12+
/**
13+
* @lrd:start
14+
* #Hello markdown
15+
* ## Documentation for /my route
16+
* @lrd:end
17+
*/
1218
public function index(WelcomeIndexRequest $request)
1319
{
1420
return 1;
1521
}
1622

23+
/**
24+
* @QAparam search_string string
25+
* @QAparam search_array array
26+
* @QAparam search_integer integer
27+
* @QAparam search_boolean boolean
28+
*/
1729
public function edit(WelcomeEditRequest $request)
1830
{
1931
return 1;

tests/TestRequests/WelcomeIndexRequest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Rakutentech\LaravelRequestDocs\Tests\TestRequests;
44

55
use Illuminate\Foundation\Http\FormRequest;
6+
use Rakutentech\LaravelRequestDocs\Tests\TestRules\Uppercase;
67

78
class WelcomeIndexRequest extends FormRequest
89
{
@@ -28,6 +29,10 @@ protected function prepareForValidation()
2829
public function rules()
2930
{
3031
return [
32+
'name' => ['nullable', 'string', 'min:5', 'max:255'],
33+
'title' => new Uppercase(),
34+
'file' => 'file',
35+
'image' => 'image',
3136
'page' => 'nullable|integer|min:1',
3237
'per_page' => 'nullable|integer|min:1|max:100',
3338
];

0 commit comments

Comments
 (0)