Skip to content
Open
11 changes: 7 additions & 4 deletions src/Plugins/Concerns/HandleArguments.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,20 @@ public function pushArgument(string $argument, array $arguments): array
}

/**
* Pops the given argument from the arguments.
* Pops the given argument from the arguments and returns a re-indexed array.
*
* @param array<int, string> $arguments
* @return array<int, string>
*/
public function popArgument(string $argument, array $arguments): array
{
$arguments = array_flip($arguments);
$key = array_search($argument, $arguments, true);

unset($arguments[$argument]);
if ($key !== false) {
unset($arguments[$key]);
}

return array_values($arguments);

return array_values(array_flip($arguments));
}
}
6 changes: 3 additions & 3 deletions src/Plugins/Coverage.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
final class Coverage implements AddsOutput, HandlesArguments
{
use Concerns\HandleArguments;

private const string COVERAGE_OPTION = 'coverage';

private const string MIN_OPTION = 'min';
Expand Down Expand Up @@ -70,11 +72,9 @@ public function handleArguments(array $originals): array
return false;
}))];

$originals = array_flip($originals);
foreach ($arguments as $argument) {
unset($originals[$argument]);
$originals = $this->popArgument($argument, $originals);
}
$originals = array_flip($originals);

$inputs = [];
$inputs[] = new InputOption(self::COVERAGE_OPTION, null, InputOption::VALUE_NONE);
Expand Down
5 changes: 4 additions & 1 deletion tests/.snapshots/success.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1608,6 +1608,8 @@
✓ method hasArgument with ('someValue', true)
✓ method hasArgument with ('--a', false)
✓ method hasArgument with ('--undefined-argument', false)
✓ popArgument keeps non-unique array item when called with missing argument
✓ popArgument keeps non-unique array item when called with existing argument

PASS Tests\Unit\Plugins\Environment
✓ environment is set to CI when --ci option is used
Expand Down Expand Up @@ -1755,6 +1757,7 @@

PASS Tests\Visual\Parallel
✓ parallel
✓ parallel can have multiple exclude-groups
✓ a parallel test can extend another test with same name

PASS Tests\Visual\SingleTestOrDirectory
Expand Down Expand Up @@ -1782,4 +1785,4 @@
✓ pass with dataset with ('my-datas-set-value')
✓ within describe → pass with dataset with ('my-datas-set-value')

Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 39 todos, 35 skipped, 1188 passed (2814 assertions)
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 39 todos, 35 skipped, 1191 passed (2818 assertions)
21 changes: 21 additions & 0 deletions tests/Unit/Plugins/Concerns/HandleArguments.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

use Pest\Plugins\Concerns\HandleArguments;

beforeEach(function () {
$this->handler = new class
{
use HandleArguments;
};
});

test('method hasArgument', function (string $argument, bool $expectedResult) {
$obj = new class
{
Expand All @@ -24,3 +31,17 @@
['--a', false],
['--undefined-argument', false],
]);

test('popArgument keeps non-unique array item when called with missing argument', function () {
$arguments = ['--verbose', '--exclude-group', 'firstGroup', '--exclude-group', 'secondGroup', '--filter=MyTest'];
$result = $this->handler->popArgument('--missingitem', $arguments);

expect($result)->toBe($arguments);
});

test('popArgument keeps non-unique array item when called with existing argument', function () {
$arguments = ['--verbose', '--exclude-group', 'firstGroup', '--exclude-group', 'secondGroup', '--filter=MyTest'];
$result = $this->handler->popArgument('--verbose', $arguments);

expect($result)->toBe(['--exclude-group', 'firstGroup', '--exclude-group', 'secondGroup', '--filter=MyTest']);
});
10 changes: 9 additions & 1 deletion tests/Visual/Parallel.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@

test('parallel', function () use ($run) {
expect($run('--exclude-group=integration'))
->toContain('Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 39 todos, 26 skipped, 1177 passed (2790 assertions)')
->toContain('Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 39 todos, 26 skipped, 1179 passed (2792 assertions)')
->toContain('Parallel: 3 processes');
})->skipOnWindows();

test('parallel can have multiple exclude-groups', function () use ($run) {
$args = ['--exclude-group=integration', '--exclude-group=container'];

expect($run(...$args))
->toContain('Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 39 todos, 26 skipped, 1173 passed (2785 assertions)')
->toContain('Parallel: 3 processes');
})->skipOnWindows();

Expand Down
2 changes: 1 addition & 1 deletion tests/Visual/Todo.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use Symfony\Component\Process\Process;

$run = function (string $target, bool $parallel) {
$process = new Process(['php', 'bin/pest', $target, $parallel ? '--parallel' : ''], dirname(__DIR__, 2),
$process = new Process(['php', 'bin/pest', $target, ...($parallel ? ['--parallel'] : [])], dirname(__DIR__, 2),
['COLLISION_PRINTER' => 'DefaultPrinter', 'COLLISION_IGNORE_DURATION' => 'true'],
);

Expand Down