Skip to content

Commit e241c7d

Browse files
committed
Filters tests and refactoring.
- Api-router path can be defined in config. - Simplify RouteCollection class. - Tests for RouteCollection class. - PHPUnit support.
1 parent 873f2de commit e241c7d

File tree

11 files changed

+240
-62
lines changed

11 files changed

+240
-62
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
/.idea/
2-
/node_modules/
2+
/node_modules/
3+
/vendor/
4+
/composer.lock

composer.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@
1212
"require": {
1313
"php": ">=5.4.0"
1414
},
15+
"require-dev": {
16+
"laravel/framework": "5.2.*",
17+
"phpunit/phpunit": "~4.0"
18+
},
19+
"autoload-dev": {
20+
"classmap": [
21+
"tests/TestCase.php"
22+
]
23+
},
1524
"autoload": {
1625
"psr-4": {
1726
"Asvae\\ApiTester\\": "src/"

config/api-tester.php

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,47 +26,65 @@
2626

2727
'middleware' => ['web'],
2828

29-
3029
/*
3130
|--------------------------------------------------------------------------
32-
| Middleware
31+
| Route repository
3332
|--------------------------------------------------------------------------
3433
|
35-
| Define list of middleware(s), that should be used for api-tester.
36-
| CRSF token will be handled automatically.
34+
| Specify class for RouteRepository.
3735
|
3836
*/
37+
3938
'repository' => \Asvae\ApiTester\DefaultRouteRepository::class,
4039

4140
/*
4241
|--------------------------------------------------------------------------
43-
| Matching pattern
42+
| Default route
4443
|--------------------------------------------------------------------------
45-
| Represent only routes that matching given pattern.
46-
|
47-
| Example:
4844
|
49-
| 'match' => [
50-
| [
51-
| 'path' => '#api/v(1|2|3)/.*#',
52-
| 'name' => '#.*#',
53-
'method' => '#(GET|POST|PUT|PATCH|DELETE)#'
54-
| ]
55-
] ]
45+
| Define the route for api router.
46+
| http://your-site.com/[route]
5647
|
5748
*/
58-
'match' => [
5949

60-
],
50+
'route' => 'api-tester',
6151

6252
/*
6353
|--------------------------------------------------------------------------
64-
| Except pattern
54+
| Filter routes
6555
|--------------------------------------------------------------------------
66-
| Except routes that matching given pattern.
56+
| All your routes will be filtered via given patterns. Both include and
57+
| exclude are always applied. You can also use regex when needed.
58+
|
59+
| ## Examples
60+
|
61+
| ### Include all
62+
| 'include' => [[]]
63+
|
64+
| ### Include some routes
65+
| 'include' => [
66+
| 'api/users',
67+
| 'api/sales',
68+
| // ...
69+
| ]
70+
|
71+
| ### Include advanced
72+
| 'include' => [
73+
| [
74+
| 'path' => 'api/v(1|2|3)/.*',
75+
| 'name' => '.*',
76+
| 'method' => '(GET|POST|PUT|PATCH|DELETE)'
77+
| ],
78+
| // ...
79+
| ]
80+
|
81+
| ### Include all except 'users'
82+
| 'include' => [[]],
83+
| 'exclude' => ['api/users'],
6784
|
6885
*/
69-
'except' => [
7086

71-
],
87+
'include' => [[]],
88+
89+
'exclude' => [],
7290
];

phpunit.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
colors="true"
5+
bootstrap="vendor/autoload.php"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false">
11+
<testsuites>
12+
<testsuite name="Application Test Suite">
13+
<directory>./tests/</directory>
14+
</testsuite>
15+
</testsuites>
16+
<filter>
17+
<whitelist>
18+
<directory suffix=".php">src/</directory>
19+
</whitelist>
20+
</filter>
21+
</phpunit>

src/DefaultRouteRepository.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@
55
use Asvae\ApiTester\Contracts\RouteRepositoryInterface;
66
use Illuminate\Routing\Router;
77

8-
/**
9-
* Class DefaultRouteRepository
10-
*
11-
* @package \Asvae\ApiTester
12-
*/
138
class DefaultRouteRepository implements RouteRepositoryInterface
149
{
1510

src/Http/Controllers/ApiTesterController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ public function index()
2323
public function routes(RouteRepositoryInterface $repository)
2424
{
2525
$data = $repository->get(
26-
config('api-tester.match'),
27-
config('api-tester.except')
26+
config('api-tester.include'),
27+
config('api-tester.exclude')
2828
);
2929

3030
return response()->json(compact('data'));

src/Http/routes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
Route::get('api-tester', 'ApiTesterController@index');
3+
Route::get(config('api-tester.route'), 'ApiTesterController@index');
44
Route::post('_api-tester/routes', 'ApiTesterController@routes');
55
Route::get('_api-tester/assets/{file}', 'AssetsController@index');
66
Route::get('_api-tester/test-routes/{type}', 'ApiTesterController@testRoutes');

src/RouteCollection.php

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,51 +12,70 @@
1212
class RouteCollection extends Collection
1313
{
1414
/**
15-
* Return collection of routes that matched pattern
15+
* Include routes that match patterns.
1616
*
17-
* @param array $matches
17+
* @param array <array|string> $patterns
1818
*
1919
* @return static
2020
*/
21-
public function filterMatch(array $matches = [])
21+
public function filterMatch(array $patterns = [])
2222
{
23-
if (!empty($matches)) {
24-
return $this->filter(function ($route, $key) use ($matches) {
25-
$isMatched = true;
26-
27-
foreach ($matches as $match) {
28-
29-
foreach ($route as $index => $value) {
30-
if (!array_key_exists($index, $match)) {
31-
continue;
32-
}
23+
// String pattern is assumed to be path.
24+
foreach ($patterns as $key => $pattern) {
25+
if (is_string($pattern)) {
26+
$patterns[$key] = ['path' => $pattern];
27+
}
28+
}
3329

34-
if (!preg_match($match[$index], $value)) {
35-
$isMatched = false;
36-
}
37-
}
30+
return $this->filter(function ($route) use ($patterns) {
31+
// If any of patterns matches the route passes.
32+
foreach ($patterns as $pattern) {
33+
if ($this->isRouteMatchesPattern($route, $pattern)) {
34+
return true;
3835
}
36+
}
3937

40-
return $isMatched;
41-
});
42-
}
43-
44-
return $this;
38+
// If all patterns don't match - route is filtered out.
39+
return false;
40+
});
4541
}
4642

43+
4744
/**
48-
* Return collection of routes except matched pattern
45+
* Exclude routes that match patterns.
4946
*
50-
* @param array $exceptions
47+
* @param array <array|string> $patterns
5148
*
5249
* @return static
5350
*/
54-
public function filterExcept(array $exceptions = [])
51+
public function filterExcept(array $patterns = [])
52+
{
53+
if (empty($patterns)) {
54+
return $this;
55+
}
56+
57+
$toExclude = $this->filterMatch($patterns)->keys()->toArray();
58+
59+
return $this->except($toExclude);
60+
}
61+
62+
/**
63+
* @param array $route
64+
* @param array $pattern
65+
* @return bool
66+
*/
67+
private function isRouteMatchesPattern(array $route, array $pattern)
5568
{
56-
if (!empty($exceptions)) {
57-
return $this->except($this->filterMatch($exceptions)->keys()->toArray());
69+
foreach ($route as $key => $value) {
70+
if (! array_key_exists($key, $pattern)) {
71+
continue;
72+
}
73+
74+
$regex = '~'.$pattern[$key].'~';
75+
76+
return ! ! preg_match($regex, $value);
5877
}
5978

60-
return $this;
79+
return true;
6180
}
6281
}

src/ServiceProvider.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,25 @@
22

33
namespace Asvae\ApiTester;
44

5+
use Asvae\ApiTester\Contracts\RouteRepositoryInterface;
56
use Asvae\ApiTester\Providers\RouteServiceProvider;
67

7-
88
class ServiceProvider extends \Illuminate\Support\ServiceProvider
99
{
1010
public function register()
1111
{
1212
$this->app->register(RouteServiceProvider::class);
13-
$this->mergeConfigFrom(__DIR__ . '/../config/api-tester.php', 'api-tester');
13+
$this->mergeConfigFrom(__DIR__.'/../config/api-tester.php',
14+
'api-tester');
1415

15-
$this->app->bind(\Asvae\ApiTester\Contracts\RouteRepositoryInterface::class, config('api-tester.repository'));
16+
$this->app->bind(RouteRepositoryInterface::class,
17+
config('api-tester.repository'));
1618
}
1719

1820
public function boot()
1921
{
2022
$this->publishes([
21-
__DIR__ . '/config/api-tester.php' => config_path('api-tester.php'),
23+
__DIR__.'/config/api-tester.php' => config_path('api-tester.php'),
2224
], 'config');
2325
}
2426
}

0 commit comments

Comments
 (0)