Merge pull request #17750 from colemanw/upgradeBump
[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 * $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.
30 * @param $testCase
31 * @param $daoName
32 * @param $id
33 * @param $match
34 * @param bool $delete
35 */
36 public function assertDBState(&$testCase, $daoName, $id, $match, $delete = FALSE) {
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
43 $object = new $daoName();
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 }
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.
81 * @param $testCase
82 * @param $daoName
83 * @param $searchValue
84 * @param $returnColumn
85 * @param $searchColumn
86 * @param $message
87 * @return null|string
88 */
89 public function assertDBNotNull(&$testCase, $daoName, $searchValue, $returnColumn, $searchColumn, $message) {
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.
101 * @param $testCase
102 * @param $daoName
103 * @param $searchValue
104 * @param $returnColumn
105 * @param $searchColumn
106 * @param $message
107 */
108 public function assertDBNull(&$testCase, $daoName, $searchValue, $returnColumn, $searchColumn, $message) {
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.
115 * @param $testCase
116 * @param $daoName
117 * @param $id
118 * @param $message
119 */
120 public function assertDBRowNotExist(&$testCase, $daoName, $id, $message) {
121 $value = CRM_Core_DAO::getFieldValue($daoName, $id, 'id', 'id');
122 $testCase->assertNull($value, $message);
123 }
124
125 /**
126 * Compare a single column value in a retrieved DB record to an expected value.
127 *
128 * @param $testCase
129 * @param string $daoName
130 * @param $searchValue
131 * @param $returnColumn
132 * @param $searchColumn
133 * @param $expectedValue
134 * @param string $message
135 */
136 public function assertDBCompareValue(
137 &$testCase, $daoName, $searchValue, $returnColumn, $searchColumn,
138 $expectedValue, $message
139 ) {
140 $value = CRM_Core_DAO::getFieldValue($daoName, $searchValue, $returnColumn, $searchColumn);
141 $testCase->assertEquals($value, $expectedValue, $message);
142 }
143
144 /**
145 * Compare all values in a single retrieved DB record to an array of expected values.
146 * @param $testCase
147 * @param $daoName
148 * @param $searchParams
149 * @param $expectedValues
150 */
151 public function assertDBCompareValues(&$testCase, $daoName, $searchParams, $expectedValues) {
152 //get the values from db
153 $dbValues = [];
154 CRM_Core_DAO::commonRetrieve($daoName, $searchParams, $dbValues);
155
156 // compare db values with expected values
157 self::assertAttributesEquals($testCase, $expectedValues, $dbValues);
158 }
159
160 /**
161 * @param $testCase
162 * @param $expectedValues
163 * @param $actualValues
164 */
165 public function assertAttributesEquals(&$testCase, &$expectedValues, &$actualValues) {
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 }
175
176 }