diff --git a/test/phpunit/CommonClassTest.class.php b/test/phpunit/CommonClassTest.class.php index 0db9b170871..ee5db8c46d4 100644 --- a/test/phpunit/CommonClassTest.class.php +++ b/test/phpunit/CommonClassTest.class.php @@ -235,6 +235,69 @@ abstract class CommonClassTest extends TestCase } } + + /** + * Call method, even if protected. + * + * @param object $obj Object on which to call method + * @param string $name Method to call + * @param array $args Arguments to provide in method call + * @return mixed Return value + */ + public static function callMethod($obj, $name, array $args = []) + { + $class = new \ReflectionClass($obj); + $method = $class->getMethod($name); + // If PHP is older then 8.1.0 + if (PHP_VERSION_ID < 80100) { + $method->setAccessible(true); + } + return $method->invokeArgs($obj, $args); + } + + + /** + * Compare all public properties values of 2 objects + * + * @param Object $oA Object operand 1 + * @param Object $oB Object operand 2 + * @param boolean $ignoretype False will not report diff if type of value differs + * @param array $fieldstoignorearray Array of fields to ignore in diff + * @return array Array with differences + */ + public function objCompare($oA, $oB, $ignoretype = true, $fieldstoignorearray = array('id')) + { + $retAr = array(); + + if (get_class($oA) !== get_class($oB)) { + $retAr[] = "Supplied objects are not of same class."; + } else { + $oVarsA = get_object_vars($oA); + $oVarsB = get_object_vars($oB); + $aKeys = array_keys($oVarsA); + if (method_exists($oA, 'deprecatedProperties')) { + // Update exclusions + foreach (self::callMethod($oA, 'deprecatedProperties') as $deprecated => $new) { + if (in_array($deprecated, $fieldstoignorearray)) { + $fieldstoignorearray[] = $new; + } + } + } + foreach ($aKeys as $sKey) { + if (in_array($sKey, $fieldstoignorearray)) { + continue; + } + if (! $ignoretype && ($oVarsA[$sKey] !== $oVarsB[$sKey])) { + $retAr[] = get_class($oA).'::'.$sKey.' : '.(is_object($oVarsA[$sKey]) ? get_class($oVarsA[$sKey]) : json_encode($oVarsA[$sKey])).' <> '.(is_object($oVarsB[$sKey]) ? get_class($oVarsB[$sKey]) : json_encode($oVarsB[$sKey])); + } + if ($ignoretype && ($oVarsA[$sKey] != $oVarsB[$sKey])) { + $retAr[] = get_class($oA).'::'.$sKey.' : '.(is_object($oVarsA[$sKey]) ? get_class($oVarsA[$sKey]) : json_encode($oVarsA[$sKey])).' <> '.(is_object($oVarsB[$sKey]) ? get_class($oVarsB[$sKey]) : json_encode($oVarsB[$sKey])); + } + } + } + return $retAr; + } + /** * Map deprecated module names to new module names */ diff --git a/test/phpunit/FactureRecTest.php b/test/phpunit/FactureRecTest.php index 2644c4fe084..801a6656131 100644 --- a/test/phpunit/FactureRecTest.php +++ b/test/phpunit/FactureRecTest.php @@ -1,6 +1,7 @@ * Copyright (C) 2023 Alexandre Janniaux + * Copyright (C) 2024 MDW * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -117,38 +118,4 @@ class FactureRecTest extends CommonClassTest $localobject->note_private = 'New note'; //$localobject->note='New note after update'; } - - /** - * Compare all public properties values of 2 objects - * - * @param Object $oA Object operand 1 - * @param Object $oB Object operand 2 - * @param boolean $ignoretype False will not report diff if type of value differs - * @param array $fieldstoignorearray Array of fields to ignore in diff - * @return array Array with differences - */ - public function objCompare($oA, $oB, $ignoretype = true, $fieldstoignorearray = array('id')) - { - $retAr = array(); - - if (get_class($oA) !== get_class($oB)) { - $retAr[] = "Supplied objects are not of same class."; - } else { - $oVarsA = get_object_vars($oA); - $oVarsB = get_object_vars($oB); - $aKeys = array_keys($oVarsA); - foreach ($aKeys as $sKey) { - if (in_array($sKey, $fieldstoignorearray)) { - continue; - } - if (! $ignoretype && ($oVarsA[$sKey] !== $oVarsB[$sKey])) { - $retAr[] = $sKey.' : '.(is_object($oVarsA[$sKey]) ? get_class($oVarsA[$sKey]) : $oVarsA[$sKey]).' <> '.(is_object($oVarsB[$sKey]) ? get_class($oVarsB[$sKey]) : $oVarsB[$sKey]); - } - if ($ignoretype && ($oVarsA[$sKey] != $oVarsB[$sKey])) { - $retAr[] = $sKey.' : '.(is_object($oVarsA[$sKey]) ? get_class($oVarsA[$sKey]) : $oVarsA[$sKey]).' <> '.(is_object($oVarsB[$sKey]) ? get_class($oVarsB[$sKey]) : $oVarsB[$sKey]); - } - } - } - return $retAr; - } } diff --git a/test/phpunit/FactureTest.php b/test/phpunit/FactureTest.php index e2f9fa5f68c..2fc0bb6d67d 100644 --- a/test/phpunit/FactureTest.php +++ b/test/phpunit/FactureTest.php @@ -2,6 +2,7 @@ /* Copyright (C) 2010 Laurent Destailleur * Copyright (C) 2018 Frédéric France * Copyright (C) 2023 Alexandre Janniaux + * Copyright (C) 2024 MDW * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -155,7 +156,7 @@ class FactureTest extends CommonClassTest $this->assertLessThan($result, 0); - // Test everything are still same than specimen + // Test everything is still the same as specimen $newlocalobject = new Facture($db); $newlocalobject->initAsSpecimen(); $this->changeProperties($newlocalobject); @@ -168,6 +169,7 @@ class FactureTest extends CommonClassTest $localobject, $newlocalobject, true, + // Not comparing: array( 'newref','oldref','id','lines','client','thirdparty','brouillon','user_creation_id','date_creation','date_validation','datem','date_modification', 'ref','statut','status','paye','specimen','ref','actiontypecode','actionmsg2','actionmsg','mode_reglement','cond_reglement', @@ -281,38 +283,4 @@ class FactureTest extends CommonClassTest $localobject->note_private = 'New note'; //$localobject->note='New note after update'; } - - /** - * Compare all public properties values of 2 objects - * - * @param Object $oA Object operand 1 - * @param Object $oB Object operand 2 - * @param boolean $ignoretype False will not report diff if type of value differs - * @param array $fieldstoignorearray Array of fields to ignore in diff - * @return array Array with differences - */ - public function objCompare($oA, $oB, $ignoretype = true, $fieldstoignorearray = array('id')) - { - $retAr = array(); - - if (get_class($oA) !== get_class($oB)) { - $retAr[] = "Supplied objects are not of same class."; - } else { - $oVarsA = get_object_vars($oA); - $oVarsB = get_object_vars($oB); - $aKeys = array_keys($oVarsA); - foreach ($aKeys as $sKey) { - if (in_array($sKey, $fieldstoignorearray)) { - continue; - } - if (! $ignoretype && ($oVarsA[$sKey] !== $oVarsB[$sKey])) { - $retAr[] = $sKey.' : '.(is_object($oVarsA[$sKey]) ? get_class($oVarsA[$sKey]) : $oVarsA[$sKey]).' <> '.(is_object($oVarsB[$sKey]) ? get_class($oVarsB[$sKey]) : $oVarsB[$sKey]); - } - if ($ignoretype && ($oVarsA[$sKey] != $oVarsB[$sKey])) { - $retAr[] = $sKey.' : '.(is_object($oVarsA[$sKey]) ? get_class($oVarsA[$sKey]) : $oVarsA[$sKey]).' <> '.(is_object($oVarsB[$sKey]) ? get_class($oVarsB[$sKey]) : $oVarsB[$sKey]); - } - } - } - return $retAr; - } } diff --git a/test/phpunit/InventoryTest.php b/test/phpunit/InventoryTest.php index 2d9808e12ce..7761608d1a6 100644 --- a/test/phpunit/InventoryTest.php +++ b/test/phpunit/InventoryTest.php @@ -2,6 +2,7 @@ /* Copyright (C) 2010 Laurent Destailleur * Copyright (C) 2018 Frédéric France * Copyright (C) 2023 Alexandre Janniaux + * Copyright (C) 2024 MDW * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -267,38 +268,4 @@ class InventoryTest extends CommonClassTest return $result; } - - /** - * Compare all public properties values of 2 objects - * - * @param Object $oA Object operand 1 - * @param Object $oB Object operand 2 - * @param boolean $ignoretype False will not report diff if type of value differs - * @param array $fieldstoignorearray Array of fields to ignore in diff - * @return array Array with differences - */ - public function objCompare($oA, $oB, $ignoretype = true, $fieldstoignorearray = array('id')) - { - $retAr = array(); - - if (get_class($oA) !== get_class($oB)) { - $retAr[] = "Supplied objects are not of same class."; - } else { - $oVarsA = get_object_vars($oA); - $oVarsB = get_object_vars($oB); - $aKeys = array_keys($oVarsA); - foreach ($aKeys as $sKey) { - if (in_array($sKey, $fieldstoignorearray)) { - continue; - } - if (! $ignoretype && ($oVarsA[$sKey] !== $oVarsB[$sKey])) { - $retAr[] = $sKey.' : '.(is_object($oVarsA[$sKey]) ? get_class($oVarsA[$sKey]) : $oVarsA[$sKey]).' <> '.(is_object($oVarsB[$sKey]) ? get_class($oVarsB[$sKey]) : $oVarsB[$sKey]); - } - if ($ignoretype && ($oVarsA[$sKey] != $oVarsB[$sKey])) { - $retAr[] = $sKey.' : '.(is_object($oVarsA[$sKey]) ? get_class($oVarsA[$sKey]) : $oVarsA[$sKey]).' <> '.(is_object($oVarsB[$sKey]) ? get_class($oVarsB[$sKey]) : $oVarsB[$sKey]); - } - } - } - return $retAr; - } } diff --git a/test/phpunit/UserTest.php b/test/phpunit/UserTest.php index 877d258d60b..d5328982f3a 100644 --- a/test/phpunit/UserTest.php +++ b/test/phpunit/UserTest.php @@ -1,6 +1,7 @@ * Copyright (C) 2023 Alexandre Janniaux + * Copyright (C) 2024 MDW * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -403,38 +404,4 @@ class UserTest extends CommonClassTest { $localobject->note_private = 'New note after update'; } - - /** - * Compare all public properties values of 2 objects - * - * @param Object $oA Object operand 1 - * @param Object $oB Object operand 2 - * @param boolean $ignoretype False will not report diff if type of value differs - * @param array $fieldstoignorearray Array of fields to ignore in diff - * @return array Array with differences - */ - public function objCompare($oA, $oB, $ignoretype = true, $fieldstoignorearray = array('id')) - { - $retAr = array(); - - if (get_class($oA) !== get_class($oB)) { - $retAr[] = "Supplied objects are not of same class."; - } else { - $oVarsA = get_object_vars($oA); - $oVarsB = get_object_vars($oB); - $aKeys = array_keys($oVarsA); - foreach ($aKeys as $sKey) { - if (in_array($sKey, $fieldstoignorearray)) { - continue; - } - if (! $ignoretype && ($oVarsA[$sKey] !== $oVarsB[$sKey])) { - $retAr[] = $sKey.' : '.(is_object($oVarsA[$sKey]) ? get_class($oVarsA[$sKey]) : $oVarsA[$sKey]).' <> '.(is_object($oVarsB[$sKey]) ? get_class($oVarsB[$sKey]) : $oVarsB[$sKey]); - } - if ($ignoretype && ($oVarsA[$sKey] != $oVarsB[$sKey])) { - $retAr[] = $sKey.' : '.(is_object($oVarsA[$sKey]) ? get_class($oVarsA[$sKey]) : $oVarsA[$sKey]).' <> '.(is_object($oVarsB[$sKey]) ? get_class($oVarsB[$sKey]) : $oVarsB[$sKey]); - } - } - } - return $retAr; - } }