|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the MiddlewareBundle |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Indra Gunawan <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Indragunawan\MiddlewareBundle\Tests\Annotation; |
|
13
|
|
|
|
|
14
|
|
|
use Indragunawan\MiddlewareBundle\Annotation\AfterFilter; |
|
15
|
|
|
use Indragunawan\MiddlewareBundle\Annotation\BeforeFilter; |
|
16
|
|
|
use PHPUnit\Framework\TestCase; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @author Indra Gunawan <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
final class FilterTest extends TestCase |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @dataProvider validArgumentProvider |
|
25
|
|
|
* |
|
26
|
|
|
* @param mixed $values |
|
27
|
|
|
*/ |
|
28
|
|
|
public function testValidArgument($values) |
|
29
|
|
|
{ |
|
30
|
|
|
$beforeFilter = new BeforeFilter($values); |
|
31
|
|
|
$this->assertSame((array) $values['value'], $beforeFilter->getNames()); |
|
32
|
|
|
$this->assertSame((array) ($values['only'] ?? []), $beforeFilter->getOnly()); |
|
33
|
|
|
$this->assertSame((array) ($values['except'] ?? []), $beforeFilter->getExcept()); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @dataProvider invalidArgumentProvider |
|
38
|
|
|
* |
|
39
|
|
|
* @param mixed $values |
|
40
|
|
|
* @param mixed $expectedErrorMessage |
|
41
|
|
|
*/ |
|
42
|
|
|
public function testInvalidArgumentBeforeFilter($values, $expectedErrorMessage) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->expectException(\InvalidArgumentException::class); |
|
45
|
|
|
$this->expectExceptionMessage($expectedErrorMessage); |
|
46
|
|
|
|
|
47
|
|
|
new AfterFilter($values); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function testIsSupportsMethod() |
|
51
|
|
|
{ |
|
52
|
|
|
$filter = new BeforeFilter(['value' => 'check']); |
|
53
|
|
|
$this->assertTrue($filter->isSupportsMethod('index')); |
|
54
|
|
|
|
|
55
|
|
|
$filter = new AfterFilter(['value' => 'check', 'only' => 'index']); |
|
56
|
|
|
$this->assertTrue($filter->isSupportsMethod('index')); |
|
57
|
|
|
$this->assertFalse($filter->isSupportsMethod('delete')); |
|
58
|
|
|
|
|
59
|
|
|
$filter = new BeforeFilter(['value' => 'check', 'except' => 'index']); |
|
60
|
|
|
$this->assertFalse($filter->isSupportsMethod('index')); |
|
61
|
|
|
$this->assertTrue($filter->isSupportsMethod('delete')); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function validArgumentProvider() |
|
65
|
|
|
{ |
|
66
|
|
|
return [ |
|
67
|
|
|
[['value' => 'check']], |
|
68
|
|
|
[['value' => 'check', 'only' => 'index']], |
|
69
|
|
|
[['value' => 'check', 'except' => 'delete']], |
|
70
|
|
|
[['value' => 'check', 'only' => 'index', 'except' => 'delete']], |
|
71
|
|
|
[['value' => ['check', 'check2'], 'only' => ['index', 'update'], 'except' => ['delete', 'create']]], |
|
72
|
|
|
]; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function invalidArgumentProvider() |
|
76
|
|
|
{ |
|
77
|
|
|
return [ |
|
78
|
|
|
[['value' => ''], 'Expected a different value than ""'], |
|
79
|
|
|
[['value' => 0], 'Expected a string. Got: integer'], |
|
80
|
|
|
[['value' => [0, 1]], 'Expected a string. Got: integer'], |
|
81
|
|
|
[['only' => 'index'], 'Expected an array to contain at least 1 elements. Got: 0'], |
|
82
|
|
|
[['value' => 'check', 'only' => ''], 'Expected a different value than ""'], |
|
83
|
|
|
[['value' => 'check', 'only' => [0, 1]], 'Expected a string. Got: integer'], |
|
84
|
|
|
[['value' => 'check', 'except' => ''], 'Expected a different value than ""'], |
|
85
|
|
|
[['value' => 'check', 'except' => [0, 1]], 'Expected a string. Got: integer'], |
|
86
|
|
|
[['value' => 'check', 'only' => ['index', 'update'], 'except' => ['index', 'create']], 'You cannot put "index" in "only" and "except" at the same time'], |
|
87
|
|
|
[['value' => 'check', 'other' => 'value'], 'does not have a property named "other"'], |
|
88
|
|
|
]; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|