@@ -27,7 +27,7 @@ final class PregTest extends TestCase
2727 public function testMatchFailing ()
2828 {
2929 $ this ->expectException (PregException::class);
30- $ this ->expectExceptionMessage ('Error occurred when calling preg_match. ' );
30+ $ this ->expectExceptionMessage ('Preg::match(): Invalid PCRE pattern "" ' );
3131
3232 Preg::match ('' , 'foo ' , $ matches );
3333 }
@@ -47,10 +47,113 @@ public function testMatch($pattern, $subject)
4747 static ::assertSame ($ expectedMatches , $ actualMatches );
4848 }
4949
50+ public function providePatternValidationCases ()
51+ {
52+ return [
53+ 'invalid_blank ' => ['' , null , PregException::class],
54+ 'invalid_open ' => ["\1" , null , PregException::class, "' \1' found " ],
55+ 'valid_control_character_delimiter ' => ["\1\1" , 1 ],
56+ 'invalid_control_character_modifier ' => ["\1\1\1" , null , PregException::class, ' Unknown modifier ' ],
57+ 'valid_slate ' => ['// ' , 1 ],
58+ 'valid_paired ' => ['() ' , 1 ],
59+ 'null_byte_injection ' => ['() ' ."\0" , null , PregException::class, ' Null byte in regex ' ],
60+ 'paired_non_utf8_only ' => ["((*UTF8) \xFF) " , null , PregException::class, 'UTF-8 ' ],
61+ 'valid_paired_non_utf8_only ' => ["( \xFF) " , 1 ],
62+ 'php_version_dependent ' => ['([ \\R]) ' , 0 , PregException::class, 'Compilation failed: escape sequence is invalid ' ],
63+ ];
64+ }
65+
66+ /**
67+ * @dataProvider providePatternValidationCases
68+ *
69+ * @param $pattern
70+ * @param null|int $expected
71+ * @param null|string $expectedException
72+ * @param null|string $expectedMessage
73+ */
74+ public function testPatternValidation ($ pattern , $ expected = null , $ expectedException = null , $ expectedMessage = null )
75+ {
76+ $ setup = function () use ($ expectedException , $ expectedMessage ) {
77+ $ i = 0 ;
78+
79+ if (null !== $ expectedException ) {
80+ ++$ i ;
81+ $ this ->expectException ($ expectedException );
82+ }
83+
84+ if (null !== $ expectedMessage ) {
85+ ++$ i ;
86+ $ this ->expectExceptionMessage ($ expectedMessage );
87+ }
88+
89+ return (bool ) $ i ;
90+ };
91+
92+ try {
93+ $ actual = Preg::match ($ pattern , "The quick brown \xFF\x00\\xXX jumps over the lazy dog \n" );
94+ } catch (\Exception $ ex ) {
95+ $ setup ();
96+
97+ throw $ ex ;
98+ }
99+
100+ if (null !== $ expected ) {
101+ static ::assertSame ($ expected , $ actual );
102+
103+ return ;
104+ }
105+
106+ $ setup () || $ this ->addToAssertionCount (1 );
107+ }
108+
109+ /**
110+ * @dataProvider providePatternValidationCases
111+ *
112+ * @param string $pattern
113+ * @param null|int $expected
114+ * @param null|string $expectedException
115+ * @param null|string $expectedMessage
116+ */
117+ public function testPatternsValidation ($ pattern , $ expected = null , $ expectedException = null , $ expectedMessage = null )
118+ {
119+ $ setup = function () use ($ expectedException , $ expectedMessage ) {
120+ $ i = 0 ;
121+
122+ if (null !== $ expectedException ) {
123+ ++$ i ;
124+ $ this ->expectException ($ expectedException );
125+ }
126+
127+ if (null !== $ expectedMessage ) {
128+ ++$ i ;
129+ $ this ->expectExceptionMessage ($ expectedMessage );
130+ }
131+
132+ return (bool ) $ i ;
133+ };
134+
135+ try {
136+ $ buffer = "The quick brown \xFF\x00\\xXX jumps over the lazy dog \n" ;
137+ $ actual = $ buffer !== Preg::replace ((array ) $ pattern , 'abc ' , $ buffer );
138+ } catch (\Exception $ ex ) {
139+ $ setup ();
140+
141+ throw $ ex ;
142+ }
143+
144+ if (null !== $ expected ) {
145+ static ::assertSame ((bool ) $ expected , $ actual );
146+
147+ return ;
148+ }
149+
150+ $ setup () || $ this ->addToAssertionCount (1 );
151+ }
152+
50153 public function testMatchAllFailing ()
51154 {
52155 $ this ->expectException (PregException::class);
53- $ this ->expectExceptionMessage ('Error occurred when calling preg_match_all. ' );
156+ $ this ->expectExceptionMessage ('Preg::matchAll(): Invalid PCRE pattern "" ' );
54157
55158 Preg::matchAll ('' , 'foo ' , $ matches );
56159 }
@@ -73,7 +176,7 @@ public function testMatchAll($pattern, $subject)
73176 public function testReplaceFailing ()
74177 {
75178 $ this ->expectException (PregException::class);
76- $ this ->expectExceptionMessage ( ' Error occurred when calling preg_replace. ' );
179+ $ this ->expectExceptionMessageRegExp ( ' ~\Q\Preg::replace()\E: Invalid PCRE pattern "": \(code: \d+\) [^(]+ \(version: \d+~ ' );
77180
78181 Preg::replace ('' , 'foo ' , 'bar ' );
79182 }
@@ -96,7 +199,7 @@ public function testReplace($pattern, $subject)
96199 public function testReplaceCallbackFailing ()
97200 {
98201 $ this ->expectException (PregException::class);
99- $ this ->expectExceptionMessage ('Error occurred when calling preg_replace_callback. ' );
202+ $ this ->expectExceptionMessage ('Preg::replaceCallback(): Invalid PCRE pattern "" ' );
100203
101204 Preg::replaceCallback ('' , 'sort ' , 'foo ' );
102205 }
@@ -140,7 +243,7 @@ public function provideArrayOfPatternsCases()
140243 public function testSplitFailing ()
141244 {
142245 $ this ->expectException (PregException::class);
143- $ this ->expectExceptionMessage ('Error occurred when calling preg_split. ' );
246+ $ this ->expectExceptionMessage ('Preg::split(): Invalid PCRE pattern "" ' );
144247
145248 Preg::split ('' , 'foo ' );
146249 }
0 commit comments