QrCodeHelperTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. * @var string
  24. */
  25. protected $testEmail;
  26. /**
  27. * SetUp method
  28. *
  29. * @return void
  30. */
  31. public function setUp(): void {
  32. parent::setUp();
  33. $this->loadRoutes();
  34. $this->testEmail = 'foo@bar.local'; // For testing normal behavior
  35. $this->QrCode = new QrCodeHelper(new View(null));
  36. }
  37. /**
  38. * TearDown method
  39. *
  40. * @return void
  41. */
  42. public function tearDown(): void {
  43. parent::tearDown();
  44. unset($this->QrCode);
  45. }
  46. /**
  47. * @return void
  48. */
  49. public function testImage() {
  50. $is = $this->QrCode->image('Foo Bar');
  51. $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="">';
  52. $this->assertSame($expected, $is);
  53. }
  54. /**
  55. * @return void
  56. */
  57. public function testFormatText() {
  58. $is = $this->QrCode->formatText(['controller' => 'Foo', 'action' => 'bar'], 'url');
  59. $this->assertSame('//localhost/foo/bar', $is);
  60. }
  61. /**
  62. * @return void
  63. */
  64. public function testFormatCard() {
  65. $data = [
  66. 'name' => 'My name',
  67. 'nickname' => 'Nick',
  68. 'note' => 'Note',
  69. 'birthday' => '2015-01-03',
  70. ];
  71. $is = $this->QrCode->formatCard($data);
  72. $expected = 'MECARD:N:My name;NICKNAME:Nick;NOTE:Note;BDAY:20151-;';
  73. $this->assertSame($expected, $is);
  74. }
  75. /**
  76. * @return void
  77. */
  78. public function testSetSize() {
  79. $is = $this->QrCode->setSize(1000);
  80. $this->assertFalse($is);
  81. $is = $this->QrCode->setSize(300);
  82. $this->assertTrue($is);
  83. }
  84. /**
  85. * @return void
  86. */
  87. public function testImagesModified() {
  88. $this->QrCode->reset();
  89. $this->QrCode->setLevel('H');
  90. $is = $this->QrCode->image(static::QR_TEST_STRING);
  91. $this->assertTrue(!empty($is));
  92. $this->QrCode->reset();
  93. $this->QrCode->setLevel('H', 20);
  94. $is = $this->QrCode->image(static::QR_TEST_STRING_UTF);
  95. $this->assertTrue(!empty($is));
  96. $this->QrCode->reset();
  97. $this->QrCode->setSize(300);
  98. $this->QrCode->setLevel('L', 1);
  99. $is = $this->QrCode->image(static::QR_TEST_STRING);
  100. $this->assertTrue(!empty($is));
  101. $this->QrCode->reset();
  102. $this->QrCode->setSize(300);
  103. $this->QrCode->setLevel('H', 1);
  104. $is = $this->QrCode->image(static::QR_TEST_STRING);
  105. $this->assertTrue(!empty($is));
  106. }
  107. /**
  108. * @return void
  109. */
  110. public function testSpecialImages() {
  111. $this->QrCode->reset();
  112. $this->QrCode->setSize(300);
  113. $this->QrCode->setLevel('H');
  114. //echo 'CARD'.BR;
  115. $string = $this->QrCode->formatCard([
  116. 'name' => 'Maier,Susanne',
  117. 'tel' => ['0111222123', '012224344'],
  118. 'nickname' => 'sssnick',
  119. 'birthday' => '1999-01-03',
  120. 'address' => 'Bluetenweg 11, 85375, Neufahrn, Deutschland',
  121. 'email' => 'test@test.de',
  122. 'note' => 'someNote;someOtherNote :)',
  123. 'url' => 'http://www.some_url.de',
  124. ]);
  125. $is = $this->QrCode->image($string);
  126. $this->assertTrue(!empty($is));
  127. }
  128. /**
  129. * @return void
  130. */
  131. public function testBitcoin() {
  132. $this->QrCode->reset();
  133. $this->QrCode->setSize(100);
  134. $this->QrCode->setLevel('H');
  135. $string = $this->QrCode->format('bitcoin', '18pnDgDYFMAKsHTA3ZqyAi6t8q9ztaWWXt');
  136. $is = $this->QrCode->image($string);
  137. $this->assertTrue(!empty($is));
  138. }
  139. }