Passed
Push — master ( 1e6084...cf392e )
by Vincent
02:40
created

AssertBase::assertNotHasMembers()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 2
dl 0
loc 12
ccs 8
cts 8
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace VGirol\JsonApiAssert\Asserts;
3
4
use VGirol\JsonApiAssert\Messages;
5
use PHPUnit\Util\InvalidArgumentHelper;
6
use PHPUnit\Framework\Assert as PHPUnit;
7
use PHPUnit\Framework\ExpectationFailedException;
8
use VGirol\JsonApiAssert\Constraint\ContainsAtLeastOneConstraint;
9
use VGirol\JsonApiAssert\Constraint\ContainsOnlyAllowedMembersConstraint;
10
11
trait AssertBase
12
{
13
    /**
14
     * Asserts that a json object has an expected member.
15
     *
16
     * @param string    $expected
17
     * @param array     $json
18
     *
19
     * @throws \PHPUnit\Framework\ExpectationFailedException
20
     * @throws \PHPUnit\Framework\Exception
21
     */
22 11
    public static function assertHasMember($expected, $json)
23
    {
24 11
        if (!\is_string($expected)) {
0 ignored issues
show
introduced by
The condition is_string($expected) is always true.
Loading history...
25 1
            throw InvalidArgumentHelper::factory(
26 1
                1,
27 1
                'string',
28 1
                $expected
29
            );
30
        }
31 10
        if (!\is_array($json)) {
0 ignored issues
show
introduced by
The condition is_array($json) is always true.
Loading history...
32 1
            throw InvalidArgumentHelper::factory(
33 1
                2,
34 1
                'array',
35 1
                $json
36
            );
37
        }
38 9
        PHPUnit::assertArrayHasKey($expected, $json, sprintf(Messages::HAS_MEMBER, $expected));
39 8
    }
40
41
    /**
42
     * Asserts that a json object has expected members.
43
     *
44
     * @param array     $expected
45
     * @param array     $json
46
     *
47
     * @throws \PHPUnit\Framework\ExpectationFailedException
48
     * @throws \PHPUnit\Framework\Exception
49
     */
50 4
    public static function assertHasMembers($expected, $json)
51
    {
52 4
        if (!\is_array($expected)) {
0 ignored issues
show
introduced by
The condition is_array($expected) is always true.
Loading history...
53 1
            throw InvalidArgumentHelper::factory(
54 1
                1,
55 1
                'array',
56 1
                $expected
57
            );
58
        }
59 3
        if (!\is_array($json)) {
0 ignored issues
show
introduced by
The condition is_array($json) is always true.
Loading history...
60 1
            throw InvalidArgumentHelper::factory(
61 1
                2,
62 1
                'array',
63 1
                $json
64
            );
65
        }
66 2
        foreach ($expected as $key) {
67 2
            PHPUnit::assertArrayHasKey($key, $json, sprintf(Messages::HAS_MEMBER, $key));
68
        }
69 1
    }
70
71
    /**
72
     * Asserts that a json object has only expected members.
73
     *
74
     * @param array     $expected
75
     * @param array     $json
76
     *
77
     * @throws \PHPUnit\Framework\ExpectationFailedException
78
     * @throws \PHPUnit\Framework\Exception
79
     */
80 4
    public static function assertHasOnlyMembers($expected, $json)
81
    {
82 4
        if (!\is_array($expected)) {
0 ignored issues
show
introduced by
The condition is_array($expected) is always true.
Loading history...
83 1
            throw InvalidArgumentHelper::factory(
84 1
                1,
85 1
                'array',
86 1
                $expected
87
            );
88
        }
89 3
        if (!\is_array($json)) {
0 ignored issues
show
introduced by
The condition is_array($json) is always true.
Loading history...
90 1
            throw InvalidArgumentHelper::factory(
91 1
                2,
92 1
                'array',
93 1
                $json
94
            );
95
        }
96
97 2
        PHPUnit::assertEquals(
98 2
            $expected,
99 2
            array_keys($json),
100 2
            sprintf(Messages::HAS_ONLY_MEMBERS, implode(', ', $expected))
101
        );
102 1
    }
103
104
    /**
105
     * Asserts that a json object not has an unexpected member.
106
     *
107
     * @param string    $expected
108
     * @param array     $json
109
     *
110
     * @throws \PHPUnit\Framework\ExpectationFailedException
111
     * @throws \PHPUnit\Framework\Exception
112
     */
113 6
    public static function assertNotHasMember($expected, $json)
114
    {
115 6
        if (!\is_string($expected)) {
0 ignored issues
show
introduced by
The condition is_string($expected) is always true.
Loading history...
116 1
            throw InvalidArgumentHelper::factory(
117 1
                1,
118 1
                'string',
119 1
                $expected
120
            );
121
        }
122 5
        if (!\is_array($json)) {
0 ignored issues
show
introduced by
The condition is_array($json) is always true.
Loading history...
123 1
            throw InvalidArgumentHelper::factory(
124 1
                2,
125 1
                'array',
126 1
                $json
127
            );
128
        }
129 4
        PHPUnit::assertArrayNotHasKey($expected, $json, sprintf(Messages::NOT_HAS_MEMBER, $expected));
130 2
    }
131
132
    /**
133
     * Asserts that a json object not has an unexpected member.
134
     *
135
     * @param array    $expected
136
     * @param array     $json
137
     *
138
     * @throws \PHPUnit\Framework\ExpectationFailedException
139
     * @throws \PHPUnit\Framework\Exception
140
     */
141 3
    public static function assertNotHasMembers($expected, $json)
142
    {
143 3
        if (!\is_array($expected)) {
0 ignored issues
show
introduced by
The condition is_array($expected) is always true.
Loading history...
144 1
            throw InvalidArgumentHelper::factory(
145 1
                1,
146 1
                'array',
147 1
                $expected
148
            );
149
        }
150
151 2
        foreach ($expected as $key) {
152 2
            static::assertNotHasMember($key, $json);
153
        }
154 1
    }
155
156
    /**
157
     * Asserts that a json object has a "data" member.
158
     *
159
     * @param array     $json
160
     *
161
     * @throws \PHPUnit\Framework\ExpectationFailedException
162
     */
163 1
    public static function assertHasData($json)
164
    {
165 1
        static::assertHasMember('data', $json);
166 1
    }
167
168
    /**
169
     * Asserts that a json object has an "attributes" member.
170
     *
171
     * @param array     $json
172
     *
173
     * @throws \PHPUnit\Framework\ExpectationFailedException
174
     */
175 1
    public static function assertHasAttributes($json)
176
    {
177 1
        static::assertHasMember('attributes', $json);
178 1
    }
179
180
    /**
181
     * Asserts that a json object has a "links" member.
182
     *
183
     * @param array     $json
184
     *
185
     * @throws \PHPUnit\Framework\ExpectationFailedException
186
     */
187 1
    public static function assertHasLinks($json)
188
    {
189 1
        static::assertHasMember('links', $json);
190 1
    }
191
192
    /**
193
     * Asserts that a json object has a "meta" member.
194
     *
195
     * @param array     $json
196
     *
197
     * @throws \PHPUnit\Framework\ExpectationFailedException
198
     */
199 1
    public static function assertHasMeta($json)
200
    {
201 1
        static::assertHasMember('meta', $json);
202 1
    }
203
204
    /**
205
     * Asserts that a json object has an "included" member.
206
     *
207
     * @param array     $json
208
     *
209
     * @throws \PHPUnit\Framework\ExpectationFailedException
210
     */
211 1
    public static function assertHasIncluded($json)
212
    {
213 1
        static::assertHasMember('included', $json);
214 1
    }
215
216
    /**
217
     * Asserts that a json object has a "relationships" member.
218
     *
219
     * @param array     $json
220
     *
221
     * @throws \PHPUnit\Framework\ExpectationFailedException
222
     */
223 1
    public static function assertHasRelationships($json)
224
    {
225 1
        static::assertHasMember('relationships', $json);
226 1
    }
227
228
    /**
229
     * Asserts that a json object has an "errors" member.
230
     *
231
     * @param array     $json
232
     *
233
     * @throws \PHPUnit\Framework\ExpectationFailedException
234
     */
235 1
    public static function assertHasErrors($json)
236
    {
237 1
        static::assertHasMember('errors', $json);
238 1
    }
239
240
    /**
241
     * Asserts that a json object contains at least one member from the list provided.
242
     *
243
     * @param array $expected   The expected members
244
     * @param array $json       The json object
245
     * @param string $message   An optional message to explain why the test failed
246
     *
247
     * @throws \PHPUnit\Framework\ExpectationFailedException
248
     */
249 49
    public static function assertContainsAtLeastOneMember($expected, $json, $message = '')
250
    {
251 49
        PHPUnit::assertThat($json, self::containsAtLeastOneMemberConstraint($expected), $message);
252 43
    }
253
254
    /**
255
     * Returns a new instance of the \VGirol\JsonApiAssert\Constraint\ContainsAtLeastOneConstraint class.
256
     *
257
     * @param array $expected   The expected members
258
     *
259
     * @return \VGirol\JsonApiAssert\Constraint\ContainsAtLeastOneConstraint
260
     */
261 58
    private static function containsAtLeastOneMemberConstraint($expected)
262
    {
263 58
        return new ContainsAtLeastOneConstraint($expected);
264
    }
265
266
    /**
267
     * Check if a json object contains at least one member from the list provided.
268
     *
269
     * @param array $expected   The expected members
270
     * @param array $json       The json object
271
     *
272
     * @return boolean
273
     */
274 25
    private static function containsAtLeastOneMember($expected, $json)
275
    {
276 25
        $constraint = static::containsAtLeastOneMemberConstraint($expected);
277
278 25
        return $constraint->check($json);
279
    }
280
281
    /**
282
     * Asserts that a json object contains only members from the list provided.
283
     *
284
     * @param array $expected   The expected members
285
     * @param array $json       The json object
286
     * @param string $message   An optional message to explain why the test failed
287
     *
288
     * @throws \PHPUnit\Framework\ExpectationFailedException
289
     */
290 95
    public static function assertContainsOnlyAllowedMembers($expected, $json, $message = '')
291
    {
292 95
        $message = Messages::ONLY_ALLOWED_MEMBERS . "\n" . $message;
293 95
        PHPUnit::assertThat($json, self::containsOnlyAllowedMembersConstraint($expected), $message);
294 76
    }
295
296
    /**
297
     * Returns a new instance of the \VGirol\JsonApiAssert\Constraint\ContainsOnlyAllowedMembersConstraint class.
298
     *
299
     * @param array $expected   The expected members
300
     *
301
     * @return \VGirol\JsonApiAssert\Constraint\ContainsOnlyAllowedMembersConstraint
302
     */
303 95
    private static function containsOnlyAllowedMembersConstraint($expected)
304
    {
305 95
        return new ContainsOnlyAllowedMembersConstraint($expected);
306
    }
307
308
    /**
309
     * Asserts that an array is an array of objects.
310
     *
311
     * @param array     $data
312
     * @param string    $message   An optional message to explain why the test failed
313
     *
314
     * @throws \PHPUnit\Framework\ExpectationFailedException
315
     */
316 29
    public static function assertIsArrayOfObjects($json, $message = '')
317
    {
318 29
        if (!\is_array($json)) {
319 1
            throw InvalidArgumentHelper::factory(
320 1
                1,
321 1
                'array',
322 1
                $json
323
            );
324
        }
325
326 28
        $message = $message ?: Messages::MUST_BE_ARRAY_OF_OBJECTS;
327 28
        PHPUnit::assertTrue(static::isArrayOfObjects($json), $message);
328 20
    }
329
330
    /**
331
     * Asserts that an array is not an array of objects.
332
     *
333
     * @param array     $data
334
     * @param string    $message   An optional message to explain why the test failed
335
     *
336
     * @throws \PHPUnit\Framework\ExpectationFailedException
337
     */
338 68
    public static function assertIsNotArrayOfObjects($json, $message = '')
339
    {
340 68
        if (!\is_array($json)) {
341 1
            throw InvalidArgumentHelper::factory(
342 1
                1,
343 1
                'array',
344 1
                $json
345
            );
346
        }
347
348 67
        $message = $message ?: Messages::MUST_NOT_BE_ARRAY_OF_OBJECTS;
349 67
        PHPUnit::assertFalse(static::isArrayOfObjects($json), $message);
350 61
    }
351
352
    /**
353
     * Asserts that a test failed.
354
     *
355
     * @param \Closure|callback $fn
356
     * @param string $expectedFailureMessage
357
     * @param mixed $args
358
     *
359
     * @throws \PHPUnit\Framework\ExpectationFailedException
360
     */
361 140
    public static function assertTestFail($fn, $expectedFailureMessage)
362
    {
363 140
        $args = array_slice(func_get_args(), 2);
364
365
        try {
366 140
            call_user_func_array($fn, $args);
367 139
        } catch (ExpectationFailedException $e) {
368 139
            if (!is_null($expectedFailureMessage)) {
369 133
                PHPUnit::assertStringContainsString($expectedFailureMessage, $e->getMessage());
370
            }
371
372 138
            return;
373
        }
374
375 1
        throw new ExpectationFailedException(Messages::TEST_FAILED);
376
    }
377
378 86
    private static function isArrayOfObjects($arr)
379
    {
380 86
        if (empty($arr)) {
381 2
            return true;
382
        }
383
384 84
        return !static::arrayIsAssociative($arr);
385
    }
386
387 84
    private static function arrayIsAssociative($arr)
388
    {
389 84
        return (array_keys($arr) !== range(0, count($arr) - 1));
390
    }
391
}
392