QrCodeHelperTest.php 3.5 KB

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