Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: handle numeric key with camel case source key modifier
Co-authored-by: Nathan Boiron <nathan.boiron@gmail.com>
- Loading branch information
Showing
2 changed files
with
128 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
tests/Unit/Mapper/Source/Modifier/CamelCaseKeysTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CuyZ\Valinor\Tests\Unit\Mapper\Source\Modifier; | ||
|
||
use CuyZ\Valinor\Mapper\Source\Modifier\CamelCaseKeys; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class CamelCaseKeysTest extends TestCase | ||
{ | ||
public function test_replace_space(): void | ||
{ | ||
$source = new CamelCaseKeys(['some key' => 'foo']); | ||
|
||
self::assertSame(['someKey' => 'foo'], iterator_to_array($source)); | ||
} | ||
|
||
public function test_replace_dash(): void | ||
{ | ||
$source = new CamelCaseKeys(['some-key' => 'foo']); | ||
|
||
self::assertSame(['someKey' => 'foo'], iterator_to_array($source)); | ||
} | ||
|
||
public function test_replace_underscore(): void | ||
{ | ||
$source = new CamelCaseKeys(['some_key' => 'foo']); | ||
|
||
self::assertSame(['someKey' => 'foo'], iterator_to_array($source)); | ||
} | ||
|
||
public function test_root_path_is_mapped(): void | ||
{ | ||
$source = new CamelCaseKeys(['level-one' => 'bar']); | ||
|
||
self::assertSame(['levelOne' => 'bar'], iterator_to_array($source)); | ||
} | ||
|
||
public function test_sub_path_is_mapped(): void | ||
{ | ||
$source = new CamelCaseKeys([ | ||
'level-one' => [ | ||
'level-two' => 'foo', | ||
], | ||
]); | ||
|
||
self::assertSame([ | ||
'levelOne' => [ | ||
'levelTwo' => 'foo', | ||
], | ||
], iterator_to_array($source)); | ||
} | ||
|
||
public function test_root_iterable_path_is_mapped(): void | ||
{ | ||
$source = new CamelCaseKeys([ | ||
['level-one' => 'foo'], | ||
['level-one' => 'bar'], | ||
]); | ||
|
||
self::assertSame([ | ||
['levelOne' => 'foo'], | ||
['levelOne' => 'bar'], | ||
], iterator_to_array($source)); | ||
} | ||
|
||
public function test_sub_iterable_numeric_path_is_mapped(): void | ||
{ | ||
$source = new CamelCaseKeys([ | ||
'level-one' => [ | ||
['level-two' => 'bar'], | ||
['level-two' => 'buz'], | ||
], | ||
]); | ||
|
||
self::assertSame([ | ||
'levelOne' => [ | ||
['levelTwo' => 'bar'], | ||
['levelTwo' => 'buz'], | ||
], | ||
], iterator_to_array($source)); | ||
} | ||
|
||
public function test_sub_iterable_string_path_is_mapped(): void | ||
{ | ||
$source = new CamelCaseKeys([ | ||
'level-one' => [ | ||
'level-two-a' => ['level-three' => 'bar'], | ||
'level-two-b' => ['level-three' => 'buz'], | ||
], | ||
]); | ||
|
||
self::assertSame([ | ||
'levelOne' => [ | ||
'levelTwoA' => ['levelThree' => 'bar'], | ||
'levelTwoB' => ['levelThree' => 'buz'], | ||
], | ||
], iterator_to_array($source)); | ||
} | ||
|
||
public function test_path_with_sub_paths_are_mapped(): void | ||
{ | ||
$source = new CamelCaseKeys([ | ||
'level-one' => [ | ||
['level-two' => 'bar'], | ||
['level-two' => 'buz'], | ||
], | ||
]); | ||
|
||
self::assertSame([ | ||
'levelOne' => [ | ||
['levelTwo' => 'bar'], | ||
['levelTwo' => 'buz'], | ||
], | ||
], iterator_to_array($source)); | ||
} | ||
} |