Skip to content

Commit 223f286

Browse files
committed
test: refactor test code with DataProvider
1 parent 1d3336b commit 223f286

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

tests/system/Helpers/TextHelperTest.php

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use CodeIgniter\Test\CIUnitTestCase;
1717
use InvalidArgumentException;
18+
use PHPUnit\Framework\Attributes\DataProvider;
1819
use PHPUnit\Framework\Attributes\Group;
1920

2021
/**
@@ -82,24 +83,34 @@ public function testReduceDoubleSlashes(): void
8283
}
8384
}
8485

85-
public function testReduceMultiples(): void
86+
#[DataProvider('provideReduceMultiples')]
87+
public function testReduceMultiples(string $str, string $expected): void
8688
{
87-
$strs = [
88-
'Fred, Bill,, Joe, Jimmy' => 'Fred, Bill, Joe, Jimmy',
89-
'Ringo, John, Paul,,' => 'Ringo, John, Paul,',
90-
];
89+
$this->assertSame($expected, reduce_multiples($str));
90+
}
9191

92-
foreach ($strs as $str => $expect) {
93-
$this->assertSame($expect, reduce_multiples($str));
94-
}
95-
$strs = [
96-
'Fred, Bill,, Joe, Jimmy' => 'Fred, Bill, Joe, Jimmy',
97-
'Ringo, John, Paul,,' => 'Ringo, John, Paul',
92+
public static function provideReduceMultiples(): iterable
93+
{
94+
yield from [
95+
// string, expected
96+
'double commas' => ['Fred, Bill,, Joe, Jimmy', 'Fred, Bill, Joe, Jimmy'],
97+
'double commas at last' => ['Ringo, John, Paul,,', 'Ringo, John, Paul,'],
9898
];
99+
}
99100

100-
foreach ($strs as $str => $expect) {
101-
$this->assertSame($expect, reduce_multiples($str, ',', true));
102-
}
101+
#[DataProvider('provideReduceMultiplesWithTrim')]
102+
public function testReduceMultiplesWithTrim(string $str, string $expected): void
103+
{
104+
$this->assertSame($expected, reduce_multiples($str, ',', true));
105+
}
106+
107+
public static function provideReduceMultiplesWithTrim(): iterable
108+
{
109+
yield from [
110+
// string, expected
111+
'double commas' => ['Fred, Bill,, Joe, Jimmy', 'Fred, Bill, Joe, Jimmy'],
112+
'double commas at last' => ['Ringo, John, Paul,,', 'Ringo, John, Paul'],
113+
];
103114
}
104115

105116
public function testRandomString(): void

0 commit comments

Comments
 (0)