Skip to content

Commit 76a78d4

Browse files
author
Andrey Helldar
authored
Merge pull request #34 from andrey-helldar/coverage
Coverage
2 parents f9160e8 + 2057a65 commit 76a78d4

File tree

7 files changed

+144
-46
lines changed

7 files changed

+144
-46
lines changed

.github/workflows/coverage.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
- name: Execute tests
3232
run: |
3333
mkdir -p build/logs
34-
vendor/bin/phpunit --coverage-clover=coverage.clover
34+
vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover
3535
3636
- name: Export
3737
run: |

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"require": {
2222
"php": "^7.2|^8.0",
2323
"ext-json": "*",
24-
"andrey-helldar/laravel-routes-core": "^1.8.2",
24+
"andrey-helldar/laravel-routes-core": "^1.9",
2525
"andrey-helldar/support": "^1.22",
2626
"illuminate/contracts": "^5.0|^6.0|^7.0|^8.0",
2727
"illuminate/http": "^5.0|^6.0|^7.0|^8.0",

phpunit.xml

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,32 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit backupGlobals="false"
3-
backupStaticAttributes="false"
4-
bootstrap="phpunit.php"
5-
colors="true"
6-
convertErrorsToExceptions="true"
7-
convertNoticesToExceptions="true"
8-
convertWarningsToExceptions="true"
9-
processIsolation="false"
10-
stopOnError="false"
11-
stopOnFailure="false"
12-
verbose="true"
2+
<phpunit
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
backupGlobals="false"
6+
backupStaticAttributes="false"
7+
bootstrap="phpunit.php"
8+
colors="true"
9+
convertErrorsToExceptions="true"
10+
convertNoticesToExceptions="true"
11+
convertWarningsToExceptions="true"
12+
processIsolation="false"
13+
stopOnError="false"
14+
stopOnFailure="false"
15+
verbose="true"
1316
>
17+
<coverage processUncoveredFiles="true">
18+
<include>
19+
<directory suffix=".php">./src</directory>
20+
</include>
21+
<exclude>
22+
<file>src/ServiceProvider.php</file>
23+
</exclude>
24+
<report>
25+
<clover outputFile="build/logs/clover.xml"/>
26+
<html outputDirectory="build/logs/coverage"/>
27+
<text outputFile="build/logs/coverage.txt"/>
28+
</report>
29+
</coverage>
1430
<php>
1531
<env name="APP_KEY" value="AckfSECXIvnK5r28GVIWUAxmbBSjTsmF"/>
1632
<env name="APP_ENV" value="testing"/>
@@ -21,17 +37,4 @@
2137
<directory suffix="Test.php">./tests</directory>
2238
</testsuite>
2339
</testsuites>
24-
<filter>
25-
<whitelist processUncoveredFilesFromWhitelist="true">
26-
<directory suffix=".php">./src</directory>
27-
<exclude>
28-
<file>src/ServiceProvider.php</file>
29-
</exclude>
30-
</whitelist>
31-
</filter>
32-
<logging>
33-
<log type="coverage-clover" target="build/logs/clover.xml"/>
34-
<log type="coverage-text" target="build/logs/coverage.txt"/>
35-
<log type="coverage-html" target="build/logs/coverage"/>
36-
</logging>
3740
</phpunit>

src/Facades/Cache.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace PrettyRoutes\Facades;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
use PrettyRoutes\Support\Cache as Support;
7+
8+
/**
9+
* @method static \PrettyRoutes\Support\Cache when($value);
10+
* @method static bool routeClear();
11+
*/
12+
final class Cache extends Facade
13+
{
14+
protected static function getFacadeAccessor()
15+
{
16+
return Support::class;
17+
}
18+
}

src/Http/PrettyRoutesController.php

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
namespace PrettyRoutes\Http;
44

55
use Helldar\LaravelRoutesCore\Support\Routes;
6+
use Illuminate\Contracts\View\View;
7+
use Illuminate\Http\JsonResponse;
68
use Illuminate\Routing\Controller as BaseController;
7-
use Illuminate\Support\Facades\Artisan;
9+
use PrettyRoutes\Facades\Cache;
10+
use PrettyRoutes\Support\Config;
811

912
class PrettyRoutesController extends BaseController
1013
{
@@ -13,7 +16,7 @@ class PrettyRoutesController extends BaseController
1316
*
1417
* @return \Illuminate\Contracts\View\View
1518
*/
16-
public function show()
19+
public function show(): View
1720
{
1821
return view('pretty-routes::layout');
1922
}
@@ -22,37 +25,33 @@ public function show()
2225
* Getting a list of routes.
2326
*
2427
* @param \Helldar\LaravelRoutesCore\Support\Routes $routes
28+
* @param \PrettyRoutes\Support\Config $config
2529
*
2630
* @return \Illuminate\Http\JsonResponse
2731
*/
28-
public function routes(Routes $routes)
32+
public function routes(Routes $routes, Config $config): JsonResponse
2933
{
30-
$content = $routes
31-
->setApiMiddlewares((array) config('pretty-routes.api_middleware'))
32-
->setWebMiddlewares((array) config('pretty-routes.web_middleware'))
33-
->setHideMethods(config('pretty-routes.hide_methods', []))
34-
->setHideMatching(config('pretty-routes.hide_matching', []))
35-
->setDomainForce(config('pretty-routes.domain_force', false))
36-
->setUrl(config('app.url'))
37-
->setNamespace(config('modules.namespace'))
38-
->get();
34+
$routes->setFromConfig($config);
3935

40-
return response()->json($content);
36+
return response()->json(
37+
$routes->get()
38+
);
4139
}
4240

4341
/**
4442
* Clearing cached routes.
4543
*
4644
* @return \Illuminate\Http\JsonResponse
4745
*/
48-
public function clear()
46+
public function clear(): JsonResponse
4947
{
50-
if (config('app.env') !== 'production' && (bool) config('app.debug') === true) {
51-
Artisan::call('route:clear');
52-
53-
return response()->json('ok');
54-
}
48+
return Cache::when($this->allow())->routeClear()
49+
? response()->json('ok')
50+
: response()->json('disabled', 400);
51+
}
5552

56-
return response()->json('disabled', 400);
53+
protected function allow(): bool
54+
{
55+
return config('app.env') !== 'production' && (bool) config('app.debug') === true;
5756
}
5857
}

src/Support/Cache.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace PrettyRoutes\Support;
4+
5+
use Illuminate\Support\Facades\Artisan;
6+
7+
final class Cache
8+
{
9+
protected $when = true;
10+
11+
public function when($value): self
12+
{
13+
$this->when = (bool) $value;
14+
15+
return $this;
16+
}
17+
18+
public function routeClear(): bool
19+
{
20+
return $this->when && $this->clear('route:clear');
21+
}
22+
23+
protected function clear(string $command): bool
24+
{
25+
Artisan::call($command);
26+
27+
return true;
28+
}
29+
}

src/Support/Config.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace PrettyRoutes\Support;
4+
5+
use Helldar\LaravelRoutesCore\Contracts\Config as ConfigContract;
6+
use Illuminate\Support\Facades\Config as Conf;
7+
8+
final class Config implements ConfigContract
9+
{
10+
public function getApiMiddleware(): array
11+
{
12+
return (array) $this->get('api_middleware');
13+
}
14+
15+
public function getWebMiddleware(): array
16+
{
17+
return (array) $this->get('web_middleware');
18+
}
19+
20+
public function getHideMethods(): array
21+
{
22+
return $this->get('hide_methods', []);
23+
}
24+
25+
public function getHideMatching(): array
26+
{
27+
return $this->get('hide_matching', []);
28+
}
29+
30+
public function getDomainForce(): bool
31+
{
32+
return (bool) $this->get('domain_force');
33+
}
34+
35+
public function getUrl(): string
36+
{
37+
return Conf::get('app.url');
38+
}
39+
40+
public function getNamespace(): ?string
41+
{
42+
return Conf::get('modules.namespace');
43+
}
44+
45+
protected function get(string $key, $default = null)
46+
{
47+
return Conf::get('pretty-routes.' . $key, $default);
48+
}
49+
}

0 commit comments

Comments
 (0)