浏览代码

Fix up QrCodeHelper.

mscherer 9 年之前
父节点
当前提交
7ce00422bd
共有 3 个文件被更改,包括 159 次插入4 次删除
  1. 2 0
      composer.json
  2. 4 4
      src/View/Helper/QrCodeHelper.php
  3. 153 0
      tests/TestCase/View/Helper/QrCodeHelperTest.php

+ 2 - 0
composer.json

@@ -41,6 +41,8 @@
 		"issues": "https://github.com/dereuromark/cakephp-tools/issues"
 	},
 	"scripts": {
+		"setup": "[ ! -f phpunit.phar ] && wget https://phar.phpunit.de/phpunit.phar",
+		"test": "php phpunit.phar",
 		"cs-check": "phpcs -p --standard=vendor/fig-r/psr2r-sniffer/PSR2R/ruleset.xml --ignore=/vendor/,/tmp/,/logs/,/tests/test_files/ --extensions=php ./",
 		"cs-fix": "phpcbf -v --standard=vendor/fig-r/psr2r-sniffer/PSR2R/ruleset.xml --ignore=/vendor/,/tmp/,/logs/,/tests/test_files --extensions=php ./"
 	}

+ 4 - 4
src/View/Helper/QrCodeHelper.php

@@ -37,7 +37,7 @@ class QrCodeHelper extends Helper {
 	/**
 	 * @var array
 	 */
-	public $helpers = ['Html'];
+	public $helpers = ['Html', 'Url'];
 
 	/**
 	 * @var string
@@ -75,7 +75,7 @@ class QrCodeHelper extends Helper {
 	 * @param \Cake\View\View $View
 	 * @param array $config
 	 */
-	public function __construct(View $View, array $config) {
+	public function __construct(View $View, array $config = []) {
 		parent::__construct($View, $config);
 
 		$this->reset();
@@ -133,7 +133,7 @@ class QrCodeHelper extends Helper {
 			case 'text':
 				break;
 			case 'url':
-				$text = $this->Html->url($text, true);
+				$text = $this->Url->build($text, true);
 				break;
 			case 'sms':
 				$text = 'smsto:' . implode(':', (array)$text);
@@ -226,7 +226,7 @@ class QrCodeHelper extends Helper {
 				case 'url':
 					$val = (array)$val;
 					foreach ($val as $v) {
-						$res[] = 'URL:' . $this->Html->url($v, true);
+						$res[] = 'URL:' . $this->Url->build($v, true);
 					}
 					break;
 			}

+ 153 - 0
tests/TestCase/View/Helper/QrCodeHelperTest.php

@@ -0,0 +1,153 @@
+<?php
+
+namespace Tools\Test\TestCase\View\Helper;
+
+use Cake\View\View;
+use Tools\TestSuite\TestCase;
+use Tools\View\Helper\HelperHelper;
+use Tools\View\Helper\QrCodeHelper;
+
+/**
+ * QrCodeHelper Test Case
+ */
+class QrCodeHelperTest extends TestCase {
+
+	CONST QR_TEST_STRING = 'Some Text to Translate';
+	const QR_TEST_STRING_UTF = 'Some äöü Test String with $ and @ etc';
+
+	/**
+	 * @var \Tools\View\Helper\QrCodeHelper
+	 */
+	protected $QrCode;
+
+	/**
+	 * SetUp method
+	 *
+	 * @return void
+	 */
+	public function setUp() {
+		parent::setUp();
+
+		$this->testEmail = 'foo@bar.local'; // For testing normal behavior
+
+		$this->QrCode = new QrCodeHelper(new View(null));
+	}
+
+	/**
+	 * TearDown method
+	 *
+	 * @return void
+	 */
+	public function tearDown() {
+		parent::tearDown();
+
+		unset($this->QrCode);
+	}
+
+	/**
+	 * @return void
+	 */
+	public function testImage() {
+		$is = $this->QrCode->image('Foo Bar');
+
+		$expected = '<img src="http://chart.apis.google.com/chart?chl=Foo%20Bar&cht=qr&choe=UTF-8&chs=74x74&chld=" alt=""/>';
+		$this->assertSame($expected, $is);
+	}
+
+	/**
+	 * @return void
+     */
+	public function testFormatText() {
+		$is = $this->QrCode->formatText(['controller' => 'Foo', 'action' => 'bar'], 'url');
+		$this->assertSame('/foo/bar', $is);
+	}
+
+	/**
+	 * @return void
+     */
+	public function testFormatCard() {
+		$data = [
+			'name' => 'My name',
+			'nickname' => 'Nick',
+			'note' => 'Note',
+			'birthday' => '2015-01-03'
+		];
+		$is = $this->QrCode->formatCard($data);
+
+		$expected = 'MECARD:N:My name;NICKNAME:Nick;NOTE:Note;BDAY:20151-;';
+		$this->assertSame($expected, $is);
+	}
+
+	/**
+	 * @return void
+	 */
+	public function testSetSize() {
+		$is = $this->QrCode->setSize(1000);
+		$this->assertFalse($is);
+
+		$is = $this->QrCode->setSize(300);
+		$this->assertTrue($is);
+	}
+
+	/**
+	 * @return void
+	 */
+	public function testImagesModified() {
+		$this->QrCode->reset();
+		$this->QrCode->setLevel('H');
+		$is = $this->QrCode->image(static::QR_TEST_STRING);
+		$this->assertTrue(!empty($is));
+
+		$this->QrCode->reset();
+		$this->QrCode->setLevel('H', 20);
+		$is = $this->QrCode->image(static::QR_TEST_STRING_UTF);
+		$this->assertTrue(!empty($is));
+
+		$this->QrCode->reset();
+		$this->QrCode->setSize(300);
+		$this->QrCode->setLevel('L', 1);
+		$is = $this->QrCode->image(static::QR_TEST_STRING);
+		$this->assertTrue(!empty($is));
+
+		$this->QrCode->reset();
+		$this->QrCode->setSize(300);
+		$this->QrCode->setLevel('H', 1);
+		$is = $this->QrCode->image(static::QR_TEST_STRING);
+		$this->assertTrue(!empty($is));
+	}
+
+	/**
+	 * @return void
+	 */
+	public function testSpecialImages() {
+		$this->QrCode->reset();
+		$this->QrCode->setSize(300);
+		$this->QrCode->setLevel('H');
+		//echo 'CARD'.BR;
+		$string = $this->QrCode->formatCard([
+			'name' => 'Maier,Susanne',
+			'tel' => ['0111222123', '012224344'],
+			'nickname' => 'sssnick',
+			'birthday' => '1999-01-03',
+			'address' => 'Bluetenweg 11, 85375, Neufahrn, Deutschland',
+			'email' => 'test@test.de',
+			'note' => 'someNote;someOtherNote :)',
+			'url' => 'http://www.some_url.de'
+		]);
+		$is = $this->QrCode->image($string);
+		$this->assertTrue(!empty($is));
+	}
+
+	/**
+	 * @return void
+	 */
+	public function testBitcoin() {
+		$this->QrCode->reset();
+		$this->QrCode->setSize(100);
+		$this->QrCode->setLevel('H');
+		$string = $this->QrCode->format('bitcoin', '18pnDgDYFMAKsHTA3ZqyAi6t8q9ztaWWXt');
+		$is = $this->QrCode->image($string);
+		$this->assertTrue(!empty($is));
+	}
+
+}