Skip to content

Commit 001039d

Browse files
committed
introducing ability to ignore helpers at runtime
1 parent 351d67f commit 001039d

File tree

5 files changed

+69
-2
lines changed

5 files changed

+69
-2
lines changed

src/Concerns/UsingThen.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ trait UsingThen
1414
*
1515
* @var array<int, string>
1616
*/
17-
private array $then = [];
17+
private ?array $then = [];
1818

1919
/**
2020
* Register a "then" hook.

src/FractionBuilder.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ public function __construct(
4545
*/
4646
public function __invoke(...$arguments): mixed
4747
{
48+
$arguments = $this->ignores($arguments);
49+
4850
if ($this->queued !== null && $this->deferred !== null) {
4951
throw new PreventDeferQueueSameTime($this->action);
5052
}
@@ -68,7 +70,7 @@ public function __invoke(...$arguments): mixed
6870
}
6971
}
7072

71-
$result = $interpreter->then($this->then)->handle($this->application);
73+
$result = $interpreter->then($this->then ?? [])->handle($this->application);
7274

7375
if ($this->logged !== null) {
7476
$this->application->make('log')
@@ -112,4 +114,27 @@ private function configuration(): array
112114

113115
return [false, []];
114116
}
117+
118+
/**
119+
* Determines if helpers usage should be ignored.
120+
*/
121+
private function ignores(array $arguments = []): array
122+
{
123+
if ($arguments === []) {
124+
return $arguments;
125+
}
126+
127+
$helpers = ['queued', 'deferred', 'rescued', 'logged', 'then'];
128+
$unset = array_intersect($helpers, array_keys($arguments));
129+
130+
foreach ($unset as $helper) {
131+
if ($arguments[$helper] === false) {
132+
$this->{$helper} = null;
133+
}
134+
135+
unset($arguments[$helper]);
136+
}
137+
138+
return $arguments;
139+
}
115140
}

tests/Feature/Execution/AsDeferTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,13 @@
3636

3737
expect($test)->toBeTrue();
3838
});
39+
40+
test('ignoring defer', function () {
41+
execute('one', function () {
42+
__output('cancelled');
43+
})->deferred();
44+
45+
run('one', deferred: false);
46+
47+
expect(__exists('cancelled'))->toBeTrue();
48+
});

tests/Feature/Execution/AsQueueTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,13 @@
6060

6161
expect(__exists('two'))->toBeTrue();
6262
});
63+
64+
test('ignoring queue', function () {
65+
execute('one', function () {
66+
__output('cancelled');
67+
})->queued();
68+
69+
run('one', queued: false);
70+
71+
expect(__exists('cancelled'))->toBeTrue();
72+
});

tests/Feature/Execution/AsSyncTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,14 @@
137137
expect($test)->toBeNull();
138138
});
139139

140+
test('ignoring rescued', function () {
141+
execute('one', function () {
142+
throw new Exception('foo');
143+
})->rescued();
144+
145+
run('one', rescued: false);
146+
})->throws(Exception::class, 'foo');
147+
140148
test('call logged', function () {
141149
LogFake::bind();
142150

@@ -155,6 +163,20 @@
155163
);
156164
});
157165

166+
test('ignoring logged', function () {
167+
LogFake::bind();
168+
169+
execute('one', function () {
170+
__output('cancelled');
171+
})->logged();
172+
173+
run('one', logged: false);
174+
175+
Log::assertNotLogged(fn (LogEntry $log) => $log->level === 'info'
176+
&& $log->message === '[Laravel] Action: [one] executed.'
177+
);
178+
});
179+
158180
test('call rescued using default value', function () {
159181
execute('one', function () {
160182
throw new Exception('foo');

0 commit comments

Comments
 (0)