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