Skip to content

Commit

Permalink
[CLEANUP] Reorder methods in PregTest (#1327)
Browse files Browse the repository at this point in the history
They are now grouped by the method tested, apart from tests for the exception
switch, which cover all methods at once.
  • Loading branch information
JakeQZ authored Sep 20, 2024
1 parent 105ddf4 commit 58bcf8b
Showing 1 changed file with 80 additions and 80 deletions.
160 changes: 80 additions & 80 deletions tests/Unit/Utilities/PregTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,46 @@ public function replaceReplaces($pattern, $replacement, string $subject, int $li
self::assertSame($expectedResult, $result);
}

/**
* @test
*/
public function replaceSetsCount(): void
{
$subject = new Preg();

$subject->replace('/a/', 'fab', 'abba', -1, $count);

self::assertSame(2, $count);
}

/**
* @test
*/
public function replaceReturnsSubjectOnError(): void
{
$subject = new Preg();

$result = @$subject->replace('/', 'fab', 'abba');

self::assertSame('abba', $result);
}

/**
* @param array<int, string> $matches
*/
private function callbackForReplaceCallback(array $matches): string
{
if (\is_array($this->replaceCallbackReplacement)) {
if ($matches[0] !== $this->lastReplaceCallbackMatch) {
++$this->replaceCallbackReplacementIndex;
$this->lastReplaceCallbackMatch = $matches[0];
}
return $this->replaceCallbackReplacement[$this->replaceCallbackReplacementIndex];
} else {
return $this->replaceCallbackReplacement;
}
}

/**
* @test
*
Expand Down Expand Up @@ -215,33 +255,39 @@ public function replaceCallbackReplaces(
/**
* @test
*/
public function replaceSetsCount(): void
public function replaceCallbackSetsCount(): void
{
$subject = new Preg();

$subject->replace('/a/', 'fab', 'abba', -1, $count);
$subject->replaceCallback(
'/a/',
static function (array $matches): string {
return 'fab';
},
'abba',
-1,
$count
);

self::assertSame(2, $count);
}

/**
* @test
*/
public function replaceCallbackSetsCount(): void
public function replaceCallbackReturnsSubjectOnError(): void
{
$subject = new Preg();

$subject->replaceCallback(
'/a/',
$result = @$subject->replaceCallback(
'/',
static function (array $matches): string {
return 'fab';
},
'abba',
-1,
$count
'abba'
);

self::assertSame(2, $count);
self::assertSame('abba', $result);
}

/**
Expand Down Expand Up @@ -298,6 +344,31 @@ public function splitSplits(string $pattern, string $subject, int $limit, int $f
self::assertSame($expectedResult, $result);
}

/**
* @test
*/
public function splitReturnsArrayContainingSubjectOnError(): void
{
$subject = new Preg();

$result = @$subject->split('/', 'abba');

self::assertSame(['abba'], $result);
}

/**
* @test
*/
public function splitWithOffsetCaptureIsNotSupported(): void
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionCode(1726506348);
$this->expectExceptionMessage('PREG_SPLIT_OFFSET_CAPTURE');
$subject = new Preg();

$result = $subject->split('/a/', 'abba', -1, PREG_SPLIT_OFFSET_CAPTURE);
}

/**
* @return array<non-empty-string, array{
* pattern: non-empty-string,
Expand Down Expand Up @@ -382,61 +453,6 @@ public function matchSetsMatches(string $pattern, string $subject, array $expect
self::assertSame($expectedMatches, $matches);
}

/**
* @test
*/
public function replaceReturnsSubjectOnError(): void
{
$subject = new Preg();

$result = @$subject->replace('/', 'fab', 'abba');

self::assertSame('abba', $result);
}

/**
* @test
*/
public function replaceCallbackReturnsSubjectOnError(): void
{
$subject = new Preg();

$result = @$subject->replaceCallback(
'/',
static function (array $matches): string {
return 'fab';
},
'abba'
);

self::assertSame('abba', $result);
}

/**
* @test
*/
public function splitReturnsArrayContainingSubjectOnError(): void
{
$subject = new Preg();

$result = @$subject->split('/', 'abba');

self::assertSame(['abba'], $result);
}

/**
* @test
*/
public function splitWithOffsetCaptureIsNotSupported(): void
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionCode(1726506348);
$this->expectExceptionMessage('PREG_SPLIT_OFFSET_CAPTURE');
$subject = new Preg();

$result = $subject->split('/a/', 'abba', -1, PREG_SPLIT_OFFSET_CAPTURE);
}

/**
* @test
*/
Expand All @@ -460,20 +476,4 @@ public function matchSetsMatchesToEmptyArrayOnError(): void

self::assertSame([], $matches);
}

/**
* @param array<int, string> $matches
*/
private function callbackForReplaceCallback(array $matches): string
{
if (\is_array($this->replaceCallbackReplacement)) {
if ($matches[0] !== $this->lastReplaceCallbackMatch) {
++$this->replaceCallbackReplacementIndex;
$this->lastReplaceCallbackMatch = $matches[0];
}
return $this->replaceCallbackReplacement[$this->replaceCallbackReplacementIndex];
} else {
return $this->replaceCallbackReplacement;
}
}
}

0 comments on commit 58bcf8b

Please sign in to comment.