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) { | |
affcc9d2 CW |
38 | $e = []; |
39 | $a = []; | |
1426d341 TO |
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) { | |
48b004d1 | 60 | if ($tolerance == 1 && is_int($expected) && is_int($actual)) { |
61 | // ^^ loose equality is on purpose | |
62 | throw new \CRM_Core_Exception('assertApproxEquals is a fractions-first thinking function and compares integers with a tolerance of 1 as if they are identical. You want a bigger number, such as 2, or 5.'); | |
63 | } | |
ff93f0ab | 64 | $diff = abs($actual - $expected); |
1426d341 | 65 | if ($message === NULL) { |
ff93f0ab | 66 | $message = sprintf("approx-equals: expected=[%.3f] actual=[%.3f] diff=[%.3f] tolerance=[%.3f]", $expected, $actual, $diff, $tolerance); |
1426d341 | 67 | } |
ff93f0ab | 68 | $this->assertTrue($diff < $tolerance, $message); |
1426d341 TO |
69 | } |
70 | ||
71 | /** | |
72 | * Assert attributes are equal. | |
73 | * | |
ff93f0ab TO |
74 | * @param array $expectedValues |
75 | * @param array $actualValues | |
1426d341 TO |
76 | * @param string $message |
77 | * | |
78 | * @throws \PHPUnit_Framework_AssertionFailedError | |
79 | */ | |
80 | public function assertAttributesEquals($expectedValues, $actualValues, $message = NULL) { | |
81 | foreach ($expectedValues as $paramName => $paramValue) { | |
82 | if (isset($actualValues[$paramName])) { | |
83 | $this->assertEquals($paramValue, $actualValues[$paramName], "Value Mismatch On $paramName - value 1 is " . print_r($paramValue, TRUE) . " value 2 is " . print_r($actualValues[$paramName], TRUE)); | |
84 | } | |
85 | else { | |
86 | $this->assertNull($expectedValues[$paramName], "Attribute '$paramName' not present in actual array and we expected it to be " . $expectedValues[$paramName]); | |
87 | } | |
88 | } | |
89 | } | |
90 | ||
91 | /** | |
ff93f0ab TO |
92 | * @param string|int $key |
93 | * @param array $list | |
1426d341 TO |
94 | */ |
95 | public function assertArrayKeyExists($key, &$list) { | |
1699214f | 96 | $result = isset($list[$key]); |
ff93f0ab | 97 | $this->assertTrue($result, sprintf("%s element exists?", $key)); |
1426d341 TO |
98 | } |
99 | ||
100 | /** | |
ff93f0ab TO |
101 | * @param string|int $key |
102 | * @param array $list | |
1426d341 TO |
103 | */ |
104 | public function assertArrayValueNotNull($key, &$list) { | |
105 | $this->assertArrayKeyExists($key, $list); | |
106 | ||
2e1f50d6 | 107 | $value = $list[$key] ?? NULL; |
1426d341 | 108 | $this->assertTrue($value, |
ff93f0ab | 109 | sprintf("%s element not null?", $key) |
1426d341 TO |
110 | ); |
111 | } | |
112 | ||
113 | /** | |
114 | * Assert the 2 arrays have the same values. | |
115 | * | |
ff93f0ab TO |
116 | * The order of arrays, and keys of the arrays, do not affect the outcome. |
117 | * | |
1426d341 TO |
118 | * @param array $array1 |
119 | * @param array $array2 | |
120 | */ | |
121 | public function assertArrayValuesEqual($array1, $array2) { | |
122 | $array1 = array_values($array1); | |
123 | $array2 = array_values($array2); | |
124 | sort($array1); | |
125 | sort($array2); | |
126 | $this->assertEquals($array1, $array2); | |
127 | } | |
128 | ||
129 | } |