Dispatcher::init()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 0
dl 0
loc 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PHPKitchen\CodeSpecsCore\Expectation\Dispatcher;
4
5
use PHPKitchen\CodeSpecsCore\Contract\ExpectationMatcher;
6
use PHPKitchen\CodeSpecsCore\Expectation\Internal\Assert;
7
use PHPKitchen\CodeSpecsCore\Expectation\Internal\StepsList;
8
use PHPKitchen\CodeSpecsCore\Expectation\Matcher\ArrayMatcher;
9
use PHPKitchen\CodeSpecsCore\Expectation\Matcher\BooleanMatcher;
10
use PHPKitchen\CodeSpecsCore\Expectation\Matcher\ClassMatcher;
11
use PHPKitchen\CodeSpecsCore\Expectation\Matcher\DirectoryMatcher;
12
use PHPKitchen\CodeSpecsCore\Expectation\Matcher\FileMatcher;
13
use PHPKitchen\CodeSpecsCore\Expectation\Matcher\NumberMatcher;
14
use PHPKitchen\CodeSpecsCore\Expectation\Matcher\ObjectMatcher;
15
use PHPKitchen\CodeSpecsCore\Expectation\Matcher\StringMatcher;
16
use PHPKitchen\CodeSpecsCore\Expectation\Matcher\ValueMatcher;
17
use PHPUnit\Framework\Test;
18
19
/**
20
 * Represents matchers dispatcher.
21
 *
22
 * Required to dispatch asserts to specific matchers.
23
 *
24
 * @package PHPKitchen\CodeSpecsCore\Expectation
25
 * @author Dmitry Kolodko <[email protected]>
26
 */
27
class Dispatcher {
28
    /**
29
     * @var mixed actual value or variable that will be matched to expectations.
30
     */
31
    protected $actual;
32
    /**
33
     * @var \PHPUnit\Framework\Test
34
     */
35
    protected $test;
36
    /**
37
     * @var string description of expectation. If expectation fails this description will be displayed in console.
38
     */
39
    protected $variableName;
40
    protected $useDelayedAsserts = false;
41
42
    public function __construct(Test $test, $actual, $variableName = '') {
43
        $this->test = $test;
44
        $this->actual = $actual;
45
        $this->variableName = $variableName;
46
        $this->init();
47
    }
48
49
    /**
50
     * Override this method if you want to initialize anything after constructor.
51
     */
52
    protected function init(): void {
53
    }
54
55
    public function isMixed(): ValueMatcher {
56
        return $this->createMatcher(ValueMatcher::class, 'value');
57
    }
58
59
    public function isString(): StringMatcher {
60
        return $this->createMatcher(StringMatcher::class, 'boolean');
61
    }
62
63
    public function isArray(): ArrayMatcher {
64
        return $this->createMatcher(ArrayMatcher::class, 'array');
65
    }
66
67
    public function isBoolean(): BooleanMatcher {
68
        return $this->createMatcher(BooleanMatcher::class, 'boolean');
69
    }
70
71
    public function isNumber(): NumberMatcher {
72
        return $this->createMatcher(NumberMatcher::class, 'number');
73
    }
74
75
    public function isObject(): ObjectMatcher {
76
        return $this->createMatcher(ObjectMatcher::class, 'object');
77
    }
78
79
    public function isClass(): ClassMatcher {
80
        return $this->createMatcher(ClassMatcher::class, 'class');
81
    }
82
83
    public function isFile(): FileMatcher {
84
        return $this->createMatcher(FileMatcher::class, 'file');
85
    }
86
87
    public function isDirectory(): DirectoryMatcher {
88
        return $this->createMatcher(DirectoryMatcher::class, 'directory');
89
    }
90
91
    protected function createMatcher($className, $id): ExpectationMatcher {
92
        $variableName = $this->variableName ?: $id;
93
94
        $stepsList = StepsList::getInstance();
95
        $assert = new Assert($stepsList, $this->test, $this->actual, "I see that {$variableName}");
96
97
        if ($this->useDelayedAsserts) {
98
            $assert->switchToDelayedExecutionStrategy();
99
        }
100
        return new $className($assert);
101
    }
102
}