123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- <?php
- /*
- * This file is part of PHP CS Fixer.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- * Dariusz Rumiński <dariusz.ruminski@gmail.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- namespace PhpCsFixer\Tests;
- use PhpCsFixer\Preg;
- use PhpCsFixer\PregException;
- /**
- * @author Kuba Werłos <werlos@gmail.com>
- *
- * @covers \PhpCsFixer\Preg
- *
- * @internal
- */
- final class PregTest extends TestCase
- {
- public function testMatchFailing()
- {
- $this->expectException(PregException::class);
- $this->expectExceptionMessage('Preg::match(): Invalid PCRE pattern ""');
- Preg::match('', 'foo', $matches);
- }
- /**
- * @param string $pattern
- * @param string $subject
- *
- * @dataProvider provideCommonCases
- */
- public function testMatch($pattern, $subject)
- {
- $expectedResult = preg_match($pattern, $subject, $expectedMatches);
- $actualResult = Preg::match($pattern, $subject, $actualMatches);
- static::assertSame($expectedResult, $actualResult);
- static::assertSame($expectedMatches, $actualMatches);
- }
- public function providePatternValidationCases()
- {
- return [
- 'invalid_blank' => ['', null, PregException::class],
- 'invalid_open' => ["\1", null, PregException::class, "'\1' found"],
- 'valid_control_character_delimiter' => ["\1\1", 1],
- 'invalid_control_character_modifier' => ["\1\1\1", null, PregException::class, ' Unknown modifier '],
- 'valid_slate' => ['//', 1],
- 'valid_paired' => ['()', 1],
- 'null_byte_injection' => ['()'."\0", null, PregException::class, ' Null byte in regex '],
- 'paired_non_utf8_only' => ["((*UTF8)\xFF)", null, PregException::class, 'UTF-8'],
- 'valid_paired_non_utf8_only' => ["(\xFF)", 1],
- 'php_version_dependent' => ['([\\R])', 0, PregException::class, 'Compilation failed: escape sequence is invalid '],
- ];
- }
- /**
- * @dataProvider providePatternValidationCases
- *
- * @param string $pattern
- * @param null|int $expected
- * @param null|string $expectedException
- * @param null|string $expectedMessage
- */
- public function testPatternValidation($pattern, $expected = null, $expectedException = null, $expectedMessage = null)
- {
- $setup = function () use ($expectedException, $expectedMessage) {
- $i = 0;
- if (null !== $expectedException) {
- ++$i;
- $this->expectException($expectedException);
- }
- if (null !== $expectedMessage) {
- ++$i;
- $this->expectExceptionMessage($expectedMessage);
- }
- return (bool) $i;
- };
- try {
- $actual = Preg::match($pattern, "The quick brown \xFF\x00\\xXX jumps over the lazy dog\n");
- } catch (\Exception $ex) {
- $setup();
- throw $ex;
- }
- if (null !== $expected) {
- static::assertSame($expected, $actual);
- return;
- }
- $setup() || $this->addToAssertionCount(1);
- }
- /**
- * @dataProvider providePatternValidationCases
- *
- * @param string $pattern
- * @param null|int $expected
- * @param null|string $expectedException
- * @param null|string $expectedMessage
- */
- public function testPatternsValidation($pattern, $expected = null, $expectedException = null, $expectedMessage = null)
- {
- $setup = function () use ($expectedException, $expectedMessage) {
- $i = 0;
- if (null !== $expectedException) {
- ++$i;
- $this->expectException($expectedException);
- }
- if (null !== $expectedMessage) {
- ++$i;
- $this->expectExceptionMessage($expectedMessage);
- }
- return (bool) $i;
- };
- try {
- $buffer = "The quick brown \xFF\x00\\xXX jumps over the lazy dog\n";
- $actual = $buffer !== Preg::replace((array) $pattern, 'abc', $buffer);
- } catch (\Exception $ex) {
- $setup();
- throw $ex;
- }
- if (null !== $expected) {
- static::assertSame((bool) $expected, $actual);
- return;
- }
- $setup() || $this->addToAssertionCount(1);
- }
- public function testMatchAllFailing()
- {
- $this->expectException(PregException::class);
- $this->expectExceptionMessage('Preg::matchAll(): Invalid PCRE pattern ""');
- Preg::matchAll('', 'foo', $matches);
- }
- /**
- * @param string $pattern
- * @param string $subject
- *
- * @dataProvider provideCommonCases
- */
- public function testMatchAll($pattern, $subject)
- {
- $expectedResult = preg_match_all($pattern, $subject, $expectedMatches);
- $actualResult = Preg::matchAll($pattern, $subject, $actualMatches);
- static::assertSame($expectedResult, $actualResult);
- static::assertSame($expectedMatches, $actualMatches);
- }
- public function testReplaceFailing()
- {
- $this->expectException(PregException::class);
- $this->expectExceptionMessageRegExp('~\Q\Preg::replace()\E: Invalid PCRE pattern "": \(code: \d+\) [^(]+ \(version: \d+~');
- Preg::replace('', 'foo', 'bar');
- }
- /**
- * @param string $pattern
- * @param string $subject
- *
- * @dataProvider provideCommonCases
- * @dataProvider provideArrayOfPatternsCases
- */
- public function testReplace($pattern, $subject)
- {
- $expectedResult = preg_replace($pattern, 'foo', $subject);
- $actualResult = Preg::replace($pattern, 'foo', $subject);
- static::assertSame($expectedResult, $actualResult);
- }
- public function testReplaceCallbackFailing()
- {
- $this->expectException(PregException::class);
- $this->expectExceptionMessage('Preg::replaceCallback(): Invalid PCRE pattern ""');
- Preg::replaceCallback('', 'sort', 'foo');
- }
- /**
- * @param string $pattern
- * @param string $subject
- *
- * @dataProvider provideCommonCases
- * @dataProvider provideArrayOfPatternsCases
- */
- public function testReplaceCallback($pattern, $subject)
- {
- $callback = static function (array $x) { return implode('-', $x); };
- $expectedResult = preg_replace_callback($pattern, $callback, $subject);
- $actualResult = Preg::replaceCallback($pattern, $callback, $subject);
- static::assertSame($expectedResult, $actualResult);
- }
- public function provideCommonCases()
- {
- return [
- ['/u/u', 'u'],
- ['/u/u', 'u/u'],
- ['/./', \chr(224).'bc'],
- ['/à/', 'àbc'],
- ['/'.\chr(224).'|í/', 'àbc'],
- ];
- }
- public function provideArrayOfPatternsCases()
- {
- return [
- [['/à/', '/í/'], 'Tàíl'],
- [['/'.\chr(174).'/', '/'.\chr(224).'/'], 'foo'],
- ];
- }
- public function testSplitFailing()
- {
- $this->expectException(PregException::class);
- $this->expectExceptionMessage('Preg::split(): Invalid PCRE pattern ""');
- Preg::split('', 'foo');
- }
- /**
- * @param string $pattern
- * @param string $subject
- *
- * @dataProvider provideCommonCases
- */
- public function testSplit($pattern, $subject)
- {
- $expectedResult = preg_split($pattern, $subject);
- $actualResult = Preg::split($pattern, $subject);
- static::assertSame($expectedResult, $actualResult);
- }
- public function testCorrectnessForUtf8String()
- {
- $pattern = '/./';
- $subject = 'àbc';
- Preg::match($pattern, $subject, $methodMatches);
- preg_match($pattern, $subject, $functionMatches);
- static::assertSame(['à'], $methodMatches);
- static::assertNotSame(['à'], $functionMatches);
- }
- public function testCorrectnessForNonUtf8String()
- {
- $pattern = '/./u';
- $subject = \chr(224).'bc';
- Preg::match($pattern, $subject, $methodMatches);
- preg_match($pattern, $subject, $functionMatches);
- static::assertSame([\chr(224)], $methodMatches);
- static::assertNotSame([\chr(224)], $functionMatches);
- }
- }
|