QrCodeHelperTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace Tools\Test\TestCase\View\Helper;
  3. use Cake\View\View;
  4. use Tools\TestSuite\TestCase;
  5. use Tools\View\Helper\HelperHelper;
  6. use Tools\View\Helper\QrCodeHelper;
  7. /**
  8. * QrCodeHelper Test Case
  9. */
  10. class QrCodeHelperTest extends TestCase {
  11. CONST QR_TEST_STRING = 'Some Text to Translate';
  12. const QR_TEST_STRING_UTF = 'Some äöü Test String with $ and @ etc';
  13. /**
  14. * @var \Tools\View\Helper\QrCodeHelper
  15. */
  16. protected $QrCode;
  17. /**
  18. * SetUp method
  19. *
  20. * @return void
  21. */
  22. public function setUp() {
  23. parent::setUp();
  24. $this->testEmail = 'foo@bar.local'; // For testing normal behavior
  25. $this->QrCode = new QrCodeHelper(new View(null));
  26. }
  27. /**
  28. * TearDown method
  29. *
  30. * @return void
  31. */
  32. public function tearDown() {
  33. parent::tearDown();
  34. unset($this->QrCode);
  35. }
  36. /**
  37. * @return void
  38. */
  39. public function testImage() {
  40. $is = $this->QrCode->image('Foo Bar');
  41. $expected = '<img src="http://chart.apis.google.com/chart?chl=Foo%20Bar&cht=qr&choe=UTF-8&chs=74x74&chld=" alt=""/>';
  42. $this->assertSame($expected, $is);
  43. }
  44. /**
  45. * @return void
  46. */
  47. public function testFormatText() {
  48. $is = $this->QrCode->formatText(['controller' => 'Foo', 'action' => 'bar'], 'url');
  49. $this->assertSame('/foo/bar', $is);
  50. }
  51. /**
  52. * @return void
  53. */
  54. public function testFormatCard() {
  55. $data = [
  56. 'name' => 'My name',
  57. 'nickname' => 'Nick',
  58. 'note' => 'Note',
  59. 'birthday' => '2015-01-03'
  60. ];
  61. $is = $this->QrCode->formatCard($data);
  62. $expected = 'MECARD:N:My name;NICKNAME:Nick;NOTE:Note;BDAY:20151-;';
  63. $this->assertSame($expected, $is);
  64. }
  65. /**
  66. * @return void
  67. */
  68. public function testSetSize() {
  69. $is = $this->QrCode->setSize(1000);
  70. $this->assertFalse($is);
  71. $is = $this->QrCode->setSize(300);
  72. $this->assertTrue($is);
  73. }
  74. /**
  75. * @return void
  76. */
  77. public function testImagesModified() {
  78. $this->QrCode->reset();
  79. $this->QrCode->setLevel('H');
  80. $is = $this->QrCode->image(static::QR_TEST_STRING);
  81. $this->assertTrue(!empty($is));
  82. $this->QrCode->reset();
  83. $this->QrCode->setLevel('H', 20);
  84. $is = $this->QrCode->image(static::QR_TEST_STRING_UTF);
  85. $this->assertTrue(!empty($is));
  86. $this->QrCode->reset();
  87. $this->QrCode->setSize(300);
  88. $this->QrCode->setLevel('L', 1);
  89. $is = $this->QrCode->image(static::QR_TEST_STRING);
  90. $this->assertTrue(!empty($is));
  91. $this->QrCode->reset();
  92. $this->QrCode->setSize(300);
  93. $this->QrCode->setLevel('H', 1);
  94. $is = $this->QrCode->image(static::QR_TEST_STRING);
  95. $this->assertTrue(!empty($is));
  96. }
  97. /**
  98. * @return void
  99. */
  100. public function testSpecialImages() {
  101. $this->QrCode->reset();
  102. $this->QrCode->setSize(300);
  103. $this->QrCode->setLevel('H');
  104. //echo 'CARD'.BR;
  105. $string = $this->QrCode->formatCard([
  106. 'name' => 'Maier,Susanne',
  107. 'tel' => ['0111222123', '012224344'],
  108. 'nickname' => 'sssnick',
  109. 'birthday' => '1999-01-03',
  110. 'address' => 'Bluetenweg 11, 85375, Neufahrn, Deutschland',
  111. 'email' => 'test@test.de',
  112. 'note' => 'someNote;someOtherNote :)',
  113. 'url' => 'http://www.some_url.de'
  114. ]);
  115. $is = $this->QrCode->image($string);
  116. $this->assertTrue(!empty($is));
  117. }
  118. /**
  119. * @return void
  120. */
  121. public function testBitcoin() {
  122. $this->QrCode->reset();
  123. $this->QrCode->setSize(100);
  124. $this->QrCode->setLevel('H');
  125. $string = $this->QrCode->format('bitcoin', '18pnDgDYFMAKsHTA3ZqyAi6t8q9ztaWWXt');
  126. $is = $this->QrCode->image($string);
  127. $this->assertTrue(!empty($is));
  128. }
  129. }