From e965b6ff0a8d0657e736e5b2b59ddb0fb943326c Mon Sep 17 00:00:00 2001 From: Rich Lott / Artful Robot Date: Wed, 9 Nov 2022 09:19:51 +0000 Subject: [PATCH] PropertyBag::setBillingCountry - Change from log warning to deprecation messsage --- Civi/Payment/PropertyBag.php | 24 ++++++++++++------- .../phpunit/Civi/Payment/PropertyBagTest.php | 6 +++++ 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/Civi/Payment/PropertyBag.php b/Civi/Payment/PropertyBag.php index c190258a14..e82be81b23 100644 --- a/Civi/Payment/PropertyBag.php +++ b/Civi/Payment/PropertyBag.php @@ -91,6 +91,15 @@ class PropertyBag implements \ArrayAccess { */ public $logs = []; + /** + * For unit tests only. Set to the name of a function, e.g. setBillingCountry + * to suppress calling CRM_Core_Error::deprecatedWarning which will break tests. + * Useful when a test is testing THAT a deprecatedWarning is thrown. + * + * @var string + */ + public string $ignoreDeprecatedWarningsInFunction = ''; + /** * @var bool * Temporary, internal variable to help ease transition to PropertyBag. @@ -698,7 +707,12 @@ class PropertyBag implements \ArrayAccess { if ($warnings) { $warnings[] = "Input: " . json_encode($input) . " was munged to: " . json_encode($munged); - $this->logWarning(__FUNCTION__ . " input warnings (may be errors in future):\n" . implode("\n", $warnings)); + $warnings = "PropertyBag::setBillingCountry input warnings (may be errors in future):\n" . implode("\n", $warnings); + $this->logs[] = $warnings; + // Emit a deprecatedWarning except in the case that we're testing this function. + if (__FUNCTION__ !== $this->ignoreDeprecatedWarningsInFunction) { + CRM_Core_Error::deprecatedWarning($warnings); + } } return $this->set('billingCountry', $label, $munged); @@ -1257,12 +1271,4 @@ class PropertyBag implements \ArrayAccess { $this->props[$label][$prop] = $value; } - /** - * For unit tests only. - */ - protected function logWarning($message) { - $this->logs[] = $message; - \Civi::log()->warning($message); - } - } diff --git a/tests/phpunit/Civi/Payment/PropertyBagTest.php b/tests/phpunit/Civi/Payment/PropertyBagTest.php index e26a466b99..0602ca5e5e 100644 --- a/tests/phpunit/Civi/Payment/PropertyBagTest.php +++ b/tests/phpunit/Civi/Payment/PropertyBagTest.php @@ -463,6 +463,7 @@ class PropertyBagTest extends \PHPUnit\Framework\TestCase implements HeadlessInt // Many sorts of nothingness foreach ([NULL, 0, FALSE] as $bad) { $propertyBag = new PropertyBag(); + $propertyBag->ignoreDeprecatedWarningsInFunction = 'setBillingCountry'; $propertyBag->setBillingCountry($bad); $this->assertCount(1, $propertyBag->logs); $latestLog = end($propertyBag->logs); @@ -472,6 +473,7 @@ class PropertyBagTest extends \PHPUnit\Framework\TestCase implements HeadlessInt // '' special case $propertyBag = new PropertyBag(); + $propertyBag->ignoreDeprecatedWarningsInFunction = 'setBillingCountry'; $propertyBag->setBillingCountry(''); $this->assertCount(1, $propertyBag->logs); $latestLog = end($propertyBag->logs); @@ -480,6 +482,7 @@ class PropertyBagTest extends \PHPUnit\Framework\TestCase implements HeadlessInt // Invalid country name $propertyBag = new PropertyBag(); + $propertyBag->ignoreDeprecatedWarningsInFunction = 'setBillingCountry'; $propertyBag->setBillingCountry('UnitedKing'); $this->assertCount(1, $propertyBag->logs); $latestLog = end($propertyBag->logs); @@ -488,6 +491,7 @@ class PropertyBagTest extends \PHPUnit\Framework\TestCase implements HeadlessInt // Valid country name $propertyBag = new PropertyBag(); + $propertyBag->ignoreDeprecatedWarningsInFunction = 'setBillingCountry'; $propertyBag->setBillingCountry('United Kingdom'); $this->assertCount(1, $propertyBag->logs); $latestLog = end($propertyBag->logs); @@ -496,6 +500,7 @@ class PropertyBagTest extends \PHPUnit\Framework\TestCase implements HeadlessInt // Invalid country ID $propertyBag = new PropertyBag(); + $propertyBag->ignoreDeprecatedWarningsInFunction = 'setBillingCountry'; $propertyBag->setBillingCountry(-1); $this->assertCount(1, $propertyBag->logs); $latestLog = end($propertyBag->logs); @@ -504,6 +509,7 @@ class PropertyBagTest extends \PHPUnit\Framework\TestCase implements HeadlessInt // Valid country ID $propertyBag = new PropertyBag(); + $propertyBag->ignoreDeprecatedWarningsInFunction = 'setBillingCountry'; $propertyBag->setBillingCountry(1154); /* should be New Zealand */ $this->assertCount(1, $propertyBag->logs); $latestLog = end($propertyBag->logs); -- 2.25.1