Skip to content

Commit 0e11783

Browse files
authored
Handle the case where no items has been returned (#178)
Closes #176
1 parent 0b63e1d commit 0e11783

File tree

5 files changed

+156
-2
lines changed

5 files changed

+156
-2
lines changed

src/Configuration.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use Webmozart\Assert\Assert;
1717
use function ceil;
18+
use function max;
1819
use function min;
1920
use function sprintf;
2021

@@ -185,10 +186,10 @@ private static function createForWithChildProcesses(
185186
);
186187
}
187188

188-
$numberOfSegments = (int) ceil($numberOfItems / $segmentSize);
189+
$numberOfSegments = max(1, (int) ceil($numberOfItems / $segmentSize));
189190
Assert::positiveInteger($numberOfSegments);
190191

191-
$numberOfSegmentsRequired = (int) ceil($numberOfItems / $segmentSize);
192+
$numberOfSegmentsRequired = max(1, (int) ceil($numberOfItems / $segmentSize));
192193
Assert::positiveInteger($numberOfSegmentsRequired);
193194

194195
$requiredNumberOfProcesses = min($numberOfProcesses, $numberOfSegmentsRequired);

tests/ConfigurationTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,16 @@ private static function childValuesProvider(): iterable
293293
2,
294294
2,
295295
);
296+
297+
yield 'no item' => $createSet(
298+
0,
299+
1,
300+
5,
301+
5,
302+
1,
303+
1,
304+
0,
305+
);
296306
}
297307

298308
/**
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Webmozarts Console Parallelization package.
5+
*
6+
* (c) Webmozarts GmbH <office@webmozarts.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Webmozarts\Console\Parallelization\Fixtures\Command;
15+
16+
use Symfony\Component\Console\Input\InputInterface;
17+
use Symfony\Component\Console\Output\OutputInterface;
18+
use Webmozarts\Console\Parallelization\Fixtures\ItemNamingCapabilities;
19+
use Webmozarts\Console\Parallelization\ParallelCommand;
20+
use Webmozarts\Console\Parallelization\UnexpectedCall;
21+
22+
final class NoItemCommand extends ParallelCommand
23+
{
24+
use ItemNamingCapabilities;
25+
26+
public function __construct()
27+
{
28+
parent::__construct('test:no-item');
29+
}
30+
31+
protected function fetchItems(InputInterface $input, OutputInterface $output): iterable
32+
{
33+
return [];
34+
}
35+
36+
protected function runSingleCommand(string $item, InputInterface $input, OutputInterface $output): void
37+
{
38+
throw UnexpectedCall::forMethod(__METHOD__);
39+
}
40+
}
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Webmozarts Console Parallelization package.
5+
*
6+
* (c) Webmozarts GmbH <office@webmozarts.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Webmozarts\Console\Parallelization\Fixtures;
15+
16+
trait ItemNamingCapabilities
17+
{
18+
/**
19+
* @param positive-int|0|null $count
20+
*/
21+
protected function getItemName(?int $count): string
22+
{
23+
if (null === $count) {
24+
return 'item(s)';
25+
}
26+
27+
return $count > 1 ? 'items' : 'item';
28+
}
29+
}
+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Webmozarts Console Parallelization package.
5+
*
6+
* (c) Webmozarts GmbH <office@webmozarts.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Webmozarts\Console\Parallelization\Integration;
15+
16+
use PHPUnit\Framework\TestCase;
17+
use Symfony\Component\Console\Application;
18+
use Symfony\Component\Console\Command\Command;
19+
use Symfony\Component\Console\Tester\CommandTester;
20+
use Webmozarts\Console\Parallelization\Fixtures\Command\NoItemCommand;
21+
22+
/**
23+
* @coversNothing
24+
*
25+
* @internal
26+
*/
27+
class NoItemCommandTest extends TestCase
28+
{
29+
private Command $command;
30+
private CommandTester $commandTester;
31+
32+
protected function setUp(): void
33+
{
34+
$this->command = (new Application())->add(new NoItemCommand());
35+
$this->commandTester = new CommandTester($this->command);
36+
}
37+
38+
protected function tearDown(): void
39+
{
40+
unset($this->command, $this->commandTester);
41+
}
42+
43+
public function test_it_can_execute_a_command_that_gives_no_item(): void
44+
{
45+
$this->commandTester->execute(
46+
['command' => 'test:no-item'],
47+
['interactive' => true],
48+
);
49+
50+
$expected = <<<'EOF'
51+
Processing 0 item in segments of 50, batches of 50, 1 round, 0 batch, with 1 child process.
52+
53+
0 [>---------------------------] 10 secs 10.0 MiB
54+
55+
// Memory usage: 10.0 MB (peak: 10.0 MB), time: 10 secs
56+
57+
Processed 0 item.
58+
59+
EOF;
60+
61+
$actual = OutputNormalizer::removeIntermediateFixedProgressBars(
62+
$this->getOutput($this->commandTester),
63+
);
64+
65+
self::assertSame($expected, $actual, $actual);
66+
}
67+
68+
private function getOutput(CommandTester $commandTester): string
69+
{
70+
$output = $commandTester->getDisplay(true);
71+
72+
return OutputNormalizer::normalize($output);
73+
}
74+
}

0 commit comments

Comments
 (0)