Skip to content

Commit b2b6700

Browse files
author
Farhad.Zand
committed
add some tests and fix consumer bug
1 parent 0732a80 commit b2b6700

File tree

3 files changed

+47
-8
lines changed

3 files changed

+47
-8
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"php": "^8.1",
88
"illuminate/queue": "^9.0|^10.0",
99
"php-amqplib/php-amqplib": "^3.6.0",
10-
"illuminate/support": "^9.18|^10.0"
10+
"illuminate/support": "^9.18|^10.0",
11+
"ext-pcntl": "Required to use all features of the queue consumer."
1112
},
1213
"require-dev": {
1314
"ext-json": "*",
@@ -44,6 +45,5 @@
4445
],
4546
"minimum-stability": "dev",
4647
"suggest": {
47-
"ext-pcntl": "Required to use all features of the queue consumer."
4848
}
4949
}

src/Console/ConsumeCommand.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,21 @@ public function handle(): int|null
4545
// Error handling
4646
echo "Could not fork process \n";
4747
exit(1);
48-
} elseif ($pid === 0) {
48+
}
49+
50+
if ($pid === 0) {
4951
// This is the child process
5052
$this->consume();
5153
exit(0);
5254
}
5355
}
5456

5557
// Wait for all child processes to finish
56-
while (pcntl_waitpid(0, $status) !== -1) {
58+
// while (pcntl_waitpid(0, $status) !== -1) {
5759
// Handle exit status if needed
58-
}
60+
// }
5961

6062
return 0;
61-
;
6263
}
6364

6465
private function consume(): void

tests/Feature/RabbitMQQueueTest.php

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44

55
use iamfarhad\LaravelRabbitMQ\Tests\FeatureTestCase;
66
use iamfarhad\LaravelRabbitMQ\Tests\Mocks\TestJobMock;
7+
use iamfarhad\LaravelRabbitMQ\Connectors\RabbitMQConnector;
8+
use Illuminate\Contracts\Events\Dispatcher;
9+
use Illuminate\Queue\Events\WorkerStopping;
710
use Illuminate\Support\Facades\Bus;
8-
use Illuminate\Support\Facades\Queue;
11+
use Illuminate\Queue\Queue;
912
use iamfarhad\LaravelRabbitMQ\Consumer;
1013
use Mockery;
1114

@@ -21,7 +24,42 @@ protected function setUp(): void
2124
$this->connection = $getQueueInstance->connection('rabbitmq');
2225
}
2326

24-
public function testConsumeMethod()
27+
public function testConnect(): void
28+
{
29+
$dispatcher = Mockery::mock(Dispatcher::class);
30+
31+
$dispatcher->expects('listen')
32+
->with(WorkerStopping::class, Mockery::any());
33+
34+
$connector = new RabbitMQConnector($dispatcher);
35+
$config = [
36+
'hosts' => [
37+
'host' => 'localhost',
38+
'port' => 5672,
39+
'user' => 'guest',
40+
'password' => 'guest',
41+
'vhost' => '/',
42+
'lazy' => false,
43+
'keepalive' => false,
44+
'heartbeat' => 60,
45+
],
46+
'options' => [
47+
'ssl_options' => [
48+
'cafile' => 'path/to/cafile',
49+
'local_cert' => 'path/to/local_cert',
50+
'local_key' => 'path/to/local_key',
51+
'verify_peer' => true,
52+
'passphrase' => 'your_passphrase',
53+
],
54+
],
55+
];
56+
57+
$queue = $connector->connect($config);
58+
59+
$this->assertInstanceOf(Queue::class, $queue);
60+
}
61+
62+
public function testConsumeMethod(): void
2563
{
2664
// Create a Mockery mock of the Consumer class
2765
$consumerMock = Mockery::mock(Consumer::class);

0 commit comments

Comments
 (0)