QrCodeHelperTest.php 3.4 KB

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