FiltersTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 9
dl 0
loc 91
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testTest() 0 4 1
A wordsProvider() 0 33 1
A assertFilteredWord() 0 24 4
A getWord() 0 4 1
1
<?php
2
3
4
namespace SixtyNine\Cloud\Tests\Filters;
5
6
7
use SixtyNine\Cloud\Filters\ChangeCase;
8
use SixtyNine\Cloud\Filters\FilterInterface;
9
use SixtyNine\Cloud\Filters\Filters;
10
use SixtyNine\Cloud\Filters\RemoveCharacters;
11
use SixtyNine\Cloud\Filters\RemoveNumbers;
12
use SixtyNine\Cloud\Filters\RemoveTrailingCharacters;
13
use SixtyNine\Cloud\Filters\RemoveWords;
14
use SixtyNine\Cloud\Model\Word;
15
use PHPUnit\Framework\TestCase;
16
17
class FiltersTest extends TestCase
18
{
19
    /**
20
     * @param $filter
21
     * @param $word
22
     * @param $expectedKeepWord
23
     * @param null $expectedWord
24
     * @dataProvider wordsProvider
25
     */
26
    public function testTest($filter, $word, $expectedKeepWord, $expectedWord = null)
27
    {
28
        $this->assertFilteredWord($filter, $word, $expectedKeepWord, $expectedWord);
29
    }
30
31
    /**
32
     * @return array
33
     */
34
    public function wordsProvider()
35
    {
36
        return array(
37
            [new ChangeCase(ChangeCase::LOWERCASE), 'foobar', true, 'foobar'],
38
            [new ChangeCase(ChangeCase::UPPERCASE), 'foobar', true, 'FOOBAR'],
39
            [new ChangeCase(ChangeCase::UCFIRST), 'foobar', true, 'Foobar'],
40
            [new RemoveWords(array('and')), 'foobar', true],
41
            [new RemoveWords(array('and')), 'and', false],
42
            [new RemoveNumbers(), '1234abcd5678', true, 'abcd'],
43
            [new RemoveNumbers(), '1234567890', true, ''],
44
            [new RemoveTrailingCharacters(), '1234', true, '1234'],
45
            [new RemoveTrailingCharacters(), '12345.', true, '12345'],
46
            [new RemoveTrailingCharacters(), '12345.,', true, '12345'],
47
            [new RemoveTrailingCharacters(), '12345!?', true, '12345'],
48
            [new RemoveTrailingCharacters(), '1234', true, '1234'],
49
            [new RemoveWords(), 'and', false],
50
            [new RemoveWords(), 'our', false],
51
            [new RemoveWords(), 'yours', false],
52
            [new RemoveWords(), 'such', false],
53
            [new RemoveWords(), 'some', true, 'some'],
54
            [new RemoveWords(), 'other', true, 'other'],
55
            [new RemoveWords(), 'words', true, 'words'],
56
            [new RemoveCharacters(), '12345', true, '12345'],
57
            [new RemoveCharacters(), '123?4', true, '1234'],
58
            [new RemoveCharacters(), '12345"', true, '12345'],
59
            [new RemoveCharacters(), '\'12345\'', true, '12345'],
60
            [new RemoveCharacters(), '?!1234!?', true, '1234'],
61
            [new RemoveTrailingCharacters(), '1234', true, '1234'],
62
            [new RemoveTrailingCharacters(), '12345.', true, '12345'],
63
            [new RemoveTrailingCharacters(), '12345,.', true, '12345'],
64
            [new RemoveTrailingCharacters(), '12345!?', true, '12345'],
65
        );
66
    }
67
68
    /**
69
     * @param FilterInterface $filter
70
     * @param string $word
71
     * @param bool $expectedKeepWord
72
     * @param null $expectedFiltered
73
     */
74
    protected function assertFilteredWord(FilterInterface $filter, $word, $expectedKeepWord, $expectedFiltered = null)
75
    {
76
        $filters = new Filters(array($filter));
77
        $filtered = $filters->apply($word);
78
        $filteredWord = $filter->filterWord($word);
79
80
        if ($expectedKeepWord) {
81
            if ($filtered !== '') {
82
                $this->assertTrue((bool)$filtered);
83
                $this->assertTrue((bool)$filteredWord);
84
            } else {
85
                $this->assertEquals('', $filtered);
86
                $this->assertEquals('', $filteredWord);
87
            }
88
        } else {
89
            $this->assertFalse($filtered, 'Expected the word to be filtered out');
0 ignored issues
show
Bug introduced by
It seems like $filtered defined by $filters->apply($word) on line 77 can also be of type string; however, PHPUnit\Framework\Assert::assertFalse() does only seem to accept boolean, 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...
90
            $this->assertFalse($filter->keepWord($word));
91
        }
92
93
        if ($expectedFiltered) {
94
            $this->assertEquals($expectedFiltered, $filtered, 'Unexpected filtered value');
95
            $this->assertEquals($expectedFiltered, $filteredWord, 'Unexpected filtered word');
96
        }
97
    }
98
99
    /**
100
     * @param string $text
101
     * @return Word
102
     */
103
    protected function getWord($text)
104
    {
105
        return (new Word())->setText($text);
106
    }
107
}
108