123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- <?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;
- /**
- * @author Kuba Werłos <werlos@gmail.com>
- *
- * @covers \PhpCsFixer\Preg
- *
- * @internal
- */
- final class PregTest extends TestCase
- {
- public function testMatchFailing()
- {
- $this->expectException(
- 'PhpCsFixer\\PregException'
- );
- $this->expectExceptionMessage(
- 'Error occurred when calling preg_match.'
- );
- 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);
- $this->assertSame($expectedResult, $actualResult);
- $this->assertSame($expectedMatches, $actualMatches);
- }
- public function testMatchAllFailing()
- {
- $this->expectException(
- 'PhpCsFixer\\PregException'
- );
- $this->expectExceptionMessage(
- 'Error occurred when calling preg_match_all.'
- );
- 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);
- $this->assertSame($expectedResult, $actualResult);
- $this->assertSame($expectedMatches, $actualMatches);
- }
- public function testReplaceFailing()
- {
- $this->expectException(
- 'PhpCsFixer\\PregException'
- );
- $this->expectExceptionMessage(
- 'Error occurred when calling preg_replace.'
- );
- 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);
- $this->assertSame($expectedResult, $actualResult);
- }
- public function testReplaceCallbackFailing()
- {
- $this->expectException(
- 'PhpCsFixer\\PregException'
- );
- $this->expectExceptionMessage(
- 'Error occurred when calling preg_replace_callback.'
- );
- Preg::replaceCallback('', 'sort', 'foo');
- }
- /**
- * @param string $pattern
- * @param string $subject
- *
- * @dataProvider provideCommonCases
- * @dataProvider provideArrayOfPatternsCases
- */
- public function testReplaceCallback($pattern, $subject)
- {
- $callback = function (array $x) { return implode('-', $x); };
- $expectedResult = preg_replace_callback($pattern, $callback, $subject);
- $actualResult = Preg::replaceCallback($pattern, $callback, $subject);
- $this->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(
- 'PhpCsFixer\\PregException'
- );
- $this->expectExceptionMessage(
- 'Error occurred when calling preg_split.'
- );
- Preg::split('', 'foo');
- }
- /**
- * @param string $pattern
- *
- * @dataProvider provideCommonCases
- */
- public function testSplit($pattern)
- {
- $expectedResult = preg_split($pattern, 'foo');
- $actualResult = Preg::split($pattern, 'foo');
- $this->assertSame($expectedResult, $actualResult);
- }
- public function testCorrectnessForUtf8String()
- {
- $pattern = '/./';
- $subject = 'àbc';
- Preg::match($pattern, $subject, $methodMatches);
- preg_match($pattern, $subject, $functionMatches);
- $this->assertSame(['à'], $methodMatches);
- $this->assertNotSame(['à'], $functionMatches);
- }
- public function testCorrectnessForNonUtf8String()
- {
- $pattern = '/./u';
- $subject = chr(224).'bc';
- Preg::match($pattern, $subject, $methodMatches);
- preg_match($pattern, $subject, $functionMatches);
- $this->assertSame([chr(224)], $methodMatches);
- $this->assertNotSame([chr(224)], $functionMatches);
- }
- }
|