Skip to content

Commit

Permalink
Enable/Disable Exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Progi1984 committed Sep 9, 2024
1 parent 59f639a commit 59f8673
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 21 deletions.
19 changes: 11 additions & 8 deletions src/WMF/Reader/WMF/GD.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use GdImage;
use PhpOffice\WMF\Exception\WMFException;

class GD implements ReaderInterface
class GD extends ReaderAbstract
{
/**
* @phpstan-ignore-next-line
Expand Down Expand Up @@ -255,7 +255,11 @@ public function load(string $filename): bool
}
break;
default:
throw new WMFException('Reader : Function not implemented : 0x' . str_pad(dechex($recordType), 4, '0', STR_PAD_LEFT));
if ($this->hasExceptionsEnabled()) {
throw new WMFException('Reader : Function not implemented : 0x' . str_pad(dechex($recordType), 4, '0', STR_PAD_LEFT));
} else {
return false;
}
}
}

Expand Down Expand Up @@ -356,12 +360,11 @@ public function save(string $filename, string $format): bool
case 'wbmp':
return imagewbmp($this->getResource(), $filename);
default:
throw new WMFException(sprintf('Format %s not supported', $format));
if ($this->hasExceptionsEnabled()) {
throw new WMFException(sprintf('Format %s not supported', $format));
} else {
return false;
}
}
}

public function getMediaType(): string
{
return 'image/wmf';
}
}
19 changes: 11 additions & 8 deletions src/WMF/Reader/WMF/Imagick.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use ImagickException;
use PhpOffice\WMF\Exception\WMFException;

class Imagick implements ReaderInterface
class Imagick extends ReaderAbstract
{
/**
* @var ImagickBase
Expand All @@ -24,7 +24,11 @@ public function load(string $filename): bool
} catch (ImagickException $e) {
$this->im->clear();

throw new WMFException('Cannot load WMG File from Imagick');
if ($this->hasExceptionsEnabled()) {
throw new WMFException('Cannot load WMG File from Imagick');
} else {
return false;
}
}
}

Expand All @@ -41,11 +45,6 @@ public function getResource(): ImagickBase
return $this->im;
}

public function getMediaType(): string
{
return 'image/wmf';
}

public function save(string $filename, string $format): bool
{
switch (strtolower($format)) {
Expand All @@ -59,7 +58,11 @@ public function save(string $filename, string $format): bool

return $this->getResource()->writeImage($filename);
default:
throw new WMFException(sprintf('Format %s not supported', $format));
if ($this->hasExceptionsEnabled()) {
throw new WMFException(sprintf('Format %s not supported', $format));
} else {
return false;
}
}
}
}
2 changes: 1 addition & 1 deletion src/WMF/Reader/WMF/Magic.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Imagick as ImagickBase;
use PhpOffice\WMF\Reader\WMF\Imagick as ImagickReader;

class Magic implements ReaderInterface
class Magic extends ReaderAbstract
{
/**
* @var ReaderInterface
Expand Down
38 changes: 38 additions & 0 deletions src/WMF/Reader/WMF/ReaderAbstract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace PhpOffice\WMF\Reader\WMF;

use GdImage;
use PhpOffice\WMF\Exception\WMFException;

abstract class ReaderAbstract implements ReaderInterface
{
protected $hasExceptionsEnabled = true;

Check failure on line 12 in src/WMF/Reader/WMF/ReaderAbstract.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (7.1)

Property PhpOffice\WMF\Reader\WMF\ReaderAbstract::$hasExceptionsEnabled has no type specified.

Check failure on line 12 in src/WMF/Reader/WMF/ReaderAbstract.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (7.2)

Property PhpOffice\WMF\Reader\WMF\ReaderAbstract::$hasExceptionsEnabled has no type specified.

Check failure on line 12 in src/WMF/Reader/WMF/ReaderAbstract.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (7.3)

Property PhpOffice\WMF\Reader\WMF\ReaderAbstract::$hasExceptionsEnabled has no type specified.

Check failure on line 12 in src/WMF/Reader/WMF/ReaderAbstract.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (7.4)

Property PhpOffice\WMF\Reader\WMF\ReaderAbstract::$hasExceptionsEnabled has no type specified.

Check failure on line 12 in src/WMF/Reader/WMF/ReaderAbstract.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.0)

Property PhpOffice\WMF\Reader\WMF\ReaderAbstract::$hasExceptionsEnabled has no type specified.

Check failure on line 12 in src/WMF/Reader/WMF/ReaderAbstract.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.1)

Property PhpOffice\WMF\Reader\WMF\ReaderAbstract::$hasExceptionsEnabled has no type specified.

Check failure on line 12 in src/WMF/Reader/WMF/ReaderAbstract.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.2)

Property PhpOffice\WMF\Reader\WMF\ReaderAbstract::$hasExceptionsEnabled has no type specified.

Check failure on line 12 in src/WMF/Reader/WMF/ReaderAbstract.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.3)

Property PhpOffice\WMF\Reader\WMF\ReaderAbstract::$hasExceptionsEnabled has no type specified.

/**
* Enable/Disable throwing exceptions
*
* By default, it's enabled
*/
public function enableExceptions(bool $enable): self
{
$this->hasExceptionsEnabled = $enable;

return $this;
}

/**
* Returns if exceptions are thrown
*/
public function hasExceptionsEnabled(): bool
{
return $this->hasExceptionsEnabled;
}

public function getMediaType(): string
{
return 'image/wmf';
}
}
37 changes: 35 additions & 2 deletions tests/WMF/Reader/WMF/GDTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,36 @@ public function testOutput(string $file): void

$reader = new GD();
$reader->load($this->getResourceDir() . $file);
$reader->save($outputFile, 'png');
$this->assertTrue($reader->save($outputFile, 'png'));

$this->assertImageCompare($outputFile, $similarFile, 0.02);

@unlink($outputFile);
}

public function testSaveWithException(): void
{
$file = 'vegetable.wmf';
$outputFile = $this->getResourceDir() . 'output_' . pathinfo($file, PATHINFO_FILENAME) . '.png';

$this->expectException(WMFException::class);

$reader = new GD();
$reader->load($this->getResourceDir() . $file);
$reader->save($outputFile, 'notanextension');
}

public function testSaveWithoutException(): void
{
$file = 'vegetable.wmf';
$outputFile = $this->getResourceDir() . 'output_' . pathinfo($file, PATHINFO_FILENAME) . '.png';

$reader = new GD();
$reader->enableExceptions(false);
$reader->load($this->getResourceDir() . $file);
$this->assertFalse($reader->save($outputFile, 'notanextension'));
}

/**
* @dataProvider dataProviderFilesWMF
*/
Expand All @@ -64,11 +87,21 @@ public function testIsWMF(string $file): void
/**
* @dataProvider dataProviderFilesWMFNotImplemented
*/
public function testNotImplemented(string $file): void
public function testNotImplementedWithExceptions(string $file): void
{
$this->expectException(WMFException::class);

$reader = new GD();
$reader->load($this->getResourceDir() . $file);
}

/**
* @dataProvider dataProviderFilesWMFNotImplemented
*/
public function testNotImplementedWithoutExceptions(string $file): void
{
$reader = new GD();
$reader->enableExceptions(false);
$this->assertFalse($reader->load($this->getResourceDir() . $file));
}
}
37 changes: 35 additions & 2 deletions tests/WMF/Reader/WMF/ImagickTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,36 @@ public function testOutput(string $file): void

$reader = new ImagickReader();
$reader->load($this->getResourceDir() . $file);
$reader->save($outputFile, 'png');
$this->assertTrue($reader->save($outputFile, 'png'));

$this->assertImageCompare($outputFile, $similarFile);

@unlink($outputFile);
}

public function testSaveWithException(): void
{
$file = 'vegetable.wmf';
$outputFile = $this->getResourceDir() . 'output_' . pathinfo($file, PATHINFO_FILENAME) . '.png';

$this->expectException(WMFException::class);

$reader = new ImagickReader();
$reader->load($this->getResourceDir() . $file);
$reader->save($outputFile, 'notanextension');
}

public function testSaveWithoutException(): void
{
$file = 'vegetable.wmf';
$outputFile = $this->getResourceDir() . 'output_' . pathinfo($file, PATHINFO_FILENAME) . '.png';

$reader = new ImagickReader();
$reader->enableExceptions(false);
$reader->load($this->getResourceDir() . $file);
$this->assertFalse($reader->save($outputFile, 'notanextension'));
}

/**
* @dataProvider dataProviderFilesWMF
*/
Expand All @@ -59,11 +82,21 @@ public function testIsWMF(string $file): void
/**
* @dataProvider dataProviderFilesWMFNotImplemented
*/
public function testNotImplemented(string $file): void
public function testNotImplementedWithExceptions(string $file): void
{
$this->expectException(WMFException::class);

$reader = new ImagickReader();
$reader->load($this->getResourceDir() . $file);
}

/**
* @dataProvider dataProviderFilesWMFNotImplemented
*/
public function testNotImplementedWithoutExceptions(string $file): void
{
$reader = new ImagickReader();
$reader->enableExceptions(false);
$this->assertFalse($reader->load($this->getResourceDir() . $file));
}
}

0 comments on commit 59f8673

Please sign in to comment.