Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
$finder = (new Finder())->in([
__DIR__ . '/config',
__DIR__ . '/src',
__DIR__ . '/stubs',
__DIR__ . '/tests',
]);

Expand Down
8 changes: 0 additions & 8 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,8 @@
<projectFiles>
<directory name="src" />
<directory name="stubs" />
Comment thread
vjik marked this conversation as resolved.
<directory name="tests" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
<issueHandlers>
<PropertyNotSetInConstructor>
<errorLevel type="suppress">
<directory name="tests" />
</errorLevel>
</PropertyNotSetInConstructor>
</issueHandlers>
</psalm>
18 changes: 15 additions & 3 deletions tests/App/MemoryAdapter.php → stubs/InMemoryAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Yiisoft\Queue\Tests\App;
namespace Yiisoft\Queue\Stubs;

use Yiisoft\Queue\Adapter\AdapterInterface;
use Yiisoft\Queue\Message\IdEnvelope;
Expand All @@ -11,9 +11,21 @@

use function count;

final class MemoryAdapter implements AdapterInterface
/**
* In-memory implementation of {@see AdapterInterface} for testing and development purposes.
*
* This adapter stores messages in a local array and processes them sequentially within the same process without any
* external queue backend. Messages are assigned incremental integer identifiers and are handled in FIFO order.
*
* Note: This implementation is not persistent and should not be used in production, as all data is lost when the
* process ends.
*/
final class InMemoryAdapter implements AdapterInterface
{
/** @var array<int, MessageInterface> */
/**
* @var MessageInterface[]
* @psalm-var array<int, MessageInterface>
*/
private array $messages = [];
private int $current = 0;

Expand Down
33 changes: 0 additions & 33 deletions stubs/StubAdapter.php

This file was deleted.

3 changes: 1 addition & 2 deletions stubs/StubLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ final class StubLoop implements LoopInterface
{
public function __construct(
private readonly bool $canContinue = true,
) {
}
) {}

public function canContinue(): bool
{
Expand Down
5 changes: 2 additions & 3 deletions tests/Unit/Provider/QueueFactoryProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Yiisoft\Queue\Provider\QueueFactoryProvider;
use Yiisoft\Queue\Provider\QueueNotFoundException;
use Yiisoft\Queue\QueueInterface;
use Yiisoft\Queue\Stubs\StubAdapter;
use Yiisoft\Queue\Stubs\InMemoryAdapter;
use Yiisoft\Queue\Stubs\StubLoop;
use Yiisoft\Queue\Stubs\StubQueue;
use Yiisoft\Queue\Tests\Unit\Support\StringEnum;
Expand Down Expand Up @@ -116,9 +116,8 @@ public function testGetHasByStringEnum(): void

public function testWithContainer(): void
{
$adapter = new StubAdapter();
$container = new SimpleContainer([
AdapterInterface::class => $adapter,
AdapterInterface::class => new InMemoryAdapter(),
]);

$provider = new QueueFactoryProvider(
Expand Down
10 changes: 5 additions & 5 deletions tests/Unit/QueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
use Yiisoft\Queue\Message\IdEnvelope;
use Yiisoft\Queue\Message\Message;
use Yiisoft\Queue\MessageStatus;
use Yiisoft\Queue\Stubs\InMemoryAdapter;
use Yiisoft\Queue\Tests\App\FakeAdapter;
use Yiisoft\Queue\Tests\App\MemoryAdapter;
use Yiisoft\Queue\Tests\TestCase;

use function extension_loaded;
Expand Down Expand Up @@ -73,7 +73,7 @@ public function testStatusReturnsNotFoundWithoutAdapter(): void

public function testRunWithAdapter(): void
{
$queue = $this->createQueue(new MemoryAdapter());
$queue = $this->createQueue(new InMemoryAdapter());
$message = new Message('simple', null);
$queue->push($message);
$queue->push(clone $message);
Expand All @@ -84,7 +84,7 @@ public function testRunWithAdapter(): void

public function testRunPartlyWithAdapter(): void
{
$queue = $this->createQueue(new MemoryAdapter());
$queue = $this->createQueue(new InMemoryAdapter());
$message = new Message('simple', null);
$queue->push($message);
$queue->push(clone $message);
Expand All @@ -95,7 +95,7 @@ public function testRunPartlyWithAdapter(): void

public function testListenWithAdapter(): void
{
$queue = $this->createQueue(new MemoryAdapter());
$queue = $this->createQueue(new InMemoryAdapter());
$message = new Message('simple', null);
$queue->push($message);
$queue->push(clone $message);
Expand All @@ -107,7 +107,7 @@ public function testListenWithAdapter(): void

public function testStatusWithAdapter(): void
{
$queue = $this->createQueue(new MemoryAdapter());
$queue = $this->createQueue(new InMemoryAdapter());
$envelope = $queue->push(new Message('simple', null));

self::assertArrayHasKey(IdEnvelope::MESSAGE_ID_KEY, $envelope->getMetadata());
Expand Down
173 changes: 173 additions & 0 deletions tests/Unit/Stubs/InMemoryAdapterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Queue\Tests\Unit\Stubs;

use PHPUnit\Framework\TestCase;
use Yiisoft\Queue\Message\IdEnvelope;
use Yiisoft\Queue\Message\Message;
use Yiisoft\Queue\Message\MessageInterface;
use Yiisoft\Queue\MessageStatus;
use Yiisoft\Queue\Stubs\InMemoryAdapter;

final class InMemoryAdapterTest extends TestCase
{
public function testPush(): void
{
$adapter = new InMemoryAdapter();

$envelope1 = $adapter->push(new Message('test', 'a'));
$envelope2 = $adapter->push(new Message('test', 'b'));
$envelope3 = $adapter->push(new Message('test', 'c'));

$this->assertInstanceOf(IdEnvelope::class, $envelope1);
$this->assertInstanceOf(IdEnvelope::class, $envelope2);
$this->assertInstanceOf(IdEnvelope::class, $envelope3);

$this->assertSame(0, $envelope1->getId());
$this->assertSame('a', $envelope1->getMessage()->getData());
$this->assertSame(1, $envelope2->getId());
$this->assertSame('b', $envelope2->getMessage()->getData());
$this->assertSame(2, $envelope3->getId());
$this->assertSame('c', $envelope3->getMessage()->getData());
}

public function testStatusWaitingForPushedMessage(): void
{
$adapter = new InMemoryAdapter();
$envelope = $adapter->push(new Message('test', null));

$this->assertInstanceOf(IdEnvelope::class, $envelope);
$this->assertSame(MessageStatus::WAITING, $adapter->status($envelope->getId()));
}

public function testStatusDoneAfterProcessing(): void
{
$adapter = new InMemoryAdapter();
$envelope = $adapter->push(new Message('test', null));

$adapter->runExisting(static fn() => true);

$this->assertInstanceOf(IdEnvelope::class, $envelope);
$this->assertSame(MessageStatus::DONE, $adapter->status($envelope->getId()));
Comment thread
vjik marked this conversation as resolved.
}

public function testStatusNotFoundForNonExistentId(): void
{
$adapter = new InMemoryAdapter();

$this->assertSame(MessageStatus::NOT_FOUND, $adapter->status(99));
}

public function testStatusNotFoundForNegativeId(): void
{
$adapter = new InMemoryAdapter();

$this->assertSame(MessageStatus::NOT_FOUND, $adapter->status(-1));
}

public function testStatusAcceptsStringId(): void
{
$adapter = new InMemoryAdapter();
$envelope = $adapter->push(new Message('test', null));

$this->assertInstanceOf(IdEnvelope::class, $envelope);
$this->assertSame(MessageStatus::WAITING, $adapter->status((string) $envelope->getId()));
}

public function testRunExistingProcessesAllMessages(): void
{
$adapter = new InMemoryAdapter();
$adapter->push(new Message('test', 'a'));
$adapter->push(new Message('test', 'b'));
$adapter->push(new Message('test', 'c'));

$processed = [];
$adapter->runExisting(
static function (MessageInterface $message) use (&$processed): bool {
$processed[] = $message->getData();
return true;
},
);

$this->assertSame(['a', 'b', 'c'], $processed);
}

public function testRunExistingStopsWhenHandlerReturnsFalse(): void
{
$adapter = new InMemoryAdapter();
$adapter->push(new Message('test', 'a'));
$adapter->push(new Message('test', 'b'));
$adapter->push(new Message('test', 'c'));

$processed = [];
$adapter->runExisting(
static function (MessageInterface $message) use (&$processed): bool {
$processed[] = $message->getData();
return false;
},
);

$this->assertSame(['a'], $processed);
}

public function testRunExistingOnEmptyQueue(): void
{
$adapter = new InMemoryAdapter();

$called = false;
$adapter->runExisting(static function () use (&$called): bool {
$called = true;
return true;
});

$this->assertFalse($called);
}

public function testRunExistingDoesNotReprocessMessages(): void
{
$adapter = new InMemoryAdapter();
$adapter->push(new Message('test', 'x'));

$count = 0;
$handler = static function () use (&$count): bool {
$count++;
return true;
};
$adapter->runExisting($handler);
$adapter->runExisting($handler);

$this->assertSame(1, $count);
}

public function testIdContinuesAfterProcessing(): void
{
$adapter = new InMemoryAdapter();
$adapter->push(new Message('test', null));
$adapter->runExisting(static fn() => true);

$envelope = $adapter->push(new Message('test', null));

$this->assertInstanceOf(IdEnvelope::class, $envelope);
$this->assertSame(1, $envelope->getId());
$this->assertSame(MessageStatus::WAITING, $adapter->status($envelope->getId()));
}

public function testSubscribeProcessesExistingMessages(): void
{
$adapter = new InMemoryAdapter();
$adapter->push(new Message('test', 'a'));
$adapter->push(new Message('test', 'b'));

$processed = [];
$adapter->subscribe(
static function (MessageInterface $message) use (&$processed): bool {
$processed[] = $message->getData();
return true;
},
);

$this->assertSame(['a', 'b'], $processed);
}
}
25 changes: 0 additions & 25 deletions tests/Unit/Stubs/StubAdapterTest.php

This file was deleted.