| 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 | /** |
| 15 | * @param string $expected |
| 16 | * Ex: 'array', 'object', 'int' |
| 17 | * @param $actual |
| 18 | * The variable/item to check. |
| 19 | * @param string $message |
| 20 | */ |
| 21 | public function assertType($expected, $actual, $message = '') { |
| 22 | return $this->assertInternalType($expected, $actual, $message); |
| 23 | } |
| 24 | |
| 25 | /** |
| 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. |
| 33 | * |
| 34 | * @param array $expected |
| 35 | * @param array $actual |
| 36 | */ |
| 37 | public function assertTreeEquals($expected, $actual) { |
| 38 | $e = []; |
| 39 | $a = []; |
| 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 | /** |
| 49 | * Assert that two numbers are approximately equal, |
| 50 | * give or take some $tolerance. |
| 51 | * |
| 52 | * @param int|float $expected |
| 53 | * @param int|float $actual |
| 54 | * @param int|float $tolerance |
| 55 | * Any differences <$tolerance are considered irrelevant. |
| 56 | * Differences >=$tolerance are considered relevant. |
| 57 | * @param string $message |
| 58 | */ |
| 59 | public function assertApproxEquals($expected, $actual, $tolerance, $message = NULL) { |
| 60 | $diff = abs($actual - $expected); |
| 61 | if ($message === NULL) { |
| 62 | $message = sprintf("approx-equals: expected=[%.3f] actual=[%.3f] diff=[%.3f] tolerance=[%.3f]", $expected, $actual, $diff, $tolerance); |
| 63 | } |
| 64 | $this->assertTrue($diff < $tolerance, $message); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Assert attributes are equal. |
| 69 | * |
| 70 | * @param array $expectedValues |
| 71 | * @param array $actualValues |
| 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 | /** |
| 88 | * @param string|int $key |
| 89 | * @param array $list |
| 90 | */ |
| 91 | public function assertArrayKeyExists($key, &$list) { |
| 92 | $result = isset($list[$key]); |
| 93 | $this->assertTrue($result, sprintf("%s element exists?", $key)); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @param string|int $key |
| 98 | * @param array $list |
| 99 | */ |
| 100 | public function assertArrayValueNotNull($key, &$list) { |
| 101 | $this->assertArrayKeyExists($key, $list); |
| 102 | |
| 103 | $value = $list[$key] ?? NULL; |
| 104 | $this->assertTrue($value, |
| 105 | sprintf("%s element not null?", $key) |
| 106 | ); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Assert the 2 arrays have the same values. |
| 111 | * |
| 112 | * The order of arrays, and keys of the arrays, do not affect the outcome. |
| 113 | * |
| 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 | } |