Completed
Push — master ( 96acaf...3216da )
by
unknown
13s queued 11s
created

PhoneFilterTest::invoke()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 5
rs 10
1
<?php
2
3
namespace TraderInteractive\Filter;
4
5
use PHPUnit\Framework\TestCase;
6
use TraderInteractive\Exceptions\FilterException;
7
8
/**
9
 * @coversDefaultClass \TraderInteractive\Filter\PhoneFilter
10
 * @covers ::__construct
11
 * @covers ::<private>
12
 */
13
final class PhoneFilterTest extends TestCase
14
{
15
    /**
16
     * @param mixed       $value         The value to be filtered.
17
     * @param bool        $allowNull     The allowNull value for the filter.
18
     * @param string      $format        The format of the filtered phone.
19
     * @param string|null $expectedValue The expected filtered value.
20
     *
21
     * @test
22
     * @covers ::filter
23
     * @dataProvider provideFilterData
24
     */
25
    public function filter($value, bool $allowNull, string $format, $expectedValue)
26
    {
27
        $actualValue = PhoneFilter::filter($value, $allowNull, $format);
28
        $this->assertSame($expectedValue, $actualValue);
29
    }
30
31
    /**
32
     * @param mixed       $value         The value to be filtered.
33
     * @param bool        $allowNull     The allowNull value for the filter.
34
     * @param string      $format        The format of the filtered phone.
35
     * @param string|null $expectedValue The expected filtered value.
36
     *
37
     * @test
38
     * @covers ::__construct
39
     * @covers ::__invoke
40
     * @dataProvider provideFilterData
41
     */
42
    public function invoke($value, bool $allowNull, string $format, $expectedValue)
43
    {
44
        $filter = new PhoneFilter($allowNull, $format);
45
        $actualValue = $filter($value);
46
        $this->assertSame($expectedValue, $actualValue);
47
    }
48
49
    /**
50
     * @return array
51
     */
52
    public function provideFilterData() : array
53
    {
54
        return [
55
            ['2345678901', false, PhoneFilter::DEFAULT_FILTERED_PHONE_FORMAT, '2345678901'],
56
            ['234 5678901', false, '({area}) {exchange}-{station}', '(234) 567-8901'],
57
            ['234 567-8901', false, PhoneFilter::DEFAULT_FILTERED_PHONE_FORMAT, '2345678901'],
58
            ['234 567.8901', false, '({area}) {exchange}-{station}', '(234) 567-8901'],
59
            ['234 567 8901', false, PhoneFilter::DEFAULT_FILTERED_PHONE_FORMAT, '2345678901'],
60
            ['234.5678901', false, '({area}) {exchange}-{station}', '(234) 567-8901'],
61
            ['234.567-8901', false, PhoneFilter::DEFAULT_FILTERED_PHONE_FORMAT, '2345678901'],
62
            ['234.567.8901', false, '({area}) {exchange}-{station}', '(234) 567-8901'],
63
            ['234.567 8901', false, PhoneFilter::DEFAULT_FILTERED_PHONE_FORMAT, '2345678901'],
64
            ['234-5678901', false, '({area}) {exchange}-{station}', '(234) 567-8901'],
65
            ['234-567-8901', false, PhoneFilter::DEFAULT_FILTERED_PHONE_FORMAT, '2345678901'],
66
            ['234-567.8901', false, '{area}-{exchange}-{station}', '234-567-8901'],
67
            ['234-567 8901', false, PhoneFilter::DEFAULT_FILTERED_PHONE_FORMAT, '2345678901'],
68
            ['234   -   567   -   8901', false, '({area}) {exchange}-{station}', '(234) 567-8901'],
69
            ['234   .   567   .   8901', false, PhoneFilter::DEFAULT_FILTERED_PHONE_FORMAT, '2345678901'],
70
            ['234       567       8901', false, '{area}.{exchange}.{station}', '234.567.8901'],
71
            ['(234)-567-8901', false, PhoneFilter::DEFAULT_FILTERED_PHONE_FORMAT, '2345678901'],
72
            ['(234).567.8901', false, '({area}) {exchange}-{station}', '(234) 567-8901'],
73
            ['(234) 567 8901', false, PhoneFilter::DEFAULT_FILTERED_PHONE_FORMAT, '2345678901'],
74
            ['     234-567-8901    ', false, '{exchange}-{station}', '567-8901'],
75
            [null, true, PhoneFilter::DEFAULT_FILTERED_PHONE_FORMAT, null],
76
        ];
77
    }
78
79
    /**
80
     * @test
81
     * @covers ::filter
82
     */
83
    public function filterWithAllowNull()
84
    {
85
        $value = null;
86
        $result = PhoneFilter::filter(null, true);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $result is correct as TraderInteractive\Filter...ter::filter(null, true) targeting TraderInteractive\Filter\PhoneFilter::filter() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
87
88
        $this->assertSame($value, $result);
89
    }
90
91
    /**
92
     * @param mixed $value The value to filter.
93
     *
94
     * @test
95
     * @covers ::__invoke
96
     * @dataProvider provideFilterThrowsException
97
     */
98
    public function filterThrowsException($value)
99
    {
100
        $this->expectException(FilterException::class);
101
        $this->expectExceptionMessage(sprintf(PhoneFilter::ERROR_INVALID_PHONE_NUMBER, $value));
102
103
        PhoneFilter::filter($value);
104
    }
105
106
    /**
107
     * @return array
108
     */
109
    public function provideFilterThrowsException() : array
110
    {
111
        return [
112
            'empty string' => [''],
113
            'not all digits' => ['234-567a'],
114
            'not enough digits' => ['234567'],
115
            'too many digits' => ['23456789012'],
116
            'invalid exchange code' => ['123-4567'],
117
            'invalid area code' => ['123-234-5678'],
118
            'invalid separator' => ['234:567:8901'],
119
            'no opening parenthesis' => ['234) 567 8901'],
120
            'no closing parenthesis' => ['(234 567 8901'],
121
        ];
122
    }
123
124
    /**
125
     * @test
126
     * @covers ::filter
127
     */
128
    public function filterThrowsExceptionOnNonStringValues()
129
    {
130
        $value = ['foo' => 'bar'];
131
        $this->expectException(FilterException::class);
132
        $this->expectExceptionMessage(sprintf(PhoneFilter::ERROR_INVALID_PHONE_NUMBER, var_export($value, true)));
133
134
        PhoneFilter::filter($value);
135
    }
136
137
    /**
138
     * @test
139
     * @covers ::filter
140
     */
141
    public function filterThrowsExceptionOnNull()
142
    {
143
        $this->expectException(FilterException::class);
144
        $this->expectExceptionMessage(PhoneFilter::ERROR_VALUE_CANNOT_BE_NULL);
145
146
        PhoneFilter::filter(null);
147
    }
148
}
149