diff --git a/tests/Config/ConfigFileTest.php b/tests/Config/ConfigFileTest.php index 9e27590f9..47d27e751 100644 --- a/tests/Config/ConfigFileTest.php +++ b/tests/Config/ConfigFileTest.php @@ -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 = << [ + '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 = << [ + '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 = << 'c', + 'a' => 'a', + 'b' => 'b', +]; + +PHP; + $this->assertEquals($expected, $config->render()); + } }