Skip to content

Commit 99c4d76

Browse files
committed
[String] wrap(): test and fix
1 parent a306d99 commit 99c4d76

File tree

5 files changed

+59
-13
lines changed

5 files changed

+59
-13
lines changed

src/Symfony/Component/String/AbstractString.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,18 @@ public static function wrap(array $values): array
7575
$keys = null;
7676

7777
foreach ($values as $k => $v) {
78-
++$i;
79-
8078
if (\is_string($k) && '' !== $k && $k !== $j = (string) new static($k)) {
8179
$keys = $keys ?? array_keys($values);
82-
array_splice($keys, $i, 1, [$j]);
80+
$keys[$i] = $j;
8381
}
8482

8583
if (\is_string($v)) {
8684
$values[$k] = new static($v);
8785
} elseif (\is_array($v) && $values[$k] !== $v = static::wrap($v)) {
8886
$values[$k] = $v;
8987
}
88+
89+
++$i;
9090
}
9191

9292
return null !== $keys ? array_combine($keys, $values) : $values;

src/Symfony/Component/String/Tests/AbstractAsciiTestCase.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,18 @@
1111

1212
abstract class AbstractAsciiTestCase extends TestCase
1313
{
14-
protected static function createFromString(string $string): AbstractString
14+
protected static function stringClass(): string
1515
{
1616
throw new \BadMethodCallException('This method must be implemented by subclasses.');
1717
}
1818

19+
protected static function createFromString(string $string): AbstractString
20+
{
21+
$class = static::stringClass();
22+
23+
return new $class($string);
24+
}
25+
1926
public function testCreateFromString()
2027
{
2128
$bytes = static::createFromString('Symfony is a PHP framework!');
@@ -34,6 +41,31 @@ public function testCreateFromEmptyString()
3441
$this->assertTrue($instance->isEmpty());
3542
}
3643

44+
/**
45+
* @dataProvider provideWrap
46+
*/
47+
public function testWrap(array $expected, array $values)
48+
{
49+
$class = static::stringClass();
50+
51+
$this->assertEquals($expected, $class::wrap($values));
52+
}
53+
54+
public static function provideWrap(): array
55+
{
56+
return [
57+
[[], []],
58+
[
59+
['abc' => static::createFromString('foo'), 1, static::createFromString('bar'), 'baz' => true],
60+
['abc' => 'foo', 1, 'bar', 'baz' => true],
61+
],
62+
[
63+
['a' => ['b' => static::createFromString('c'), [static::createFromString('d')]], static::createFromString('e')],
64+
['a' => ['b' => 'c', ['d']], 'e'],
65+
],
66+
];
67+
}
68+
3769
/**
3870
* @dataProvider provideLength
3971
*/

src/Symfony/Component/String/Tests/ByteStringTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@
1111

1212
namespace Symfony\Component\String\Tests;
1313

14-
use Symfony\Component\String\AbstractString;
1514
use Symfony\Component\String\ByteString;
1615

1716
class ByteStringTest extends AbstractAsciiTestCase
1817
{
19-
protected static function createFromString(string $string): AbstractString
18+
protected static function stringClass(): string
2019
{
21-
return new ByteString($string);
20+
return ByteString::class;
2221
}
2322

2423
public static function provideLength(): array

src/Symfony/Component/String/Tests/CodePointStringTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@
1111

1212
namespace Symfony\Component\String\Tests;
1313

14-
use Symfony\Component\String\AbstractString;
1514
use Symfony\Component\String\CodePointString;
1615

1716
class CodePointStringTest extends AbstractUnicodeTestCase
1817
{
19-
protected static function createFromString(string $string): AbstractString
18+
protected static function stringClass(): string
2019
{
21-
return new CodePointString($string);
20+
return CodePointString::class;
2221
}
2322

2423
public static function provideLength(): array

src/Symfony/Component/String/Tests/UnicodeStringTest.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,30 @@
1111

1212
namespace Symfony\Component\String\Tests;
1313

14-
use Symfony\Component\String\AbstractString;
1514
use Symfony\Component\String\UnicodeString;
1615

1716
class UnicodeStringTest extends AbstractUnicodeTestCase
1817
{
19-
protected static function createFromString(string $string): AbstractString
18+
protected static function stringClass(): string
2019
{
21-
return new UnicodeString($string);
20+
return UnicodeString::class;
21+
}
22+
23+
public static function provideWrap(): array
24+
{
25+
return array_merge(
26+
parent::provideWrap(),
27+
[
28+
[
29+
['Käse' => static::createFromString('köstlich'), 'fromage' => static::createFromString('délicieux')],
30+
["Ka\u{0308}se" => "ko\u{0308}stlich", 'fromage' => 'délicieux'],
31+
],
32+
[
33+
['a' => 1, 'ä' => ['ö' => 2, 'ü' => 3]],
34+
['a' => 1, "a\u{0308}" => ["o\u{0308}" => 2, 'ü' => 3]],
35+
],
36+
]
37+
);
2238
}
2339

2440
public static function provideLength(): array

0 commit comments

Comments
 (0)