فهرست منبع

added string output type exception test

smiley 10 سال پیش
والد
کامیت
645e571d4f
2فایلهای تغییر یافته به همراه16 افزوده شده و 10 حذف شده
  1. 10 9
      src/Output/QRString.php
  2. 6 1
      tests/Output/StringTest.php

+ 10 - 9
src/Output/QRString.php

@@ -25,6 +25,7 @@ class QRString extends QROutputBase implements QROutputInterface{
 
 
 	/**
 	/**
 	 * @var \chillerlan\QRCode\Output\QRStringOptions $outputOptions
 	 * @var \chillerlan\QRCode\Output\QRStringOptions $outputOptions
+	 * @throws \chillerlan\QRCode\Output\QRCodeOutputException
 	 */
 	 */
 	public function __construct(QRStringOptions $outputOptions = null){
 	public function __construct(QRStringOptions $outputOptions = null){
 		$this->options = $outputOptions;
 		$this->options = $outputOptions;
@@ -33,6 +34,10 @@ class QRString extends QROutputBase implements QROutputInterface{
 			$this->options = new QRStringOptions;
 			$this->options = new QRStringOptions;
 		}
 		}
 
 
+		if(!in_array($this->options->type ,[QRCode::OUTPUT_STRING_TEXT, QRCode::OUTPUT_STRING_JSON, QRCode::OUTPUT_STRING_HTML])){
+			throw new QRCodeOutputException('Invalid string output type!');
+		}
+
 	}
 	}
 
 
 	/**
 	/**
@@ -40,15 +45,11 @@ class QRString extends QROutputBase implements QROutputInterface{
 	 * @throws \chillerlan\QRCode\Output\QRCodeOutputException
 	 * @throws \chillerlan\QRCode\Output\QRCodeOutputException
 	 */
 	 */
 	public function dump(){
 	public function dump(){
-
-		switch($this->options->type){
-			case QRCode::OUTPUT_STRING_TEXT: return $this->toText();
-			case QRCode::OUTPUT_STRING_JSON: return $this->toJSON();
-			case QRCode::OUTPUT_STRING_HTML: return $this->toHTML();
-			default:
-				throw new QRCodeOutputException('Invalid string output type!');
-		}
-
+		return call_user_func([$this, [
+			QRCode::OUTPUT_STRING_TEXT => 'toText',
+			QRCode::OUTPUT_STRING_JSON => 'toJSON',
+			QRCode::OUTPUT_STRING_HTML => 'toHTML',
+		][$this->options->type]]);
 	}
 	}
 
 
 	/**
 	/**

+ 6 - 1
tests/Output/StringTest.php

@@ -42,11 +42,16 @@ class StringTest extends \PHPUnit_Framework_TestCase{
 	/**
 	/**
 	 * @dataProvider stringDataProvider
 	 * @dataProvider stringDataProvider
 	 */
 	 */
-	public function testImageOutput($data, $type, $expected){
+	public function testStringOutput($data, $type, $expected){
 		$this->options->type = $type;
 		$this->options->type = $type;
 		$this->assertEquals($expected, (new QRCode($data, new QRString($this->options)))->output());
 		$this->assertEquals($expected, (new QRCode($data, new QRString($this->options)))->output());
 	}
 	}
 
 
+	public function testOutputTypeException(){
+		$this->options->type = 'foo';
+		new QRString($this->options);
+	}
+
 	/**
 	/**
 	 * @expectedException \chillerlan\QRCode\Output\QRCodeOutputException
 	 * @expectedException \chillerlan\QRCode\Output\QRCodeOutputException
 	 * @expectedExceptionMessage Invalid matrix!
 	 * @expectedExceptionMessage Invalid matrix!