Completed
Push — master ( bfd868...ee3a72 )
by Vincent
12:29 queued 10:26
created

AssertBase   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 283
Duplicated Lines 0 %

Test Coverage

Coverage 97.4%

Importance

Changes 0
Metric Value
eloc 43
dl 0
loc 283
ccs 75
cts 77
cp 0.974
rs 10
c 0
b 0
f 0
wmc 28

21 Methods

Rating   Name   Duplication   Size   Complexity  
A containsAtLeastOneMember() 0 5 1
A assertHasData() 0 3 1
A containsOnlyAllowedMembersConstraint() 0 3 1
A assertHasMembers() 0 4 2
A assertHasOnlyMembers() 0 6 1
A assertHasIncluded() 0 3 1
A assertHasRelationships() 0 3 1
A assertContainsOnlyAllowedMembers() 0 4 1
A assertNotHasMember() 0 3 1
A assertHasErrors() 0 3 1
A assertTestFail() 0 15 3
A arrayIsAssociative() 0 3 1
A assertIsArrayOfObjects() 0 5 2
A assertHasMeta() 0 3 1
A isArrayOfObjects() 0 10 3
A assertHasMember() 0 3 1
A assertHasAttributes() 0 3 1
A containsAtLeastOneMemberConstraint() 0 3 1
A assertContainsAtLeastOneMember() 0 3 1
A assertHasLinks() 0 3 1
A assertIsNotArrayOfObjects() 0 5 2
1
<?php
2
namespace VGirol\JsonApiAssert\Asserts;
3
4
use PHPUnit\Framework\Assert as PHPUnit;
5
use VGirol\JsonApiAssert\Messages;
6
use PHPUnit\Framework\ExpectationFailedException;
7
use VGirol\JsonApiAssert\Constraint\ContainsAtLeastOneConstraint;
8
use VGirol\JsonApiAssert\Constraint\ContainsOnlyAllowedMembersConstraint;
9
10
trait AssertBase
11
{
12
    /**
13
     * Asserts that a json object has expected members.
14
     *
15
     * @param array     $expected
16
     * @param array     $json
17
     *
18
     * @throws \PHPUnit\Framework\ExpectationFailedException
19
     */
20 2
    public static function assertHasMembers(array $expected, $json)
21
    {
22 2
        foreach ($expected as $key) {
23 2
            PHPUnit::assertArrayHasKey($key, $json, sprintf(Messages::HAS_MEMBER, $key));
24
        }
25 1
    }
26
27
    /**
28
     * Asserts that a json object has an expected member.
29
     *
30
     * @param string    $expected
31
     * @param array     $json
32
     *
33
     * @throws \PHPUnit\Framework\ExpectationFailedException
34
     */
35 9
    public static function assertHasMember($expected, $json)
36
    {
37 9
        PHPUnit::assertArrayHasKey($expected, $json, sprintf(Messages::HAS_MEMBER, $expected));
38 8
    }
39
40
    /**
41
     * Asserts that a json object has only expected members.
42
     *
43
     * @param array     $expected
44
     * @param array     $json
45
     *
46
     * @throws \PHPUnit\Framework\ExpectationFailedException
47
     */
48 2
    public static function assertHasOnlyMembers(array $expected, $json)
49
    {
50 2
        PHPUnit::assertEquals(
51 2
            $expected,
52 2
            array_keys($json),
53 2
            sprintf(Messages::HAS_ONLY_MEMBERS, implode(', ', $expected))
54
        );
55 1
    }
56
57
    /**
58
     * Asserts that a json object not has an unexpected member.
59
     *
60
     * @param string    $expected
61
     * @param array     $json
62
     *
63
     * @throws \PHPUnit\Framework\ExpectationFailedException
64
     */
65 2
    public static function assertNotHasMember($expected, $json)
66
    {
67 2
        PHPUnit::assertArrayNotHasKey($expected, $json, sprintf(Messages::NOT_HAS_MEMBER, $expected));
68 1
    }
69
70
    /**
71
     * Asserts that a json object has a "data" member.
72
     *
73
     * @param array     $json
74
     *
75
     * @throws \PHPUnit\Framework\ExpectationFailedException
76
     */
77 1
    public static function assertHasData($json)
78
    {
79 1
        static::assertHasMember('data', $json);
80 1
    }
81
82
    /**
83
     * Asserts that a json object has an "attributes" member.
84
     *
85
     * @param array     $json
86
     *
87
     * @throws \PHPUnit\Framework\ExpectationFailedException
88
     */
89 1
    public static function assertHasAttributes($json)
90
    {
91 1
        static::assertHasMember('attributes', $json);
92 1
    }
93
94
    /**
95
     * Asserts that a json object has a "links" member.
96
     *
97
     * @param array     $json
98
     *
99
     * @throws \PHPUnit\Framework\ExpectationFailedException
100
     */
101 1
    public static function assertHasLinks($json)
102
    {
103 1
        static::assertHasMember('links', $json);
104 1
    }
105
106
    /**
107
     * Asserts that a json object has a "meta" member.
108
     *
109
     * @param array     $json
110
     *
111
     * @throws \PHPUnit\Framework\ExpectationFailedException
112
     */
113 1
    public static function assertHasMeta($json)
114
    {
115 1
        static::assertHasMember('meta', $json);
116 1
    }
117
118
    /**
119
     * Asserts that a json object has an "included" member.
120
     *
121
     * @param array     $json
122
     *
123
     * @throws \PHPUnit\Framework\ExpectationFailedException
124
     */
125 1
    public static function assertHasIncluded($json)
126
    {
127 1
        static::assertHasMember('included', $json);
128 1
    }
129
130
    /**
131
     * Asserts that a json object has a "relationships" member.
132
     *
133
     * @param array     $json
134
     *
135
     * @throws \PHPUnit\Framework\ExpectationFailedException
136
     */
137 1
    public static function assertHasRelationships($json)
138
    {
139 1
        static::assertHasMember('relationships', $json);
140 1
    }
141
142
    /**
143
     * Asserts that a json object has an "errors" member.
144
     *
145
     * @param array     $json
146
     *
147
     * @throws \PHPUnit\Framework\ExpectationFailedException
148
     */
149 1
    public static function assertHasErrors($json)
150
    {
151 1
        static::assertHasMember('errors', $json);
152 1
    }
153
154
    /**
155
     * Asserts that a json object contains at least one member from the list provided.
156
     *
157
     * @param array $expected   The expected members
158
     * @param array $json       The json object
159
     * @param string $message   An optional message to explain why the test failed
160
     *
161
     * @throws \PHPUnit\Framework\ExpectationFailedException
162
     */
163 32
    public static function assertContainsAtLeastOneMember($expected, $json, $message = '')
164
    {
165 32
        PHPUnit::assertThat($json, self::containsAtLeastOneMemberConstraint($expected), $message);
166 26
    }
167
168
    /**
169
     * Returns a new instance of the \VGirol\JsonApiAssert\Constraint\ContainsAtLeastOneConstraint class.
170
     *
171
     * @param array $expected   The expected members
172
     *
173
     * @return \VGirol\JsonApiAssert\Constraint\ContainsAtLeastOneConstraint
174
     */
175 40
    public static function containsAtLeastOneMemberConstraint($expected)
176
    {
177 40
        return new ContainsAtLeastOneConstraint($expected);
178
    }
179
180
    /**
181
     * Check if a json object contains at least one member from the list provided.
182
     *
183
     * @param array $expected   The expected members
184
     * @param array $json       The json object
185
     *
186
     * @return boolean
187
     */
188 16
    public static function containsAtLeastOneMember($expected, $json)
189
    {
190 16
        $constraint = static::containsAtLeastOneMemberConstraint($expected);
191
192 16
        return $constraint->check($json);
193
    }
194
195
    /**
196
     * Asserts that a json object contains only members from the list provided.
197
     *
198
     * @param array $expected   The expected members
199
     * @param array $json       The json object
200
     * @param string $message   An optional message to explain why the test failed
201
     *
202
     * @throws \PHPUnit\Framework\ExpectationFailedException
203
     */
204 64
    public static function assertContainsOnlyAllowedMembers($expected, $json, $message = '')
205
    {
206 64
        $message = Messages::ONLY_ALLOWED_MEMBERS . "\n" . $message;
207 64
        PHPUnit::assertThat($json, self::containsOnlyAllowedMembersConstraint($expected), $message);
208 52
    }
209
210
    /**
211
     * Returns a new instance of the \VGirol\JsonApiAssert\Constraint\ContainsOnlyAllowedMembersConstraint class.
212
     *
213
     * @param array $expected   The expected members
214
     *
215
     * @return \VGirol\JsonApiAssert\Constraint\ContainsOnlyAllowedMembersConstraint
216
     */
217 64
    public static function containsOnlyAllowedMembersConstraint($expected)
218
    {
219 64
        return new ContainsOnlyAllowedMembersConstraint($expected);
220
    }
221
222
    /**
223
     * Asserts that an array is an array of objects.
224
     *
225
     * @param array     $data
226
     * @param string    $message   An optional message to explain why the test failed
227
     *
228
     * @throws \PHPUnit\Framework\ExpectationFailedException
229
     */
230 22
    public static function assertIsArrayOfObjects($data, $message = '')
231
    {
232 22
        $message = $message ?: Messages::MUST_BE_ARRAY_OF_OBJECTS;
233 22
        PHPUnit::assertIsArray($data, $message);
234 19
        PHPUnit::assertTrue(static::isArrayOfObjects($data), $message);
235 15
    }
236
237
    /**
238
     * Asserts that an array is not an array of objects.
239
     *
240
     * @param array     $data
241
     * @param string    $message   An optional message to explain why the test failed
242
     *
243
     * @throws \PHPUnit\Framework\ExpectationFailedException
244
     */
245 47
    public static function assertIsNotArrayOfObjects($data, $message = '')
246
    {
247 47
        $message = $message ?: Messages::MUST_NOT_BE_ARRAY_OF_OBJECTS;
248 47
        PHPUnit::assertIsArray($data, $message);
249 41
        PHPUnit::assertFalse(static::isArrayOfObjects($data), $message);
250 38
    }
251
252
    /**
253
     * Asserts that a test failed.
254
     *
255
     * @param \Closure|callback $fn
256
     * @param string $expectedFailureMessage
257
     * @param mixed $args
258
     *
259
     * @throws \PHPUnit\Framework\ExpectationFailedException
260
     */
261 105
    public static function assertTestFail($fn, $expectedFailureMessage)
262
    {
263 105
        $args = array_slice(func_get_args(), 2);
264
265
        try {
266 105
            call_user_func_array($fn, $args);
267 105
        } catch (ExpectationFailedException $e) {
268 105
            if (!is_null($expectedFailureMessage)) {
269 97
                PHPUnit::assertStringContainsString($expectedFailureMessage, $e->getMessage());
270
            }
271
272 105
            return;
273
        }
274
275
        throw new ExpectationFailedException(Messages::TEST_FAILED);
276
    }
277
278 54
    private static function isArrayOfObjects($arr)
279
    {
280 54
        if (!is_array($arr)) {
281
            return false;
282
        }
283 54
        if (empty($arr)) {
284 1
            return true;
285
        }
286
287 53
        return !static::arrayIsAssociative($arr);
288
    }
289
290 53
    private static function arrayIsAssociative($arr)
291
    {
292 53
        return (array_keys($arr) !== range(0, count($arr) - 1));
293
    }
294
}
295