Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7d61e75f | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
7d61e75f TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | class CiviDBAssert { | |
18 | ||
19 | /** | |
20 | * Generic function to compare expected values after an api call to retrieved | |
21 | * DB values. | |
22 | * | |
23 | * @daoName string DAO Name of object we're evaluating. | |
24 | * @id int Id of object | |
25 | * @match array Associative array of field name => expected value. Empty if asserting | |
26 | * that a DELETE occurred | |
27 | * @delete boolean True if we're checking that a DELETE action occurred. | |
1e1fdcf6 EM |
28 | * @param $testCase |
29 | * @param $daoName | |
30 | * @param $id | |
31 | * @param $match | |
32 | * @param bool $delete | |
6a488035 | 33 | */ |
00be9182 | 34 | public function assertDBState(&$testCase, $daoName, $id, $match, $delete = FALSE) { |
6a488035 TO |
35 | if (empty($id)) { |
36 | // adding this here since developers forget to check for an id | |
37 | // and hence we get the first value in the db | |
38 | $testCase->fail('ID not populated. Please fix your assertDBState usage!!!'); | |
39 | } | |
40 | ||
92915c55 | 41 | $object = new $daoName(); |
6a488035 TO |
42 | $object->id = $id; |
43 | $verifiedCount = 0; | |
44 | ||
45 | // If we're asserting successful record deletion, make sure object is NOT found. | |
46 | if ($delete) { | |
47 | if ($object->find(TRUE)) { | |
48 | $testCase->fail("Object not deleted by delete operation: $daoName, $id"); | |
49 | } | |
50 | return; | |
51 | } | |
52 | ||
53 | // Otherwise check matches of DAO field values against expected values in $match. | |
54 | if ($object->find(TRUE)) { | |
55 | $fields = &$object->fields(); | |
56 | foreach ($fields as $name => $value) { | |
57 | $dbName = $value['name']; | |
58 | if (isset($match[$name])) { | |
59 | $verifiedCount++; | |
60 | $testCase->assertEquals($object->$dbName, $match[$name]); | |
61 | } | |
62 | elseif (isset($match[$dbName])) { | |
63 | $verifiedCount++; | |
64 | $testCase->assertEquals($object->$dbName, $match[$dbName]); | |
65 | } | |
66 | } | |
67 | } | |
68 | else { | |
69 | $testCase->fail("Could not retrieve object: $daoName, $id"); | |
70 | } | |
6a488035 TO |
71 | $matchSize = count($match); |
72 | if ($verifiedCount != $matchSize) { | |
73 | $testCase->fail("Did not verify all fields in match array: $daoName, $id. Verified count = $verifiedCount. Match array size = $matchSize"); | |
74 | } | |
75 | } | |
76 | ||
77 | /** | |
78 | * Request a record from the DB by seachColumn+searchValue. Success if a record is found. | |
1e1fdcf6 EM |
79 | * @param $testCase |
80 | * @param $daoName | |
81 | * @param $searchValue | |
82 | * @param $returnColumn | |
83 | * @param $searchColumn | |
84 | * @param $message | |
85 | * @return null|string | |
6a488035 | 86 | */ |
00be9182 | 87 | public function assertDBNotNull(&$testCase, $daoName, $searchValue, $returnColumn, $searchColumn, $message) { |
6a488035 TO |
88 | if (empty($searchValue)) { |
89 | $testCase->fail("empty value passed to assertDBNotNull"); | |
90 | } | |
91 | $value = CRM_Core_DAO::getFieldValue($daoName, $searchValue, $returnColumn, $searchColumn); | |
92 | $testCase->assertNotNull($value, $message); | |
93 | ||
94 | return $value; | |
95 | } | |
96 | ||
97 | /** | |
98 | * Request a record from the DB by seachColumn+searchValue. Success if returnColumn value is NULL. | |
1e1fdcf6 EM |
99 | * @param $testCase |
100 | * @param $daoName | |
101 | * @param $searchValue | |
102 | * @param $returnColumn | |
103 | * @param $searchColumn | |
104 | * @param $message | |
6a488035 | 105 | */ |
00be9182 | 106 | public function assertDBNull(&$testCase, $daoName, $searchValue, $returnColumn, $searchColumn, $message) { |
6a488035 TO |
107 | $value = CRM_Core_DAO::getFieldValue($daoName, $searchValue, $returnColumn, $searchColumn); |
108 | $testCase->assertNull($value, $message); | |
109 | } | |
110 | ||
111 | /** | |
112 | * Request a record from the DB by id. Success if row not found. | |
1e1fdcf6 EM |
113 | * @param $testCase |
114 | * @param $daoName | |
115 | * @param $id | |
116 | * @param $message | |
6a488035 | 117 | */ |
00be9182 | 118 | public function assertDBRowNotExist(&$testCase, $daoName, $id, $message) { |
6a488035 TO |
119 | $value = CRM_Core_DAO::getFieldValue($daoName, $id, 'id', 'id'); |
120 | $testCase->assertNull($value, $message); | |
121 | } | |
122 | ||
123 | /** | |
eceb18cc | 124 | * Compare a single column value in a retrieved DB record to an expected value. |
9d06e7e6 EM |
125 | * |
126 | * @param $testCase | |
c490a46a | 127 | * @param string $daoName |
9d06e7e6 EM |
128 | * @param $searchValue |
129 | * @param $returnColumn | |
130 | * @param $searchColumn | |
131 | * @param $expectedValue | |
132 | * @param string $message | |
6a488035 | 133 | */ |
7c550ca0 | 134 | public function assertDBCompareValue( |
5896d037 | 135 | &$testCase, $daoName, $searchValue, $returnColumn, $searchColumn, |
6a488035 TO |
136 | $expectedValue, $message |
137 | ) { | |
138 | $value = CRM_Core_DAO::getFieldValue($daoName, $searchValue, $returnColumn, $searchColumn); | |
139 | $testCase->assertEquals($value, $expectedValue, $message); | |
140 | } | |
141 | ||
142 | /** | |
eceb18cc | 143 | * Compare all values in a single retrieved DB record to an array of expected values. |
1e1fdcf6 EM |
144 | * @param $testCase |
145 | * @param $daoName | |
146 | * @param $searchParams | |
147 | * @param $expectedValues | |
6a488035 | 148 | */ |
00be9182 | 149 | public function assertDBCompareValues(&$testCase, $daoName, $searchParams, $expectedValues) { |
6a488035 | 150 | //get the values from db |
affcc9d2 | 151 | $dbValues = []; |
6a488035 TO |
152 | CRM_Core_DAO::commonRetrieve($daoName, $searchParams, $dbValues); |
153 | ||
6a488035 TO |
154 | // compare db values with expected values |
155 | self::assertAttributesEquals($testCase, $expectedValues, $dbValues); | |
156 | } | |
157 | ||
4cbe18b8 EM |
158 | /** |
159 | * @param $testCase | |
160 | * @param $expectedValues | |
161 | * @param $actualValues | |
162 | */ | |
00be9182 | 163 | public function assertAttributesEquals(&$testCase, &$expectedValues, &$actualValues) { |
6a488035 TO |
164 | foreach ($expectedValues as $paramName => $paramValue) { |
165 | if (isset($actualValues[$paramName])) { | |
166 | $testCase->assertEquals($paramValue, $actualValues[$paramName]); | |
167 | } | |
168 | else { | |
169 | $testCase->fail("Attribute '$paramName' not present in actual array."); | |
170 | } | |
171 | } | |
172 | } | |
96025800 | 173 | |
6a488035 | 174 | } |