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