From 670ff73e000e7b2d43f02bcfc96dc2e500877a30 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 28 Apr 2024 09:04:44 +0900 Subject: [PATCH] fix: setup command cannot update Config\Autoload::$helpers with multiple lines --- src/Commands/Setup.php | 16 +++++++++++++--- tests/Commands/SetupTest.php | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 3 deletions(-) diff --git a/src/Commands/Setup.php b/src/Commands/Setup.php index 9d6e629cf..0087649f0 100644 --- a/src/Commands/Setup.php +++ b/src/Commands/Setup.php @@ -256,10 +256,8 @@ private function setAutoloadHelpers(): void $helpers = $config->helpers; $newHelpers = array_unique(array_merge($helpers, ['auth', 'setting'])); - $pattern = '/^ public \$helpers = \[.*\];/mu'; - $replace = ' public $helpers = [\'' . implode("', '", $newHelpers) . '\'];'; $content = file_get_contents($path); - $output = preg_replace($pattern, $replace, $content); + $output = $this->updateAutoloadHelpers($content, $newHelpers); // check if the content is updated if ($output === $content) { @@ -277,6 +275,18 @@ private function setAutoloadHelpers(): void } } + /** + * @param string $content The content of Config\Autoload. + * @param list $newHelpers The list of helpers. + */ + private function updateAutoloadHelpers(string $content, array $newHelpers): string + { + $pattern = '/^ public \$helpers = \[.*?\];/msu'; + $replace = ' public $helpers = [\'' . implode("', '", $newHelpers) . '\'];'; + + return preg_replace($pattern, $replace, $content); + } + private function removeHelperLoadingInBaseController(): void { $file = 'Controllers/BaseController.php'; diff --git a/tests/Commands/SetupTest.php b/tests/Commands/SetupTest.php index 632f604bf..f695cce21 100644 --- a/tests/Commands/SetupTest.php +++ b/tests/Commands/SetupTest.php @@ -131,6 +131,41 @@ public function testRunEmailConfigIsFine(): void ); } + public function testUpdateAutoloadHelpers(): void + { + $command = new Setup(Services::logger(), Services::commands()); + + $updateAutoloadHelpers = $this->getPrivateMethodInvoker($command, 'updateAutoloadHelpers'); + + $content = <<<'EOL' + class Autoload extends AutoloadConfig + { + /** + * ------------------------------------------------------------------- + * Helpers + * ------------------------------------------------------------------- + * Prototype: + * $helpers = [ + * 'form', + * ]; + * + * @var list + */ + public $helpers = [ + 'text', + 'form', + ]; + } + EOL; + $helpers = ['text', 'form', 'auth', 'setting']; + $output = $updateAutoloadHelpers($content, $helpers); + + $this->assertStringContainsString( + "public \$helpers = ['text', 'form', 'auth', 'setting'];", + $output + ); + } + /** * @return string app folder path */