Passed
Pull Request — 1.x (#1)
by Kevin
02:07
created

Assert::pass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Zenstruck\Browser;
4
5
use Zenstruck\Browser\Assert\Assertion;
6
use Zenstruck\Browser\Assert\Assertion\Fail;
7
use Zenstruck\Browser\Assert\Assertion\IsTrue;
8
use Zenstruck\Browser\Assert\Assertion\MinkExpectation;
9
use Zenstruck\Browser\Assert\Exception\AssertionFailed;
10
use Zenstruck\Browser\Assert\Handler;
11
use Zenstruck\Browser\Assert\Handler\DefaultHandler;
12
13
/**
14
 * @author Kevin Bond <[email protected]>
15
 */
16
final class Assert
17
{
18
    private static ?Handler $handler = null;
19
20
    public static function that(Assertion $assertion): void
21
    {
22
        try {
23
            $assertion();
24
        } catch (AssertionFailed $exception) {
25
            self::handler()->onFailure($exception);
26
27
            return;
28
        }
29
30
        self::handler()->onSuccess();
31
    }
32
33
    public static function true(bool $expression, string $message, ...$args): void
34
    {
35
        self::that(new IsTrue($expression, $message, ...$args));
36
    }
37
38
    public static function false(bool $expression, string $message, ...$args): void
39
    {
40
        self::that(new IsTrue(!$expression, $message, ...$args));
41
    }
42
43
    /**
44
     * @psalm-return never-returns
45
     */
46
    public static function fail(string $message, ...$args): void
47
    {
48
        self::that(new Fail($message, ...$args));
49
    }
50
51
    public static function pass(): void
52
    {
53
        self::handler()->onSuccess();
54
    }
55
56
    public static function wrapMinkExpectation(callable $callback): void
57
    {
58
        self::that(new MinkExpectation($callback));
59
    }
60
61
    public static function setHandler(Handler $handler): void
62
    {
63
        self::$handler = $handler;
64
    }
65
66
    private static function handler(): Handler
67
    {
68
        return self::$handler ??= new DefaultHandler();
69
    }
70
}
71