Skip to content

Commit b06d00d

Browse files
committed
initial test
1 parent 8b88ebd commit b06d00d

16 files changed

+428
-95
lines changed

composer.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55
"type": "library",
66
"require": {
77
"php": "^8.1",
8+
"illuminate/queue": "^9.0",
89
"php-amqplib/php-amqplib": "^3.4",
9-
"illuminate/queue": "^9.0"
10+
"illuminate/support": "^9.18"
1011
},
1112
"require-dev": {
1213
"ext-json": "*",
1314
"phpunit/phpunit": "^9.5",
14-
"squizlabs/php_codesniffer": "^3.7"
15+
"squizlabs/php_codesniffer": "^3.7",
16+
"mockery/mockery": "^1.5.1",
17+
"dg/bypass-finals": "dev-master"
1518
},
1619
"autoload": {
1720
"psr-4": {
@@ -20,7 +23,7 @@
2023
},
2124
"autoload-dev": {
2225
"psr-4": {
23-
"iamfarhad\\LaravelRabbitMQ\\Tests\\Unit\\": "tests/Unit"
26+
"iamfarhad\\LaravelRabbitMQ\\Tests\\": "tests/"
2427
}
2528
},
2629
"extra": {

phpcs.xml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
<?xml version="1.0"?>
22
<ruleset name="PHP_CodeSniffer">
33
<description>The coding standard for our project.</description>
4+
<file>src</file>
5+
<file>tests</file>
6+
47
<rule ref="PSR12"/>
5-
<exclude-pattern>tests/*</exclude-pattern>
8+
69

710
<!-- Show progression -->
811
<arg value="p"/>
@@ -12,6 +15,4 @@
1215
<exclude name="Generic.WhiteSpace.DisallowTabIndent"/>
1316
<exclude name="Generic.Files.LineLength"/>
1417
</rule>
15-
16-
<file>src</file>
1718
</ruleset>

phpunit.xml.dist

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
</coverage>
1515
<testsuites>
1616
<testsuite name="Package Test Suite">
17-
<directory suffix=".php">./tests/</directory>
17+
<directory suffix="Test.php">./tests/</directory>
1818
</testsuite>
1919
</testsuites>
2020
<php>
21-
<env name="HOST" value="127.0.0.1"/>
22-
<env name="PORT" value="5672"/>
21+
<env name="RABBITMQ_HOST" value="127.0.0.1"/>
22+
<env name="QUEUE_CONNECTION" value="rabbitmq"/>
23+
<env name="RABBITMQ_PORT" value="5672"/>
2324
<env name="PORT_SSL" value="5671"/>
2425
<env name="RABBITMQ_SSL_CAFILE" value="./tests/files/rootCA.pem"/>
2526
</php>

src/Connectors/RabbitMQConnector.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010
use PhpAmqpLib\Connection\AMQPConnectionConfig;
1111
use PhpAmqpLib\Connection\AMQPConnectionFactory;
1212

13-
final class RabbitMQConnector implements ConnectorInterface
13+
class RabbitMQConnector implements ConnectorInterface
1414
{
1515
public function __construct(private readonly Dispatcher $dispatcher)
1616
{
17-
}
17+
}//end __construct()
18+
1819

1920
public function connect(array $config = []): Queue
2021
{
@@ -31,7 +32,6 @@ public function connect(array $config = []): Queue
3132
$amqpConnectionConfig->setKeepalive(config('queue.connections.rabbitmq.hosts.keepalive'));
3233
$amqpConnectionConfig->setHeartbeat(config('queue.connections.rabbitmq.hosts.heartbeat'));
3334

34-
3535
// set SSL Options
3636
$amqpConnectionConfig->setSslCaCert(config('queue.connections.rabbitmq.options.ssl_options.cafile'));
3737
$amqpConnectionConfig->setSslCert(config('queue.connections.rabbitmq.options.ssl_options.local_cert'));
@@ -40,15 +40,13 @@ public function connect(array $config = []): Queue
4040
$amqpConnectionConfig->setSslPassPhrase(config('queue.connections.rabbitmq.options.ssl_options.passphrase'));
4141

4242
// Create AMQP Connection
43-
$connection = AMQPConnectionFactory::create($amqpConnectionConfig);
43+
$connection = AMQPConnectionFactory::create($amqpConnectionConfig);
4444
$defaultQueue = config('queue.connections.rabbitmq.queue');
4545

4646
$rabbitQueue = new RabbitQueue($connection, $defaultQueue);
4747

48-
$this->dispatcher->listen(WorkerStopping::class, static function () use ($rabbitQueue): void {
49-
$rabbitQueue->close();
50-
});
48+
$this->dispatcher->listen(WorkerStopping::class, fn() => $rabbitQueue->close());
5149

5250
return $rabbitQueue;
53-
}
54-
}
51+
}//end connect()
52+
}//end class

src/Console/ConsumeCommand.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,12 @@ final class ConsumeCommand extends WorkCommand
3434

3535
protected $description = 'Consume messages';
3636

37+
3738
public function handle(): void
3839
{
39-
/** @var Consumer $consumer */
40+
/*
41+
* @var Consumer $consumer
42+
*/
4043
$consumer = $this->worker;
4144

4245
$consumer->setContainer($this->laravel);
@@ -47,20 +50,24 @@ public function handle(): void
4750
$consumer->setPrefetchCount((int) $this->option('prefetch-count'));
4851

4952
parent::handle();
50-
}
53+
}//end handle()
54+
5155

5256
private function consumerTag(): string
5357
{
5458
if ($consumerTag = $this->option('consumer-tag')) {
5559
return $consumerTag;
5660
}
5761

58-
$consumerTag = implode('_', [
59-
Str::slug(config('app.name', 'laravel')),
60-
Str::slug($this->option('name')),
61-
md5(serialize($this->options()) . Str::random(16) . getmypid()),
62-
]);
62+
$consumerTag = implode(
63+
'_',
64+
[
65+
Str::slug(config('app.name', 'laravel')),
66+
Str::slug($this->option('name')),
67+
md5(serialize($this->options()) . Str::random(16) . getmypid()),
68+
]
69+
);
6370

6471
return Str::substr($consumerTag, 0, 255);
65-
}
66-
}
72+
}//end consumerTag()
73+
}//end class

src/Consumer.php

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use Throwable;
1313
use iamfarhad\LaravelRabbitMQ\RabbitQueue;
1414

15-
final class Consumer extends Worker
15+
class Consumer extends Worker
1616
{
1717
private Container $container;
1818

@@ -26,38 +26,44 @@ final class Consumer extends Worker
2626

2727
private AMQPChannel $amqpChannel;
2828

29-
private ?object $currentJob = null;
29+
private object|null $currentJob = null;
30+
3031

3132
public function setContainer(Container $container): void
3233
{
3334
$this->container = $container;
34-
}
35+
}//end setContainer()
36+
3537

3638
public function setConsumerTag(string $value): void
3739
{
3840
$this->consumerTag = $value;
39-
}
41+
}//end setConsumerTag()
42+
4043

4144
public function setMaxPriority(int $value): void
4245
{
4346
$this->maxPriority = $value;
44-
}
47+
}//end setMaxPriority()
48+
4549

4650
public function setPrefetchSize(int $value): void
4751
{
4852
$this->prefetchSize = $value;
49-
}
53+
}//end setPrefetchSize()
54+
5055

5156
public function setPrefetchCount(int $value): void
5257
{
5358
$this->prefetchCount = $value;
54-
}
59+
}//end setPrefetchCount()
60+
5561

5662
/**
5763
* Listen to the given queue in a loop.
5864
*
59-
* @param string $connectionName
60-
* @param string $queue
65+
* @param string $connectionName
66+
* @param string $queue
6167
* @return int
6268
* @throws Throwable
6369
*/
@@ -68,7 +74,7 @@ public function daemon($connectionName, $queue, WorkerOptions $workerOptions)
6874
}
6975

7076
$timestampOfLastQueueRestart = $this->getTimestampOfLastQueueRestart();
71-
$startTime = hrtime(true) / 1e9;
77+
$startTime = (hrtime(true) / 1e9);
7278
$jobsProcessed = 0;
7379

7480
$connection = $this->manager->connection($connectionName);
@@ -82,10 +88,13 @@ public function daemon($connectionName, $queue, WorkerOptions $workerOptions)
8288
null
8389
);
8490

85-
$jobClass = $connection->getJobClass();
91+
$jobClass = $connection->getJobClass();
8692
$arguments = [];
8793
if ($this->maxPriority !== 0) {
88-
$arguments['priority'] = ['I', $this->maxPriority];
94+
$arguments['priority'] = [
95+
'I',
96+
$this->maxPriority,
97+
];
8998
}
9099

91100
$this->amqpChannel->basic_consume(
@@ -166,19 +175,21 @@ function (AMQPMessage $amqpMessage) use ($connection, $workerOptions, $connectio
166175
}
167176

168177
$this->currentJob = null;
169-
}
170-
}
178+
}//end while
179+
}//end daemon()
180+
171181

172182
/**
173183
* Determine if the daemon should process on this iteration.
174184
*
175-
* @param string $connectionName
176-
* @param string $queue
185+
* @param string $connectionName
186+
* @param string $queue
177187
*/
178188
protected function daemonShouldRun(WorkerOptions $workerOptions, $connectionName, $queue): bool
179189
{
180190
return !(($this->isDownForMaintenance)() && ! $workerOptions->force) && !$this->paused;
181-
}
191+
}//end daemonShouldRun()
192+
182193

183194
public function stop($status = 0, $options = []): int
184195
{
@@ -187,5 +198,5 @@ public function stop($status = 0, $options = []): int
187198
$this->amqpChannel->basic_cancel($this->consumerTag, false, true);
188199

189200
return parent::stop($status);
190-
}
191-
}
201+
}//end stop()
202+
}//end class

0 commit comments

Comments
 (0)