From 0027d46300c1ad8ed27b7d7a71e1729268691c54 Mon Sep 17 00:00:00 2001 From: tienvx Date: Mon, 18 Apr 2022 00:43:01 +0700 Subject: [PATCH] Set min pair length (for dispatchers) --- src/Reducer/DispatcherTemplate.php | 6 ++- src/Reducer/Random/RandomDispatcher.php | 12 ++++- src/Reducer/Split/SplitDispatcher.php | 11 +++- tests/Reducer/DispatcherTestCase.php | 49 +++++++---------- tests/Reducer/Random/RandomDispatcherTest.php | 26 +++++++++ tests/Reducer/Split/SplitDispatcherTest.php | 54 ++++++++++++++++--- 6 files changed, 117 insertions(+), 41 deletions(-) diff --git a/src/Reducer/DispatcherTemplate.php b/src/Reducer/DispatcherTemplate.php index fd083d55..7c5c7459 100644 --- a/src/Reducer/DispatcherTemplate.php +++ b/src/Reducer/DispatcherTemplate.php @@ -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) @@ -19,7 +21,7 @@ public function dispatch(BugInterface $bug): int { $steps = $bug->getSteps(); - if (count($steps) <= 2) { + if (count($steps) < $this->minSteps()) { return 0; } @@ -39,4 +41,6 @@ protected function maxPairs(array $steps): int { return ceil(sqrt(count($steps))); } + + abstract protected function minSteps(): int; } diff --git a/src/Reducer/Random/RandomDispatcher.php b/src/Reducer/Random/RandomDispatcher.php index c7ed50a9..995bb262 100644 --- a/src/Reducer/Random/RandomDispatcher.php +++ b/src/Reducer/Random/RandomDispatcher.php @@ -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); + } } diff --git a/src/Reducer/Split/SplitDispatcher.php b/src/Reducer/Split/SplitDispatcher.php index c88029ad..ebfa9e11 100644 --- a/src/Reducer/Split/SplitDispatcher.php +++ b/src/Reducer/Split/SplitDispatcher.php @@ -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; + } } diff --git a/tests/Reducer/DispatcherTestCase.php b/tests/Reducer/DispatcherTestCase.php index b0b6727e..075db5c5 100644 --- a/tests/Reducer/DispatcherTestCase.php +++ b/tests/Reducer/DispatcherTestCase.php @@ -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; } diff --git a/tests/Reducer/Random/RandomDispatcherTest.php b/tests/Reducer/Random/RandomDispatcherTest.php index d132a36a..d5f83ab7 100644 --- a/tests/Reducer/Random/RandomDispatcherTest.php +++ b/tests/Reducer/Random/RandomDispatcherTest.php @@ -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); + } + } } diff --git a/tests/Reducer/Split/SplitDispatcherTest.php b/tests/Reducer/Split/SplitDispatcherTest.php index 54ab7f40..54ff22b9 100644 --- a/tests/Reducer/Split/SplitDispatcherTest.php +++ b/tests/Reducer/Split/SplitDispatcherTest.php @@ -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); } }