Merge pull request #14044 from totten/master-test-trait-2
[civicrm-core.git] / Civi / Test / GenericAssertionsTrait.php
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 $expected
16 * @param $actual
17 * @param string $message
18 */
19 public function assertType($expected, $actual, $message = '') {
20 return $this->assertInternalType($expected, $actual, $message);
21 }
22
23 /**
24 * Assert that two array-trees are exactly equal, notwithstanding
25 * the sorting of keys
26 *
27 * @param array $expected
28 * @param array $actual
29 */
30 public function assertTreeEquals($expected, $actual) {
31 $e = array();
32 $a = array();
33 \CRM_Utils_Array::flatten($expected, $e, '', ':::');
34 \CRM_Utils_Array::flatten($actual, $a, '', ':::');
35 ksort($e);
36 ksort($a);
37
38 $this->assertEquals($e, $a);
39 }
40
41 /**
42 * Assert that two numbers are approximately equal.
43 *
44 * @param int|float $expected
45 * @param int|float $actual
46 * @param int|float $tolerance
47 * @param string $message
48 */
49 public function assertApproxEquals($expected, $actual, $tolerance, $message = NULL) {
50 if ($message === NULL) {
51 $message = sprintf("approx-equals: expected=[%.3f] actual=[%.3f] tolerance=[%.3f]", $expected, $actual, $tolerance);
52 }
53 $this->assertTrue(abs($actual - $expected) < $tolerance, $message);
54 }
55
56 /**
57 * Assert attributes are equal.
58 *
59 * @param $expectedValues
60 * @param $actualValues
61 * @param string $message
62 *
63 * @throws \PHPUnit_Framework_AssertionFailedError
64 */
65 public function assertAttributesEquals($expectedValues, $actualValues, $message = NULL) {
66 foreach ($expectedValues as $paramName => $paramValue) {
67 if (isset($actualValues[$paramName])) {
68 $this->assertEquals($paramValue, $actualValues[$paramName], "Value Mismatch On $paramName - value 1 is " . print_r($paramValue, TRUE) . " value 2 is " . print_r($actualValues[$paramName], TRUE));
69 }
70 else {
71 $this->assertNull($expectedValues[$paramName], "Attribute '$paramName' not present in actual array and we expected it to be " . $expectedValues[$paramName]);
72 }
73 }
74 }
75
76 /**
77 * @param $key
78 * @param $list
79 */
80 public function assertArrayKeyExists($key, &$list) {
81 $result = isset($list[$key]) ? TRUE : FALSE;
82 $this->assertTrue($result, ts("%1 element exists?",
83 array(1 => $key)
84 ));
85 }
86
87 /**
88 * @param $key
89 * @param $list
90 */
91 public function assertArrayValueNotNull($key, &$list) {
92 $this->assertArrayKeyExists($key, $list);
93
94 $value = isset($list[$key]) ? $list[$key] : NULL;
95 $this->assertTrue($value,
96 ts("%1 element not null?",
97 array(1 => $key)
98 )
99 );
100 }
101
102 /**
103 * Assert the 2 arrays have the same values.
104 *
105 * @param array $array1
106 * @param array $array2
107 */
108 public function assertArrayValuesEqual($array1, $array2) {
109 $array1 = array_values($array1);
110 $array2 = array_values($array2);
111 sort($array1);
112 sort($array2);
113 $this->assertEquals($array1, $array2);
114 }
115
116 }