Skip to content

Commit b0601e1

Browse files
author
Robert Nicholson
committed
Add some more pcre tests
1 parent a5f9d8b commit b0601e1

27 files changed

+1771
-0
lines changed

ext/pcre/tests/preg_grep_basic.phpt

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
--TEST--
2+
Test preg_grep() function : basic functionality
3+
--FILE--
4+
<?php
5+
/*
6+
* proto array preg_grep(string regex, array input [, int flags])
7+
* Function is implemented in ext/pcre/php_pcre.c
8+
*/
9+
$array = array('HTTP://WWW.EXAMPLE.COM', '/index.html', '/info/stat/', 'http://test.uk.com/index/html', '/display/dept.php');
10+
var_dump($array);
11+
var_dump(preg_grep('@^HTTP(.*?)\w{2,}$@i', $array)); //finds a string starting with http (regardless of case) (matches two)
12+
var_dump(preg_grep('@(/\w+\.*/*)+@', $array)); //finds / followed by one or more of a-z, A-Z and 0-9, followed by zero or more . followed by zero or more / all more than once. (matches all)
13+
var_dump(preg_grep('@^http://[^w]{3}.*$@i', $array)); //finds http:// (at the beginning of a string) not followed by 3 characters that aren't w's then anything to the end of the sttring (matches one)
14+
var_dump(preg_grep('@.*?\.co\.uk$@i', $array)); //finds any address ending in .co.uk (matches none)
15+
var_dump(preg_grep('@^HTTP(.*?)\w{2,}$@i', $array, PREG_GREP_INVERT)); //same as first example but the array created contains everything that is NOT matched but the regex (matches three)
16+
17+
?>
18+
--EXPECT--
19+
array(5) {
20+
[0]=>
21+
string(22) "HTTP://WWW.EXAMPLE.COM"
22+
[1]=>
23+
string(11) "/index.html"
24+
[2]=>
25+
string(11) "/info/stat/"
26+
[3]=>
27+
string(29) "http://test.uk.com/index/html"
28+
[4]=>
29+
string(17) "/display/dept.php"
30+
}
31+
array(2) {
32+
[0]=>
33+
string(22) "HTTP://WWW.EXAMPLE.COM"
34+
[3]=>
35+
string(29) "http://test.uk.com/index/html"
36+
}
37+
array(5) {
38+
[0]=>
39+
string(22) "HTTP://WWW.EXAMPLE.COM"
40+
[1]=>
41+
string(11) "/index.html"
42+
[2]=>
43+
string(11) "/info/stat/"
44+
[3]=>
45+
string(29) "http://test.uk.com/index/html"
46+
[4]=>
47+
string(17) "/display/dept.php"
48+
}
49+
array(1) {
50+
[3]=>
51+
string(29) "http://test.uk.com/index/html"
52+
}
53+
array(0) {
54+
}
55+
array(3) {
56+
[1]=>
57+
string(11) "/index.html"
58+
[2]=>
59+
string(11) "/info/stat/"
60+
[4]=>
61+
string(17) "/display/dept.php"
62+
}
63+
64+
--UEXPECTF--
65+
array(5) {
66+
[0]=>
67+
unicode(22) "HTTP://WWW.EXAMPLE.COM"
68+
[1]=>
69+
unicode(11) "/index.html"
70+
[2]=>
71+
unicode(11) "/info/stat/"
72+
[3]=>
73+
unicode(29) "http://test.uk.com/index/html"
74+
[4]=>
75+
unicode(17) "/display/dept.php"
76+
}
77+
array(2) {
78+
[0]=>
79+
unicode(22) "HTTP://WWW.EXAMPLE.COM"
80+
[3]=>
81+
unicode(29) "http://test.uk.com/index/html"
82+
}
83+
array(5) {
84+
[0]=>
85+
unicode(22) "HTTP://WWW.EXAMPLE.COM"
86+
[1]=>
87+
unicode(11) "/index.html"
88+
[2]=>
89+
unicode(11) "/info/stat/"
90+
[3]=>
91+
unicode(29) "http://test.uk.com/index/html"
92+
[4]=>
93+
unicode(17) "/display/dept.php"
94+
}
95+
array(1) {
96+
[3]=>
97+
unicode(29) "http://test.uk.com/index/html"
98+
}
99+
array(0) {
100+
}
101+
array(3) {
102+
[1]=>
103+
unicode(11) "/index.html"
104+
[2]=>
105+
unicode(11) "/info/stat/"
106+
[4]=>
107+
unicode(17) "/display/dept.php"
108+
}

ext/pcre/tests/preg_grep_error.phpt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
--TEST--
2+
Test preg_grep() function : error conditions - wrong numbers of parameters
3+
--FILE--
4+
<?php
5+
/*
6+
* proto array preg_grep(string regex, array input [, int flags])
7+
* Function is implemented in ext/pcre/php_pcre.c
8+
*/
9+
echo "*** Testing preg_grep() : error conditions ***\n";
10+
// Zero arguments
11+
echo "\n-- Testing preg_grep() function with Zero arguments --\n";
12+
var_dump(preg_grep());
13+
//Test preg_grep with one more than the expected number of arguments
14+
echo "\n-- Testing preg_grep() function with more than expected no. of arguments --\n";
15+
$regex = '/\d/';
16+
$input = array(1, 2);
17+
$flags = 0;
18+
$extra_arg = 10;
19+
var_dump(preg_grep($regex, $input, $flags, $extra_arg));
20+
// Testing preg_grep withone less than the expected number of arguments
21+
echo "\n-- Testing preg_grep() function with less than expected no. of arguments --\n";
22+
$regex = 'string_val';
23+
var_dump(preg_grep($regex));
24+
echo "Done"
25+
?>
26+
--EXPECTF--
27+
*** Testing preg_grep() : error conditions ***
28+
29+
-- Testing preg_grep() function with Zero arguments --
30+
31+
Warning: preg_grep() expects at least 2 parameters, 0 given in %spreg_grep_error.php on line %d
32+
NULL
33+
34+
-- Testing preg_grep() function with more than expected no. of arguments --
35+
36+
Warning: preg_grep() expects at most 3 parameters, 4 given in %spreg_grep_error.php on line %d
37+
NULL
38+
39+
-- Testing preg_grep() function with less than expected no. of arguments --
40+
41+
Warning: preg_grep() expects at least 2 parameters, 1 given in %spreg_grep_error.php on line %d
42+
NULL
43+
Done

ext/pcre/tests/preg_grep_error1.phpt

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
--TEST--
2+
Test preg_grep() function : error conditions - bad regular expressions
3+
--FILE--
4+
<?php
5+
/*
6+
* proto array preg_grep(string regex, array input [, int flags])
7+
* Function is implemented in ext/pcre/php_pcre.c
8+
*/
9+
error_reporting(E_ALL&~E_NOTICE);
10+
/*
11+
* Testing how preg_grep reacts to being passed bad regexes
12+
*/
13+
echo "*** Testing preg_grep() : error conditions ***\n";
14+
$values = array('abcdef', //Regex without delimeter
15+
'/[a-zA-Z]', //Regex without closing delimeter
16+
'[a-zA-Z]/', //Regex without opening delimeter
17+
'/[a-zA-Z]/F', array('[a-z]', //Array of Regexes
18+
'[A-Z]', '[0-9]'), '/[a-zA-Z]/', //Regex string
19+
);
20+
$array = array(123, 'abc', 'test');
21+
foreach($values as $value) {
22+
print "\nArg value is $value\n";
23+
var_dump(preg_grep($value, $array));
24+
}
25+
$value = new stdclass(); //Object
26+
var_dump(preg_grep($value, $array));
27+
echo "Done"
28+
?>
29+
--EXPECTF--
30+
31+
*** Testing preg_grep() : error conditions ***
32+
33+
Arg value is abcdef
34+
35+
Warning: preg_grep(): Delimiter must not be alphanumeric or backslash in %spreg_grep_error1.php on line %d
36+
bool(false)
37+
38+
Arg value is /[a-zA-Z]
39+
40+
Warning: preg_grep(): No ending delimiter '/' found in %spreg_grep_error1.php on line %d
41+
bool(false)
42+
43+
Arg value is [a-zA-Z]/
44+
45+
Warning: preg_grep(): Unknown modifier '/' in %spreg_grep_error1.php on line %d
46+
bool(false)
47+
48+
Arg value is /[a-zA-Z]/F
49+
50+
Warning: preg_grep(): Unknown modifier 'F' in %spreg_grep_error1.php on line %d
51+
bool(false)
52+
53+
Arg value is Array
54+
55+
Warning: preg_grep() expects parameter 1 to be string (Unicode or binary), array given in %spreg_grep_error1.php on line %d
56+
NULL
57+
58+
Arg value is /[a-zA-Z]/
59+
array(2) {
60+
[1]=>
61+
string(3) "abc"
62+
[2]=>
63+
string(4) "test"
64+
}
65+
66+
Warning: preg_grep() expects parameter 1 to be string (Unicode or binary), object given in %spreg_grep_error1.php on line %d
67+
NULL
68+
Done
69+
--UEXPECTF--
70+
*** Testing preg_grep() : error conditions ***
71+
72+
Arg value is abcdef
73+
74+
Warning: preg_grep(): Delimiter must not be alphanumeric or backslash in %spreg_grep_error1.php on line %d
75+
bool(false)
76+
77+
Arg value is /[a-zA-Z]
78+
79+
Warning: preg_grep(): No ending delimiter '/' found in %spreg_grep_error1.php on line %d
80+
bool(false)
81+
82+
Arg value is [a-zA-Z]/
83+
84+
Warning: preg_grep(): Unknown modifier '/' in %spreg_grep_error1.php on line %d
85+
bool(false)
86+
87+
Arg value is /[a-zA-Z]/F
88+
89+
Warning: preg_grep(): Unknown modifier 'F' in %spreg_grep_error1.php on line %d
90+
bool(false)
91+
92+
Arg value is Array
93+
94+
Warning: preg_grep() expects parameter 1 to be string (Unicode or binary), array given in %spreg_grep_error1.php on line %d
95+
NULL
96+
97+
Arg value is /[a-zA-Z]/
98+
array(2) {
99+
[1]=>
100+
unicode(3) "abc"
101+
[2]=>
102+
unicode(4) "test"
103+
}
104+
105+
Warning: preg_grep() expects parameter 1 to be string (Unicode or binary), object given in %spreg_grep_error1.php on line %d
106+
NULL
107+
Done

ext/pcre/tests/preg_grep_error2.phpt

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
--TEST--
2+
Test preg_grep() function : error conditions - wrong arg types
3+
--FILE--
4+
<?php
5+
/*
6+
* proto array preg_grep(string regex, array input [, int flags])
7+
* Function is implemented in ext/pcre/php_pcre.c
8+
*/
9+
error_reporting(E_ALL&~E_NOTICE);
10+
/*
11+
* Testing how preg_grep reacts to being passed the wrong type of input argument
12+
*/
13+
echo "*** Testing preg_grep() : error conditions ***\n";
14+
$regex = '/[a-zA-Z]/';
15+
$input = array('this is a string', array('this is', 'a subarray'),);
16+
foreach($input as $value) {
17+
print "\nArg value is: $value\n";
18+
var_dump(preg_grep($regex, $value));
19+
}
20+
$value = new stdclass(); //Object
21+
var_dump(preg_grep($regex, $value));
22+
echo "Done";
23+
?>
24+
--EXPECTF--
25+
*** Testing preg_grep() : error conditions ***
26+
27+
Arg value is: this is a string
28+
29+
Warning: preg_grep() expects parameter 2 to be array, string given in %spreg_grep_error2.php on line %d
30+
NULL
31+
32+
Arg value is: Array
33+
array(2) {
34+
[0]=>
35+
string(7) "this is"
36+
[1]=>
37+
string(10) "a subarray"
38+
}
39+
40+
Warning: preg_grep() expects parameter 2 to be array, object given in %spreg_grep_error2.php on line %d
41+
NULL
42+
Done
43+
--UEXPECTF--
44+
*** Testing preg_grep() : error conditions ***
45+
46+
Arg value is: this is a string
47+
48+
Warning: preg_grep() expects parameter 2 to be array, Unicode string given in %spreg_grep_error2.php on line %d
49+
NULL
50+
51+
Arg value is: Array
52+
array(2) {
53+
[0]=>
54+
unicode(7) "this is"
55+
[1]=>
56+
unicode(10) "a subarray"
57+
}
58+
59+
Warning: preg_grep() expects parameter 2 to be array, object given in %spreg_grep_error2.php on line %d
60+
NULL
61+
Done

0 commit comments

Comments
 (0)