Skip to content

Commit a41f780

Browse files
committed
Ensure that api filters are the first in action array
1 parent 1e9320d commit a41f780

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

src/Routing/Route.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,12 @@ protected function parseAction($action)
4242
}
4343

4444
foreach ([static::API_FILTER_THROTTLE, static::API_FILTER_AUTH] as $filter) {
45-
if (array_search($filter, $action['before']) === false) {
46-
array_unshift($action['before'], $filter);
45+
$key = array_search($filter, $action['before']);
46+
if ($key !== false) {
47+
unset($action['before'][$key]);
4748
}
49+
50+
array_unshift($action['before'], $filter);
4851
}
4952

5053
return $action;

tests/Routing/RouteTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Dingo\Api\Tests\Routing;
4+
5+
use Dingo\Api\Routing\Route;
6+
use Mockery as m;
7+
use PHPUnit_Framework_TestCase;
8+
9+
class RouteTest extends PHPUnit_Framework_TestCase
10+
{
11+
12+
public function tearDown()
13+
{
14+
m::close();
15+
}
16+
17+
public function testApiFiltersAreSet()
18+
{
19+
// Example : my_uri is a route with a 'foo' before filter
20+
$route = new Route(
21+
['GET'],
22+
'my_uri',
23+
[
24+
'as' => 'my.uri',
25+
'before' => ['foo'],
26+
function() {
27+
return 'bar';
28+
}
29+
]
30+
);
31+
32+
$action = $route->getAction();
33+
$this->assertEquals([Route::API_FILTER_AUTH, Route::API_FILTER_THROTTLE, 'foo'], $action['before']);
34+
}
35+
36+
public function testApiFiltersAreFirstBeforeFilters()
37+
{
38+
// Example : my_uri is a route of a group with a 'foo' before filter
39+
$route = new Route(
40+
['GET'],
41+
'my_uri',
42+
[
43+
'as' => 'my.uri',
44+
'before' => ['foo', Route::API_FILTER_THROTTLE, Route::API_FILTER_AUTH],
45+
function() {
46+
return 'bar';
47+
}
48+
]
49+
);
50+
51+
$action = $route->getAction();
52+
$this->assertEquals([Route::API_FILTER_AUTH, Route::API_FILTER_THROTTLE, 'foo'], $action['before']);
53+
}
54+
}

0 commit comments

Comments
 (0)