Skip to content

Commit fa623aa

Browse files
committed
Merge branch 'release/1.4.0'
2 parents ab64b50 + cc5fe67 commit fa623aa

File tree

4 files changed

+67
-31
lines changed

4 files changed

+67
-31
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
22
"name": "sempro/phpunit-pretty-print",
3-
"version": "1.3.0",
3+
"version": "1.4.0",
44
"description": "Prettify PHPUnit output",
55
"type": "library",
66
"license": "MIT",
77
"minimum-stability": "dev",
88
"prefer-stable": true,
99
"require": {
1010
"php": ">=7.1.0",
11-
"phpunit/phpunit": ">=7.0.0 ^9.0.0"
11+
"phpunit/phpunit": "^7 || ^8 || ^9"
1212
},
1313
"autoload": {
1414
"psr-4": {

src/PrettyPrinterTrait.php

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,23 @@ public function endTest(Test $test, float $time): void
2929
parent::endTest($test, $time);
3030

3131
$testMethodName = \PHPUnit\Util\Test::describe($test);
32+
33+
$parts = preg_split('/ with data set /', $testMethodName[1]);
34+
$methodName = array_shift($parts);
35+
$dataSet = array_shift($parts);
3236

3337
// Convert capitalized words to lowercase
34-
$testMethodName[1] = preg_replace_callback('/([A-Z]{2,})/', function ($matches) {
38+
$methodName = preg_replace_callback('/([A-Z]{2,})/', function ($matches) {
3539
return strtolower($matches[0]);
36-
}, $testMethodName[1]);
40+
}, $methodName);
3741

3842
// Convert non-breaking method name to camelCase
39-
$testMethodName[1] = str_replace(' ', '', ucwords($testMethodName[1], ' '));
43+
$methodName = str_replace(' ', '', ucwords($methodName, ' '));
4044

4145
// Convert snakeCase method name to camelCase
42-
$testMethodName[1] = str_replace('_', '', ucwords($testMethodName[1], '_'));
46+
$methodName = str_replace('_', '', ucwords($methodName, '_'));
4347

44-
preg_match_all('/((?:^|[A-Z])[a-z0-9]+)/', $testMethodName[1], $matches);
48+
preg_match_all('/((?:^|[A-Z])[a-z0-9]+)/', $methodName, $matches);
4549

4650
// Prepend all numbers with a space
4751
$replaced = preg_replace('/(\d+)/', ' $1', $matches[0]);
@@ -54,7 +58,12 @@ public function endTest(Test $test, float $time): void
5458
$name = preg_replace('/^test /', '', $name, 1);
5559

5660
// Get the data set name
57-
$name = $this->handleDataSetName($name, $testMethodName[1]);
61+
if ($dataSet) {
62+
// Note: Use preg_replace() instead of trim() because the dataset may end with a quote
63+
// (double quotes) and trim() would remove both from the end. This matches only a single
64+
// quote from the beginning and end of the dataset that was added by PHPUnit itself.
65+
$name .= ' [ ' . preg_replace('/^"|"$/', '', $dataSet) . ' ]';
66+
}
5867

5968
$color = 'fg-green';
6069
if ($test->getStatus() !== 0) {
@@ -189,17 +198,6 @@ protected function formatExceptionMsg($exceptionMessage): string
189198
return $exceptionMessage;
190199
}
191200

192-
private function handleDataSetName($name, $testMethodName): string
193-
{
194-
preg_match('/\bwith data set "([^"]+)"/', $testMethodName, $dataSetMatch);
195-
196-
if (empty($dataSetMatch)) {
197-
return $name;
198-
}
199-
200-
return $name . ' [' . $dataSetMatch[1] . ']';
201-
}
202-
203201
private function printProgress()
204202
{
205203
if (filter_var(getenv('PHPUNIT_PRETTY_PRINT_PROGRESS'), FILTER_VALIDATE_BOOLEAN)) {

tests/Output.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ public function testError(): void
2121
throw new Exception('error');
2222
}
2323

24+
public function testRisky(): void
25+
{
26+
}
27+
2428
public function testSkip(): void
2529
{
2630
$this->markTestSkipped('skipped');
@@ -60,4 +64,20 @@ public function test_should_preserve_CAPITALIZED_and_paRTiaLLY_CAPitaLIZed_words
6064
{
6165
$this->assertTrue(true);
6266
}
67+
68+
public function dataProvider()
69+
{
70+
yield 'dataset1' => ['test'];
71+
yield 'DataSet2' => ['test'];
72+
yield 'data set 3' => ['test'];
73+
}
74+
75+
/**
76+
* @dataProvider dataProvider
77+
*/
78+
public function testWithNamedDatasets(string $value)
79+
{
80+
$this->assertEquals('test', $value);
81+
}
82+
6383
}

tests/PrinterTest.php

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,72 +25,90 @@ public function testThirdTestShouldThrowAnError()
2525
$this->assertStringContainsString('⚈ error', $lines[6]);
2626
}
2727

28-
public function testForthTestShouldBeSkipped()
28+
public function testFourthTestShouldBeRisked()
2929
{
3030
$lines = $this->getOutput();
3131

32-
$this->assertStringContainsString('→ skip', $lines[7]);
32+
$this->assertStringContainsString('⌽ risky', $lines[7]);
3333
}
3434

35-
public function testFifthTestShouldBeIncomplete()
35+
public function testFifthTestShouldBeSkipped()
3636
{
3737
$lines = $this->getOutput();
3838

39-
$this->assertStringContainsString('∅ incomplete', $lines[8]);
39+
$this->assertStringContainsString('→ skip', $lines[8]);
40+
}
41+
42+
public function testSixthTestShouldBeIncomplete()
43+
{
44+
$lines = $this->getOutput();
45+
46+
$this->assertStringContainsString('∅ incomplete', $lines[9]);
4047
}
4148

4249
public function testTestNamesCanBeTitleCased()
4350
{
4451
$lines = $this->getOutput();
4552

46-
$this->assertStringContainsString('✓ should convert title case to lowercased words', $lines[9]);
53+
$this->assertStringContainsString('✓ should convert title case to lowercased words', $lines[10]);
4754
}
4855

4956
public function testTestNameCanBeSnakeCased()
5057
{
5158
$lines = $this->getOutput();
5259

53-
$this->assertStringContainsString('✓ should convert snake case to lowercased words', $lines[10]);
60+
$this->assertStringContainsString('✓ should convert snake case to lowercased words', $lines[11]);
5461
}
5562

5663
public function testTestNameCanBeNonBreakingSpaced()
5764
{
5865
$lines = $this->getOutput();
5966

60-
$this->assertStringContainsString('✓ should convert non breaking spaces to lowercased words', $lines[11]);
67+
$this->assertStringContainsString('✓ should convert non breaking spaces to lowercased words', $lines[12]);
6168
}
6269

6370
public function testTestNameCanContainNumbers()
6471
{
6572
$lines = $this->getOutput();
6673

67-
$this->assertStringContainsString('✓ can contain 1 or 99 numbers', $lines[12]);
74+
$this->assertStringContainsString('✓ can contain 1 or 99 numbers', $lines[13]);
6875
}
6976

7077
public function testTestNameCanStartOrEndWithANumber()
7178
{
7279
$lines = $this->getOutput();
7380

74-
$this->assertStringContainsString('✓ 123 can start or end with numbers 456', $lines[13]);
81+
$this->assertStringContainsString('✓ 123 can start or end with numbers 456', $lines[14]);
7582
}
7683

7784
public function testTestNameCanContainCapitalizedWords()
7885
{
7986
$lines = $this->getOutput();
8087

81-
$this->assertStringContainsString('✓ should preserve capitalized and partially capitalized words', $lines[14]);
88+
$this->assertStringContainsString('✓ should preserve capitalized and partially capitalized words', $lines[15]);
8289
}
8390

8491
public function testItCanShowProgressWhileRunningTests()
8592
{
8693
putenv('PHPUNIT_PRETTY_PRINT_PROGRESS=true');
8794

88-
$lines = array_slice($this->getOutput(), 4, 11);
95+
$lines = array_slice($this->getOutput(), 4, 15);
8996
$count = count($lines);
9097

9198
foreach ($lines as $index => $line) {
92-
$this->assertStringContainsString(vsprintf('%s/%s', [$index+1, $count]), $line);
99+
$this->assertStringContainsString(vsprintf('%s/%s', [$index + 1, $count]), $line);
93100
}
101+
102+
putenv('PHPUNIT_PRETTY_PRINT_PROGRESS=false');
103+
}
104+
105+
public function testItShowsDatasetName()
106+
{
107+
$lines = $this->getOutput();
108+
109+
$this->assertStringContainsString('✓ with named datasets [ dataset1 ]', $lines[16]);
110+
$this->assertStringContainsString('✓ with named datasets [ DataSet2 ]', $lines[17]);
111+
$this->assertStringContainsString('✓ with named datasets [ data set 3 ]', $lines[18]);
94112
}
95113

96114
private function getOutput(): array

0 commit comments

Comments
 (0)