Skip to content

Commit

Permalink
Handle the case where no items has been returned (#178)
Browse files Browse the repository at this point in the history
Closes #176
  • Loading branch information
theofidry authored Oct 30, 2022
1 parent 0b63e1d commit 0e11783
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Webmozart\Assert\Assert;
use function ceil;
use function max;
use function min;
use function sprintf;

Expand Down Expand Up @@ -185,10 +186,10 @@ private static function createForWithChildProcesses(
);
}

$numberOfSegments = (int) ceil($numberOfItems / $segmentSize);
$numberOfSegments = max(1, (int) ceil($numberOfItems / $segmentSize));
Assert::positiveInteger($numberOfSegments);

$numberOfSegmentsRequired = (int) ceil($numberOfItems / $segmentSize);
$numberOfSegmentsRequired = max(1, (int) ceil($numberOfItems / $segmentSize));
Assert::positiveInteger($numberOfSegmentsRequired);

$requiredNumberOfProcesses = min($numberOfProcesses, $numberOfSegmentsRequired);
Expand Down
10 changes: 10 additions & 0 deletions tests/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,16 @@ private static function childValuesProvider(): iterable
2,
2,
);

yield 'no item' => $createSet(
0,
1,
5,
5,
1,
1,
0,
);
}

/**
Expand Down
40 changes: 40 additions & 0 deletions tests/Fixtures/Command/NoItemCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of the Webmozarts Console Parallelization package.
*
* (c) Webmozarts GmbH <office@webmozarts.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Webmozarts\Console\Parallelization\Fixtures\Command;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Webmozarts\Console\Parallelization\Fixtures\ItemNamingCapabilities;
use Webmozarts\Console\Parallelization\ParallelCommand;
use Webmozarts\Console\Parallelization\UnexpectedCall;

final class NoItemCommand extends ParallelCommand
{
use ItemNamingCapabilities;

public function __construct()
{
parent::__construct('test:no-item');
}

protected function fetchItems(InputInterface $input, OutputInterface $output): iterable
{
return [];
}

protected function runSingleCommand(string $item, InputInterface $input, OutputInterface $output): void
{
throw UnexpectedCall::forMethod(__METHOD__);
}
}
29 changes: 29 additions & 0 deletions tests/Fixtures/ItemNamingCapabilities.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of the Webmozarts Console Parallelization package.
*
* (c) Webmozarts GmbH <office@webmozarts.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Webmozarts\Console\Parallelization\Fixtures;

trait ItemNamingCapabilities
{
/**
* @param positive-int|0|null $count
*/
protected function getItemName(?int $count): string
{
if (null === $count) {
return 'item(s)';
}

return $count > 1 ? 'items' : 'item';
}
}
74 changes: 74 additions & 0 deletions tests/Integration/NoItemCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

/*
* This file is part of the Webmozarts Console Parallelization package.
*
* (c) Webmozarts GmbH <office@webmozarts.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Webmozarts\Console\Parallelization\Integration;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Tester\CommandTester;
use Webmozarts\Console\Parallelization\Fixtures\Command\NoItemCommand;

/**
* @coversNothing
*
* @internal
*/
class NoItemCommandTest extends TestCase
{
private Command $command;
private CommandTester $commandTester;

protected function setUp(): void
{
$this->command = (new Application())->add(new NoItemCommand());
$this->commandTester = new CommandTester($this->command);
}

protected function tearDown(): void
{
unset($this->command, $this->commandTester);
}

public function test_it_can_execute_a_command_that_gives_no_item(): void
{
$this->commandTester->execute(
['command' => 'test:no-item'],
['interactive' => true],
);

$expected = <<<'EOF'
Processing 0 item in segments of 50, batches of 50, 1 round, 0 batch, with 1 child process.
0 [>---------------------------] 10 secs 10.0 MiB
// Memory usage: 10.0 MB (peak: 10.0 MB), time: 10 secs
Processed 0 item.

EOF;

$actual = OutputNormalizer::removeIntermediateFixedProgressBars(
$this->getOutput($this->commandTester),
);

self::assertSame($expected, $actual, $actual);
}

private function getOutput(CommandTester $commandTester): string
{
$output = $commandTester->getDisplay(true);

return OutputNormalizer::normalize($output);
}
}

0 comments on commit 0e11783

Please sign in to comment.