Merge pull request #19276 from civicrm/5.33
[civicrm-core.git] / tests / phpunit / CiviTest / CiviDBAssert.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
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.
28 * @param $testCase
29 * @param $daoName
30 * @param $id
31 * @param $match
32 * @param bool $delete
33 */
34 public function assertDBState(&$testCase, $daoName, $id, $match, $delete = FALSE) {
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
41 $object = new $daoName();
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 }
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.
79 * @param $testCase
80 * @param $daoName
81 * @param $searchValue
82 * @param $returnColumn
83 * @param $searchColumn
84 * @param $message
85 * @return null|string
86 */
87 public function assertDBNotNull(&$testCase, $daoName, $searchValue, $returnColumn, $searchColumn, $message) {
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.
99 * @param $testCase
100 * @param $daoName
101 * @param $searchValue
102 * @param $returnColumn
103 * @param $searchColumn
104 * @param $message
105 */
106 public function assertDBNull(&$testCase, $daoName, $searchValue, $returnColumn, $searchColumn, $message) {
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.
113 * @param $testCase
114 * @param $daoName
115 * @param $id
116 * @param $message
117 */
118 public function assertDBRowNotExist(&$testCase, $daoName, $id, $message) {
119 $value = CRM_Core_DAO::getFieldValue($daoName, $id, 'id', 'id');
120 $testCase->assertNull($value, $message);
121 }
122
123 /**
124 * Compare a single column value in a retrieved DB record to an expected value.
125 *
126 * @param $testCase
127 * @param string $daoName
128 * @param $searchValue
129 * @param $returnColumn
130 * @param $searchColumn
131 * @param $expectedValue
132 * @param string $message
133 */
134 public function assertDBCompareValue(
135 &$testCase, $daoName, $searchValue, $returnColumn, $searchColumn,
136 $expectedValue, $message
137 ) {
138 $value = CRM_Core_DAO::getFieldValue($daoName, $searchValue, $returnColumn, $searchColumn);
139 $testCase->assertEquals($value, $expectedValue, $message);
140 }
141
142 /**
143 * Compare all values in a single retrieved DB record to an array of expected values.
144 * @param $testCase
145 * @param $daoName
146 * @param $searchParams
147 * @param $expectedValues
148 */
149 public function assertDBCompareValues(&$testCase, $daoName, $searchParams, $expectedValues) {
150 //get the values from db
151 $dbValues = [];
152 CRM_Core_DAO::commonRetrieve($daoName, $searchParams, $dbValues);
153
154 // compare db values with expected values
155 self::assertAttributesEquals($testCase, $expectedValues, $dbValues);
156 }
157
158 /**
159 * @param $testCase
160 * @param $expectedValues
161 * @param $actualValues
162 */
163 public function assertAttributesEquals(&$testCase, &$expectedValues, &$actualValues) {
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 }
173
174 }