Skip to content

Commit

Permalink
Fix fragile test comparing console output
Browse files Browse the repository at this point in the history
  • Loading branch information
stloyd committed May 25, 2024
1 parent 03c0b7a commit 96ff7ef
Showing 1 changed file with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use Flow\ParquetViewer\Parquet;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Style\{OutputStyle, SymfonyStyle};
use Symfony\Component\Console\Tester\ApplicationTester;

final class ReadMetadataTest extends TestCase
Expand All @@ -24,10 +27,11 @@ public function test_reading_metadata_from_non_json_file() : void
'file' => $path,
]);

self::assertStringContainsString(
'not a valid parquet file',
$tester->getDisplay()
$expected = self::captureConsoleOutput(
fn (OutputStyle $io) => $io->error("File \"{$path}\" is not a valid parquet file")
);

self::assertStringContainsString($expected, $tester->getDisplay());
self::assertSame(1, $tester->getStatusCode());
}

Expand Down Expand Up @@ -56,4 +60,15 @@ public function test_reading_metadata_from_parquet_file() : void
self::assertStringContainsString('Page Headers', $tester->getDisplay());
self::assertSame(0, $tester->getStatusCode());
}

private function captureConsoleOutput(\Closure $closure) : string
{
$output = new BufferedOutput();

$io = new SymfonyStyle(new ArrayInput([]), $output);

$closure($io);

return $output->fetch();
}
}

0 comments on commit 96ff7ef

Please sign in to comment.