forked from mongodb/laravel-mongodb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabaseFailedJobProviderTest.php
154 lines (116 loc) · 4.49 KB
/
DatabaseFailedJobProviderTest.php
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
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
namespace MongoDB\Laravel\Tests\Queue\Failed;
use Illuminate\Queue\Failed\DatabaseFailedJobProvider;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\DB;
use MongoDB\BSON\ObjectId;
use MongoDB\BSON\UTCDateTime;
use MongoDB\Laravel\Tests\TestCase;
use OutOfBoundsException;
use function array_map;
use function range;
use function sprintf;
/**
* Ensure the Laravel class {@see DatabaseFailedJobProvider} works with a MongoDB connection.
*/
class DatabaseFailedJobProviderTest extends TestCase
{
public function setUp(): void
{
parent::setUp();
DB::connection('mongodb')
->table('failed_jobs')
->raw()
->insertMany(array_map(static fn ($i) => [
'_id' => new ObjectId(sprintf('%024d', $i)),
'connection' => 'mongodb',
'queue' => $i % 2 ? 'default' : 'other',
'failed_at' => new UTCDateTime(Date::now()->subHours($i)),
], range(1, 5)));
}
public function tearDown(): void
{
DB::connection('mongodb')
->table('failed_jobs')
->raw()
->drop();
parent::tearDown();
}
public function testLog(): void
{
$provider = $this->getProvider();
$provider->log('mongodb', 'default', '{"foo":"bar"}', new OutOfBoundsException('This is the error'));
$ids = $provider->ids();
$this->assertCount(6, $ids);
$inserted = $provider->find($ids[0]);
$this->assertSame('mongodb', $inserted->connection);
$this->assertSame('default', $inserted->queue);
$this->assertSame('{"foo":"bar"}', $inserted->payload);
$this->assertStringContainsString('OutOfBoundsException: This is the error', $inserted->exception);
$this->assertInstanceOf(ObjectId::class, $inserted->id);
}
public function testCount(): void
{
$provider = $this->getProvider();
$this->assertEquals(5, $provider->count());
$this->assertEquals(3, $provider->count('mongodb', 'default'));
$this->assertEquals(2, $provider->count('mongodb', 'other'));
}
public function testAll(): void
{
$all = $this->getProvider()->all();
$this->assertCount(5, $all);
$this->assertInstanceOf(ObjectId::class, $all[0]->id);
$this->assertEquals(new ObjectId(sprintf('%024d', 5)), $all[0]->id);
$this->assertEquals(sprintf('%024d', 5), (string) $all[0]->id, 'id field is added for compatibility with DatabaseFailedJobProvider');
}
public function testFindAndForget(): void
{
$provider = $this->getProvider();
$id = sprintf('%024d', 2);
$found = $provider->find($id);
$this->assertIsObject($found, 'The job is found');
$this->assertInstanceOf(ObjectId::class, $found->id);
$this->assertEquals(new ObjectId($id), $found->id);
$this->assertObjectHasProperty('failed_at', $found);
// Delete the job
$result = $provider->forget($id);
$this->assertTrue($result, 'forget return true when the job have been deleted');
$this->assertNull($provider->find($id), 'the job have been deleted');
// Delete the same job again
$result = $provider->forget($id);
$this->assertFalse($result, 'forget return false when the job does not exist');
$this->assertCount(4, $provider->ids(), 'Other jobs are kept');
}
public function testIds(): void
{
$ids = $this->getProvider()->ids();
$this->assertCount(5, $ids);
$this->assertEquals(new ObjectId(sprintf('%024d', 5)), $ids[0]);
}
public function testIdsFilteredByQuery(): void
{
$ids = $this->getProvider()->ids('other');
$this->assertCount(2, $ids);
$this->assertEquals(new ObjectId(sprintf('%024d', 4)), $ids[0]);
}
public function testFlush(): void
{
$provider = $this->getProvider();
$this->assertEquals(5, $provider->count());
$provider->flush(4);
$this->assertEquals(3, $provider->count());
}
public function testPrune(): void
{
$provider = $this->getProvider();
$this->assertEquals(5, $provider->count());
$result = $provider->prune(Date::now()->subHours(4));
$this->assertEquals(2, $result);
$this->assertEquals(3, $provider->count());
}
private function getProvider(): DatabaseFailedJobProvider
{
return new DatabaseFailedJobProvider(DB::getFacadeRoot(), '', 'failed_jobs');
}
}