From d4d94cd287c268ff632bd758dac840602fd0f201 Mon Sep 17 00:00:00 2001 From: Vincent Terraillon Date: Sun, 23 Apr 2023 22:08:19 +0200 Subject: [PATCH] Fix key matching (need to match whole key) Added test to avoid regression --- src/Updater/CronUpdater.php | 2 +- tests/Updater/CronUpdaterTest.php | 36 +++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/Updater/CronUpdater.php b/src/Updater/CronUpdater.php index 29cf4bd..4fd9810 100644 --- a/src/Updater/CronUpdater.php +++ b/src/Updater/CronUpdater.php @@ -32,7 +32,7 @@ private function updateContent(Cron $cron, string $key): string $content = $this->cronManipulator->getContent(); $count = 0; - $pattern = '/\r?\n' . $this->beginKey($key) . '.*?' . self::KEY_END . '/s'; + $pattern = '/\r?\n' . $this->beginKey($key) . '\r?\n.*?' . self::KEY_END . '/s'; $replacedContent = \preg_replace($pattern, $this->wrapInKey($cron, $key), $content, -1, $count); if ($count > 0) { diff --git a/tests/Updater/CronUpdaterTest.php b/tests/Updater/CronUpdaterTest.php index 2ec3017..ad96751 100644 --- a/tests/Updater/CronUpdaterTest.php +++ b/tests/Updater/CronUpdaterTest.php @@ -101,6 +101,42 @@ public function shouldReplaceKeyIfExist(): void foo # END +CRON; + + $this->assertEquals($expectedCron, $this->manipulatorStub->contents); + } + + /** + * @test + */ + public function shouldReplaceExactKeyOnly(): void + { + $this->manipulatorStub->contents = <<comment('replace'); + $this->updater->updateWith($cron, 'key1'); + + $expectedCron = <<assertEquals($expectedCron, $this->manipulatorStub->contents);