Add MessageTest
This commit is contained in:
@ -19,6 +19,7 @@
|
|||||||
"psr/log": "^1.1"
|
"psr/log": "^1.1"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
|
"dg/bypass-finals": "^1.3",
|
||||||
"ergebnis/composer-normalize": "9999999-dev",
|
"ergebnis/composer-normalize": "9999999-dev",
|
||||||
"friendsofphp/php-cs-fixer": "^2.18",
|
"friendsofphp/php-cs-fixer": "^2.18",
|
||||||
"phpstan/phpstan": "^0.12.68",
|
"phpstan/phpstan": "^0.12.68",
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
|
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
|
||||||
colors="true"
|
colors="true"
|
||||||
|
bootstrap="tests/bootstrap.php"
|
||||||
>
|
>
|
||||||
<coverage processUncoveredFiles="true">
|
<coverage processUncoveredFiles="true">
|
||||||
<include>
|
<include>
|
||||||
|
@ -4,8 +4,6 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Nsq;
|
namespace Nsq;
|
||||||
|
|
||||||
use LogicException;
|
|
||||||
|
|
||||||
final class Message
|
final class Message
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
@ -50,7 +48,7 @@ final class Message
|
|||||||
public function finish(): void
|
public function finish(): void
|
||||||
{
|
{
|
||||||
if ($this->finished) {
|
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);
|
$this->consumer->fin($this->id);
|
||||||
@ -60,7 +58,7 @@ final class Message
|
|||||||
public function requeue(int $timeout): void
|
public function requeue(int $timeout): void
|
||||||
{
|
{
|
||||||
if ($this->finished) {
|
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);
|
$this->consumer->req($this->id, $timeout);
|
||||||
@ -70,7 +68,7 @@ final class Message
|
|||||||
public function touch(): void
|
public function touch(): void
|
||||||
{
|
{
|
||||||
if ($this->finished) {
|
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);
|
$this->consumer->touch($this->id);
|
||||||
|
68
tests/MessageTest.php
Normal file
68
tests/MessageTest.php
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
use Nsq\Consumer;
|
||||||
|
use Nsq\Exception;
|
||||||
|
use Nsq\Message;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
final class MessageTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @dataProvider messages
|
||||||
|
*/
|
||||||
|
public function testDoubleFinish(Message $message): void
|
||||||
|
{
|
||||||
|
self::assertFalse($message->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<int, array{0: Message}>
|
||||||
|
*/
|
||||||
|
public function messages(): Generator
|
||||||
|
{
|
||||||
|
yield [new Message(0, 0, 'id', 'body', $this->createStub(Consumer::class))];
|
||||||
|
}
|
||||||
|
}
|
7
tests/bootstrap.php
Normal file
7
tests/bootstrap.php
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
require dirname(__DIR__).'/vendor/autoload.php';
|
||||||
|
|
||||||
|
DG\BypassFinals::enable();
|
Reference in New Issue
Block a user