Skip to content

Commit 83d76db

Browse files
bug #33970 [String] wrap(): test and fix (gharlan)
This PR was merged into the 5.0-dev branch. Discussion ---------- [String] wrap(): test and fix | Q | A | ------------- | --- | Branch? | master | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | n/a | License | MIT | Doc PR | symfony/symfony-docs/pull/12440 Commits ------- 015b81a2f5 [String] wrap(): test and fix
2 parents 87fdbf8 + 65ca78e commit 83d76db

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

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;

Tests/AbstractAsciiTestCase.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,31 @@ public function testCreateFromEmptyString()
3434
$this->assertTrue($instance->isEmpty());
3535
}
3636

37+
/**
38+
* @dataProvider provideWrap
39+
*/
40+
public function testWrap(array $expected, array $values)
41+
{
42+
$s = static::createFromString('');
43+
44+
$this->assertEquals($expected, $s::wrap($values));
45+
}
46+
47+
public static function provideWrap(): array
48+
{
49+
return [
50+
[[], []],
51+
[
52+
['abc' => static::createFromString('foo'), 1, static::createFromString('bar'), 'baz' => true],
53+
['abc' => 'foo', 1, 'bar', 'baz' => true],
54+
],
55+
[
56+
['a' => ['b' => static::createFromString('c'), [static::createFromString('d')]], static::createFromString('e')],
57+
['a' => ['b' => 'c', ['d']], 'e'],
58+
],
59+
];
60+
}
61+
3762
/**
3863
* @dataProvider provideLength
3964
*/

Tests/UnicodeStringTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,23 @@ protected static function createFromString(string $string): AbstractString
2121
return new UnicodeString($string);
2222
}
2323

24+
public static function provideWrap(): array
25+
{
26+
return array_merge(
27+
parent::provideWrap(),
28+
[
29+
[
30+
['Käse' => static::createFromString('köstlich'), 'fromage' => static::createFromString('délicieux')],
31+
["Ka\u{0308}se" => "ko\u{0308}stlich", 'fromage' => 'délicieux'],
32+
],
33+
[
34+
['a' => 1, 'ä' => ['ö' => 2, 'ü' => 3]],
35+
['a' => 1, "a\u{0308}" => ["o\u{0308}" => 2, 'ü' => 3]],
36+
],
37+
]
38+
);
39+
}
40+
2441
public static function provideLength(): array
2542
{
2643
return array_merge(

0 commit comments

Comments
 (0)