FilterTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 67
rs 10
c 0
b 0
f 0
wmc 5

5 Methods

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