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+
}

0 commit comments

Comments
 (0)