Skip to content

Commit 583f01a

Browse files
committed
Plugin: Add tests
1 parent a8d37f8 commit 583f01a

File tree

2 files changed

+117
-1
lines changed

2 files changed

+117
-1
lines changed

src/Detector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function getEnabler(): Enabler
9292
public function isDebugMode(?bool $default = false): ?bool
9393
{
9494
foreach ($this->detections as $detection) {
95-
if (($result = $detection()) !== null) {
95+
if (($result = $detection($this)) !== null) {
9696
return $result;
9797
}
9898
}

tests/DetectorTest.php

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Redbitcz\DebugMode\Detector;
88
use Redbitcz\DebugMode\Enabler;
99
use Redbitcz\DebugMode\InconsistentEnablerModeException;
10+
use Redbitcz\DebugMode\Plugin\Plugin;
1011
use Tester\Assert;
1112
use Tester\Helpers;
1213
use Tester\TestCase;
@@ -212,6 +213,121 @@ public function testMissingEnablerShortcut(): void
212213
Detector::detect(Detector::MODE_FULL);
213214
}, InconsistentEnablerModeException::class);
214215
}
216+
217+
public function testPluginPrepend()
218+
{
219+
$detector = new Detector(0);
220+
$detector->prependPlugin(
221+
new class implements Plugin {
222+
public function __invoke(Detector $detector): ?bool
223+
{
224+
Assert::true(true);
225+
return null;
226+
}
227+
}
228+
);
229+
$detector->isDebugMode();
230+
}
231+
232+
public function testPluginAppend()
233+
{
234+
$detector = new Detector(0);
235+
$detector->appendPlugin(
236+
new class implements Plugin {
237+
public function __invoke(Detector $detector): ?bool
238+
{
239+
Assert::true(true);
240+
return null;
241+
}
242+
}
243+
);
244+
$detector->isDebugMode();
245+
}
246+
247+
public function getPluginPrependReturnsProvider(): array
248+
{
249+
return [
250+
[null, true],
251+
[true, true],
252+
[false, false],
253+
];
254+
}
255+
256+
/**
257+
* @dataProvider getPluginPrependReturnsProvider
258+
*/
259+
public function testPluginPrependReturn(?bool $state, ?bool $result): void
260+
{
261+
$detector = new Detector(0);
262+
$detector->prependPlugin(
263+
new class() implements Plugin {
264+
public function __invoke(Detector $detector): ?bool
265+
{
266+
return true;
267+
}
268+
}
269+
);
270+
$detector->prependPlugin(
271+
new class($state) implements Plugin {
272+
private ?bool $state;
273+
274+
public function __construct(?bool $state)
275+
{
276+
$this->state = $state;
277+
}
278+
279+
public function __invoke(Detector $detector): ?bool
280+
{
281+
return $this->state;
282+
}
283+
}
284+
);
285+
286+
Assert::equal($result, $detector->isDebugMode());
287+
}
288+
289+
290+
public function getPluginAppendReturnsProvider(): array
291+
{
292+
return [
293+
[null, false],
294+
[true, false],
295+
[false, false],
296+
];
297+
}
298+
299+
/**
300+
* @dataProvider getPluginAppendReturnsProvider
301+
*/
302+
public function testPluginAppendReturn(?bool $state, ?bool $result): void
303+
{
304+
$detector = new Detector(0);
305+
$detector->appendPlugin(
306+
new class() implements Plugin {
307+
public function __invoke(Detector $detector): ?bool
308+
{
309+
return false;
310+
}
311+
}
312+
);
313+
$detector->appendPlugin(
314+
new class($state) implements Plugin {
315+
private ?bool $state;
316+
317+
public function __construct(?bool $state)
318+
{
319+
$this->state = $state;
320+
}
321+
322+
public function __invoke(Detector $detector): ?bool
323+
{
324+
return $this->state;
325+
}
326+
}
327+
);
328+
329+
Assert::equal($result, $detector->isDebugMode());
330+
}
215331
}
216332

217333
(new DetectorTest())->run();

0 commit comments

Comments
 (0)