Skip to content

Commit 3979c59

Browse files
committed
First test
1 parent f70407d commit 3979c59

File tree

2 files changed

+129
-20
lines changed

2 files changed

+129
-20
lines changed

src/Bus/MongoBatchRepository.php

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace MongoDB\Laravel\Bus;
44

5-
use BadMethodCallException;
65
use Carbon\CarbonImmutable;
76
use Closure;
87
use DateTimeInterface;
@@ -12,15 +11,14 @@
1211
use Illuminate\Bus\PendingBatch;
1312
use Illuminate\Bus\PrunableBatchRepository;
1413
use Illuminate\Bus\UpdatedBatchJobCounts;
15-
use Illuminate\Database\Connection;
1614
use Illuminate\Support\Carbon;
1715
use MongoDB\BSON\ObjectId;
1816
use MongoDB\BSON\UTCDateTime;
1917
use MongoDB\Driver\ReadPreference;
2018
use MongoDB\Laravel\Collection;
19+
use MongoDB\Laravel\Connection;
2120
use Override;
2221

23-
use function assert;
2422
use function date_default_timezone_get;
2523
use function is_string;
2624

@@ -35,7 +33,6 @@ public function __construct(
3533
Connection $connection,
3634
string $collection,
3735
) {
38-
assert($connection instanceof \MongoDB\Laravel\Connection);
3936
$this->collection = $connection->getCollection($collection);
4037

4138
parent::__construct($factory, $connection, $collection);
@@ -65,38 +62,43 @@ public function find(string $batchId): ?Batch
6562

6663
$batch = $this->collection->findOne(
6764
['_id' => $batchId],
68-
['readPreference' => ReadPreference::PRIMARY],
65+
[
66+
'readPreference' => new ReadPreference(ReadPreference::PRIMARY),
67+
'typeMap' => ['root' => 'array', 'array' => 'array', 'document' => 'array'],
68+
],
6969
);
7070

7171
return $this->factory->make(
7272
$this,
73-
$batch['id'],
73+
$batch['_id'],
7474
$batch['name'],
7575
$batch['total_jobs'],
7676
$batch['pending_jobs'],
7777
$batch['failed_jobs'],
7878
$batch['failed_job_ids'],
7979
$batch['options'],
80-
CarbonImmutable::createFromTimestamp($batch['created_at']->getTimestamp(), date_default_timezone_get()),
81-
$batch['cancelled_at'] ? CarbonImmutable::createFromTimestamp($batch['cancelled_at']->getTimestamp(), date_default_timezone_get()) : null,
82-
$batch['finished_at'] ? CarbonImmutable::createFromTimestamp($batch['finished_at']->getTimestamp(), date_default_timezone_get()) : null,
80+
$this->toCarbon($batch['created_at']),
81+
$this->toCarbon($batch['cancelled_at']),
82+
$this->toCarbon($batch['finished_at']),
8383
);
8484
}
8585

8686
#[Override]
8787
public function store(PendingBatch $batch): Batch
8888
{
89-
$this->collection->insertOne([
89+
$result = $this->collection->insertOne([
9090
'name' => $batch->name,
9191
'total_jobs' => 0,
9292
'pending_jobs' => 0,
9393
'failed_jobs' => 0,
94-
'failed_job_ids' => '[]',
95-
'options' => $this->serialize($batch->options),
94+
'failed_job_ids' => [],
95+
'options' => $batch->options,
9696
'created_at' => new UTCDateTime(Carbon::now()),
9797
'cancelled_at' => null,
9898
'finished_at' => null,
9999
]);
100+
101+
return $this->find($result->getInsertedId());
100102
}
101103

102104
#[Override]
@@ -195,19 +197,16 @@ public function delete(string $batchId): void
195197
#[Override]
196198
public function transaction(Closure $callback): mixed
197199
{
198-
// Transactions are not necessary
199-
return $callback();
200+
return $this->connection->transaction(fn () => $callback());
200201
}
201202

202203
/**
203204
* Rollback the last database transaction for the connection.
204-
*
205-
* Not implemented.
206205
*/
207206
#[Override]
208207
public function rollBack(): void
209208
{
210-
throw new BadMethodCallException('Not implemented');
209+
$this->connection->rollBack();
211210
}
212211

213212
/** Mark the batch that has the given ID as finished. */
@@ -259,9 +258,19 @@ protected function toBatch($batch): Batch
259258
$batch->failed_jobs,
260259
$batch->failed_job_ids,
261260
$batch->options,
262-
CarbonImmutable::createFromTimestamp($batch->created_at->getTimestamp(), date_default_timezone_get()),
263-
$batch->cancelled_at ? CarbonImmutable::createFromTimestamp($batch->cancelled_at->getTimestamp(), date_default_timezone_get()) : null,
264-
$batch->finished_at ? CarbonImmutable::createFromTimestamp($batch->finished_at->getTimestamp(), date_default_timezone_get()) : null,
261+
$this->toCarbon($batch->created_at),
262+
$this->toCarbon($batch->cancelled_at),
263+
$this->toCarbon($batch->finished_at),
265264
);
266265
}
266+
267+
/** @return ($date is null ? null : CarbonImmutable) */
268+
private function toCarbon(?UTCDateTime $date): ?CarbonImmutable
269+
{
270+
if ($date === null) {
271+
return null;
272+
}
273+
274+
return CarbonImmutable::createFromTimestamp((string) $date, date_default_timezone_get());
275+
}
267276
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
namespace MongoDB\Laravel\Tests\Bus;
4+
5+
use Carbon\CarbonImmutable;
6+
use Illuminate\Bus\Batch;
7+
use Illuminate\Bus\Batchable;
8+
use Illuminate\Bus\BatchFactory;
9+
use Illuminate\Bus\PendingBatch;
10+
use Illuminate\Container\Container;
11+
use Illuminate\Contracts\Queue\Factory;
12+
use Illuminate\Queue\CallQueuedClosure;
13+
use Illuminate\Queue\Queue;
14+
use Illuminate\Support\Facades\DB;
15+
use Illuminate\Tests\Bus\BusBatchTest;
16+
use Mockery as m;
17+
use MongoDB\Laravel\Bus\MongoBatchRepository;
18+
use MongoDB\Laravel\Connection;
19+
use MongoDB\Laravel\Tests\TestCase;
20+
use stdClass;
21+
22+
use function collect;
23+
use function is_string;
24+
25+
final class MongoBatchRepositoryTest extends TestCase
26+
{
27+
private Queue $queue;
28+
29+
/** @see BusBatchTest::test_jobs_can_be_added_to_the_batch */
30+
public function testJobsCanBeAddedToTheBatch(): void
31+
{
32+
$queue = m::mock(Factory::class);
33+
34+
$batch = $this->createTestBatch($queue);
35+
36+
$job = new class
37+
{
38+
use Batchable;
39+
};
40+
41+
$secondJob = new class
42+
{
43+
use Batchable;
44+
};
45+
46+
$thirdJob = function () {
47+
};
48+
49+
$queue->shouldReceive('connection')->once()
50+
->with('test-connection')
51+
->andReturn($connection = m::mock(stdClass::class));
52+
53+
$connection->shouldReceive('bulk')->once()->with(m::on(function ($args) use ($job, $secondJob) {
54+
return $args[0] === $job &&
55+
$args[1] === $secondJob &&
56+
$args[2] instanceof CallQueuedClosure
57+
&& is_string($args[2]->batchId);
58+
}), '', 'test-queue');
59+
60+
$batch = $batch->add([$job, $secondJob, $thirdJob]);
61+
62+
$this->assertEquals(3, $batch->totalJobs);
63+
$this->assertEquals(3, $batch->pendingJobs);
64+
$this->assertIsString($job->batchId);
65+
$this->assertInstanceOf(CarbonImmutable::class, $batch->createdAt);
66+
}
67+
68+
/** @see BusBatchTest::createTestBatch() */
69+
private function createTestBatch($queue, $allowFailures = false)
70+
{
71+
$connection = DB::connection('mongodb');
72+
$this->assertInstanceOf(Connection::class, $connection);
73+
74+
$repository = new MongoBatchRepository(new BatchFactory($queue), $connection, 'job_batches');
75+
76+
$pendingBatch = (new PendingBatch(new Container(), collect()))
77+
->progress(function (Batch $batch) {
78+
$_SERVER['__progress.batch'] = $batch;
79+
$_SERVER['__progress.count']++;
80+
})
81+
->then(function (Batch $batch) {
82+
$_SERVER['__then.batch'] = $batch;
83+
$_SERVER['__then.count']++;
84+
})
85+
->catch(function (Batch $batch, $e) {
86+
$_SERVER['__catch.batch'] = $batch;
87+
$_SERVER['__catch.exception'] = $e;
88+
$_SERVER['__catch.count']++;
89+
})
90+
->finally(function (Batch $batch) {
91+
$_SERVER['__finally.batch'] = $batch;
92+
$_SERVER['__finally.count']++;
93+
})
94+
->allowFailures($allowFailures)
95+
->onConnection('test-connection')
96+
->onQueue('test-queue');
97+
98+
return $repository->store($pendingBatch);
99+
}
100+
}

0 commit comments

Comments
 (0)