From 4dfc0b23e58ae0060af6bf72af6e80c557848386 Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Tue, 22 Sep 2020 08:53:15 -0400 Subject: [PATCH] [String] allow passing null to string functions --- Resources/functions.php | 12 ++++++----- Tests/FunctionsTest.php | 46 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/Resources/functions.php b/Resources/functions.php index 69d90e3..a60e2ce 100644 --- a/Resources/functions.php +++ b/Resources/functions.php @@ -11,20 +11,22 @@ namespace Symfony\Component\String; -function u(string $string = ''): UnicodeString +function u(?string $string = ''): UnicodeString { - return new UnicodeString($string); + return new UnicodeString($string ?? ''); } -function b(string $string = ''): ByteString +function b(?string $string = ''): ByteString { - return new ByteString($string); + return new ByteString($string ?? ''); } /** * @return UnicodeString|ByteString */ -function s(string $string): AbstractString +function s(?string $string = ''): AbstractString { + $string = $string ?? ''; + return preg_match('//u', $string) ? new UnicodeString($string) : new ByteString($string); } diff --git a/Tests/FunctionsTest.php b/Tests/FunctionsTest.php index 28d0d56..1710edd 100644 --- a/Tests/FunctionsTest.php +++ b/Tests/FunctionsTest.php @@ -13,27 +13,67 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\String\AbstractString; +use function Symfony\Component\String\b; use Symfony\Component\String\ByteString; use function Symfony\Component\String\s; +use function Symfony\Component\String\u; use Symfony\Component\String\UnicodeString; final class FunctionsTest extends TestCase { /** - * @dataProvider provideStrings + * @dataProvider provideSStrings */ - public function testS(AbstractString $expected, string $input) + public function testS(AbstractString $expected, ?string $input) { $this->assertEquals($expected, s($input)); } - public function provideStrings(): array + public function provideSStrings(): array { return [ + [new UnicodeString(''), ''], + [new UnicodeString(''), null], [new UnicodeString('foo'), 'foo'], [new UnicodeString('अनुच्छेद'), 'अनुच्छेद'], [new ByteString("b\x80ar"), "b\x80ar"], [new ByteString("\xfe\xff"), "\xfe\xff"], ]; } + + /** + * @dataProvider provideUStrings + */ + public function testU(UnicodeString $expected, ?string $input) + { + $this->assertEquals($expected, u($input)); + } + + public function provideUStrings(): array + { + return [ + [new UnicodeString(''), ''], + [new UnicodeString(''), null], + [new UnicodeString('foo'), 'foo'], + [new UnicodeString('अनुच्छेद'), 'अनुच्छेद'], + ]; + } + + /** + * @dataProvider provideBStrings + */ + public function testB(ByteString $expected, ?string $input) + { + $this->assertEquals($expected, b($input)); + } + + public function provideBStrings(): array + { + return [ + [new ByteString(''), ''], + [new ByteString(''), null], + [new ByteString("b\x80ar"), "b\x80ar"], + [new ByteString("\xfe\xff"), "\xfe\xff"], + ]; + } }