Assert::notOk()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Peridot\Leo\Interfaces;
4
5
use Peridot\Leo\Assertion;
6
use Peridot\Leo\Interfaces\Assert\CollectionAssertTrait;
7
use Peridot\Leo\Interfaces\Assert\ObjectAssertTrait;
8
use Peridot\Leo\Interfaces\Assert\TypeAssertTrait;
9
use Peridot\Leo\Leo;
10
11
/**
12
 * Assert is a non-chainable, object oriented interface
13
 * on top of a Leo Assertion.
14
 *
15
 * @method instanceOf() instanceOf(object $actual, string $expected, string $message = "") Perform an instanceof assertion.
16
 * @method include() include(array $haystack, string $expected, string $message = "") Perform an inclusion assertion.
17
 *
18
 * @package Peridot\Leo\Interfaces
19
 */
20
class Assert
21
{
22
    use TypeAssertTrait;
23
    use ObjectAssertTrait;
24
    use CollectionAssertTrait;
25
26
    /**
27
     * An array of operators mapping to assertions.
28
     *
29
     * @var array
30
     */
31
    public static $operators = [
32
        '==' => 'loosely->equal',
33
        '===' => 'equal',
34
        '>' => 'above',
35
        '>=' => 'least',
36
        '<' => 'below',
37
        '<=' => 'most',
38
        '!=' => 'not->loosely->equal',
39
        '!==' => 'not->equal',
40
    ];
41
42
    /**
43
     * @var Assertion
44
     */
45
    protected $assertion;
46
47
    /**
48
     * @param Assertion $assertion
49
     */
50
    public function __construct(Assertion $assertion = null)
51
    {
52
        if ($assertion === null) {
53
            $assertion = Leo::assertion();
54
        }
55
        $this->assertion = $assertion;
56
    }
57
58
    /**
59
     * Perform an a loose equality assertion.
60
     *
61
     * @param mixed  $actual
62
     * @param mixed  $expected
63
     * @param string $message
64
     */
65
    public function equal($actual, $expected, $message = '')
66
    {
67
        $this->assertion->setActual($actual);
68
69
        return $this->assertion->to->loosely->equal($expected, $message);
70
    }
71
72
    /**
73
     * Perform a negated loose equality assertion.
74
     *
75
     * @param mixed  $actual
76
     * @param mixed  $expected
77
     * @param string $message
78
     */
79
    public function notEqual($actual, $expected, $message = '')
80
    {
81
        $this->assertion->setActual($actual);
82
83
        return $this->assertion->to->not->equal($expected, $message);
84
    }
85
86
    /**
87
     * Performs a throw assertion.
88
     *
89
     * @param callable $fn
90
     * @param $exceptionType
91
     * @param string $exceptionMessage
92
     * @param string $message
93
     */
94
    public function throws(callable $fn, $exceptionType, $exceptionMessage = '', $message = '')
95
    {
96
        $this->assertion->setActual($fn);
97
98
        return $this->assertion->to->throw($exceptionType, $exceptionMessage, $message);
99
    }
100
101
    /**
102
     * Performs a negated throw assertion.
103
     *
104
     * @param callable $fn
105
     * @param $exceptionType
106
     * @param string $exceptionMessage
107
     * @param string $message
108
     */
109
    public function doesNotThrow(callable $fn, $exceptionType, $exceptionMessage = '', $message = '')
110
    {
111
        $this->assertion->setActual($fn);
112
113
        return $this->assertion->not->to->throw($exceptionType, $exceptionMessage, $message);
114
    }
115
116
    /**
117
     * Perform an ok assertion.
118
     *
119
     * @param mixed  $object
120
     * @param string $message
121
     */
122
    public function ok($object, $message = '')
123
    {
124
        $this->assertion->setActual($object);
125
126
        return $this->assertion->to->be->ok($message);
127
    }
128
129
    /**
130
     * Perform a negated assertion.
131
     *
132
     * @param mixed  $object
133
     * @param string $message
134
     */
135
    public function notOk($object, $message = '')
136
    {
137
        $this->assertion->setActual($object);
138
139
        return $this->assertion->to->not->be->ok($message);
140
    }
141
142
    /**
143
     * Perform a strict equality assertion.
144
     *
145
     * @param mixed  $actual
146
     * @param mixed  $expected
147
     * @param string $message
148
     */
149
    public function strictEqual($actual, $expected, $message = '')
150
    {
151
        $this->assertion->setActual($actual);
152
153
        return $this->assertion->to->equal($expected, $message);
154
    }
155
156
    /**
157
     * Perform a negated strict equality assertion.
158
     *
159
     * @param mixed  $actual
160
     * @param mixed  $expected
161
     * @param string $message
162
     */
163
    public function notStrictEqual($actual, $expected, $message = '')
164
    {
165
        $this->assertion->setActual($actual);
166
167
        return $this->assertion->to->not->equal($expected, $message);
168
    }
169
170
    /**
171
     * Perform a pattern assertion.
172
     *
173
     * @param string $value
174
     * @param string $pattern
175
     * @param string $message
176
     */
177
    public function match($value, $pattern, $message = '')
178
    {
179
        $this->assertion->setActual($value);
180
181
        return $this->assertion->to->match($pattern, $message);
182
    }
183
184
    /**
185
     * Perform a negated pattern assertion.
186
     *
187
     * @param string $value
188
     * @param string $pattern
189
     * @param string $message
190
     */
191
    public function notMatch($value, $pattern, $message = '')
192
    {
193
        $this->assertion->setActual($value);
194
195
        return $this->assertion->to->not->match($pattern, $message);
196
    }
197
198
    /**
199
     * Compare two values using the given operator.
200
     *
201
     * @param mixed  $left
202
     * @param string $operator
203
     * @param mixed  $right
204
     * @param string $message
205
     */
206
    public function operator($left, $operator, $right, $message = '')
207
    {
208
        if (!isset(static::$operators[$operator])) {
209
            throw new \InvalidArgumentException("Invalid operator $operator");
210
        }
211
        $this->assertion->setActual($left);
212
213
        return $this->assertion->{static::$operators[$operator]}($right, $message);
214
    }
215
216
    /**
217
     * Defined to allow use of reserved words for methods.
218
     *
219
     * @param $method
220
     * @param $args
221
     */
222
    public function __call($method, $args)
223
    {
224
        switch ($method) {
225
            case 'instanceOf':
226
                return call_user_func_array([$this, 'isInstanceOf'], $args);
227
            case 'include':
228
                return call_user_func_array([$this, 'isIncluded'], $args);
229
            default:
230
                throw new \BadMethodCallException("Call to undefined method $method");
231
        }
232
    }
233
}
234