Skip to content

Commit

Permalink
Merge branch 'release/1.4.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
ventrec committed Jan 4, 2021
2 parents ab64b50 + cc5fe67 commit fa623aa
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 31 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "sempro/phpunit-pretty-print",
"version": "1.3.0",
"version": "1.4.0",
"description": "Prettify PHPUnit output",
"type": "library",
"license": "MIT",
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": ">=7.1.0",
"phpunit/phpunit": ">=7.0.0 ^9.0.0"
"phpunit/phpunit": "^7 || ^8 || ^9"
},
"autoload": {
"psr-4": {
Expand Down
32 changes: 15 additions & 17 deletions src/PrettyPrinterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,23 @@ public function endTest(Test $test, float $time): void
parent::endTest($test, $time);

$testMethodName = \PHPUnit\Util\Test::describe($test);

$parts = preg_split('/ with data set /', $testMethodName[1]);
$methodName = array_shift($parts);
$dataSet = array_shift($parts);

// Convert capitalized words to lowercase
$testMethodName[1] = preg_replace_callback('/([A-Z]{2,})/', function ($matches) {
$methodName = preg_replace_callback('/([A-Z]{2,})/', function ($matches) {
return strtolower($matches[0]);
}, $testMethodName[1]);
}, $methodName);

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

// Convert snakeCase method name to camelCase
$testMethodName[1] = str_replace('_', '', ucwords($testMethodName[1], '_'));
$methodName = str_replace('_', '', ucwords($methodName, '_'));

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

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

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

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

private function handleDataSetName($name, $testMethodName): string
{
preg_match('/\bwith data set "([^"]+)"/', $testMethodName, $dataSetMatch);

if (empty($dataSetMatch)) {
return $name;
}

return $name . ' [' . $dataSetMatch[1] . ']';
}

private function printProgress()
{
if (filter_var(getenv('PHPUNIT_PRETTY_PRINT_PROGRESS'), FILTER_VALIDATE_BOOLEAN)) {
Expand Down
20 changes: 20 additions & 0 deletions tests/Output.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public function testError(): void
throw new Exception('error');
}

public function testRisky(): void
{
}

public function testSkip(): void
{
$this->markTestSkipped('skipped');
Expand Down Expand Up @@ -60,4 +64,20 @@ public function test_should_preserve_CAPITALIZED_and_paRTiaLLY_CAPitaLIZed_words
{
$this->assertTrue(true);
}

public function dataProvider()
{
yield 'dataset1' => ['test'];
yield 'DataSet2' => ['test'];
yield 'data set 3' => ['test'];
}

/**
* @dataProvider dataProvider
*/
public function testWithNamedDatasets(string $value)
{
$this->assertEquals('test', $value);
}

}
42 changes: 30 additions & 12 deletions tests/PrinterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,72 +25,90 @@ public function testThirdTestShouldThrowAnError()
$this->assertStringContainsString('⚈ error', $lines[6]);
}

public function testForthTestShouldBeSkipped()
public function testFourthTestShouldBeRisked()
{
$lines = $this->getOutput();

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

public function testFifthTestShouldBeIncomplete()
public function testFifthTestShouldBeSkipped()
{
$lines = $this->getOutput();

$this->assertStringContainsString('∅ incomplete', $lines[8]);
$this->assertStringContainsString('→ skip', $lines[8]);
}

public function testSixthTestShouldBeIncomplete()
{
$lines = $this->getOutput();

$this->assertStringContainsString('∅ incomplete', $lines[9]);
}

public function testTestNamesCanBeTitleCased()
{
$lines = $this->getOutput();

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

public function testTestNameCanBeSnakeCased()
{
$lines = $this->getOutput();

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

public function testTestNameCanBeNonBreakingSpaced()
{
$lines = $this->getOutput();

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

public function testTestNameCanContainNumbers()
{
$lines = $this->getOutput();

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

public function testTestNameCanStartOrEndWithANumber()
{
$lines = $this->getOutput();

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

public function testTestNameCanContainCapitalizedWords()
{
$lines = $this->getOutput();

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

public function testItCanShowProgressWhileRunningTests()
{
putenv('PHPUNIT_PRETTY_PRINT_PROGRESS=true');

$lines = array_slice($this->getOutput(), 4, 11);
$lines = array_slice($this->getOutput(), 4, 15);
$count = count($lines);

foreach ($lines as $index => $line) {
$this->assertStringContainsString(vsprintf('%s/%s', [$index+1, $count]), $line);
$this->assertStringContainsString(vsprintf('%s/%s', [$index + 1, $count]), $line);
}

putenv('PHPUNIT_PRETTY_PRINT_PROGRESS=false');
}

public function testItShowsDatasetName()
{
$lines = $this->getOutput();

$this->assertStringContainsString('✓ with named datasets [ dataset1 ]', $lines[16]);
$this->assertStringContainsString('✓ with named datasets [ DataSet2 ]', $lines[17]);
$this->assertStringContainsString('✓ with named datasets [ data set 3 ]', $lines[18]);
}

private function getOutput(): array
Expand Down

0 comments on commit fa623aa

Please sign in to comment.