Skip to content

Commit

Permalink
update test
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonvarga committed May 23, 2024
1 parent 1a56ad6 commit 4bfea93
Showing 1 changed file with 58 additions and 27 deletions.
85 changes: 58 additions & 27 deletions tests/Modifiers/FlattenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,78 @@
use Statamic\Modifiers\Modify;
use Tests\TestCase;

/**
* @todo Add test with numeric indices
*/
class FlattenTest extends TestCase
{
/** @test */
public function it_flattens_a_multidimensional_array(): void
/**
* @test
*
* @dataProvider flattenProvider
*/
public function it_flattens_an_array($depth, $expected): void
{
$input = [
'ingredients' => [
'spices' => ['garlic', 'cumin', 'ginger', 'turmeric', 'paprika', 'curry powder'],
'vegetables' => ['tomatoes', 'onion'],
'meat' => ['chicken'],
[
'foo',
[
'bar',
[
'baz',
],
],
],
'zap',
];

$expected = [
'garlic',
'cumin',
'ginger',
'turmeric',
'paprika',
'curry powder',
'tomatoes',
'onion',
'chicken',
];

$modified = $this->modify($input);
$modified = $this->modify($input, $depth);
$this->assertEquals($expected, $modified);
}

public static function flattenProvider()
{
return [
'depth null' => [null, ['foo', 'bar', 'baz', 'zap']],
'depth 1' => [1, ['foo', ['bar', ['baz']], 'zap']],
'depth 2' => [2, ['foo', 'bar', ['baz'], 'zap']],
'depth 3' => [3, ['foo', 'bar', 'baz', 'zap']],
'depth 4' => [4, ['foo', 'bar', 'baz', 'zap']],
];
}

$expected = [
['garlic', 'cumin', 'ginger', 'turmeric', 'paprika', 'curry powder'],
['tomatoes', 'onion'],
['chicken'],
/**
* @test
*
* @dataProvider flattenWithKeysProvider
*/
public function it_flattens_an_array_with_keys($depth, $expected): void
{
$input = [
[
'foo',
'alfa' => [
'bar',
'bravo' => [
'baz',
],
],
],
'zap',
];

$modified = $this->modify($input, 1);
$modified = $this->modify($input, $depth);
$this->assertEquals($expected, $modified);
}

public static function flattenWithKeysProvider()
{
return [
'depth null' => [null, ['foo', 'bar', 'baz', 'zap']],
'depth 1' => [1, ['foo', ['bar', 'bravo' => ['baz']], 'zap']],
'depth 2' => [2, ['foo', 'bar', ['baz'], 'zap']],
'depth 3' => [3, ['foo', 'bar', 'baz', 'zap']],
'depth 4' => [4, ['foo', 'bar', 'baz', 'zap']],
];
}

private function modify(array $value, $param = null)
{
return Modify::value($value)->flatten($param)->fetch();
Expand Down

0 comments on commit 4bfea93

Please sign in to comment.