From 1426d341e65f955fc46f0ab26c5f91e31a1dceb8 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Fri, 12 Apr 2019 17:32:15 -0700 Subject: [PATCH] Move helpers from CiviUnitTestCase to GenericAssertionsTrait * `assertType(...)` * `assertTreeEquals(...)` * `assertApproxEquals(...)` * `assertAttributesEquals(...)` * `assertArrayKeyExists(...)` * `assertArrayValueNotNull(...)` * `assertArrayValuesEqual(...)` --- Civi/Test/GenericAssertionsTrait.php | 116 ++++++++++++++++++++ tests/phpunit/CiviTest/CiviUnitTestCase.php | 103 +---------------- 2 files changed, 117 insertions(+), 102 deletions(-) create mode 100644 Civi/Test/GenericAssertionsTrait.php diff --git a/Civi/Test/GenericAssertionsTrait.php b/Civi/Test/GenericAssertionsTrait.php new file mode 100644 index 0000000000..1e969f7c39 --- /dev/null +++ b/Civi/Test/GenericAssertionsTrait.php @@ -0,0 +1,116 @@ +assertInternalType($expected, $actual, $message); + } + + /** + * 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); + } + +} diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index 5361420e6d..754cb16e30 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -54,6 +54,7 @@ define('API_LATEST_VERSION', 3); class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { use \Civi\Test\Api3DocTrait; + use \Civi\Test\GenericAssertionsTrait; use \Civi\Test\ContactTestTrait; /** @@ -587,108 +588,6 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { ); } - /** - * 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); - } - /** * Create a batch of external API calls which can * be executed concurrently. -- 2.25.1