Skip to content

Commit

Permalink
Merge 0027d46 into f4faea8
Browse files Browse the repository at this point in the history
  • Loading branch information
tienvx committed Apr 17, 2022
2 parents f4faea8 + 0027d46 commit 3feb6b3
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 41 deletions.
6 changes: 5 additions & 1 deletion src/Reducer/DispatcherTemplate.php
Expand Up @@ -8,6 +8,8 @@

abstract class DispatcherTemplate implements DispatcherInterface
{
protected const MIN_PAIR_LENGTH = 2; // 3 steps

protected MessageBusInterface $messageBus;

public function __construct(MessageBusInterface $messageBus)
Expand All @@ -19,7 +21,7 @@ public function dispatch(BugInterface $bug): int
{
$steps = $bug->getSteps();

if (count($steps) <= 2) {
if (count($steps) < $this->minSteps()) {
return 0;
}

Expand All @@ -39,4 +41,6 @@ protected function maxPairs(array $steps): int
{
return ceil(sqrt(count($steps)));
}

abstract protected function minSteps(): int;
}
12 changes: 11 additions & 1 deletion src/Reducer/Random/RandomDispatcher.php
Expand Up @@ -14,11 +14,21 @@ protected function getPairs(array $steps): array

while (count($pairs) < $maxPairs) {
$pair = array_rand(range(0, $length - 1), 2);
if (!in_array($pair, $pairs)) {
if ($pair[1] - $pair[0] >= static::MIN_PAIR_LENGTH && !in_array($pair, $pairs)) {
$pairs[] = $pair;
}
}

return $pairs;
}

protected function minSteps(): int
{
return 3;
}

protected function maxPairs(array $steps): int
{
return count($steps) <= 3 ? 1 : parent::maxPairs($steps);
}
}
11 changes: 9 additions & 2 deletions src/Reducer/Split/SplitDispatcher.php
Expand Up @@ -12,14 +12,21 @@ protected function getPairs(array $steps): array
$maxPairs = $this->maxPairs($steps);
$pairs = [];

$range = range(0, $length - 1, ceil($length / $maxPairs));
$range = range(0, $length - 1, (int) ceil($length / $maxPairs));
if (end($range) !== $length - 1) {
$range[] = $length - 1;
}
for ($i = 0; $i < count($range) - 1; ++$i) {
$pairs[] = [$range[$i], $range[$i + 1]];
if ($range[$i + 1] - $range[$i] >= static::MIN_PAIR_LENGTH) {
$pairs[] = [$range[$i], $range[$i + 1]];
}
}

return $pairs;
}

protected function minSteps(): int
{
return 5;
}
}
49 changes: 19 additions & 30 deletions tests/Reducer/DispatcherTestCase.php
Expand Up @@ -25,54 +25,43 @@ protected function setUp(): void
$this->bug = new Bug();
$this->bug->setId(123);
$this->bug->setMessage('Something wrong');
$this->bug->setSteps(array_map(fn () => $this->createMock(StepInterface::class), range(1, 11)));
}

public function testDispatchTooShortSteps(): void
{
$this->bug->setSteps([$this->createMock(StepInterface::class)]);
$this->messageBus->expects($this->never())->method('dispatch');
$this->assertSame(0, $this->dispatcher->dispatch($this->bug));
$this->assertPairs(0);
}

public function testDispatch(): void
/**
* @dataProvider stepsProvider
*/
public function testDispatch(int $length, array $expectedPairs): void
{
if ($length > 0) {
$this->bug->setSteps(array_map(fn () => $this->createMock(StepInterface::class), range(0, $length - 1)));
}
$this->messageBus
->expects($this->exactly(4))
->expects($this->exactly(count($expectedPairs)))
->method('dispatch')
->with($this->assertMessage())
->with($this->assertMessage($length))
->willReturn(new Envelope(new \stdClass()));
$this->assertSame(4, $this->dispatcher->dispatch($this->bug));
$this->assertPairs();
$this->assertSame(count($expectedPairs), $this->dispatcher->dispatch($this->bug));
$this->assertPairs($expectedPairs);
}

protected function assertMessage(): Callback
protected function assertMessage(int $length): Callback
{
return $this->callback(function ($message) {
return $this->callback(function ($message) use ($length) {
if (!$message instanceof ReduceStepsMessage) {
return false;
}
if (
$message->getBugId() !== $this->bug->getId()
|| $message->getFrom() >= $message->getTo()
|| 11 !== $message->getLength()
) {
return false;
}
$pair = [$message->getFrom(), $message->getTo()];
if (!in_array($pair, $this->pairs)) {
$this->pairs[] = $pair;

return true;
}

return false;
return $message->getBugId() === $this->bug->getId() &&
$message->getFrom() + 2 <= $message->getTo() &&
$length === $message->getLength();
});
}

protected function assertPairs(int $count = 4): void
{
$this->assertCount($count, $this->pairs);
}
abstract protected function assertPairs(array $expectedPairs): void;

abstract public function stepsProvider(): array;
}
26 changes: 26 additions & 0 deletions tests/Reducer/Random/RandomDispatcherTest.php
Expand Up @@ -21,4 +21,30 @@ protected function setUp(): void
parent::setUp();
$this->dispatcher = new RandomDispatcher($this->messageBus);
}

public function stepsProvider(): array
{
return [
[0, []],
[1, []],
[2, []],
[3, [
[0, 2],
]],
[4, range(1, 2)],
[5, range(1, 3)],
[6, range(1, 3)],
[9, range(1, 3)],
[11, range(1, 4)],
];
}

protected function assertPairs(array $expectedPairs): void
{
if (count($expectedPairs) > 1) {
$this->assertCount(count($expectedPairs), $this->pairs);
} else {
$this->assertSame($expectedPairs, $this->pairs);
}
}
}
54 changes: 47 additions & 7 deletions tests/Reducer/Split/SplitDispatcherTest.php
Expand Up @@ -22,16 +22,56 @@ protected function setUp(): void
$this->dispatcher = new SplitDispatcher($this->messageBus);
}

protected function assertPairs(int $count = 4): void
public function stepsProvider(): array
{
parent::assertPairs($count);
if (4 === $count) {
$this->assertSame([
return [
[0, []],
[1, []],
[2, []],
[3, []],
[4, []],
[5, [
[0, 2],
[2, 4],
]],
[6, [
[0, 2],
[2, 4],
]],
[7, [
[0, 3],
[3, 6],
]],
[8, [
[0, 3],
[3, 6],
]],
[9, [
[0, 3],
[3, 6],
[6, 8],
]],
[10, [
[0, 3],
[3, 6],
[6, 9],
[9, 10],
], $this->pairs);
}
]],
[11, [
[0, 3],
[3, 6],
[6, 9],
]],
[12, [
[0, 3],
[3, 6],
[6, 9],
[9, 11],
]],
];
}

protected function assertPairs(array $expectedPairs): void
{
$this->assertSame($expectedPairs, $this->pairs);
}
}

0 comments on commit 3feb6b3

Please sign in to comment.