Kaynağa Gözat

:octocat: rework output interface invocation

smiley 2 yıl önce
ebeveyn
işleme
c474cc704e
2 değiştirilmiş dosya ile 37 ekleme ve 20 silme
  1. 8 16
      src/QRCode.php
  2. 29 4
      tests/QRCodeTest.php

+ 8 - 16
src/QRCode.php

@@ -267,28 +267,20 @@ class QRCode{
 	 * @throws \chillerlan\QRCode\Output\QRCodeOutputException
 	 */
 	protected function initOutputInterface(QRMatrix $matrix):QROutputInterface{
+		$outputInterface = QROutputInterface::MODES[$this->options->outputType] ?? null;
 
 		if($this->options->outputType === QROutputInterface::CUSTOM){
-
-			if(!class_exists($this->options->outputInterface)){
-				throw new QRCodeOutputException('invalid custom output module');
-			}
-
-			if(!in_array(QROutputInterface::class, class_implements($this->options->outputInterface))){
-				throw new QRCodeOutputException('custom output module does not implement QROutputInterface');
-			}
-
-			/** @phan-suppress-next-line PhanTypeExpectedObjectOrClassName */
-			return new $this->options->outputInterface($this->options, $matrix);
+			$outputInterface = $this->options->outputInterface;
 		}
 
-		$outputInterface = QROutputInterface::MODES[$this->options->outputType] ?? false;
-
-		if($outputInterface){
-			return new $outputInterface($this->options, $matrix);
+		if(!$outputInterface || !class_exists($outputInterface)){
+			throw new QRCodeOutputException('invalid output module');
 		}
 
-		throw new QRCodeOutputException('invalid output type');
+		if(!in_array(QROutputInterface::class, class_implements($outputInterface))){
+			throw new QRCodeOutputException('output module does not implement QROutputInterface');
+		}
+		return new $outputInterface($this->options, $matrix);
 	}
 
 	/**

+ 29 - 4
tests/QRCodeTest.php

@@ -11,16 +11,16 @@
 namespace chillerlan\QRCodeTest;
 
 use chillerlan\QRCode\{QROptions, QRCode};
-use chillerlan\QRCode\Data\QRCodeDataException;
-use chillerlan\QRCode\Output\QRCodeOutputException;
+use chillerlan\QRCode\Output\{QRCodeOutputException, QROutputInterface};
 use PHPUnit\Framework\TestCase;
+use stdClass;
 
 /**
  * Tests basic functions of the QRCode class
  */
 final class QRCodeTest extends TestCase{
 
-	private QRCode $qrcode;
+	private QRCode    $qrcode;
 	private QROptions $options;
 
 	/**
@@ -36,11 +36,36 @@ final class QRCodeTest extends TestCase{
 	 */
 	public function testInitOutputInterfaceException():void{
 		$this->expectException(QRCodeOutputException::class);
-		$this->expectExceptionMessage('invalid output type');
+		$this->expectExceptionMessage('invalid output module');
 
 		$this->options->outputType = 'foo';
 
 		(new QRCode($this->options))->render('test');
 	}
 
+	/**
+	 * tests if an exception is thrown if the given output class does not exist
+	 */
+	public function testInitCustomOutputInterfaceNotExistsException():void{
+		$this->expectException(QRCodeOutputException::class);
+		$this->expectExceptionMessage('invalid output module');
+
+		$this->options->outputType = QROutputInterface::CUSTOM;
+
+		(new QRCode($this->options))->render('test');
+	}
+
+	/**
+	 * tests if an exception is thrown if the given output class does not implement QROutputInterface
+	 */
+	public function testInitCustomOutputInterfaceNotImplementsException():void{
+		$this->expectException(QRCodeOutputException::class);
+		$this->expectExceptionMessage('output module does not implement QROutputInterface');
+
+		$this->options->outputType      = QROutputInterface::CUSTOM;
+		$this->options->outputInterface = stdClass::class;
+
+		(new QRCode($this->options))->render('test');
+	}
+
 }