IQRCodeProviderTest.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace Tests\Providers\Qr;
  3. use PHPUnit\Framework\TestCase;
  4. use RobThree\Auth\TwoFactorAuth;
  5. use RobThree\Auth\TwoFactorAuthException;
  6. class IQRCodeProviderTest extends TestCase
  7. {
  8. /**
  9. * @param string $datauri
  10. *
  11. * @return null|array
  12. */
  13. private function DecodeDataUri($datauri)
  14. {
  15. if (preg_match('/data:(?P<mimetype>[\w\.\-\/]+);(?P<encoding>\w+),(?P<data>.*)/', $datauri, $m) === 1) {
  16. return array(
  17. 'mimetype' => $m['mimetype'],
  18. 'encoding' => $m['encoding'],
  19. 'data' => base64_decode($m['data'])
  20. );
  21. }
  22. return null;
  23. }
  24. /**
  25. * @return void
  26. */
  27. public function testTotpUriIsCorrect()
  28. {
  29. $qr = new TestQrProvider();
  30. $tfa = new TwoFactorAuth('Test&Issuer', 6, 30, 'sha1', $qr);
  31. $data = $this->DecodeDataUri($tfa->getQRCodeImageAsDataUri('Test&Label', 'VMR466AB62ZBOKHE'));
  32. $this->assertEquals('test/test', $data['mimetype']);
  33. $this->assertEquals('base64', $data['encoding']);
  34. $this->assertEquals('otpauth://totp/Test%26Label?secret=VMR466AB62ZBOKHE&issuer=Test%26Issuer&period=30&algorithm=SHA1&digits=6@200', $data['data']);
  35. }
  36. /**
  37. * @return void
  38. */
  39. public function testGetQRCodeImageAsDataUriThrowsOnInvalidSize()
  40. {
  41. $qr = new TestQrProvider();
  42. $tfa = new TwoFactorAuth('Test', 6, 30, 'sha1', $qr);
  43. $this->expectException(TwoFactorAuthException::class);
  44. $tfa->getQRCodeImageAsDataUri('Test', 'VMR466AB62ZBOKHE', 0);
  45. }
  46. }