Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added tests #172

Merged
merged 5 commits into from
Jul 28, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ class ChannelIncorrectlyConfigured extends InvalidArgumentException implements F
*
* @param string $channel
* @param mixed|object $definition
* @param int $code
* @param Throwable|null $previous
*/
public function __construct(string $channel, $definition, int $code = 0, ?Throwable $previous = null)
{
$adapterClass = AdapterInterface::class;
$realType = is_object($definition) ? get_class($definition) : gettype($definition);
$realType = get_debug_type($definition);
$message = "Channel \"$channel\" is not properly configured: definition must return $adapterClass, $realType returned";

$this->channel = $channel;
Expand Down
30 changes: 18 additions & 12 deletions tests/Integration/QueueFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Yiisoft\Yii\Queue\Middleware\Push\PushMiddlewareDispatcher;
use Yiisoft\Yii\Queue\Queue;
use Yiisoft\Yii\Queue\QueueFactory;
use Yiisoft\Yii\Queue\QueueFactoryInterface;
use Yiisoft\Yii\Queue\Tests\App\FakeAdapter;
use Yiisoft\Yii\Queue\Worker\WorkerInterface;

Expand All @@ -23,12 +24,7 @@ final class QueueFactoryTest extends TestCase
public function testQuickChange(): void
{
$worker = $this->createMock(WorkerInterface::class);
$queue = new Queue(
$worker,
$this->createMock(LoopInterface::class),
$this->createMock(LoggerInterface::class),
new PushMiddlewareDispatcher($this->createMock(MiddlewareFactoryPushInterface::class)),
);
$queue = $this->getDefaultQueue($worker);
$container = $this->createMock(ContainerInterface::class);
$factory = new QueueFactory(
[],
Expand All @@ -48,19 +44,18 @@ public function testQuickChange(): void
public function testConfiguredChange(): void
{
$worker = $this->createMock(WorkerInterface::class);
$queue = new Queue(
$worker,
$this->createMock(LoopInterface::class),
$this->createMock(LoggerInterface::class),
new PushMiddlewareDispatcher($this->createMock(MiddlewareFactoryPushInterface::class)),
);
$queue = $this->getDefaultQueue($worker);
$container = $this->createMock(ContainerInterface::class);
$factory = new QueueFactory(
[
'test-channel' => [
'class' => FakeAdapter::class,
'withChannel()' => ['test-channel'],
],
QueueFactoryInterface::DEFAULT_CHANNEL_NAME => [
'class' => FakeAdapter::class,
'withChannel()' => [QueueFactoryInterface::DEFAULT_CHANNEL_NAME],
],
],
$queue,
$container,
Expand All @@ -72,5 +67,16 @@ public function testConfiguredChange(): void
$queue = $factory->get('test-channel');

self::assertEquals('test-channel', $queue->getChannelName());
self::assertEquals(QueueFactoryInterface::DEFAULT_CHANNEL_NAME, $factory->get()->getChannelName());
}

private function getDefaultQueue(WorkerInterface $worker): Queue
{
return new Queue(
$worker,
$this->createMock(LoopInterface::class),
$this->createMock(LoggerInterface::class),
new PushMiddlewareDispatcher($this->createMock(MiddlewareFactoryPushInterface::class)),
);
}
}
14 changes: 14 additions & 0 deletions tests/Unit/JobStatusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
namespace Yiisoft\Yii\Queue\Tests\Unit;

use Yiisoft\Yii\Queue\Enum\JobStatus;
use Yiisoft\Yii\Queue\Exception\InvalidStatusException;
use Yiisoft\Yii\Queue\Tests\TestCase;
use Yiisoft\Yii\Queue\Tests\Unit\Support\TestJobStatus;

final class JobStatusTest extends TestCase
{
Expand Down Expand Up @@ -55,4 +57,16 @@ public function testInstanceValue(string $statusName, string $positiveMethod, ar
self::assertFalse($status->$negative(), "$negative must be false for status $statusName");
}
}

public function testException(): void
{
try {
TestJobStatus::withStatus(4)->isDone();
} catch (InvalidStatusException $exception) {
self::assertSame($exception::class, InvalidStatusException::class);
self::assertSame($exception->getName(), 'Invalid job status provided');
self::assertSame($exception->getStatus(), 4);
$this->assertMatchesRegularExpression('/JobStatus::DONE/', $exception->getSolution());
}
}
}
5 changes: 3 additions & 2 deletions tests/Unit/Middleware/Consume/MiddlewareDispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ final class MiddlewareDispatcherTest extends TestCase
public function testCallableMiddlewareCalled(): void
{
$request = $this->getConsumeRequest();
$queue = $this->createMock(QueueInterface::class);

$dispatcher = $this->createDispatcher()->withMiddlewares(
[
static function (ConsumeRequest $request): ConsumeRequest {
return $request->withMessage(new Message('test', 'New closure test data'));
static function (ConsumeRequest $request) use ($queue): ConsumeRequest {
return $request->withMessage(new Message('test', 'New closure test data'))->withQueue($queue);
},
]
);
Expand Down
60 changes: 34 additions & 26 deletions tests/Unit/QueueFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,36 +69,44 @@ public function testThrowExceptionOnEmptyAdapter(): void

public function testThrowExceptionOnEmptyDefinition(): void
{
$this->expectException(ChannelNotConfiguredException::class);

$queue = $this->createMock(QueueInterface::class);
$factory = new QueueFactory(
[],
$queue,
$this->getContainer(),
new CallableFactory($this->getContainer()),
new Injector($this->getContainer()),
false
);

$factory->get('test');
try {
$queue = $this->createMock(QueueInterface::class);
$factory = new QueueFactory(
[],
$queue,
$this->getContainer(),
new CallableFactory($this->getContainer()),
new Injector($this->getContainer()),
false
);

$factory->get('test');
} catch (ChannelNotConfiguredException $exception) {
self::assertSame($exception::class, ChannelNotConfiguredException::class);
self::assertSame($exception->getName(), 'Queue channel is not properly configured');
$this->assertMatchesRegularExpression('/"test"/', $exception->getSolution());
}
}

public function testThrowExceptionOnIncorrectDefinition(): void
{
$this->expectException(ChannelIncorrectlyConfigured::class);

$queue = $this->createMock(QueueInterface::class);
$factory = new QueueFactory(
['test' => new stdClass()],
$queue,
$this->getContainer(),
new CallableFactory($this->getContainer()),
new Injector($this->getContainer()),
false
);

$factory->get('test');
try {
$queue = $this->createMock(QueueInterface::class);
$factory = new QueueFactory(
['test' => new stdClass()],
$queue,
$this->getContainer(),
new CallableFactory($this->getContainer()),
new Injector($this->getContainer()),
false
);

$factory->get('test');
} catch (ChannelIncorrectlyConfigured $exception) {
self::assertSame($exception::class, ChannelIncorrectlyConfigured::class);
self::assertSame($exception->getName(), 'Incorrect queue channel configuration');
$this->assertMatchesRegularExpression('/"test"/', $exception->getSolution());
}
}

public function testSuccessfulDefinitionWithDefaultAdapter(): void
Expand Down
42 changes: 42 additions & 0 deletions tests/Unit/QueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Yiisoft\Yii\Queue\Tests\Unit;

use Yiisoft\Yii\Queue\Cli\SignalLoop;
use Yiisoft\Yii\Queue\Exception\AdapterConfiguration\AdapterNotConfiguredException;
use Yiisoft\Yii\Queue\Message\Message;
use Yiisoft\Yii\Queue\Tests\App\FakeAdapter;
use Yiisoft\Yii\Queue\Tests\TestCase;
Expand Down Expand Up @@ -94,4 +96,44 @@ public function testStatus(): void
$status = $queue->status($id);
self::assertTrue($status->isDone());
}

public function testAdapterNotConfiguredException(): void
{
try {
$queue = $this->getQueue();
$message = new Message('simple', null);
$queue->push($message);
$queue->status($message->getId());
} catch (AdapterNotConfiguredException $exception) {
self::assertSame($exception::class, AdapterNotConfiguredException::class);
self::assertSame($exception->getName(), 'Adapter is not configured');
$this->assertMatchesRegularExpression('/withAdapter/', $exception->getSolution());
}
}

public function testAdapterNotConfiguredExceptionForRun(): void
{
try {
$this->getQueue()->run();
} catch (AdapterNotConfiguredException $exception) {
self::assertSame($exception::class, AdapterNotConfiguredException::class);
self::assertSame($exception->getName(), 'Adapter is not configured');
$this->assertMatchesRegularExpression('/withAdapter/', $exception->getSolution());
}
}

public function testRunWithSignalLoop(): void
{
$this->loop = new SignalLoop();
$queue = $this
->getQueue()
->withAdapter($this->getAdapter());
$message = new Message('simple', null);
$message2 = clone $message;
$queue->push($message);
$queue->push($message2);
$queue->run();

self::assertEquals(2, $this->executionTimes);
}
}
15 changes: 15 additions & 0 deletions tests/Unit/Support/TestJobStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\Queue\Tests\Unit\Support;

use Yiisoft\Yii\Queue\Enum\JobStatus;

class TestJobStatus extends JobStatus
{
public static function withStatus(int $status): self
{
return new self($status);
}
}
9 changes: 4 additions & 5 deletions tests/Unit/WorkerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,6 @@ public function testJobFailWithDefinitionClassNotFoundInContainerHandler(): void

public function testJobFailWithDefinitionHandlerException(): void
{
$this->expectException(JobFailureException::class);
$this->expectExceptionMessage(
"Processing of message #null is stopped because of an exception:\nTest exception."
);

$message = new Message('simple', ['test-data']);
$logger = new SimpleLogger();
$handler = new FakeHandler();
Expand All @@ -176,6 +171,10 @@ public function testJobFailWithDefinitionHandlerException(): void

try {
$worker->process($message, $queue);
} catch (JobFailureException $exception) {
self::assertSame($exception::class, JobFailureException::class);
self::assertSame($exception->getMessage(), "Processing of message #null is stopped because of an exception:\nTest exception.");
self::assertEquals(['test-data'], $exception->getQueueMessage()->getData());
} finally {
$messages = $logger->getMessages();
$this->assertNotEmpty($messages);
Expand Down