3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
22 * Generic function to compare expected values after an api call to retrieved
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.
36 public function assertDBState(&$testCase, $daoName, $id, $match, $delete = FALSE) {
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!!!');
43 $object = new $daoName();
47 // If we're asserting successful record deletion, make sure object is NOT found.
49 if ($object->find(TRUE)) {
50 $testCase->fail("Object not deleted by delete operation: $daoName, $id");
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])) {
62 $testCase->assertEquals($object->$dbName, $match[$name]);
64 elseif (isset($match[$dbName])) {
66 $testCase->assertEquals($object->$dbName, $match[$dbName]);
71 $testCase->fail("Could not retrieve object: $daoName, $id");
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");
80 * Request a record from the DB by seachColumn+searchValue. Success if a record is found.
84 * @param $returnColumn
85 * @param $searchColumn
89 public function assertDBNotNull(&$testCase, $daoName, $searchValue, $returnColumn, $searchColumn, $message) {
90 if (empty($searchValue)) {
91 $testCase->fail("empty value passed to assertDBNotNull");
93 $value = CRM_Core_DAO
::getFieldValue($daoName, $searchValue, $returnColumn, $searchColumn);
94 $testCase->assertNotNull($value, $message);
100 * Request a record from the DB by seachColumn+searchValue. Success if returnColumn value is NULL.
103 * @param $searchValue
104 * @param $returnColumn
105 * @param $searchColumn
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);
114 * Request a record from the DB by id. Success if row not found.
120 public function assertDBRowNotExist(&$testCase, $daoName, $id, $message) {
121 $value = CRM_Core_DAO
::getFieldValue($daoName, $id, 'id', 'id');
122 $testCase->assertNull($value, $message);
126 * Compare a single column value in a retrieved DB record to an expected value.
129 * @param string $daoName
130 * @param $searchValue
131 * @param $returnColumn
132 * @param $searchColumn
133 * @param $expectedValue
134 * @param string $message
136 public function assertDBCompareValue(
137 &$testCase, $daoName, $searchValue, $returnColumn, $searchColumn,
138 $expectedValue, $message
140 $value = CRM_Core_DAO
::getFieldValue($daoName, $searchValue, $returnColumn, $searchColumn);
141 $testCase->assertEquals($value, $expectedValue, $message);
145 * Compare all values in a single retrieved DB record to an array of expected values.
148 * @param $searchParams
149 * @param $expectedValues
151 public function assertDBCompareValues(&$testCase, $daoName, $searchParams, $expectedValues) {
152 //get the values from db
154 CRM_Core_DAO
::commonRetrieve($daoName, $searchParams, $dbValues);
156 // compare db values with expected values
157 self
::assertAttributesEquals($testCase, $expectedValues, $dbValues);
162 * @param $expectedValues
163 * @param $actualValues
165 public function assertAttributesEquals(&$testCase, &$expectedValues, &$actualValues) {
166 foreach ($expectedValues as $paramName => $paramValue) {
167 if (isset($actualValues[$paramName])) {
168 $testCase->assertEquals($paramValue, $actualValues[$paramName]);
171 $testCase->fail("Attribute '$paramName' not present in actual array.");