-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
157 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace romanzipp\ProjectableAggregates\Command; | ||
|
||
use Illuminate\Console\Command; | ||
use romanzipp\ProjectableAggregates\Jobs\CalculateBulkAggregatesJob; | ||
|
||
final class CalculateBulkAggregatesCommand extends Command | ||
{ | ||
protected $signature = 'aggregates:bulk-aggregate | ||
{--queued : Push a job to the queue} | ||
{--queue= : The queue to push the job to} | ||
{--class=}'; | ||
|
||
public function handle(): int | ||
{ | ||
$classes = []; | ||
|
||
$job = new CalculateBulkAggregatesJob($classes); | ||
|
||
if ($this->option('queued')) { | ||
$this->info('Pushing aggregation to queue...'); | ||
|
||
$pendingDispatch = dispatch($job); | ||
|
||
if ($this->hasOption('queue')) { | ||
$pendingDispatch->onQueue( | ||
$this->option('queue') | ||
); | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
$job->handle(); | ||
|
||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace romanzipp\ProjectableAggregates\Jobs; | ||
|
||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Queue\SerializesModels; | ||
use romanzipp\ProjectableAggregates\ProjectableAggregateRegistry; | ||
|
||
final class CalculateBulkAggregatesJob | ||
{ | ||
use SerializesModels; | ||
use Queueable; | ||
|
||
/** | ||
* @param array<class-string> $consumerClasses | ||
*/ | ||
public function __construct( | ||
public array $consumerClasses = [] | ||
) { | ||
} | ||
|
||
public function handle(): void | ||
{ | ||
$registry = app(ProjectableAggregateRegistry::class); | ||
$registry->bulkAggregate(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
namespace romanzipp\ProjectableAggregates\Tests; | ||
|
||
use Illuminate\Support\Facades\Bus; | ||
use romanzipp\ProjectableAggregates\Jobs\CalculateBulkAggregatesJob; | ||
use romanzipp\ProjectableAggregates\ProjectableAggregateRegistry; | ||
use romanzipp\ProjectableAggregates\Tests\Support\PivotConsumer; | ||
use romanzipp\ProjectableAggregates\Tests\Support\PivotProvider; | ||
use romanzipp\ProjectableAggregates\Tests\Support\PivotProviderConsumerPivot; | ||
|
||
class BulkCommandTest extends TestCase | ||
{ | ||
public function testCommand() | ||
{ | ||
$registry = app(ProjectableAggregateRegistry::class); | ||
$registry->registerConsumers([PivotConsumer::class]); | ||
$registry->registerProviders([PivotProvider::class]); | ||
|
||
PivotProviderConsumerPivot::query()->create([ | ||
'consumer_id' => ($consumer = PivotConsumer::query()->create()->refresh())->id, | ||
'provider_id' => ($provider = PivotProvider::query()->create())->id, | ||
]); | ||
|
||
self::assertSame(1, $consumer->providers()->count()); | ||
self::assertSame(0, $consumer->projection_providers_count); | ||
|
||
// Command ----------------------------------------------------- | ||
$this->artisan('aggregates:bulk-aggregate')->assertSuccessful(); | ||
// ------------------------------------------------------------- | ||
|
||
$consumer->refresh(); | ||
|
||
self::assertSame(1, $consumer->providers()->count()); | ||
self::assertSame(1, $consumer->projection_providers_count); | ||
} | ||
|
||
public function testQueued() | ||
{ | ||
$bus = Bus::fake(); | ||
|
||
$this->artisan('aggregates:bulk-aggregate --queued'); | ||
|
||
$bus->assertDispatched( | ||
CalculateBulkAggregatesJob::class | ||
); | ||
} | ||
|
||
public function testQueuedOnOtherQueue() | ||
{ | ||
$bus = Bus::fake(); | ||
|
||
$this->artisan('aggregates:bulk-aggregate --queued --queue=foobar'); | ||
|
||
$bus->assertDispatched( | ||
CalculateBulkAggregatesJob::class, | ||
fn (CalculateBulkAggregatesJob $job) => 'foobar' === $job->queue | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters