Skip to content

Commit 23b3cc0

Browse files
committed
Expanded unit test for "GoogleQr" class.
1 parent b19b27a commit 23b3cc0

File tree

3 files changed

+195
-57
lines changed

3 files changed

+195
-57
lines changed

source/Jocic/GoogleAuthenticator/Qr/Remote/GoogleQr.php

+43-8
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
namespace Jocic\GoogleAuthenticator\Qr\Remote;
3333

34+
use Jocic\Encoders\Base\Base16;
3435
use Jocic\Encoders\Base\Base32;
3536
use Jocic\GoogleAuthenticator\Qr\QrInterface;
3637
use Jocic\GoogleAuthenticator\Account;
@@ -50,7 +51,23 @@ class GoogleQr implements QrInterface, RemoteQrInterface
5051
|* CORE CONSTANTS *|
5152
\******************/
5253

53-
// CORE CONSTANTS GO HERE
54+
/**
55+
* Encoding constant - for getting QR code image in <i>Base 16</i>.
56+
*
57+
* @var integer
58+
* @access public
59+
*/
60+
61+
public const E_BASE_16 = 0;
62+
63+
/**
64+
* Encoding constant - for getting QR code image in <i>Base 32</i>.
65+
*
66+
* @var integer
67+
* @access public
68+
*/
69+
70+
public const E_BASE_32 = 1;
5471

5572
/******************\
5673
|* CORE VARIABLES *|
@@ -221,27 +238,45 @@ public function getFilename($account)
221238
* @copyright 2018 All Rights Reserved
222239
* @version 1.0.0
223240
*
224-
* @return string
225-
* Encoded value of the generated QR code in <i>Base 32</i> format.
241+
* @param $account
242+
* Account that should be used for generating the QR code.
243+
* @param integer $encoding
244+
* ID of an encoding that should be used.
226245
* @param integer $bufferSize
227246
* Buffer size in bytes that will be used for loading.
228247
* @return string
229-
* Encoded value of the QR code - Base 32 encoding.
248+
* Encoded value of the QR code in a selected format.
230249
*/
231250

232-
public function getEncodedValue($account, $bufferSize = 1024)
251+
public function getEncodedValue($account, $encoding = 1, $bufferSize = 1024)
233252
{
234253
// Core Variables
235254

236-
$encoder = new Base32();
255+
$encoder = null;
237256

238257
// File Variables
239258

240259
$fileLocation = $this->getFileLocation($account);
241260
$fileHandler = null;
242261
$fileData = null;
243262

244-
// Logic
263+
// Step 1 -
264+
265+
switch ($encoding)
266+
{
267+
case 0:
268+
$encoder = new Base16();
269+
break;
270+
271+
case 1:
272+
$encoder = new Base32();
273+
break;
274+
275+
default:
276+
throw new \Exception("Invalid encoding ID provided.");
277+
}
278+
279+
// Step 2 - Encode Generated Code
245280

246281
try
247282
{
@@ -326,7 +361,7 @@ public function getUrl($account)
326361

327362
// Step 3 - Check Account Details
328363

329-
if ($account->getServiceName() == "" && $account->getAccountName())
364+
if ($account->getServiceName() == "" && $account->getAccountName() == "")
330365
{
331366
throw new \Exception("Set account is without details.");
332367
}

source/Jocic/GoogleAuthenticator/Validator.php

+3-8
Original file line numberDiff line numberDiff line change
@@ -217,18 +217,13 @@ private function generateCode($secret, $offset, $codeLength = 6)
217217
$bitMask = ($bitMask << 4) | 0x0F;
218218
}
219219

220-
// Step 2 - Generate Code
220+
// Step 2 - Generate & Return Code
221221

222222
$code = unpack("N", $hashPart);
223223

224-
if (isset($code[1]))
225-
{
226-
$code = $code[1] & $bitMask;
227-
228-
return str_pad(($code % $moduo), $codeLength, "0", STR_PAD_LEFT);
229-
}
224+
$code = $code[1] & $bitMask;
230225

231-
return "0";
226+
return str_pad(($code % $moduo), $codeLength, "0", STR_PAD_LEFT);
232227
}
233228
}
234229

0 commit comments

Comments
 (0)