diff --git a/CHANGELOG.md b/CHANGELOG.md index bd011a9..f311c9f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,17 @@ All Notable changes to `Redsys` will be documented in this file +## Version 1.5.0 (2025-01-16) +### Added +- None + +### Changed +- Updated isEmpty method to properly handle non-string values + +### Fixed +- Fixed deprecated warning in isEmpty method when passing null values to trim(). +- Fixed incorrect validation of boolean values in isEmpty method + ## Version 1.4.9 (2025-01-11) ### Added - None diff --git a/src/Sermepa/Tpv/Tpv.php b/src/Sermepa/Tpv/Tpv.php index 1e0adb0..47b9955 100644 --- a/src/Sermepa/Tpv/Tpv.php +++ b/src/Sermepa/Tpv/Tpv.php @@ -1076,7 +1076,11 @@ protected function decodeParameters($data) */ protected function isEmpty($value) { - return '' === trim($value); + if ($value === null) { + return true; + } + + return is_string($value) && '' === trim($value); } /** diff --git a/tests/TpvTest.php b/tests/TpvTest.php index 0bdaa4a..33b5383 100644 --- a/tests/TpvTest.php +++ b/tests/TpvTest.php @@ -2,11 +2,76 @@ namespace Sermepa\Tpv; +use ReflectionClass; use PHPUnit\Framework\TestCase as PHPUnitTestCase; class TpvTest extends PHPUnitTestCase { + private $isEmptyMethod; + private $redsys; + + protected function setUp(): void + { + $this->redsys = new Tpv(); + $reflection = new ReflectionClass(Tpv::class); + $this->isEmptyMethod = $reflection->getMethod('isEmpty'); + $this->isEmptyMethod->setAccessible(true); + } + + /** + * Data provider for the isEmpty method + * + * @return array Array of empty values + */ + public function emptyValuesProvider() + { + return [ + 'null value' => [null], + 'empty string' => [''], + 'whitespace string' => [' '], + 'tabs' => ["\t\t"], + 'newlines' => ["\n\r"], + ]; + } + + /** + * @test + * @dataProvider emptyValuesProvider + */ + public function isEmpty_should_handle_empty_values($value) + { + $result = $this->isEmptyMethod->invoke($this->redsys, $value); + $this->assertTrue($result); + } + + /** + * Data provider for the isEmpty method + * + * @return array Array of empty values + */ + public function nonEmptyValuesProvider() + { + return [ + 'boolean true' => [true], + 'boolean false' => [false], + 'empty array' => [[]], + 'zero as string' => ['0'], + 'zero as integer' => [0], + 'normal string' => ['test'], + ]; + } + + /** + * @test + * @dataProvider nonEmptyValuesProvider + */ + public function isEmpty_returns_false_for_non_empty_values($value) + { + $result = $this->isEmptyMethod->invoke($this->redsys, $value); + $this->assertFalse($result); + } + /** @test */ public function identifier_by_default_required() { @@ -650,7 +715,6 @@ public function test_should_validate_a_merchant_cof_ini($cofIni) $parameters = $redsys->getParameters(); $this->assertArrayHasKey('DS_MERCHANT_COF_INI', $parameters); $this->assertContains($parameters['DS_MERCHANT_COF_INI'], [$cofIni]); - } public function invalidSetMerchantCofIni()