Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-08-04-22-25-32
[civicrm-core.git] / tests / phpunit / CiviTest / CiviDBAssert.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35 class CiviDBAssert {
36
37 /**
38 * Generic function to compare expected values after an api call to retrieved
39 * DB values.
40 *
41 * @daoName string DAO Name of object we're evaluating.
42 * @id int Id of object
43 * @match array Associative array of field name => expected value. Empty if asserting
44 * that a DELETE occurred
45 * @delete boolean True if we're checking that a DELETE action occurred.
46 */
47 function assertDBState(&$testCase, $daoName, $id, $match, $delete = FALSE) {
48 if (empty($id)) {
49 // adding this here since developers forget to check for an id
50 // and hence we get the first value in the db
51 $testCase->fail('ID not populated. Please fix your assertDBState usage!!!');
52 }
53
54 $object = new $daoName();
55 $object->id = $id;
56 $verifiedCount = 0;
57
58 // If we're asserting successful record deletion, make sure object is NOT found.
59 if ($delete) {
60 if ($object->find(TRUE)) {
61 $testCase->fail("Object not deleted by delete operation: $daoName, $id");
62 }
63 return;
64 }
65
66 // Otherwise check matches of DAO field values against expected values in $match.
67 if ($object->find(TRUE)) {
68 $fields = &$object->fields();
69 foreach ($fields as $name => $value) {
70 $dbName = $value['name'];
71 if (isset($match[$name])) {
72 $verifiedCount++;
73 $testCase->assertEquals($object->$dbName, $match[$name]);
74 }
75 elseif (isset($match[$dbName])) {
76 $verifiedCount++;
77 $testCase->assertEquals($object->$dbName, $match[$dbName]);
78 }
79 }
80 }
81 else {
82 $testCase->fail("Could not retrieve object: $daoName, $id");
83 }
84 $object->free();
85 $matchSize = count($match);
86 if ($verifiedCount != $matchSize) {
87 $testCase->fail("Did not verify all fields in match array: $daoName, $id. Verified count = $verifiedCount. Match array size = $matchSize");
88 }
89 }
90
91 /**
92 * Request a record from the DB by seachColumn+searchValue. Success if a record is found.
93 */
94 function assertDBNotNull(&$testCase, $daoName, $searchValue, $returnColumn, $searchColumn, $message) {
95 if (empty($searchValue)) {
96 $testCase->fail("empty value passed to assertDBNotNull");
97 }
98 $value = CRM_Core_DAO::getFieldValue($daoName, $searchValue, $returnColumn, $searchColumn);
99 $testCase->assertNotNull($value, $message);
100
101 return $value;
102 }
103
104 /**
105 * Request a record from the DB by seachColumn+searchValue. Success if returnColumn value is NULL.
106 */
107 function assertDBNull(&$testCase, $daoName, $searchValue, $returnColumn, $searchColumn, $message) {
108 $value = CRM_Core_DAO::getFieldValue($daoName, $searchValue, $returnColumn, $searchColumn);
109 $testCase->assertNull($value, $message);
110 }
111
112 /**
113 * Request a record from the DB by id. Success if row not found.
114 */
115 function assertDBRowNotExist(&$testCase, $daoName, $id, $message) {
116 $value = CRM_Core_DAO::getFieldValue($daoName, $id, 'id', 'id');
117 $testCase->assertNull($value, $message);
118 }
119
120 /**
121 * Compare a single column value in a retrieved DB record to an expected value
122 *
123 * @param $testCase
124 * @param $daoName
125 * @param $searchValue
126 * @param $returnColumn
127 * @param $searchColumn
128 * @param $expectedValue
129 * @param string $message
130 */
131 function assertDBCompareValue(&$testCase, $daoName, $searchValue, $returnColumn, $searchColumn,
132 $expectedValue, $message
133 ) {
134 $value = CRM_Core_DAO::getFieldValue($daoName, $searchValue, $returnColumn, $searchColumn);
135 $testCase->assertEquals($value, $expectedValue, $message);
136 }
137
138 /**
139 * Compare all values in a single retrieved DB record to an array of expected values
140 */
141 function assertDBCompareValues(&$testCase, $daoName, $searchParams, $expectedValues) {
142 //get the values from db
143 $dbValues = array();
144 CRM_Core_DAO::commonRetrieve($daoName, $searchParams, $dbValues);
145
146
147 // compare db values with expected values
148 self::assertAttributesEquals($testCase, $expectedValues, $dbValues);
149 }
150
151 /**
152 * @param $testCase
153 * @param $expectedValues
154 * @param $actualValues
155 */
156 function assertAttributesEquals(&$testCase, &$expectedValues, &$actualValues) {
157 foreach ($expectedValues as $paramName => $paramValue) {
158 if (isset($actualValues[$paramName])) {
159 $testCase->assertEquals($paramValue, $actualValues[$paramName]);
160 }
161 else {
162 $testCase->fail("Attribute '$paramName' not present in actual array.");
163 }
164 }
165 }
166 }