Skip to content

Commit

Permalink
Added config sort tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jaxwilko committed Jan 14, 2022
1 parent dcef6b1 commit bae73fa
Showing 1 changed file with 107 additions and 0 deletions.
107 changes: 107 additions & 0 deletions tests/Config/ConfigFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -457,4 +457,111 @@ public function testInsertNull()

$this->assertEquals($expected, $config->render());
}

public function testSortAsc()
{
$file = __DIR__ . '/../fixtures/config/empty.php';
$config = ConfigFile::read($file, true);

$config->set([
'b.b' => 'b',
'b.a' => 'a',
'a.a.b' => 'b',
'a.a.a' => 'a',
'a.c' => 'c',
'a.b' => 'b',
]);

$config->sort();

$expected = <<<PHP
<?php
return [
'a' => [
'a' => [
'a' => 'a',
'b' => 'b',
],
'b' => 'b',
'c' => 'c',
],
'b' => [
'a' => 'a',
'b' => 'b',
],
];
PHP;

$this->assertEquals($expected, $config->render());
}


public function testSortDesc()
{
$file = __DIR__ . '/../fixtures/config/empty.php';
$config = ConfigFile::read($file, true);

$config->set([
'b.a' => 'a',
'a.a.a' => 'a',
'a.a.b' => 'b',
'a.b' => 'b',
'a.c' => 'c',
'b.b' => 'b',
]);

$config->sort(ConfigFile::SORT_DESC);

$expected = <<<PHP
<?php
return [
'b' => [
'b' => 'b',
'a' => 'a',
],
'a' => [
'c' => 'c',
'b' => 'b',
'a' => [
'b' => 'b',
'a' => 'a',
],
],
];
PHP;

$this->assertEquals($expected, $config->render());
}

public function testSortUsort()
{
$file = __DIR__ . '/../fixtures/config/empty.php';
$config = ConfigFile::read($file, true);

$config->set([
'a' => 'a',
'c' => 'c',
'b' => 'b'
]);

$config->sort(function ($a, $b) {
return $a->key->value === 'b' || $b->key->value === 'b' ? 0 : 1;
});

$expected = <<<PHP
<?php
return [
'c' => 'c',
'a' => 'a',
'b' => 'b',
];
PHP;
$this->assertEquals($expected, $config->render());
}
}

0 comments on commit bae73fa

Please sign in to comment.