From 8c68b9c9226b597e52c7126fc62def828dd85e07 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Fri, 12 Apr 2019 17:34:56 -0700 Subject: [PATCH] Move helpers from CiviUnitTestCase to DbTestTrait * `assertDBState(...)` * `assertDBNotNull(...)` * `assertDBNull(...)` * `assertDBRowNotExist(...)` * `assertDBRowExist(...)` * `assertDBCompareValue(...)` * `assertDBCompareValues(...)` * `assertDBQuery(...)` --- Civi/Test/DbTestTrait.php | 189 ++++++++++++++++++++ tests/phpunit/CiviTest/CiviUnitTestCase.php | 175 +----------------- 2 files changed, 190 insertions(+), 174 deletions(-) create mode 100644 Civi/Test/DbTestTrait.php diff --git a/Civi/Test/DbTestTrait.php b/Civi/Test/DbTestTrait.php new file mode 100644 index 0000000000..e8f7209e79 --- /dev/null +++ b/Civi/Test/DbTestTrait.php @@ -0,0 +1,189 @@ + 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 + $this->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) + ) + ); + } + +} diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index 754cb16e30..c5e8ff08ce 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -55,6 +55,7 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { use \Civi\Test\Api3DocTrait; use \Civi\Test\GenericAssertionsTrait; + use \Civi\Test\DbTestTrait; use \Civi\Test\ContactTestTrait; /** @@ -414,180 +415,6 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { $this->unsetExtensionSystem(); } - /** - * 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) - ) - ); - } - /** * Create a batch of external API calls which can * be executed concurrently. -- 2.25.1