QrCodeHelperTest.php 3.6 KB

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