-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathNotifierQueueTest.php
More file actions
141 lines (118 loc) · 4.09 KB
/
Copy pathNotifierQueueTest.php
File metadata and controls
141 lines (118 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
namespace Bow\Tests\Queue;
use Bow\Cache\CacheConfiguration;
use Bow\Configuration\EnvConfiguration;
use Bow\Configuration\LoggerConfiguration;
use Bow\Database\DatabaseConfiguration;
use Bow\Security\CryptoConfiguration;
use Bow\Mail\MailConfiguration;
use Bow\Notifier\Notifier;
use Bow\Notifier\NotifierQueueTask;
use Bow\Queue\Adapters\QueueAdapter;
use Bow\Queue\Connection as QueueConnection;
use Bow\Queue\QueueConfiguration;
use Bow\Tests\Config\TestingConfiguration;
use Bow\Tests\Notifier\Stubs\MockChannelAdapter;
use Bow\Tests\Notifier\Stubs\TestNotifier;
use Bow\Tests\Notifier\Stubs\TestNotifiableModel;
use Bow\View\ViewConfiguration;
use PHPUnit\Framework\TestCase;
class NotifierQueueTest extends TestCase
{
private static QueueConnection $connection;
public static function setUpBeforeClass(): void
{
// Suppress queue task logging during tests
QueueAdapter::suppressLogging(true);
TestingConfiguration::withConfigurations([
CacheConfiguration::class,
DatabaseConfiguration::class,
QueueConfiguration::class,
EnvConfiguration::class,
CryptoConfiguration::class,
LoggerConfiguration::class,
MailConfiguration::class,
ViewConfiguration::class,
]);
$config = TestingConfiguration::getConfig();
$config->boot();
static::$connection = new QueueConnection($config["queue"]);
// Mock external notification channels to avoid requiring real credentials
Notifier::pushChannels([
'mail' => MockChannelAdapter::class,
'telegram' => MockChannelAdapter::class,
'slack' => MockChannelAdapter::class,
'sms' => MockChannelAdapter::class,
]);
}
protected function setUp(): void
{
parent::setUp();
MockChannelAdapter::reset();
}
private function createNotifierTask(): NotifierQueueTask
{
return new NotifierQueueTask(new TestNotifiableModel(), new TestNotifier());
}
public function test_can_send_message_synchronously(): void
{
$context = new TestNotifiableModel();
$message = $this->getMockBuilder(TestNotifier::class)
->onlyMethods(['process'])
->getMock();
$message->expects($this->once())
->method('process')
->with($context);
$context->sendMessage($message);
}
/**
* @dataProvider connectionProvider
*/
public function test_can_push_notifier_to_queue(string $connection): void
{
$task = $this->createNotifierTask();
$this->assertInstanceOf(NotifierQueueTask::class, $task);
try {
$result = static::$connection->setConnection($connection)->getAdapter()->push($task);
$this->assertTrue($result);
} catch (\Exception $e) {
$this->markTestSkipped("Service {$connection} is not available: " . $e->getMessage());
}
}
/**
* @dataProvider connectionProvider
*/
public function test_can_push_notifier_with_queue_and_delay_options(string $connection): void
{
$task = $this->createNotifierTask();
$adapter = static::$connection->setConnection($connection)->getAdapter();
$adapter->setQueue('notifications');
$adapter->setSleep(3600);
try {
$result = $adapter->push($task);
$this->assertTrue($result);
} catch (\Exception $e) {
$this->markTestSkipped("Service {$connection} is not available: " . $e->getMessage());
}
}
/**
* @return array<string, array{string}>
*/
public static function connectionProvider(): array
{
$data = [
"beanstalkd" => ["beanstalkd"],
"database" => ["database"],
"redis" => ["redis"],
"rabbitmq" => ["rabbitmq"],
"sync" => ["sync"],
];
if (getenv("AWS_SQS_URL")) {
$data["sqs"] = ["sqs"];
}
if (extension_loaded('rdkafka')) {
$data["kafka"] = ["kafka"];
}
return $data;
}
}