- /**
- * Generic function to compare expected values after an api call to retrieved.
- * DB values.
- *
- * @daoName string DAO Name of object we're evaluating.
- * @id int Id of object
- * @match array Associative array of field name => expected value. Empty if asserting
- * that a DELETE occurred
- * @delete boolean True if we're checking that a DELETE action occurred.
- * @param $daoName
- * @param $id
- * @param $match
- * @param bool $delete
- * @throws \PHPUnit_Framework_AssertionFailedError
- */
- public function assertDBState($daoName, $id, $match, $delete = FALSE) {
- if (empty($id)) {
- // adding this here since developers forget to check for an id
- // and hence we get the first value in the db
- $this->fail('ID not populated. Please fix your assertDBState usage!!!');
- }
-
- $object = new $daoName();
- $object->id = $id;
- $verifiedCount = 0;
-
- // If we're asserting successful record deletion, make sure object is NOT found.
- if ($delete) {
- if ($object->find(TRUE)) {
- $this->fail("Object not deleted by delete operation: $daoName, $id");
- }
- return;
- }
-
- // Otherwise check matches of DAO field values against expected values in $match.
- if ($object->find(TRUE)) {
- $fields = &$object->fields();
- foreach ($fields as $name => $value) {
- $dbName = $value['name'];
- if (isset($match[$name])) {
- $verifiedCount++;
- $this->assertEquals($object->$dbName, $match[$name]);
- }
- elseif (isset($match[$dbName])) {
- $verifiedCount++;
- $this->assertEquals($object->$dbName, $match[$dbName]);
- }
- }
- }
- else {
- $this->fail("Could not retrieve object: $daoName, $id");
- }
- $object->free();
- $matchSize = count($match);
- if ($verifiedCount != $matchSize) {
- $this->fail("Did not verify all fields in match array: $daoName, $id. Verified count = $verifiedCount. Match array size = $matchSize");
- }
- }
-
- /**
- * Request a record from the DB by seachColumn+searchValue. Success if a record is found.
- * @param string $daoName
- * @param $searchValue
- * @param $returnColumn
- * @param $searchColumn
- * @param $message
- *
- * @return null|string
- * @throws PHPUnit_Framework_AssertionFailedError
- */
- public function assertDBNotNull($daoName, $searchValue, $returnColumn, $searchColumn, $message) {
- if (empty($searchValue)) {
- $this->fail("empty value passed to assertDBNotNull");
- }
- $value = CRM_Core_DAO::getFieldValue($daoName, $searchValue, $returnColumn, $searchColumn, TRUE);
- $this->assertNotNull($value, $message);
-
- return $value;
- }
-
- /**
- * Request a record from the DB by seachColumn+searchValue. Success if returnColumn value is NULL.
- * @param string $daoName
- * @param $searchValue
- * @param $returnColumn
- * @param $searchColumn
- * @param $message
- */
- public function assertDBNull($daoName, $searchValue, $returnColumn, $searchColumn, $message) {
- $value = CRM_Core_DAO::getFieldValue($daoName, $searchValue, $returnColumn, $searchColumn, TRUE);
- $this->assertNull($value, $message);
- }
-
- /**
- * Request a record from the DB by id. Success if row not found.
- * @param string $daoName
- * @param int $id
- * @param null $message
- */
- public function assertDBRowNotExist($daoName, $id, $message = NULL) {
- $message = $message ? $message : "$daoName (#$id) should not exist";
- $value = CRM_Core_DAO::getFieldValue($daoName, $id, 'id', 'id', TRUE);
- $this->assertNull($value, $message);
- }
-
- /**
- * Request a record from the DB by id. Success if row not found.
- * @param string $daoName
- * @param int $id
- * @param null $message
- */
- public function assertDBRowExist($daoName, $id, $message = NULL) {
- $message = $message ? $message : "$daoName (#$id) should exist";
- $value = CRM_Core_DAO::getFieldValue($daoName, $id, 'id', 'id', TRUE);
- $this->assertEquals($id, $value, $message);
- }
-
- /**
- * Compare a single column value in a retrieved DB record to an expected value.
- * @param string $daoName
- * @param $searchValue
- * @param $returnColumn
- * @param $searchColumn
- * @param $expectedValue
- * @param $message
- */
- public function assertDBCompareValue(
- $daoName, $searchValue, $returnColumn, $searchColumn,
- $expectedValue, $message
- ) {
- $value = CRM_Core_DAO::getFieldValue($daoName, $searchValue, $returnColumn, $searchColumn, TRUE);
- $this->assertEquals($expectedValue, $value, $message);
- }
-
- /**
- * Compare all values in a single retrieved DB record to an array of expected values.
- * @param string $daoName
- * @param array $searchParams
- * @param $expectedValues
- */
- public function assertDBCompareValues($daoName, $searchParams, $expectedValues) {
- //get the values from db
- $dbValues = array();
- CRM_Core_DAO::commonRetrieve($daoName, $searchParams, $dbValues);
-
- // compare db values with expected values
- self::assertAttributesEquals($expectedValues, $dbValues);
- }
-
- /**
- * Assert that a SQL query returns a given value.
- *
- * The first argument is an expected value. The remaining arguments are passed
- * to CRM_Core_DAO::singleValueQuery
- *
- * Example: $this->assertSql(2, 'select count(*) from foo where foo.bar like "%1"',
- * array(1 => array("Whiz", "String")));
- * @param $expected
- * @param $query
- * @param array $params
- * @param string $message
- */
- public function assertDBQuery($expected, $query, $params = array(), $message = '') {
- if ($message) {
- $message .= ': ';
- }
- $actual = CRM_Core_DAO::singleValueQuery($query, $params);
- $this->assertEquals($expected, $actual,
- sprintf('%sexpected=[%s] actual=[%s] query=[%s]',
- $message, $expected, $actual, CRM_Core_DAO::composeQuery($query, $params, FALSE)
- )
- );
- }
-
- /**
- * Assert that two array-trees are exactly equal, notwithstanding
- * the sorting of keys
- *
- * @param array $expected
- * @param array $actual
- */
- public function assertTreeEquals($expected, $actual) {
- $e = array();
- $a = array();
- CRM_Utils_Array::flatten($expected, $e, '', ':::');
- CRM_Utils_Array::flatten($actual, $a, '', ':::');
- ksort($e);
- ksort($a);
-
- $this->assertEquals($e, $a);
- }
-
- /**
- * Assert that two numbers are approximately equal.
- *
- * @param int|float $expected
- * @param int|float $actual
- * @param int|float $tolerance
- * @param string $message
- */
- public function assertApproxEquals($expected, $actual, $tolerance, $message = NULL) {
- if ($message === NULL) {
- $message = sprintf("approx-equals: expected=[%.3f] actual=[%.3f] tolerance=[%.3f]", $expected, $actual, $tolerance);
- }
- $this->assertTrue(abs($actual - $expected) < $tolerance, $message);
- }
-
- /**
- * Assert attributes are equal.
- *
- * @param $expectedValues
- * @param $actualValues
- * @param string $message
- *
- * @throws PHPUnit_Framework_AssertionFailedError
- */
- public function assertAttributesEquals($expectedValues, $actualValues, $message = NULL) {
- foreach ($expectedValues as $paramName => $paramValue) {
- if (isset($actualValues[$paramName])) {
- $this->assertEquals($paramValue, $actualValues[$paramName], "Value Mismatch On $paramName - value 1 is " . print_r($paramValue, TRUE) . " value 2 is " . print_r($actualValues[$paramName], TRUE));
- }
- else {
- $this->assertNull($expectedValues[$paramName], "Attribute '$paramName' not present in actual array and we expected it to be " . $expectedValues[$paramName]);
- }
- }
- }
-
- /**
- * @param $key
- * @param $list
- */
- public function assertArrayKeyExists($key, &$list) {
- $result = isset($list[$key]) ? TRUE : FALSE;
- $this->assertTrue($result, ts("%1 element exists?",
- array(1 => $key)
- ));
- }
-
- /**
- * @param $key
- * @param $list
- */
- public function assertArrayValueNotNull($key, &$list) {
- $this->assertArrayKeyExists($key, $list);
-
- $value = isset($list[$key]) ? $list[$key] : NULL;
- $this->assertTrue($value,
- ts("%1 element not null?",
- array(1 => $key)
- )
- );
- }
-
- /**
- * Assert the 2 arrays have the same values.
- *
- * @param array $array1
- * @param array $array2
- */
- public function assertArrayValuesEqual($array1, $array2) {
- $array1 = array_values($array1);
- $array2 = array_values($array2);
- sort($array1);
- sort($array2);
- $this->assertEquals($array1, $array2);
- }
-
- /**
- * @param $expected
- * @param $actual
- * @param string $message
- */
- public function assertType($expected, $actual, $message = '') {
- return $this->assertInternalType($expected, $actual, $message);
- }
-