| 1 | <?php |
| 2 | |
| 3 | namespace Civi\Test; |
| 4 | |
| 5 | /** |
| 6 | * Class DbTestTrait |
| 7 | * @package Civi\Test |
| 8 | * |
| 9 | * This trait is intended for use with PHPUnit-based test cases. |
| 10 | */ |
| 11 | trait DbTestTrait { |
| 12 | |
| 13 | abstract public function assertAttributesEquals($expectedValues, $actualValues, $message = NULL); |
| 14 | |
| 15 | /** |
| 16 | * Generic function to compare expected values after an api call to retrieved. |
| 17 | * DB values. |
| 18 | * |
| 19 | * @daoName string DAO Name of object we're evaluating. |
| 20 | * @id int Id of object |
| 21 | * @match array Associative array of field name => expected value. Empty if asserting |
| 22 | * that a DELETE occurred |
| 23 | * @delete boolean True if we're checking that a DELETE action occurred. |
| 24 | * @param $daoName |
| 25 | * @param $id |
| 26 | * @param $match |
| 27 | * @param bool $delete |
| 28 | * @throws \PHPUnit_Framework_AssertionFailedError |
| 29 | */ |
| 30 | public function assertDBState($daoName, $id, $match, $delete = FALSE) { |
| 31 | if (empty($id)) { |
| 32 | // adding this here since developers forget to check for an id |
| 33 | // and hence we get the first value in the db |
| 34 | $this->fail('ID not populated. Please fix your assertDBState usage!!!'); |
| 35 | } |
| 36 | |
| 37 | $object = new $daoName(); |
| 38 | $object->id = $id; |
| 39 | $verifiedCount = 0; |
| 40 | |
| 41 | // If we're asserting successful record deletion, make sure object is NOT found. |
| 42 | if ($delete) { |
| 43 | if ($object->find(TRUE)) { |
| 44 | $this->fail("Object not deleted by delete operation: $daoName, $id"); |
| 45 | } |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | // Otherwise check matches of DAO field values against expected values in $match. |
| 50 | if ($object->find(TRUE)) { |
| 51 | $fields = &$object->fields(); |
| 52 | foreach ($fields as $name => $value) { |
| 53 | $dbName = $value['name']; |
| 54 | if (isset($match[$name])) { |
| 55 | $verifiedCount++; |
| 56 | $this->assertEquals($object->$dbName, $match[$name]); |
| 57 | } |
| 58 | elseif (isset($match[$dbName])) { |
| 59 | $verifiedCount++; |
| 60 | $this->assertEquals($object->$dbName, $match[$dbName]); |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | else { |
| 65 | $this->fail("Could not retrieve object: $daoName, $id"); |
| 66 | } |
| 67 | |
| 68 | $matchSize = count($match); |
| 69 | if ($verifiedCount != $matchSize) { |
| 70 | $this->fail("Did not verify all fields in match array: $daoName, $id. Verified count = $verifiedCount. Match array size = $matchSize"); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Request a record from the DB by seachColumn+searchValue. Success if a record is found. |
| 76 | * @param string $daoName |
| 77 | * @param $searchValue |
| 78 | * @param $returnColumn |
| 79 | * @param $searchColumn |
| 80 | * @param $message |
| 81 | * |
| 82 | * @return null|string |
| 83 | * @throws \PHPUnit_Framework_AssertionFailedError |
| 84 | */ |
| 85 | public function assertDBNotNull($daoName, $searchValue, $returnColumn, $searchColumn, $message) { |
| 86 | if (empty($searchValue)) { |
| 87 | $this->fail("empty value passed to assertDBNotNull"); |
| 88 | } |
| 89 | $value = \CRM_Core_DAO::getFieldValue($daoName, $searchValue, $returnColumn, $searchColumn, TRUE); |
| 90 | $this->assertNotNull($value, $message); |
| 91 | |
| 92 | return $value; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Request a record from the DB by seachColumn+searchValue. Success if returnColumn value is NULL. |
| 97 | * @param string $daoName |
| 98 | * @param $searchValue |
| 99 | * @param $returnColumn |
| 100 | * @param $searchColumn |
| 101 | * @param $message |
| 102 | */ |
| 103 | public function assertDBNull($daoName, $searchValue, $returnColumn, $searchColumn, $message) { |
| 104 | $value = \CRM_Core_DAO::getFieldValue($daoName, $searchValue, $returnColumn, $searchColumn, TRUE); |
| 105 | $this->assertNull($value, $message); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Request a record from the DB by id. Success if row not found. |
| 110 | * @param string $daoName |
| 111 | * @param int $id |
| 112 | * @param null $message |
| 113 | */ |
| 114 | public function assertDBRowNotExist($daoName, $id, $message = NULL) { |
| 115 | $message = $message ? $message : "$daoName (#$id) should not exist"; |
| 116 | $value = \CRM_Core_DAO::getFieldValue($daoName, $id, 'id', 'id', TRUE); |
| 117 | $this->assertNull($value, $message); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Request a record from the DB by id. Success if row not found. |
| 122 | * @param string $daoName |
| 123 | * @param int $id |
| 124 | * @param null $message |
| 125 | */ |
| 126 | public function assertDBRowExist($daoName, $id, $message = NULL) { |
| 127 | $message = $message ? $message : "$daoName (#$id) should exist"; |
| 128 | $value = \CRM_Core_DAO::getFieldValue($daoName, $id, 'id', 'id', TRUE); |
| 129 | $this->assertEquals($id, $value, $message); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Compare a single column value in a retrieved DB record to an expected value. |
| 134 | * @param string $daoName |
| 135 | * @param $searchValue |
| 136 | * @param $returnColumn |
| 137 | * @param $searchColumn |
| 138 | * @param $expectedValue |
| 139 | * @param $message |
| 140 | */ |
| 141 | public function assertDBCompareValue( |
| 142 | $daoName, $searchValue, $returnColumn, $searchColumn, |
| 143 | $expectedValue, $message |
| 144 | ) { |
| 145 | $value = \CRM_Core_DAO::getFieldValue($daoName, $searchValue, $returnColumn, $searchColumn, TRUE); |
| 146 | $this->assertEquals(trim($expectedValue), trim($value), $message); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Compare all values in a single retrieved DB record to an array of expected values. |
| 151 | * @param string $daoName |
| 152 | * @param array $searchParams |
| 153 | * @param $expectedValues |
| 154 | */ |
| 155 | public function assertDBCompareValues($daoName, $searchParams, $expectedValues) { |
| 156 | //get the values from db |
| 157 | $dbValues = []; |
| 158 | \CRM_Core_DAO::commonRetrieve($daoName, $searchParams, $dbValues); |
| 159 | |
| 160 | // compare db values with expected values |
| 161 | $this->assertAttributesEquals($expectedValues, $dbValues); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Assert that a SQL query returns a given value. |
| 166 | * |
| 167 | * The first argument is an expected value. The remaining arguments are passed |
| 168 | * to CRM_Core_DAO::singleValueQuery |
| 169 | * |
| 170 | * Example: $this->assertSql(2, 'select count(*) from foo where foo.bar like "%1"', |
| 171 | * array(1 => array("Whiz", "String"))); |
| 172 | * |
| 173 | * @param $expected |
| 174 | * @param $query |
| 175 | * @param array $params |
| 176 | * @param string $message |
| 177 | * |
| 178 | * @throws \CRM_Core_Exception |
| 179 | */ |
| 180 | public function assertDBQuery($expected, $query, $params = [], $message = '') { |
| 181 | if ($message) { |
| 182 | $message .= ': '; |
| 183 | } |
| 184 | $actual = \CRM_Core_DAO::singleValueQuery($query, $params); |
| 185 | $this->assertEquals($expected, $actual, |
| 186 | sprintf('%sexpected=[%s] actual=[%s] query=[%s]', |
| 187 | $message, $expected, $actual, \CRM_Core_DAO::composeQuery($query, $params) |
| 188 | ) |
| 189 | ); |
| 190 | } |
| 191 | |
| 192 | } |