|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Yiisoft\Queue\Tests\Unit\Provider; |
| 6 | + |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use Yiisoft\Definitions\Reference; |
| 9 | +use Yiisoft\Queue\Adapter\AdapterInterface; |
| 10 | +use Yiisoft\Queue\Provider\InvalidQueueConfigException; |
| 11 | +use Yiisoft\Queue\Provider\QueueFactoryProvider; |
| 12 | +use Yiisoft\Queue\Provider\QueueNotFoundException; |
| 13 | +use Yiisoft\Queue\QueueInterface; |
| 14 | +use Yiisoft\Queue\Stubs\StubAdapter; |
| 15 | +use Yiisoft\Queue\Stubs\StubLoop; |
| 16 | +use Yiisoft\Queue\Stubs\StubQueue; |
| 17 | +use Yiisoft\Queue\Tests\Unit\Support\StringEnum; |
| 18 | +use Yiisoft\Test\Support\Container\SimpleContainer; |
| 19 | + |
| 20 | +use function sprintf; |
| 21 | + |
| 22 | +final class QueueFactoryProviderTest extends TestCase |
| 23 | +{ |
| 24 | + public function testBase(): void |
| 25 | + { |
| 26 | + $provider = new QueueFactoryProvider( |
| 27 | + [ |
| 28 | + 'queue1' => StubQueue::class, |
| 29 | + ], |
| 30 | + ); |
| 31 | + |
| 32 | + $queue = $provider->get('queue1'); |
| 33 | + |
| 34 | + $this->assertInstanceOf(StubQueue::class, $queue); |
| 35 | + $this->assertTrue($provider->has('queue1')); |
| 36 | + $this->assertFalse($provider->has('not-exist-queue')); |
| 37 | + } |
| 38 | + |
| 39 | + public function testGetTwice(): void |
| 40 | + { |
| 41 | + $provider = new QueueFactoryProvider( |
| 42 | + [ |
| 43 | + 'queue1' => StubQueue::class, |
| 44 | + ], |
| 45 | + ); |
| 46 | + |
| 47 | + $queue1 = $provider->get('queue1'); |
| 48 | + $queue2 = $provider->get('queue1'); |
| 49 | + |
| 50 | + $this->assertSame($queue1, $queue2); |
| 51 | + } |
| 52 | + |
| 53 | + public function testGetNotExistQueue(): void |
| 54 | + { |
| 55 | + $provider = new QueueFactoryProvider( |
| 56 | + [ |
| 57 | + 'queue1' => StubQueue::class, |
| 58 | + ], |
| 59 | + ); |
| 60 | + |
| 61 | + $this->expectException(QueueNotFoundException::class); |
| 62 | + $this->expectExceptionMessage('Queue with name "not-exist-queue" not found.'); |
| 63 | + $provider->get('not-exist-queue'); |
| 64 | + } |
| 65 | + |
| 66 | + public function testInvalidQueueConfig(): void |
| 67 | + { |
| 68 | + $definitions = [ |
| 69 | + 'queue1' => [ |
| 70 | + 'class' => StubQueue::class, |
| 71 | + '__construct()' => 'hello', |
| 72 | + ], |
| 73 | + ]; |
| 74 | + |
| 75 | + $this->expectException(InvalidQueueConfigException::class); |
| 76 | + $this->expectExceptionMessage( |
| 77 | + 'Invalid definition: incorrect constructor arguments. Expected array, got string.', |
| 78 | + ); |
| 79 | + new QueueFactoryProvider($definitions); |
| 80 | + } |
| 81 | + |
| 82 | + public function testInvalidQueueConfigOnGet(): void |
| 83 | + { |
| 84 | + $provider = new QueueFactoryProvider( |
| 85 | + [ |
| 86 | + 'queue1' => StubLoop::class, |
| 87 | + ], |
| 88 | + ); |
| 89 | + |
| 90 | + $this->expectException(InvalidQueueConfigException::class); |
| 91 | + $this->expectExceptionMessage( |
| 92 | + sprintf( |
| 93 | + 'Queue must implement "%s". For queue "%s" got "%s" instead.', |
| 94 | + QueueInterface::class, |
| 95 | + 'queue1', |
| 96 | + StubLoop::class, |
| 97 | + ), |
| 98 | + ); |
| 99 | + $provider->get('queue1'); |
| 100 | + } |
| 101 | + |
| 102 | + public function testGetHasByStringEnum(): void |
| 103 | + { |
| 104 | + $provider = new QueueFactoryProvider( |
| 105 | + [ |
| 106 | + 'red' => StubQueue::class, |
| 107 | + ], |
| 108 | + ); |
| 109 | + |
| 110 | + $queue = $provider->get(StringEnum::RED); |
| 111 | + |
| 112 | + $this->assertInstanceOf(StubQueue::class, $queue); |
| 113 | + $this->assertTrue($provider->has(StringEnum::RED)); |
| 114 | + $this->assertFalse($provider->has(StringEnum::GREEN)); |
| 115 | + } |
| 116 | + |
| 117 | + public function testWithContainer(): void |
| 118 | + { |
| 119 | + $adapter = new StubAdapter(); |
| 120 | + $container = new SimpleContainer([ |
| 121 | + AdapterInterface::class => $adapter, |
| 122 | + ]); |
| 123 | + |
| 124 | + $provider = new QueueFactoryProvider( |
| 125 | + [ |
| 126 | + 'queue1' => [ |
| 127 | + 'class' => StubQueue::class, |
| 128 | + '__construct()' => [ |
| 129 | + 'adapter' => Reference::to(AdapterInterface::class), |
| 130 | + ], |
| 131 | + ], |
| 132 | + ], |
| 133 | + $container, |
| 134 | + ); |
| 135 | + |
| 136 | + $queue = $provider->get('queue1'); |
| 137 | + |
| 138 | + $this->assertInstanceOf(StubQueue::class, $queue); |
| 139 | + $this->assertSame($adapter, $queue->getAdapter()); |
| 140 | + } |
| 141 | + |
| 142 | + public function testValidateFalse(): void |
| 143 | + { |
| 144 | + $provider = new QueueFactoryProvider( |
| 145 | + [ |
| 146 | + 'queue1' => [ |
| 147 | + 'class' => StubQueue::class, |
| 148 | + '__construct()' => 'hello', |
| 149 | + ], |
| 150 | + ], |
| 151 | + validate: false, |
| 152 | + ); |
| 153 | + |
| 154 | + $this->assertTrue($provider->has('queue1')); |
| 155 | + } |
| 156 | +} |
0 commit comments