From d0594c601c984f0f8ee37348fdfd497fd7465655 Mon Sep 17 00:00:00 2001 From: Konstantin Grachev Date: Sat, 23 Jan 2021 03:39:35 +0300 Subject: [PATCH] Add MessageTest --- composer.json | 1 + phpunit.xml.dist | 1 + src/Message.php | 8 ++--- tests/MessageTest.php | 68 +++++++++++++++++++++++++++++++++++++++++++ tests/bootstrap.php | 7 +++++ 5 files changed, 80 insertions(+), 5 deletions(-) create mode 100644 tests/MessageTest.php create mode 100644 tests/bootstrap.php diff --git a/composer.json b/composer.json index 2e79b0e..66c10d6 100644 --- a/composer.json +++ b/composer.json @@ -19,6 +19,7 @@ "psr/log": "^1.1" }, "require-dev": { + "dg/bypass-finals": "^1.3", "ergebnis/composer-normalize": "9999999-dev", "friendsofphp/php-cs-fixer": "^2.18", "phpstan/phpstan": "^0.12.68", diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 4492973..a572623 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -3,6 +3,7 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd" colors="true" + bootstrap="tests/bootstrap.php" > diff --git a/src/Message.php b/src/Message.php index c7513c0..732f7b7 100644 --- a/src/Message.php +++ b/src/Message.php @@ -4,8 +4,6 @@ declare(strict_types=1); namespace Nsq; -use LogicException; - final class Message { /** @@ -50,7 +48,7 @@ final class Message public function finish(): void { if ($this->finished) { - throw new LogicException('Can\'t finish message as it already finished.'); + throw new Exception('Can\'t finish message as it already finished.'); } $this->consumer->fin($this->id); @@ -60,7 +58,7 @@ final class Message public function requeue(int $timeout): void { if ($this->finished) { - throw new LogicException('Can\'t requeue message as it already finished.'); + throw new Exception('Can\'t requeue message as it already finished.'); } $this->consumer->req($this->id, $timeout); @@ -70,7 +68,7 @@ final class Message public function touch(): void { if ($this->finished) { - throw new LogicException('Can\'t touch message as it already finished.'); + throw new Exception('Can\'t touch message as it already finished.'); } $this->consumer->touch($this->id); diff --git a/tests/MessageTest.php b/tests/MessageTest.php new file mode 100644 index 0000000..d767a36 --- /dev/null +++ b/tests/MessageTest.php @@ -0,0 +1,68 @@ +isFinished()); + + $message->finish(); + + self::assertTrue($message->isFinished()); + + $this->expectException(Exception::class); + $this->expectExceptionMessage('Can\'t finish message as it already finished.'); + + $message->finish(); + } + + /** + * @dataProvider messages + */ + public function testDoubleRequeue(Message $message): void + { + self::assertFalse($message->isFinished()); + + $message->requeue(1); + + self::assertTrue($message->isFinished()); + + $this->expectException(Exception::class); + $this->expectExceptionMessage('Can\'t requeue message as it already finished.'); + + $message->requeue(5); + } + + /** + * @dataProvider messages + */ + public function testTouchAfterFinish(Message $message): void + { + self::assertFalse($message->isFinished()); + + $message->finish(); + + $this->expectException(Exception::class); + $this->expectExceptionMessage('Can\'t touch message as it already finished.'); + + $message->touch(); + } + + /** + * @return Generator + */ + public function messages(): Generator + { + yield [new Message(0, 0, 'id', 'body', $this->createStub(Consumer::class))]; + } +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..57c922c --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,7 @@ +