Skip to content

Commit 5d36d4f

Browse files
committed
Merge remote-tracking branch 'origin/test-specific-provider'
2 parents e76f31e + cb45226 commit 5d36d4f

File tree

5 files changed

+127
-20
lines changed

5 files changed

+127
-20
lines changed

.github/workflows/test-bacon.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Test Bacon QR Code Provider
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
jobs:
8+
build:
9+
runs-on: ubuntu-latest
10+
11+
strategy:
12+
matrix:
13+
php-version: ['5.6', '7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1']
14+
15+
steps:
16+
- uses: actions/checkout@v2
17+
18+
- uses: shivammathur/setup-php@v2
19+
with:
20+
php-version: ${{ matrix.php-version }}
21+
tools: composer
22+
coverage: xdebug
23+
ini-values: error_reporting=E_ALL
24+
25+
- uses: ramsey/composer-install@v1
26+
27+
- run: composer require bacon/bacon-qr-code
28+
29+
- run: composer lint
30+
- run: composer test testsDependency/BaconQRCodeTest.php

lib/Providers/Qr/BaconQrCodeProvider.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,19 @@ private function handleColour($colour)
125125
{
126126
if (is_string($colour) && $colour[0] == '#') {
127127
$hexToRGB = function ($input) {
128+
// ensure input no longer has a # for more predictable division
129+
// PHP 8.1 does not like implicitly casting a float to an int
130+
$input = trim($input, '#');
131+
132+
if (strlen($input) != 3 && strlen($input) != 6) {
133+
throw new \RuntimeException('Colour should be a 3 or 6 character value after the #');
134+
}
135+
128136
// split the array into three chunks
129-
$split = str_split(trim($input, '#'), strlen($input) / 3);
137+
$split = str_split($input, strlen($input) / 3);
130138

131139
// cope with three character hex reference
132-
// three characters plus a # = 4
133-
if (strlen($input) == 4) {
140+
if (strlen($input) == 3) {
134141
array_walk($split, function (&$character) {
135142
$character = str_repeat($character, 2);
136143
});

lib/Providers/Qr/HandlesDataUri.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace RobThree\Auth\Providers\Qr;
4+
5+
trait HandlesDataUri
6+
{
7+
/**
8+
* @param string $datauri
9+
*
10+
* @return null|array
11+
*/
12+
private function DecodeDataUri($datauri)
13+
{
14+
if (preg_match('/data:(?P<mimetype>[\w\.\-\+\/]+);(?P<encoding>\w+),(?P<data>.*)/', $datauri, $m) === 1) {
15+
return array(
16+
'mimetype' => $m['mimetype'],
17+
'encoding' => $m['encoding'],
18+
'data' => base64_decode($m['data'])
19+
);
20+
}
21+
22+
return null;
23+
}
24+
}

tests/Providers/Qr/IQRCodeProviderTest.php

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,11 @@
55
use PHPUnit\Framework\TestCase;
66
use RobThree\Auth\TwoFactorAuth;
77
use RobThree\Auth\TwoFactorAuthException;
8+
use RobThree\Auth\Providers\Qr\HandlesDataUri;
89

910
class IQRCodeProviderTest extends TestCase
1011
{
11-
/**
12-
* @param string $datauri
13-
*
14-
* @return null|array
15-
*/
16-
private function DecodeDataUri($datauri)
17-
{
18-
if (preg_match('/data:(?P<mimetype>[\w\.\-\/]+);(?P<encoding>\w+),(?P<data>.*)/', $datauri, $m) === 1) {
19-
return array(
20-
'mimetype' => $m['mimetype'],
21-
'encoding' => $m['encoding'],
22-
'data' => base64_decode($m['data'])
23-
);
24-
}
25-
26-
return null;
27-
}
12+
use HandlesDataUri;
2813

2914
/**
3015
* @return void

testsDependency/BaconQRCodeTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace TestsDependency;
4+
5+
use BaconQrCode\Renderer\Image\ImagickImageBackEnd;
6+
use PHPUnit\Framework\TestCase;
7+
use RobThree\Auth\Providers\Qr\BaconQrCodeProvider;
8+
use RobThree\Auth\TwoFactorAuth;
9+
use RobThree\Auth\Providers\Qr\HandlesDataUri;
10+
11+
class BaconQRCodeTest extends TestCase
12+
{
13+
use HandlesDataUri;
14+
15+
public function testDependency()
16+
{
17+
// php < 7.1 will install an older Bacon QR Code
18+
if (! class_exists(ImagickImageBackEnd::class)) {
19+
$this->expectException(\RuntimeException::class);
20+
21+
$qr = new BaconQrCodeProvider(1, '#000', '#FFF', 'svg');
22+
} else {
23+
$qr = new BaconQrCodeProvider(1, '#000', '#FFF', 'svg');
24+
25+
$tfa = new TwoFactorAuth('Test&Issuer', 6, 30, 'sha1', $qr);
26+
27+
$data = $this->DecodeDataUri($tfa->getQRCodeImageAsDataUri('Test&Label', 'VMR466AB62ZBOKHE'));
28+
$this->assertEquals('image/svg+xml', $data['mimetype']);
29+
}
30+
}
31+
32+
public function testBadTextColour()
33+
{
34+
$this->expectException(\RuntimeException::class);
35+
36+
new BaconQrCodeProvider(1, 'not-a-colour', '#FFF');
37+
}
38+
39+
public function testBadBackgroundColour()
40+
{
41+
$this->expectException(\RuntimeException::class);
42+
43+
new BaconQrCodeProvider(1, '#000', 'not-a-colour');
44+
}
45+
46+
public function testBadTextColourHexRef()
47+
{
48+
$this->expectException(\RuntimeException::class);
49+
50+
new BaconQrCodeProvider(1, '#AAAA', '#FFF');
51+
}
52+
53+
public function testBadBackgroundColourHexRef()
54+
{
55+
$this->expectException(\RuntimeException::class);
56+
57+
new BaconQrCodeProvider(1, '#000', '#AAAA');
58+
}
59+
60+
61+
}

0 commit comments

Comments
 (0)