Completed
Push — master ( 09303e...b0f758 )
by
unknown
10s queued 10s
created

InvokableFiltererTest::getSpecification()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
dl 11
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace TraderInteractive;
4
5
use PHPUnit\Framework\TestCase;
6
use TraderInteractive\Exceptions\FilterException;
7
8
/**
9
 * @coversDefaultClass \TraderInteractive\InvokableFilterer
10
 * @covers ::__construct
11
 * @covers ::<private>
12
 */
13
final class InvokableFiltererTest extends TestCase
14
{
15
    /**
16
     * @test
17
     * @covers ::execute
18
     */
19 View Code Duplication
    public function execute()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
20
    {
21
        $expected = new FilterResponse(['some' => 'response']);
22
        $mock = $this->getMockFilterer();
23
        $mock->method('execute')->willReturn($expected);
24
25
        $filterer = new InvokableFilterer($mock);
0 ignored issues
show
Bug introduced by
It seems like $mock defined by $this->getMockFilterer() on line 22 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, TraderInteractive\InvokableFilterer::__construct() does only seem to accept object<TraderInteractive\FiltererInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
26
        $actual = $filterer->execute([]);
27
28
        $this->assertSame($expected, $actual);
29
    }
30
31
    /**
32
     * @test
33
     * @covers ::getAliases
34
     */
35 View Code Duplication
    public function getAliases()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
36
    {
37
        $expected = ['some' => 'alias'];
38
        $mock = $this->getMockFilterer();
39
        $mock->method('getAliases')->willReturn($expected);
40
41
        $filterer = new InvokableFilterer($mock);
0 ignored issues
show
Bug introduced by
It seems like $mock defined by $this->getMockFilterer() on line 38 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, TraderInteractive\InvokableFilterer::__construct() does only seem to accept object<TraderInteractive\FiltererInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
42
        $actual = $filterer->getAliases();
43
44
        $this->assertSame($expected, $actual);
45
    }
46
47
    /**
48
     * @test
49
     * @covers ::getSpecification
50
     */
51 View Code Duplication
    public function getSpecification()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
    {
53
        $expected = ['some' => 'specification'];
54
        $mock = $this->getMockFilterer();
55
        $mock->method('getSpecification')->willReturn($expected);
56
57
        $filterer = new InvokableFilterer($mock);
0 ignored issues
show
Bug introduced by
It seems like $mock defined by $this->getMockFilterer() on line 54 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, TraderInteractive\InvokableFilterer::__construct() does only seem to accept object<TraderInteractive\FiltererInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
58
        $actual = $filterer->getSpecification();
59
60
        $this->assertSame($expected, $actual);
61
    }
62
63
    /**
64
     * @test
65
     * @covers ::withAliases
66
     */
67 View Code Duplication
    public function withAliases()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68
    {
69
        $mock = $this->getMockFilterer();
70
        $mock->method('withAliases')->willReturnSelf();
71
72
        $filterer = new InvokableFilterer($mock);
0 ignored issues
show
Bug introduced by
It seems like $mock defined by $this->getMockFilterer() on line 69 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, TraderInteractive\InvokableFilterer::__construct() does only seem to accept object<TraderInteractive\FiltererInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
73
        $actual = $filterer->withAliases([]);
74
75
        $this->assertSame($mock, $actual);
76
    }
77
78
    /**
79
     * @test
80
     * @covers ::withSpecification
81
     */
82 View Code Duplication
    public function withSpecification()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
    {
84
        $mock = $this->getMockFilterer();
85
        $mock->method('withSpecification')->willReturnSelf();
86
87
        $filterer = new InvokableFilterer($mock);
0 ignored issues
show
Bug introduced by
It seems like $mock defined by $this->getMockFilterer() on line 84 can also be of type object<PHPUnit\Framework\MockObject\MockObject>; however, TraderInteractive\InvokableFilterer::__construct() does only seem to accept object<TraderInteractive\FiltererInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
88
        $actual = $filterer->withSpecification([]);
89
90
        $this->assertSame($mock, $actual);
91
    }
92
93
    /**
94
     * @test
95
     * @covers ::__invoke
96
     * @dataProvider provideInvoke
97
     *
98
     * @param array      $filter
99
     * @param array      $options
100
     * @param array|null $filterAliases
101
     * @param mixed      $value
102
     * @param array      $expected
103
     */
104
    public function invoke(array $filter, array $options, $filterAliases, $value, array $expected)
105
    {
106
        $filterer = new InvokableFilterer(new Filterer($filter, $options, $filterAliases));
107
        $response = $filterer($value);
108
109
        $this->assertSame($expected, $response);
110
    }
111
112
    /**
113
     * @returns array
114
     */
115
    public function provideInvoke() : array
116
    {
117
        return [
118
            'empty' => [
119
                'filter' => [],
120
                'options' => [],
121
                'filterAliases' => null,
122
                'value' => [],
123
                'expected' => [],
124
            ],
125
            'basic use' => [
126
                'filter' => ['id' => [['uint']]],
127
                'options' => ['defaultRequired' => true],
128
                'filterAliases' => null,
129
                'value' => ['id' => '1'],
130
                'expected' => ['id' => 1],
131
            ],
132
            'with custom alias' => [
133
                'filter' => ['id' => [['hocuspocus']]],
134
                'options' => ['defaultRequired' => true],
135
                'filterAliases' => ['hocuspocus' => 'intval'],
136
                'value' => ['id' => '1'],
137
                'expected' => ['id' => 1],
138
            ],
139
        ];
140
    }
141
142
    /**
143
     * @test
144
     * @covers ::__invoke
145
     */
146
    public function invokeThrowsFilterException()
147
    {
148
        $filter = ['id' => ['required' => true]];
149
        $options = [];
150
        $value = [];
151
152
        $this->expectException(FilterException::class);
153
        $this->expectExceptionMessage("Field 'id' was required and not present");
154
155
        $filterer = new InvokableFilterer(new Filterer($filter, $options));
156
        $filterer($value);
157
    }
158
159
    /**
160
     * @return FiltererInterface|\PHPUnit\Framework\MockObject\MockObject
161
     */
162
    private function getMockFilterer()
163
    {
164
        return $this->getMockBuilder(FiltererInterface::class)->getMock();
165
    }
166
}
167