diff --git a/src/Formatter/Cron.php b/src/Formatter/Cron.php index 682bf51..814e8bf 100644 --- a/src/Formatter/Cron.php +++ b/src/Formatter/Cron.php @@ -11,7 +11,9 @@ class Cron public function header(): Header { - $this->header = new Header($this); + if ($this->header === null) { + $this->header = new Header($this); + } return $this->header; } diff --git a/tests/Formatter/CronTest.php b/tests/Formatter/CronTest.php index 2997a66..de084ed 100644 --- a/tests/Formatter/CronTest.php +++ b/tests/Formatter/CronTest.php @@ -65,4 +65,25 @@ public function shouldBuildConfiguration(): void $this->assertEquals($expected, $this->cron->format()); } + + /** @test */ + public function headerShouldReturnSameInstance(): void + { + $h1 = $this->cron->header(); + $h2 = $this->cron->header(); + + $this->assertSame($h1, $h2); + } + + /** @test */ + public function headerShouldNotLoseStateWhenCalledMultipleTimes(): void + { + $this->cron->header()->setPath('path')->end(); + $this->cron->header()->setMailto('test@example.com')->end(); + + $output = $this->cron->format(); + + $this->assertStringContainsString("PATH=path", $output); + $this->assertStringContainsString("MAILTO=test@example.com", $output); + } }