Commit | Line | Data |
---|---|---|
1426d341 TO |
1 | <?php |
2 | ||
3 | namespace Civi\Test; | |
4 | ||
5 | /** | |
6 | * Class ExtraAssertionsTrait | |
7 | * @package Civi\Test | |
8 | * | |
9 | * A small library of generic assertions - which are slightly more sophisticated than | |
10 | * the default (`assertEquals()`, `assertTrue()`) but *not* domain specific. | |
11 | */ | |
12 | trait GenericAssertionsTrait { | |
13 | ||
14 | /** | |
ff93f0ab TO |
15 | * @param string $expected |
16 | * Ex: 'array', 'object', 'int' | |
1426d341 | 17 | * @param $actual |
ff93f0ab | 18 | * The variable/item to check. |
1426d341 TO |
19 | * @param string $message |
20 | */ | |
21 | public function assertType($expected, $actual, $message = '') { | |
22 | return $this->assertInternalType($expected, $actual, $message); | |
23 | } | |
24 | ||
25 | /** | |
ff93f0ab TO |
26 | * Assert that two array-trees are exactly equal. |
27 | * | |
28 | * The ordering of keys do not affect the outcome (within either the roots | |
29 | * or in any child elements). | |
30 | * | |
31 | * Error messages will reveal a readable -path-, regardless of how many | |
32 | * levels of nesting are present. | |
1426d341 TO |
33 | * |
34 | * @param array $expected | |
35 | * @param array $actual | |
36 | */ | |
37 | public function assertTreeEquals($expected, $actual) { | |
38 | $e = array(); | |
39 | $a = array(); | |
40 | \CRM_Utils_Array::flatten($expected, $e, '', ':::'); | |
41 | \CRM_Utils_Array::flatten($actual, $a, '', ':::'); | |
42 | ksort($e); | |
43 | ksort($a); | |
44 | ||
45 | $this->assertEquals($e, $a); | |
46 | } | |
47 | ||
48 | /** | |
ff93f0ab TO |
49 | * Assert that two numbers are approximately equal, |
50 | * give or take some $tolerance. | |
1426d341 TO |
51 | * |
52 | * @param int|float $expected | |
53 | * @param int|float $actual | |
54 | * @param int|float $tolerance | |
ff93f0ab TO |
55 | * Any differences <$tolerance are considered irrelevant. |
56 | * Differences >=$tolerance are considered relevant. | |
1426d341 TO |
57 | * @param string $message |
58 | */ | |
59 | public function assertApproxEquals($expected, $actual, $tolerance, $message = NULL) { | |
ff93f0ab | 60 | $diff = abs($actual - $expected); |
1426d341 | 61 | if ($message === NULL) { |
ff93f0ab | 62 | $message = sprintf("approx-equals: expected=[%.3f] actual=[%.3f] diff=[%.3f] tolerance=[%.3f]", $expected, $actual, $diff, $tolerance); |
1426d341 | 63 | } |
ff93f0ab | 64 | $this->assertTrue($diff < $tolerance, $message); |
1426d341 TO |
65 | } |
66 | ||
67 | /** | |
68 | * Assert attributes are equal. | |
69 | * | |
ff93f0ab TO |
70 | * @param array $expectedValues |
71 | * @param array $actualValues | |
1426d341 TO |
72 | * @param string $message |
73 | * | |
74 | * @throws \PHPUnit_Framework_AssertionFailedError | |
75 | */ | |
76 | public function assertAttributesEquals($expectedValues, $actualValues, $message = NULL) { | |
77 | foreach ($expectedValues as $paramName => $paramValue) { | |
78 | if (isset($actualValues[$paramName])) { | |
79 | $this->assertEquals($paramValue, $actualValues[$paramName], "Value Mismatch On $paramName - value 1 is " . print_r($paramValue, TRUE) . " value 2 is " . print_r($actualValues[$paramName], TRUE)); | |
80 | } | |
81 | else { | |
82 | $this->assertNull($expectedValues[$paramName], "Attribute '$paramName' not present in actual array and we expected it to be " . $expectedValues[$paramName]); | |
83 | } | |
84 | } | |
85 | } | |
86 | ||
87 | /** | |
ff93f0ab TO |
88 | * @param string|int $key |
89 | * @param array $list | |
1426d341 TO |
90 | */ |
91 | public function assertArrayKeyExists($key, &$list) { | |
1699214f | 92 | $result = isset($list[$key]); |
ff93f0ab | 93 | $this->assertTrue($result, sprintf("%s element exists?", $key)); |
1426d341 TO |
94 | } |
95 | ||
96 | /** | |
ff93f0ab TO |
97 | * @param string|int $key |
98 | * @param array $list | |
1426d341 TO |
99 | */ |
100 | public function assertArrayValueNotNull($key, &$list) { | |
101 | $this->assertArrayKeyExists($key, $list); | |
102 | ||
2e1f50d6 | 103 | $value = $list[$key] ?? NULL; |
1426d341 | 104 | $this->assertTrue($value, |
ff93f0ab | 105 | sprintf("%s element not null?", $key) |
1426d341 TO |
106 | ); |
107 | } | |
108 | ||
109 | /** | |
110 | * Assert the 2 arrays have the same values. | |
111 | * | |
ff93f0ab TO |
112 | * The order of arrays, and keys of the arrays, do not affect the outcome. |
113 | * | |
1426d341 TO |
114 | * @param array $array1 |
115 | * @param array $array2 | |
116 | */ | |
117 | public function assertArrayValuesEqual($array1, $array2) { | |
118 | $array1 = array_values($array1); | |
119 | $array2 = array_values($array2); | |
120 | sort($array1); | |
121 | sort($array2); | |
122 | $this->assertEquals($array1, $array2); | |
123 | } | |
124 | ||
125 | } |