Skip to content

Introduce an input class #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
May 25, 2020
42 changes: 28 additions & 14 deletions src/ParallelizationInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,34 @@ public function __construct(
int $segmentSize,
int $batchSize
) {
Assert::greaterThan(
$segmentSize,
0,
sprintf(
'Expected the segment size should allow at least 1 item. Got "%s"',
$segmentSize
)
);
Assert::greaterThan(
$batchSize,
0,
sprintf(
'Expected the batch size should allow at least 1 item. Got "%s"',
$batchSize
)
);
// We always check those (and not the calculated ones) since they come from the command
// configuration so an issue there hints on a misconfiguration which should be fixed.
Assert::greaterThanEq(
$segmentSize,
$batchSize,
sprintf(
'The segment size ("%s") should always be greater or equal to the batch size ("%s")',
$segmentSize,
$batchSize
)
);

/** @var string|null $processes */
$processes = $input->getOption(self::PROCESSES);

Expand Down Expand Up @@ -140,20 +168,6 @@ public function __construct(
$this->batchSize = $batchSize;
$this->rounds = (int) (1 === $this->numberOfProcesses ? 1 : ceil($this->itemsCount / $segmentSize));
$this->batches = (int) (ceil($segmentSize / $batchSize) * $this->rounds);

if (!$hasItem && 1 !== $this->numberOfProcesses) {
// Shouldn't check this when only one item has been specified or
// when no child processes is used
Assert::greaterThanEq(
$segmentSize,
$batchSize,
sprintf(
'The segment size ("%s") should always be greater or equal to the batch size ("%s")',
$segmentSize,
$batchSize
)
);
}
}

public function isNumberOfProcessesDefined(): bool
Expand Down
52 changes: 48 additions & 4 deletions tests/ParallelizationInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,29 @@ public function test_it_normalizes_the_fetched_items(
$this->assertSame($expectedItems, $parallelizationInput->getItems());
}

/**
* @dataProvider invalidSegmentAndBatchSizeProvider
*/
public function test_it_cannot_accept_invalid_segment_or_batch_sizes(
int $segmentSize,
int $batchSize,
string $errorMessage
): void {
$input = new StringInput('');

self::bindInput($input);

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage($errorMessage);

new ParallelizationInput(
$input,
$this->createFakeClosure(),
$segmentSize,
$batchSize
);
}

public static function inputProvider(): iterable
{
yield 'empty input' => self::createInputArgs(
Expand Down Expand Up @@ -263,13 +286,13 @@ static function (): array {
return ['item0', 'item1', 'item2', 'item3'];
},
1,
5,
1,
false,
1,
['item0', 'item1', 'item2', 'item3'],
4,
4,
5,
1,
1,
1
);
Expand Down Expand Up @@ -297,13 +320,13 @@ static function (): array {
return ['item0', 'item1', 'item2', 'item3'];
},
1,
5,
1,
false,
1,
['item0', 'item1', 'item2', 'item3'],
4,
4,
5,
1,
1,
1
);
Expand Down Expand Up @@ -508,6 +531,27 @@ static function () {
];
}

public static function invalidSegmentAndBatchSizeProvider(): iterable
{
yield 'segment size smaller than batch size' => [
10,
50,
'The segment size ("10") should always be greater or equal to the batch size ("50")',
];

yield 'invalid segment size' => [
0,
0,
'Expected the segment size should allow at least 1 item. Got "0"',
];

yield 'invalid batch size' => [
10,
0,
'Expected the batch size should allow at least 1 item. Got "0"',
];
}

private static function createFakeClosure(): Closure
{
return static function () {
Expand Down