9 * This trait is intended for use with PHPUnit-based test cases.
13 abstract public function assertAttributesEquals($expectedValues, $actualValues, $message = NULL);
16 * Generic function to compare expected values after an api call to retrieved.
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.
28 * @throws \PHPUnit_Framework_AssertionFailedError
30 public function assertDBState($daoName, $id, $match, $delete = FALSE) {
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!!!');
37 $object = new $daoName();
41 // If we're asserting successful record deletion, make sure object is NOT found.
43 if ($object->find(TRUE)) {
44 $this->fail("Object not deleted by delete operation: $daoName, $id");
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])) {
56 $this->assertEquals($object->$dbName, $match[$name]);
58 elseif (isset($match[$dbName])) {
60 $this->assertEquals($object->$dbName, $match[$dbName]);
65 $this->fail("Could not retrieve object: $daoName, $id");
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");
75 * Request a record from the DB by seachColumn+searchValue. Success if a record is found.
76 * @param string $daoName
78 * @param $returnColumn
79 * @param $searchColumn
83 * @throws \PHPUnit_Framework_AssertionFailedError
85 public function assertDBNotNull($daoName, $searchValue, $returnColumn, $searchColumn, $message) {
86 if (empty($searchValue)) {
87 $this->fail("empty value passed to assertDBNotNull");
89 $value = \CRM_Core_DAO
::getFieldValue($daoName, $searchValue, $returnColumn, $searchColumn, TRUE);
90 $this->assertNotNull($value, $message);
96 * Request a record from the DB by seachColumn+searchValue. Success if returnColumn value is NULL.
97 * @param string $daoName
99 * @param $returnColumn
100 * @param $searchColumn
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);
109 * Request a record from the DB by id. Success if row not found.
110 * @param string $daoName
112 * @param null $message
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);
121 * Request a record from the DB by id. Success if row not found.
122 * @param string $daoName
124 * @param null $message
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);
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
141 public function assertDBCompareValue(
142 $daoName, $searchValue, $returnColumn, $searchColumn,
143 $expectedValue, $message
145 $value = \CRM_Core_DAO
::getFieldValue($daoName, $searchValue, $returnColumn, $searchColumn, TRUE);
146 $this->assertEquals(trim($expectedValue), trim($value), $message);
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
155 public function assertDBCompareValues($daoName, $searchParams, $expectedValues) {
156 //get the values from db
158 \CRM_Core_DAO
::commonRetrieve($daoName, $searchParams, $dbValues);
160 // compare db values with expected values
161 $this->assertAttributesEquals($expectedValues, $dbValues);
165 * Assert that a SQL query returns a given value.
167 * The first argument is an expected value. The remaining arguments are passed
168 * to CRM_Core_DAO::singleValueQuery
170 * Example: $this->assertSql(2, 'select count(*) from foo where foo.bar like "%1"',
171 * array(1 => array("Whiz", "String")));
175 * @param array $params
176 * @param string $message
178 * @throws \CRM_Core_Exception
180 public function assertDBQuery($expected, $query, $params = [], $message = '') {
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)