tests: refactor visual testing

This commit is contained in:
Nuno Maduro
2020-06-05 20:49:14 +02:00
parent a13c19cc29
commit afbbc35984
7 changed files with 56 additions and 42 deletions

View File

@ -2,53 +2,42 @@
use Symfony\Component\Process\Process;
$run = function (string $target) {
$run = function (string $target, $decorated = false) {
$process = new Process(['php', 'bin/pest', $target], dirname(__DIR__, 2));
$process->run();
return preg_replace('#\\x1b[[][^A-Za-z]*[A-Za-z]#', '', $process->getOutput());
return $decorated ? $process->getOutput() : preg_replace('#\\x1b[[][^A-Za-z]*[A-Za-z]#', '', $process->getOutput());
};
test('allows to run a single test', function () use ($run) {
assertStringContainsString(<<<EOF
PASS Tests\Fixtures\DirectoryWithTests\ExampleTest
it example 1
$testsPath = dirname(__DIR__);
$snapshot = fn (string $name) => file_get_contents(implode(DIRECTORY_SEPARATOR, [
$testsPath,
'.snapshots',
"$name.txt",
]));
Tests: 1 passed
EOF, $run('tests/Fixtures/DirectoryWithTests/ExampleTest.php'));
test('allows to run a single test', function () use ($run, $snapshot) {
assertStringContainsString(
$snapshot('allows-to-run-a-single-test'),
$run('tests/Fixtures/DirectoryWithTests/ExampleTest.php'));
});
test('allows to run a directory', function () use ($run) {
assertStringContainsString(<<<EOF
PASS Tests\Fixtures\DirectoryWithTests\ExampleTest
it example 1
PASS Tests\Fixtures\ExampleTest
it example 2
Tests: 2 passed
EOF, $run('tests/Fixtures'));
test('allows to run a directory', function () use ($run, $snapshot) {
assertStringContainsString(
$snapshot('allows-to-run-a-directory'),
$run('tests/Fixtures')
);
});
it('has ascii chars (decorated printer)', function () {
$process = new Process([
'php',
'./bin/pest',
'tests/Fixtures/DirectoryWithTests/ExampleTest.php',
], dirname(__DIR__, 2));
$process->run();
$output = $process->getOutput();
assertStringContainsString(<<<EOF
\e[30;42;1m PASS \e[39;49;22m\e[39m Tests\Fixtures\DirectoryWithTests\ExampleTest\e[39m
\e[32;1m✓\e[39;22m\e[39m \e[2mit example 1\e[22m\e[39m
\e[37;1mTests: \e[39;22m\e[32;1m1 passed\e[39;22m
EOF, $output);
it('has ascii chars', function () use ($run, $snapshot) {
assertStringContainsString(
$snapshot('has-ascii-chars'),
$run('tests/Fixtures/DirectoryWithTests/ExampleTest.php', true)
);
});
it('disable decorating printer when colors is set to never', function () {
it('disable decorating printer when colors is set to never', function () use ($snapshot) {
$process = new Process([
'php',
'./bin/pest',
@ -58,10 +47,8 @@ it('disable decorating printer when colors is set to never', function () {
$process->run();
$output = $process->getOutput();
assertStringContainsString(<<<EOF
PASS Tests\Fixtures\DirectoryWithTests\ExampleTest
\e[2mit example 1\e[22m
Tests: 1 passed
EOF, $output);
assertStringContainsString(
$snapshot('disable-decorating-printer'),
$output
);
});

6
tests/Visual/foo.txt Normal file
View File

@ -0,0 +1,6 @@
 PASS  Tests\Fixtures\DirectoryWithTests\ExampleTest
✓ it example 1
Tests: 1 passed
Time: 0.04s