Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06a1bc01 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
06a1bc01 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | class CiviDBAssert { | |
36 | ||
37 | /** | |
38 | * Generic function to compare expected values after an api call to retrieved | |
39 | * DB values. | |
40 | * | |
41 | * @daoName string DAO Name of object we're evaluating. | |
42 | * @id int Id of object | |
43 | * @match array Associative array of field name => expected value. Empty if asserting | |
44 | * that a DELETE occurred | |
45 | * @delete boolean True if we're checking that a DELETE action occurred. | |
1e1fdcf6 EM |
46 | * @param $testCase |
47 | * @param $daoName | |
48 | * @param $id | |
49 | * @param $match | |
50 | * @param bool $delete | |
6a488035 | 51 | */ |
00be9182 | 52 | public function assertDBState(&$testCase, $daoName, $id, $match, $delete = FALSE) { |
6a488035 TO |
53 | if (empty($id)) { |
54 | // adding this here since developers forget to check for an id | |
55 | // and hence we get the first value in the db | |
56 | $testCase->fail('ID not populated. Please fix your assertDBState usage!!!'); | |
57 | } | |
58 | ||
92915c55 | 59 | $object = new $daoName(); |
6a488035 TO |
60 | $object->id = $id; |
61 | $verifiedCount = 0; | |
62 | ||
63 | // If we're asserting successful record deletion, make sure object is NOT found. | |
64 | if ($delete) { | |
65 | if ($object->find(TRUE)) { | |
66 | $testCase->fail("Object not deleted by delete operation: $daoName, $id"); | |
67 | } | |
68 | return; | |
69 | } | |
70 | ||
71 | // Otherwise check matches of DAO field values against expected values in $match. | |
72 | if ($object->find(TRUE)) { | |
73 | $fields = &$object->fields(); | |
74 | foreach ($fields as $name => $value) { | |
75 | $dbName = $value['name']; | |
76 | if (isset($match[$name])) { | |
77 | $verifiedCount++; | |
78 | $testCase->assertEquals($object->$dbName, $match[$name]); | |
79 | } | |
80 | elseif (isset($match[$dbName])) { | |
81 | $verifiedCount++; | |
82 | $testCase->assertEquals($object->$dbName, $match[$dbName]); | |
83 | } | |
84 | } | |
85 | } | |
86 | else { | |
87 | $testCase->fail("Could not retrieve object: $daoName, $id"); | |
88 | } | |
89 | $object->free(); | |
90 | $matchSize = count($match); | |
91 | if ($verifiedCount != $matchSize) { | |
92 | $testCase->fail("Did not verify all fields in match array: $daoName, $id. Verified count = $verifiedCount. Match array size = $matchSize"); | |
93 | } | |
94 | } | |
95 | ||
96 | /** | |
97 | * Request a record from the DB by seachColumn+searchValue. Success if a record is found. | |
1e1fdcf6 EM |
98 | * @param $testCase |
99 | * @param $daoName | |
100 | * @param $searchValue | |
101 | * @param $returnColumn | |
102 | * @param $searchColumn | |
103 | * @param $message | |
104 | * @return null|string | |
6a488035 | 105 | */ |
00be9182 | 106 | public function assertDBNotNull(&$testCase, $daoName, $searchValue, $returnColumn, $searchColumn, $message) { |
6a488035 TO |
107 | if (empty($searchValue)) { |
108 | $testCase->fail("empty value passed to assertDBNotNull"); | |
109 | } | |
110 | $value = CRM_Core_DAO::getFieldValue($daoName, $searchValue, $returnColumn, $searchColumn); | |
111 | $testCase->assertNotNull($value, $message); | |
112 | ||
113 | return $value; | |
114 | } | |
115 | ||
116 | /** | |
117 | * Request a record from the DB by seachColumn+searchValue. Success if returnColumn value is NULL. | |
1e1fdcf6 EM |
118 | * @param $testCase |
119 | * @param $daoName | |
120 | * @param $searchValue | |
121 | * @param $returnColumn | |
122 | * @param $searchColumn | |
123 | * @param $message | |
6a488035 | 124 | */ |
00be9182 | 125 | public function assertDBNull(&$testCase, $daoName, $searchValue, $returnColumn, $searchColumn, $message) { |
6a488035 TO |
126 | $value = CRM_Core_DAO::getFieldValue($daoName, $searchValue, $returnColumn, $searchColumn); |
127 | $testCase->assertNull($value, $message); | |
128 | } | |
129 | ||
130 | /** | |
131 | * Request a record from the DB by id. Success if row not found. | |
1e1fdcf6 EM |
132 | * @param $testCase |
133 | * @param $daoName | |
134 | * @param $id | |
135 | * @param $message | |
6a488035 | 136 | */ |
00be9182 | 137 | public function assertDBRowNotExist(&$testCase, $daoName, $id, $message) { |
6a488035 TO |
138 | $value = CRM_Core_DAO::getFieldValue($daoName, $id, 'id', 'id'); |
139 | $testCase->assertNull($value, $message); | |
140 | } | |
141 | ||
142 | /** | |
eceb18cc | 143 | * Compare a single column value in a retrieved DB record to an expected value. |
9d06e7e6 EM |
144 | * |
145 | * @param $testCase | |
c490a46a | 146 | * @param string $daoName |
9d06e7e6 EM |
147 | * @param $searchValue |
148 | * @param $returnColumn | |
149 | * @param $searchColumn | |
150 | * @param $expectedValue | |
151 | * @param string $message | |
6a488035 | 152 | */ |
7c550ca0 | 153 | public function assertDBCompareValue( |
5896d037 | 154 | &$testCase, $daoName, $searchValue, $returnColumn, $searchColumn, |
6a488035 TO |
155 | $expectedValue, $message |
156 | ) { | |
157 | $value = CRM_Core_DAO::getFieldValue($daoName, $searchValue, $returnColumn, $searchColumn); | |
158 | $testCase->assertEquals($value, $expectedValue, $message); | |
159 | } | |
160 | ||
161 | /** | |
eceb18cc | 162 | * Compare all values in a single retrieved DB record to an array of expected values. |
1e1fdcf6 EM |
163 | * @param $testCase |
164 | * @param $daoName | |
165 | * @param $searchParams | |
166 | * @param $expectedValues | |
6a488035 | 167 | */ |
00be9182 | 168 | public function assertDBCompareValues(&$testCase, $daoName, $searchParams, $expectedValues) { |
6a488035 TO |
169 | //get the values from db |
170 | $dbValues = array(); | |
171 | CRM_Core_DAO::commonRetrieve($daoName, $searchParams, $dbValues); | |
172 | ||
6a488035 TO |
173 | // compare db values with expected values |
174 | self::assertAttributesEquals($testCase, $expectedValues, $dbValues); | |
175 | } | |
176 | ||
4cbe18b8 EM |
177 | /** |
178 | * @param $testCase | |
179 | * @param $expectedValues | |
180 | * @param $actualValues | |
181 | */ | |
00be9182 | 182 | public function assertAttributesEquals(&$testCase, &$expectedValues, &$actualValues) { |
6a488035 TO |
183 | foreach ($expectedValues as $paramName => $paramValue) { |
184 | if (isset($actualValues[$paramName])) { | |
185 | $testCase->assertEquals($paramValue, $actualValues[$paramName]); | |
186 | } | |
187 | else { | |
188 | $testCase->fail("Attribute '$paramName' not present in actual array."); | |
189 | } | |
190 | } | |
191 | } | |
96025800 | 192 | |
6a488035 | 193 | } |