From 66e1e030ba236be2041ebf74361a2b50f7458014 Mon Sep 17 00:00:00 2001 From: William Hall Date: Sun, 19 Dec 2021 19:32:45 +0000 Subject: [PATCH 1/4] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20re=20arrange=20useful?= =?UTF-8?q?=20testing=20code=20for=20provider=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/Providers/Qr/HandlesDataUri.php | 24 ++++++++++++++++++++++ tests/Providers/Qr/IQRCodeProviderTest.php | 19 ++--------------- 2 files changed, 26 insertions(+), 17 deletions(-) create mode 100644 lib/Providers/Qr/HandlesDataUri.php diff --git a/lib/Providers/Qr/HandlesDataUri.php b/lib/Providers/Qr/HandlesDataUri.php new file mode 100644 index 0000000..c9f8571 --- /dev/null +++ b/lib/Providers/Qr/HandlesDataUri.php @@ -0,0 +1,24 @@ +[\w\.\-\+\/]+);(?P\w+),(?P.*)/', $datauri, $m) === 1) { + return array( + 'mimetype' => $m['mimetype'], + 'encoding' => $m['encoding'], + 'data' => base64_decode($m['data']) + ); + } + + return null; + } +} diff --git a/tests/Providers/Qr/IQRCodeProviderTest.php b/tests/Providers/Qr/IQRCodeProviderTest.php index 579cb51..bf52d6e 100644 --- a/tests/Providers/Qr/IQRCodeProviderTest.php +++ b/tests/Providers/Qr/IQRCodeProviderTest.php @@ -5,26 +5,11 @@ namespace Tests\Providers\Qr; use PHPUnit\Framework\TestCase; use RobThree\Auth\TwoFactorAuth; use RobThree\Auth\TwoFactorAuthException; +use RobThree\Auth\Providers\Qr\HandlesDataUri; class IQRCodeProviderTest extends TestCase { - /** - * @param string $datauri - * - * @return null|array - */ - private function DecodeDataUri($datauri) - { - if (preg_match('/data:(?P[\w\.\-\/]+);(?P\w+),(?P.*)/', $datauri, $m) === 1) { - return array( - 'mimetype' => $m['mimetype'], - 'encoding' => $m['encoding'], - 'data' => base64_decode($m['data']) - ); - } - - return null; - } + use HandlesDataUri; /** * @return void From ae4da10ff1266479311244896d40e68214122d9c Mon Sep 17 00:00:00 2001 From: William Hall Date: Sun, 19 Dec 2021 19:33:11 +0000 Subject: [PATCH 2/4] =?UTF-8?q?=F0=9F=9A=A8=20test=20bacon=20qr=20code=20p?= =?UTF-8?q?rovider?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/test-bacon.yml | 30 +++++++++++++++++++++++ testsDependency/BaconQRCodeTest.php | 37 +++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 .github/workflows/test-bacon.yml create mode 100644 testsDependency/BaconQRCodeTest.php diff --git a/.github/workflows/test-bacon.yml b/.github/workflows/test-bacon.yml new file mode 100644 index 0000000..98c45dd --- /dev/null +++ b/.github/workflows/test-bacon.yml @@ -0,0 +1,30 @@ +name: Test Bacon QR Code Provider + +on: + push: + pull_request: + +jobs: + build: + runs-on: ubuntu-latest + + strategy: + matrix: + php-version: ['5.6', '7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1'] + + steps: + - uses: actions/checkout@v2 + + - uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-version }} + tools: composer + coverage: xdebug + ini-values: error_reporting=E_ALL + + - uses: ramsey/composer-install@v1 + + - run: composer require bacon/bacon-qr-code + + - run: composer lint + - run: composer test testsDependency/BaconQRCodeTest.php diff --git a/testsDependency/BaconQRCodeTest.php b/testsDependency/BaconQRCodeTest.php new file mode 100644 index 0000000..e30988a --- /dev/null +++ b/testsDependency/BaconQRCodeTest.php @@ -0,0 +1,37 @@ +DecodeDataUri($tfa->getQRCodeImageAsDataUri('Test&Label', 'VMR466AB62ZBOKHE')); + $this->assertEquals('image/svg+xml', $data['mimetype']); + } + + public function testBadTextColour() + { + $this->expectException(\RuntimeException::class); + + new BaconQrCodeProvider(1, 'not-a-colour', '#FFF'); + } + + public function testBadBackgroundColour() + { + $this->expectException(\RuntimeException::class); + + new BaconQrCodeProvider(1, '#000', 'not-a-colour'); + } +} From 2d1ec8d26519b5ad8154fc521b8736af2f2dc37b Mon Sep 17 00:00:00 2001 From: William Hall Date: Sun, 19 Dec 2021 19:44:10 +0000 Subject: [PATCH 3/4] =?UTF-8?q?=F0=9F=90=9B=20be=20more=20specific=20with?= =?UTF-8?q?=20hex=20colour=20handling?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/Providers/Qr/BaconQrCodeProvider.php | 13 ++++++++++--- testsDependency/BaconQRCodeTest.php | 16 ++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/lib/Providers/Qr/BaconQrCodeProvider.php b/lib/Providers/Qr/BaconQrCodeProvider.php index 4d5cf01..65fb977 100644 --- a/lib/Providers/Qr/BaconQrCodeProvider.php +++ b/lib/Providers/Qr/BaconQrCodeProvider.php @@ -125,12 +125,19 @@ class BaconQrCodeProvider implements IQRCodeProvider { if (is_string($colour) && $colour[0] == '#') { $hexToRGB = function ($input) { + // ensure input no longer has a # for more predictable division + // PHP 8.1 does not like implicitly casting a float to an int + $input = trim($input, '#'); + + if (strlen($input) != 3 && strlen($input) != 6) { + throw new \RuntimeException('Colour should be a 3 or 6 character value after the #'); + } + // split the array into three chunks - $split = str_split(trim($input, '#'), strlen($input) / 3); + $split = str_split($input, strlen($input) / 3); // cope with three character hex reference - // three characters plus a # = 4 - if (strlen($input) == 4) { + if (strlen($input) == 3) { array_walk($split, function (&$character) { $character = str_repeat($character, 2); }); diff --git a/testsDependency/BaconQRCodeTest.php b/testsDependency/BaconQRCodeTest.php index e30988a..9b426a7 100644 --- a/testsDependency/BaconQRCodeTest.php +++ b/testsDependency/BaconQRCodeTest.php @@ -34,4 +34,20 @@ class BaconQRCodeTest extends TestCase new BaconQrCodeProvider(1, '#000', 'not-a-colour'); } + + public function testBadTextColourHexRef() + { + $this->expectException(\RuntimeException::class); + + new BaconQrCodeProvider(1, '#AAAA', '#FFF'); + } + + public function testBadBackgroundColourHexRef() + { + $this->expectException(\RuntimeException::class); + + new BaconQrCodeProvider(1, '#000', '#AAAA'); + } + + } From cb452268004ce11a7d4cc818d67341a1a37c5c33 Mon Sep 17 00:00:00 2001 From: William Hall Date: Sun, 19 Dec 2021 19:50:25 +0000 Subject: [PATCH 4/4] =?UTF-8?q?=F0=9F=9A=A8=20test=20in=20php=20<=207.1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- testsDependency/BaconQRCodeTest.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/testsDependency/BaconQRCodeTest.php b/testsDependency/BaconQRCodeTest.php index 9b426a7..e46f13d 100644 --- a/testsDependency/BaconQRCodeTest.php +++ b/testsDependency/BaconQRCodeTest.php @@ -2,6 +2,7 @@ namespace TestsDependency; +use BaconQrCode\Renderer\Image\ImagickImageBackEnd; use PHPUnit\Framework\TestCase; use RobThree\Auth\Providers\Qr\BaconQrCodeProvider; use RobThree\Auth\TwoFactorAuth; @@ -13,12 +14,19 @@ class BaconQRCodeTest extends TestCase public function testDependency() { - $qr = new BaconQrCodeProvider(1, '#000', '#FFF', 'svg'); + // php < 7.1 will install an older Bacon QR Code + if (! class_exists(ImagickImageBackEnd::class)) { + $this->expectException(\RuntimeException::class); - $tfa = new TwoFactorAuth('Test&Issuer', 6, 30, 'sha1', $qr); + $qr = new BaconQrCodeProvider(1, '#000', '#FFF', 'svg'); + } else { + $qr = new BaconQrCodeProvider(1, '#000', '#FFF', 'svg'); - $data = $this->DecodeDataUri($tfa->getQRCodeImageAsDataUri('Test&Label', 'VMR466AB62ZBOKHE')); - $this->assertEquals('image/svg+xml', $data['mimetype']); + $tfa = new TwoFactorAuth('Test&Issuer', 6, 30, 'sha1', $qr); + + $data = $this->DecodeDataUri($tfa->getQRCodeImageAsDataUri('Test&Label', 'VMR466AB62ZBOKHE')); + $this->assertEquals('image/svg+xml', $data['mimetype']); + } } public function testBadTextColour()