QrCodeHelperTest.php 3.3 KB

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