|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Hypervel\Tests\Queue; |
| 6 | + |
| 7 | +use Exception; |
| 8 | +use Hyperf\Di\Container; |
| 9 | +use Hyperf\Di\Definition\DefinitionSource; |
| 10 | +use Hypervel\Database\TransactionManager; |
| 11 | +use Hypervel\Queue\Contracts\QueueableEntity; |
| 12 | +use Hypervel\Queue\Contracts\ShouldQueueAfterCommit; |
| 13 | +use Hypervel\Queue\CoroutineQueue; |
| 14 | +use Hypervel\Queue\InteractsWithQueue; |
| 15 | +use Hypervel\Queue\Jobs\SyncJob; |
| 16 | +use Mockery as m; |
| 17 | +use PHPUnit\Framework\TestCase; |
| 18 | +use Psr\EventDispatcher\EventDispatcherInterface; |
| 19 | + |
| 20 | +use function Hyperf\Coroutine\run; |
| 21 | + |
| 22 | +/** |
| 23 | + * @internal |
| 24 | + * @coversNothing |
| 25 | + */ |
| 26 | +class QueueCoroutineQueueTest extends TestCase |
| 27 | +{ |
| 28 | + public function testPushShouldCoroutine() |
| 29 | + { |
| 30 | + unset($_SERVER['__coroutine.test']); |
| 31 | + |
| 32 | + $coroutine = new CoroutineQueue(); |
| 33 | + $coroutine->setConnectionName('coroutine'); |
| 34 | + $container = $this->getContainer(); |
| 35 | + $coroutine->setContainer($container); |
| 36 | + $coroutine->setConnectionName('coroutine'); |
| 37 | + |
| 38 | + run(fn () => $coroutine->push(CoroutineQueueTestHandler::class, ['foo' => 'bar'])); |
| 39 | + |
| 40 | + $this->assertInstanceOf(SyncJob::class, $_SERVER['__coroutine.test'][0]); |
| 41 | + $this->assertEquals(['foo' => 'bar'], $_SERVER['__coroutine.test'][1]); |
| 42 | + } |
| 43 | + |
| 44 | + public function testFailedJobGetsHandledWhenAnExceptionIsThrown() |
| 45 | + { |
| 46 | + unset($_SERVER['__coroutine.failed']); |
| 47 | + |
| 48 | + $result = null; |
| 49 | + |
| 50 | + $coroutine = new CoroutineQueue(); |
| 51 | + $coroutine->setExceptionCallback(function ($exception) use (&$result) { |
| 52 | + $result = $exception; |
| 53 | + }); |
| 54 | + $coroutine->setConnectionName('coroutine'); |
| 55 | + $container = $this->getContainer(); |
| 56 | + $events = m::mock(EventDispatcherInterface::class); |
| 57 | + $events->shouldReceive('dispatch')->times(3); |
| 58 | + $container->set(EventDispatcherInterface::class, $events); |
| 59 | + $coroutine->setContainer($container); |
| 60 | + |
| 61 | + run(function () use ($coroutine) { |
| 62 | + $coroutine->push(FailingCoroutineQueueTestHandler::class, ['foo' => 'bar']); |
| 63 | + }); |
| 64 | + |
| 65 | + $this->assertInstanceOf(Exception::class, $result); |
| 66 | + $this->assertTrue($_SERVER['__coroutine.failed']); |
| 67 | + } |
| 68 | + |
| 69 | + public function testItAddsATransactionCallbackForAfterCommitJobs() |
| 70 | + { |
| 71 | + $coroutine = new CoroutineQueue(); |
| 72 | + $container = $this->getContainer(); |
| 73 | + $transactionManager = m::mock(TransactionManager::class); |
| 74 | + $transactionManager->shouldReceive('addCallback')->once()->andReturn(null); |
| 75 | + $container->set(TransactionManager::class, $transactionManager); |
| 76 | + |
| 77 | + $coroutine->setContainer($container); |
| 78 | + run(fn () => $coroutine->push(new CoroutineQueueAfterCommitJob())); |
| 79 | + } |
| 80 | + |
| 81 | + public function testItAddsATransactionCallbackForInterfaceBasedAfterCommitJobs() |
| 82 | + { |
| 83 | + $coroutine = new CoroutineQueue(); |
| 84 | + $container = $this->getContainer(); |
| 85 | + $transactionManager = m::mock(TransactionManager::class); |
| 86 | + $transactionManager->shouldReceive('addCallback')->once()->andReturn(null); |
| 87 | + $container->set(TransactionManager::class, $transactionManager); |
| 88 | + |
| 89 | + $coroutine->setContainer($container); |
| 90 | + run(fn () => $coroutine->push(new CoroutineQueueAfterCommitInterfaceJob())); |
| 91 | + } |
| 92 | + |
| 93 | + protected function getContainer(): Container |
| 94 | + { |
| 95 | + return new Container( |
| 96 | + new DefinitionSource([]) |
| 97 | + ); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +class CoroutineQueueTestEntity implements QueueableEntity |
| 102 | +{ |
| 103 | + public function getQueueableId(): mixed |
| 104 | + { |
| 105 | + return 1; |
| 106 | + } |
| 107 | + |
| 108 | + public function getQueueableConnection(): ?string |
| 109 | + { |
| 110 | + return null; |
| 111 | + } |
| 112 | + |
| 113 | + public function getQueueableRelations(): array |
| 114 | + { |
| 115 | + return []; |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +class CoroutineQueueTestHandler |
| 120 | +{ |
| 121 | + public function fire($job, $data) |
| 122 | + { |
| 123 | + $_SERVER['__coroutine.test'] = func_get_args(); |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +class FailingCoroutineQueueTestHandler |
| 128 | +{ |
| 129 | + public function fire($job, $data) |
| 130 | + { |
| 131 | + throw new Exception(); |
| 132 | + } |
| 133 | + |
| 134 | + public function failed() |
| 135 | + { |
| 136 | + $_SERVER['__coroutine.failed'] = true; |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +class CoroutineQueueAfterCommitJob |
| 141 | +{ |
| 142 | + use InteractsWithQueue; |
| 143 | + |
| 144 | + public $afterCommit = true; |
| 145 | + |
| 146 | + public function handle() |
| 147 | + { |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +class CoroutineQueueAfterCommitInterfaceJob implements ShouldQueueAfterCommit |
| 152 | +{ |
| 153 | + use InteractsWithQueue; |
| 154 | + |
| 155 | + public function handle() |
| 156 | + { |
| 157 | + } |
| 158 | +} |
0 commit comments