-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathEventQueueTest.php
More file actions
120 lines (98 loc) · 3.4 KB
/
Copy pathEventQueueTest.php
File metadata and controls
120 lines (98 loc) · 3.4 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
<?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\Event\EventQueueTask;
use Bow\Mail\MailConfiguration;
use Bow\Queue\Adapters\QueueAdapter;
use Bow\Queue\Connection;
use Bow\Queue\QueueConfiguration;
use Bow\Tests\Config\TestingConfiguration;
use Bow\Tests\Events\Stubs\UserEventListenerStub;
use Bow\Tests\Events\Stubs\UserEventStub;
use Bow\View\ViewConfiguration;
use PHPUnit\Framework\TestCase;
class EventQueueTest extends TestCase
{
private const CACHE_FILENAME = TESTING_RESOURCE_BASE_DIRECTORY . '/event.txt';
private static Connection $connection;
public static function setUpBeforeClass(): void
{
// Suppress queue task logging during tests
QueueAdapter::suppressLogging(true);
TestingConfiguration::withConfigurations([
CacheConfiguration::class,
QueueConfiguration::class,
DatabaseConfiguration::class,
EnvConfiguration::class,
CryptoConfiguration::class,
LoggerConfiguration::class,
MailConfiguration::class,
ViewConfiguration::class,
]);
$config = TestingConfiguration::getConfig();
$config->boot();
static::$connection = new Connection($config["queue"]);
}
protected function tearDown(): void
{
$this->cleanupCacheFile();
parent::tearDown();
}
private function cleanupCacheFile(): void
{
@unlink(self::CACHE_FILENAME);
}
/**
* @dataProvider connectionProvider
*/
public function test_should_queue_and_process_event(string $connection): void
{
$this->cleanupCacheFile();
$adapter = static::$connection->setConnection($connection)->getAdapter();
$expectedPayload = "$connection-bowphp";
$task = new EventQueueTask(new UserEventListenerStub(), new UserEventStub($expectedPayload));
$this->assertInstanceOf(EventQueueTask::class, $task);
try {
$result = $adapter->push($task);
$this->assertTrue($result);
$adapter->setSleep(0);
$adapter->setTries(0);
$adapter->run();
$this->assertFileExists(self::CACHE_FILENAME);
$this->assertSame($expectedPayload, file_get_contents(self::CACHE_FILENAME));
} catch (\Exception $e) {
$this->markTestSkipped('Service is not available: ' . $e->getMessage());
}
}
public function test_should_create_event_queue_job_with_listener_and_payload(): void
{
$listener = new UserEventListenerStub();
$event = new UserEventStub("test-data");
$task = new EventQueueTask($listener, $event);
$this->assertInstanceOf(EventQueueTask::class, $task);
}
/**
* @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;
}
}