Completed
Push — master ( a15c22...e7ceec )
by Jefersson
13s queued 11s
created

RegexTest::valid_regex_and_content()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
7
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
8
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
9
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
10
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
11
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
12
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
13
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
14
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
15
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
16
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17
 *
18
 * This software consists of voluntary contributions made by many individuals
19
 * and is licensed under the MIT license.
20
 */
21
namespace DocHeaderTest\Validator;
22
23
use DocHeader\Validator\RegExp;
24
use PHPUnit\Framework\TestCase;
25
use function sprintf;
26
27
/**
28
 * Tests for {@see \DocHeader\Validator\Regex}.
29
 *
30
 * @group   Unitary
31
 * @covers  \DocHeader\Validator\RegExp
32
 */
33
final class RegexTest extends TestCase
34
{
35
    /**
36
     * @test
37
     * @dataProvider valid_regex_and_content
38
     */
39
    public function it_should_assert_given_regex_on_content($regex, $content) : void
40
    {
41
        $filter = new RegExp($regex);
42
43
        $this->assertTrue(
44
            $filter->__invoke($content),
45
            sprintf('"%s" does NOT match on content "%s"', $regex, $content)
46
        );
47
    }
48
49
    /**
50
     * @test
51
     * @dataProvider invalid_regex_and_content
52
     */
53
    public function it_should_not_assert_given_regex_on_content($regex, $content) : void
54
    {
55
        $filter = new RegExp($regex);
56
57
        $this->assertFalse($filter->__invoke($content));
58
    }
59
60
    /**
61
     * @test
62
     */
63
    public function it_should_return_false_if_header_does_not_have_regex_placeholder() : void
64
    {
65
        $filter = new RegExp('No regex');
66
67
        $this->assertFalse($filter->__invoke('No regex'));
68
    }
69
70
    public function valid_regex_and_content()
71
    {
72
        return [
73
            'Space around content' => ['Heya %regexp:\d{2}%', '             Heya 12            '],
74
            'Content' => ['Heya %regexp:\d{2}+%', 'If you are reading it? Heya 12, you should buy me sushi.'],
75
            'Number' => ['Heya %regexp:\d{2}%', 'Heya 12'],
76
            'Mixed chars' => ['Heya %regexp:\d{2}-\d{1}\w\s+%', 'Heya 12-1a '],
77
            'Year format' => ['Heya %regexp:20\d{2}%', 'Heya 2020'],
78
            'Year format 2' => ['Heya 20%regexp:\d{2}%', 'Heya 2020'],
79
            'Year format 3' => ['Heya %regexp:20\d{2}%-%year%', 'Heya 2020-%year%'],
80
            'Year format 4' => ['Heya 20%regexp:\d{2}%-%year%', 'Heya 2020-%year%'],
81
            'Year format 5' => ['Heya 20%regexp:\d\d%-%year%', 'Heya 2020-%year%'],
82
            'Year format 6' => ['Heya 20%regexp:\\d\\d%-%year%', 'Heya 2020-%year%'],
83
            'Year format 7' => ['Heya 20%regexp:[0-9][0-9]%-%year%', 'Heya 2020-%year%'],
84
            'Year format 8' => ['Heya 20%regexp:(\d{2}?)%-%year%', 'Heya 2020-%year%'],
85
            'Year format 9' => ['Heya 20%regexp:(\d{2})?%-%year%', 'Heya 20-%year%'],
86
            'Multiple regex' => ['Heya %regexp:20\d{2}%-%year% %regexp:19-\d{1}%', 'Heya 2099-%year% 19-1'],
87
            'Max Number chars 1' => ['Heya %regexp:\d{1,2}%', 'Heya 2'],
88
            'Max Number chars 2' => ['Heya %regexp:\d{1,2}%', 'Heya 12'],
89
        ];
90
    }
91
92
    public function invalid_regex_and_content()
93
    {
94
        return [
95
            'Space around content' => ['Heya %regexp:\d{2}+%', '             Heya 1 23            '],
96
            'Content' => ['Heya %regexp:\d{2}+%', 'If you are reading it? Heya 1a2, you should buy me sushi.'],
97
            'No Content' => ['%regexp:\d{2}+%', ''],
98
        ];
99
    }
100
}
101