|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2015 SURFnet B.V. |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace OpenConext\Profile; |
|
20
|
|
|
|
|
21
|
|
|
use Assert\Assertion as BaseAssertion; |
|
22
|
|
|
use OpenConext\Profile\Exception\AssertionFailedException; |
|
23
|
|
|
|
|
24
|
|
|
class Assert extends BaseAssertion |
|
25
|
|
|
{ |
|
26
|
|
|
protected static $exceptionClass = 'OpenConext\Profile\Exception\AssertionFailedException'; |
|
27
|
|
|
|
|
28
|
|
|
public static function keysAre(array $array, array $expectedKeys, $propertyPath = null) |
|
29
|
|
|
{ |
|
30
|
|
|
$givenKeys = array_keys($array); |
|
31
|
|
|
|
|
32
|
|
|
sort($givenKeys); |
|
33
|
|
|
sort($expectedKeys); |
|
34
|
|
|
|
|
35
|
|
|
if ($givenKeys === $expectedKeys) { |
|
36
|
|
|
return; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$givenCount = count($givenKeys); |
|
40
|
|
|
$expectedCount = count($expectedKeys); |
|
41
|
|
|
|
|
42
|
|
|
if ($givenCount < $expectedCount) { |
|
43
|
|
|
$message = sprintf( |
|
44
|
|
|
'Required keys "%s" are missing', |
|
45
|
|
|
implode('", "', array_diff($expectedKeys, $givenKeys)) |
|
46
|
|
|
); |
|
47
|
|
|
} elseif ($givenCount > $expectedCount) { |
|
48
|
|
|
$message = sprintf( |
|
49
|
|
|
'Additional keys "%s" found', |
|
50
|
|
|
implode('", "', array_diff($givenKeys, $expectedKeys)) |
|
51
|
|
|
); |
|
52
|
|
|
} else { |
|
53
|
|
|
$additional = array_diff($givenKeys, $expectedKeys); |
|
54
|
|
|
$required = array_diff($expectedKeys, $givenKeys); |
|
55
|
|
|
|
|
56
|
|
|
$message = 'Keys do not match requirements'; |
|
57
|
|
|
if (!empty($additional)) { |
|
58
|
|
|
$message .= sprintf( |
|
59
|
|
|
', additional keys "%s" found', |
|
60
|
|
|
implode('", "', array_diff($givenKeys, $expectedKeys)) |
|
61
|
|
|
); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if (!empty($required)) { |
|
65
|
|
|
$message .= sprintf( |
|
66
|
|
|
', required keys "%s" are missing', |
|
67
|
|
|
implode('", "', array_diff($expectedKeys, $givenKeys)) |
|
68
|
|
|
); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
throw new AssertionFailedException($message, 0, $propertyPath, $array); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|