Passed
Pull Request — 1.x (#1)
by Kevin
01:50
created

Assert   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
dl 0
loc 53
rs 10
c 1
b 0
f 0
wmc 9

8 Methods

Rating   Name   Duplication   Size   Complexity  
A that() 0 11 2
A wrapMinkExpectation() 0 3 1
A false() 0 3 1
A fail() 0 3 1
A setHandler() 0 3 1
A pass() 0 3 1
A handler() 0 3 1
A true() 0 3 1
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