|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Asparagus\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use Asparagus\RegexHelper; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @covers Asparagus\RegexHelper |
|
9
|
|
|
* |
|
10
|
|
|
* @license GNU GPL v2+ |
|
11
|
|
|
* @author Bene* < [email protected] > |
|
12
|
|
|
*/ |
|
13
|
|
|
class RegexHelperTest extends \PHPUnit_Framework_TestCase { |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @dataProvider provideMatchingExpressions |
|
17
|
|
|
*/ |
|
18
|
|
|
public function testMatchesRegex_matching( $regex, $expression ) { |
|
19
|
|
|
$regexHelper = new RegexHelper(); |
|
20
|
|
|
|
|
21
|
|
|
$this->assertTrue( $regexHelper->matchesRegex( $regex, $expression ) ); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function provideMatchingExpressions() { |
|
25
|
|
|
return array( |
|
26
|
|
|
array( '\{variable}', '?abc' ), |
|
27
|
|
|
array( '\{variable}', '$abc' ), |
|
28
|
|
|
array( 'AS \{variable}', 'AS ?nyan' ), |
|
29
|
|
|
array( '\{iri}', 'http://www.example.com/test#' ), |
|
30
|
|
|
array( '\{prefix}', 'foo_bar42' ), |
|
31
|
|
|
array( '\{name}', 'foo_bar42' ), |
|
32
|
|
|
array( '\{prefixed_iri}', '<http://www.example.com/test#>' ), |
|
33
|
|
|
array( '\{prefixed_iri}', 'nyan:cat' ), |
|
34
|
|
|
array( '\{native}', '123' ), |
|
35
|
|
|
array( '\{native}', '"foo bar"' ), |
|
36
|
|
|
array( '\{path}', 'nyan:cat' ), |
|
37
|
|
|
array( '\{path}', '^nyan:cat' ), |
|
38
|
|
|
array( '\{path}', 'nyan:cat' ), |
|
39
|
|
|
array( '\{path}', 'nyan:cat/nyan:kitten' ), |
|
40
|
|
|
array( '\{path}', 'nyan:cat/nyan:kitten/cat:kitten' ), |
|
41
|
|
|
array( '\{path}', 'nyan:cat|nyan:kitten' ), |
|
42
|
|
|
array( '\{path}', 'nyan:cat*' ), |
|
43
|
|
|
array( '\{path}', 'nyan:cat+' ), |
|
44
|
|
|
array( '\{path}', 'nyan:cat?' ), |
|
45
|
|
|
array( '\{path}', '!nyan:cat' ), |
|
46
|
|
|
array( '\{path}', '!(nyan:cat|nyan:kitten)' ), |
|
47
|
|
|
array( '\{path}', '!^nyan:cat' ), |
|
48
|
|
|
array( '\{path}', '!(^nyan:cat|^nyan:kitten)' ), |
|
49
|
|
|
array( '\{path}', '(nyan:cat)' ), |
|
50
|
|
|
array( '\{function}', 'COUNT (?x)' ), |
|
51
|
|
|
array( '\{function}', '?x + ?y' ), |
|
52
|
|
|
array( '\{function}', '!BOUND (?x)' ), |
|
53
|
|
|
array( '\{function}', '<http://www.example.com/test#nyan>' ), |
|
54
|
|
|
); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @dataProvider provideNonMatchingExpressions |
|
59
|
|
|
*/ |
|
60
|
|
|
public function testMatchesRegex_nonMatching( $regex, $expression ) { |
|
61
|
|
|
$regexHelper = new RegexHelper(); |
|
62
|
|
|
|
|
63
|
|
|
$this->assertFalse( $regexHelper->matchesRegex( $regex, $expression ) ); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function provideNonMatchingExpressions() { |
|
67
|
|
|
return array( |
|
68
|
|
|
array( '\{variable}', 'foobar' ), |
|
69
|
|
|
array( '\{variable}', '?foo bar' ), |
|
70
|
|
|
array( 'AS \{variable}', 'AS ?nyan foobar' ), |
|
71
|
|
|
array( '\{iri}', '<http://www.example.com/test#>' ), |
|
72
|
|
|
array( '\{iri}', 'http://www.example.com/te>st#' ), |
|
73
|
|
|
array( '\{iri}', 'http://www.example.com/te}st#' ), |
|
74
|
|
|
array( '\{iri}', 'http://www.example.com/te st#' ), |
|
75
|
|
|
array( '\{prefix}', 'nyan cat' ), |
|
76
|
|
|
array( '\{prefix}', 'nyan:cat' ), |
|
77
|
|
|
array( '\{name}', 'nyan cat' ), |
|
78
|
|
|
array( '\{name}', 'nyan:cat' ), |
|
79
|
|
|
array( '\{prefixed_iri}', 'http://www.example.com/test#nyan' ), |
|
80
|
|
|
array( '\{prefixed_iri}', 'nyan:cat:kitten' ), |
|
81
|
|
|
array( '\{native}', '"abc' ), |
|
82
|
|
|
array( '\{native}', 'ab123' ), |
|
83
|
|
|
array( '\{path}', 'foobar' ), |
|
84
|
|
|
array( '\{path}', '?foobar' ), |
|
85
|
|
|
array( '\{path}', '^!nyan:cat' ), |
|
86
|
|
|
array( '\{path}', '!!!nyan:cat' ), |
|
87
|
|
|
array( '\{path}', '()' ), |
|
88
|
|
|
array( '\{function}', 'FOO BAR' ), |
|
89
|
|
|
array( '\{function}', '(COUNT (?x))' ) |
|
90
|
|
|
); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @dataProvider provideGetMatches |
|
95
|
|
|
*/ |
|
96
|
|
|
public function testGetMatches( $regex, $expression, $matches ) { |
|
97
|
|
|
$regexHelper = new RegexHelper(); |
|
98
|
|
|
|
|
99
|
|
|
$this->assertEquals( $matches, $regexHelper->getMatches( $regex, $expression ) ); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function provideGetMatches() { |
|
103
|
|
|
return array( |
|
104
|
|
|
array( '\{variable}', ' ?abc nyan $def ', array( 'abc', 'def' ) ), |
|
105
|
|
|
array( 'AS \{variable}', 'FOO (?x) AS ?nyan', array( 'nyan' ) ), |
|
106
|
|
|
array( 'AS \{variable}', 'FOO (<http://www.example.com/test?kitten> = "X AS ?kitten") AS ?nyan', array( 'nyan' ) ) |
|
107
|
|
|
); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @dataProvider provideEscapeSequences |
|
112
|
|
|
*/ |
|
113
|
|
|
public function testEscapeSequences( $expression, array $matches ) { |
|
114
|
|
|
$regexHelper = new RegexHelper(); |
|
115
|
|
|
$escaped = $regexHelper->escapeSequences( $expression, $replacements ); |
|
116
|
|
|
|
|
117
|
|
|
$this->assertEquals( $matches, array_values( $replacements ) ); |
|
118
|
|
|
$this->assertEquals( $expression, strtr( $escaped, $replacements ) ); |
|
|
|
|
|
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function provideEscapeSequences() { |
|
122
|
|
|
return array( |
|
123
|
|
|
array( 'foo bar', array() ), |
|
124
|
|
|
array( 'cat " kitten', array() ), |
|
125
|
|
|
array( 'I\'m a "nyan cat" and this is cool', array( '"nyan cat"' ) ), |
|
126
|
|
|
array( 'This is "so sweet" and it\'s a "nyan" cat', array( '"so sweet"', '"nyan"' ) ), |
|
127
|
|
|
array( 'This <iri> is cool', array( '<iri>' ) ), |
|
128
|
|
|
array( 'This <iri> is > confusing', array( '<iri>' ) ), |
|
129
|
|
|
array( 'A "nyan cat" with <iri> 42', array( '"nyan cat"', '<iri>' ) ), |
|
130
|
|
|
array( 'A "nyan <iri> cat" with 42', array( '"nyan <iri> cat"' ) ), |
|
131
|
|
|
array( 'A "nyan <iri cat" with <iri> 42', array( '"nyan <iri cat"', '<iri>' ) ) |
|
132
|
|
|
); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
} |
|
136
|
|
|
|
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:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.