Skip to content
This repository has been archived by the owner on Dec 3, 2024. It is now read-only.

Use conditional clauses when building haystack #32

Merged
merged 1 commit into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Builders/HaystackBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
use Closure;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Traits\Conditionable;
use Sammyjo20\LaravelHaystack\Models\Haystack;
use Sammyjo20\LaravelHaystack\Helpers\ClosureHelper;
use Sammyjo20\LaravelHaystack\Contracts\StackableJob;
use Sammyjo20\LaravelHaystack\Data\PendingHaystackBale;

class HaystackBuilder
{
use Conditionable;

/**
* The name of the haystack.
*
Expand Down
23 changes: 23 additions & 0 deletions tests/Unit/HaystackBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,26 @@

expect($haystack->started)->toBeTrue();
});

test('you can use conditional clauses when building your haystack', function () {
$builder = new HaystackBuilder;
$neilJob = new NameJob('Neil');

$builder->when(true, function($haystack) use ($neilJob) {
$haystack->addJob($neilJob);
})->when(false, function ($haystack) {
$haystack->withDelay(30);
}, function ($haystack) {
$haystack->withDelay(50);
})->when(true, function ($haystack) {
$haystack->onConnection('database');
});

$jobs = $builder->getJobs();

expect($jobs)->toHaveCount(1);
expect($jobs[0]->job)->toEqual($neilJob);

expect($builder->getGlobalDelayInSeconds())->toEqual(50);
expect($builder->getGlobalConnection())->toEqual('database');
});