diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e918b15..fb245926 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,11 +2,17 @@ All notable changes to `Browsershot` will be documented in this file + +### 3.11.1 - 2017-11-18 + +- improve error handling for when no extension is provided + ### 3.11.0 - 2017-11-16 - add `setOption` - refactor internals + ### 3.10.0 - 2017-11-13 - add `setProxyServer` diff --git a/src/Browsershot.php b/src/Browsershot.php index 0561acc2..2063cb1b 100644 --- a/src/Browsershot.php +++ b/src/Browsershot.php @@ -252,7 +252,13 @@ public function __call($name, $arguments) public function save(string $targetPath) { - if (strtolower(pathinfo($targetPath, PATHINFO_EXTENSION)) === 'pdf') { + $extension = strtolower(pathinfo($targetPath, PATHINFO_EXTENSION)); + + if ($extension === '') { + throw CouldNotTakeBrowsershot::outputFileDidNotHaveAnExtension($targetPath); + } + + if ($extension === 'pdf') { return $this->savePdf($targetPath); } diff --git a/src/Exceptions/CouldNotTakeBrowsershot.php b/src/Exceptions/CouldNotTakeBrowsershot.php index 0ab55c89..66f7ae2f 100644 --- a/src/Exceptions/CouldNotTakeBrowsershot.php +++ b/src/Exceptions/CouldNotTakeBrowsershot.php @@ -10,4 +10,9 @@ public static function chromeOutputEmpty(string $screenShotPath) { return new static("For some reason Chrome did not write a file at `{$screenShotPath}`."); } + + public static function outputFileDidNotHaveAnExtension(string $path) + { + return new static("The given path `{$path}` did not contain an extension. Please append an extension."); + } } diff --git a/tests/BrowsershotTest.php b/tests/BrowsershotTest.php index 42fe8b39..71b29310 100644 --- a/tests/BrowsershotTest.php +++ b/tests/BrowsershotTest.php @@ -3,6 +3,7 @@ namespace Spatie\Browsershot\Test; use Spatie\Browsershot\Browsershot; +use Spatie\Browsershot\Exceptions\CouldNotTakeBrowsershot; use Symfony\Component\Process\Exception\ProcessFailedException; class BrowsershotTest extends TestCase @@ -55,6 +56,17 @@ public function it_can_take_a_high_density_screenshot() $this->assertFileExists($targetPath); } + /** @test */ + public function it_cannot_save_without_an_extension() + { + $this->expectException(CouldNotTakeBrowsershot::class); + + $targetPath = __DIR__.'/temp/testScreenshot'; + + Browsershot::url('https://example.com') + ->save($targetPath); + } + /** @test */ public function it_can_take_a_full_page_screenshot() {