-
Notifications
You must be signed in to change notification settings - Fork 1.5k
PHPORM-167 Fix and test MongoDB failed queue provider #2838
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
<?php | ||
|
||
namespace MongoDB\Laravel\Tests\Queue\Failed; | ||
|
||
use Illuminate\Support\Facades\Date; | ||
use Illuminate\Support\Facades\DB; | ||
use MongoDB\BSON\ObjectId; | ||
use MongoDB\BSON\UTCDateTime; | ||
use MongoDB\Laravel\Queue\Failed\MongoFailedJobProvider; | ||
use MongoDB\Laravel\Tests\TestCase; | ||
use OutOfBoundsException; | ||
|
||
use function array_map; | ||
use function range; | ||
use function sprintf; | ||
|
||
class MongoFailedJobProviderTest extends TestCase | ||
{ | ||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
DB::connection('mongodb') | ||
->collection('failed_jobs') | ||
->raw() | ||
->insertMany(array_map(static fn ($i) => [ | ||
'_id' => new ObjectId(sprintf('%024d', $i)), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Documents are added by the |
||
'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') | ||
->collection('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); | ||
$this->assertSame((string) $inserted->_id, $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->assertEquals(new ObjectId(sprintf('%024d', 5)), $all[0]->_id); | ||
$this->assertEquals(sprintf('%024d', 5), $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->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(): MongoFailedJobProvider | ||
{ | ||
return new MongoFailedJobProvider(DB::getFacadeRoot(), '', 'failed_jobs'); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.