From 3349dcbd817d471cfc46831243b188c82928cba4 Mon Sep 17 00:00:00 2001 From: Antonio Ribeiro Date: Sun, 20 Oct 2024 13:52:48 +0200 Subject: [PATCH 1/2] Add PHPUnit to the linters --- README.md | 9 + tests/Google2FATest.php | 472 ++++++++++++++++++++++++---------------- tools/linters.sh | 36 +++ 3 files changed, 335 insertions(+), 182 deletions(-) diff --git a/README.md b/README.md index d7e11d3..96bfa6c 100644 --- a/README.md +++ b/README.md @@ -409,6 +409,15 @@ Google2FA is licensed under the MIT License - see the [LICENSE](LICENSE.md) file Pull requests and issues are more than welcome. +### Contribution setup + +If you want to contribute, make sure code is checked, tested, linted and formatted before pushing it. Please install NPM packages to setup a husky pre-commit hook that will help you with that: + +```bash +npm install +npm run prepare +``` + ## Sponsorships ### Direct diff --git a/tests/Google2FATest.php b/tests/Google2FATest.php index 39945ac..1c55eb3 100644 --- a/tests/Google2FATest.php +++ b/tests/Google2FATest.php @@ -1,5 +1,6 @@ google2fa - ); + Assert::assertInstanceOf(Google2FA::class, $this->google2fa); } public function testGeneratesAValidSecretKey(): void @@ -31,80 +29,156 @@ public function testGeneratesAValidSecretKey(): void Assert::assertEquals( 32, - strlen($this->google2fa->generateSecretKey(32)) + strlen($this->google2fa->generateSecretKey(32)), ); Assert::assertStringStartsWith( 'MFXHI', - $this->google2fa->generateSecretKey(59, 'ant') + $this->google2fa->generateSecretKey(59, 'ant'), ); Assert::assertStringStartsWith( 'MFXHI', - $this->google2fa->generateSecretKey(59, 'ant') + $this->google2fa->generateSecretKey(59, 'ant'), ); Assert::assertEquals( $key = $this->google2fa->generateSecretKey(), preg_replace( - '/[^'.Google2FAConstants::VALID_FOR_B32.']/', + '/[^' . Google2FAConstants::VALID_FOR_B32 . ']/', '', - $key - ) + $key, + ), ); } public function testGeneratesASecretKeysCompatibleWithGoogleAuthenticator(): void { - Assert::assertEquals($size = 16, strlen($this->google2fa->setEnforceGoogleAuthenticatorCompatibility(true)->generateSecretKey($size))); /// minimum = 128 bits - Assert::assertEquals($size = 20, strlen($this->google2fa->setEnforceGoogleAuthenticatorCompatibility(false)->generateSecretKey($size))); /// recommended = 160 bits - not compatible - Assert::assertEquals($size = 32, strlen($this->google2fa->setEnforceGoogleAuthenticatorCompatibility(true)->generateSecretKey($size))); /// recommended = 256 bits - compatible - Assert::assertEquals($size = 64, strlen($this->google2fa->setEnforceGoogleAuthenticatorCompatibility(true)->generateSecretKey($size))); - Assert::assertEquals($size = 128, strlen($this->google2fa->setEnforceGoogleAuthenticatorCompatibility(true)->generateSecretKey($size))); + Assert::assertEquals( + $size = 16, + strlen( + $this->google2fa + ->setEnforceGoogleAuthenticatorCompatibility(true) + ->generateSecretKey($size), + ), + ); /// minimum = 128 bits + Assert::assertEquals( + $size = 20, + strlen( + $this->google2fa + ->setEnforceGoogleAuthenticatorCompatibility(false) + ->generateSecretKey($size), + ), + ); /// recommended = 160 bits - not compatible + Assert::assertEquals( + $size = 32, + strlen( + $this->google2fa + ->setEnforceGoogleAuthenticatorCompatibility(true) + ->generateSecretKey($size), + ), + ); /// recommended = 256 bits - compatible + Assert::assertEquals( + $size = 64, + strlen( + $this->google2fa + ->setEnforceGoogleAuthenticatorCompatibility(true) + ->generateSecretKey($size), + ), + ); + Assert::assertEquals( + $size = 128, + strlen( + $this->google2fa + ->setEnforceGoogleAuthenticatorCompatibility(true) + ->generateSecretKey($size), + ), + ); } public function testGeneratesASecretKeysGenerationSize(): void { // 128 bits are allowed - Assert::assertEquals($size = 16, strlen($this->google2fa->generateSecretKey($size))); /// minimum = 128 bits + Assert::assertEquals( + $size = 16, + strlen($this->google2fa->generateSecretKey($size)), + ); /// minimum = 128 bits // anything below 128 bits are NOT allowed - $this->expectException(\PragmaRX\Google2FA\Exceptions\SecretKeyTooShortException::class); + $this->expectException( + \PragmaRX\Google2FA\Exceptions\SecretKeyTooShortException::class, + ); - Assert::assertEquals($size = 2, strlen($this->google2fa->generateSecretKey($size))); /// minimum = 128 bits - Assert::assertEquals($size = 4, strlen($this->google2fa->generateSecretKey($size))); /// minimum = 128 bits - Assert::assertEquals($size = 8, strlen($this->google2fa->generateSecretKey($size))); /// minimum = 128 bits + Assert::assertEquals( + $size = 2, + strlen($this->google2fa->generateSecretKey($size)), + ); /// minimum = 128 bits + Assert::assertEquals( + $size = 4, + strlen($this->google2fa->generateSecretKey($size)), + ); /// minimum = 128 bits + Assert::assertEquals( + $size = 8, + strlen($this->google2fa->generateSecretKey($size)), + ); /// minimum = 128 bits } public function testGeneratesASecretKeysNotCompatibleWithGoogleAuthenticator(): void { - $this->expectException(\PragmaRX\Google2FA\Exceptions\IncompatibleWithGoogleAuthenticatorException::class); - Assert::assertEquals($size = 15, strlen($this->google2fa->setEnforceGoogleAuthenticatorCompatibility(true)->generateSecretKey($size))); + $this->expectException( + \PragmaRX\Google2FA\Exceptions\IncompatibleWithGoogleAuthenticatorException::class, + ); + Assert::assertEquals( + $size = 15, + strlen( + $this->google2fa + ->setEnforceGoogleAuthenticatorCompatibility(true) + ->generateSecretKey($size), + ), + ); - $this->expectException(\PragmaRX\Google2FA\Exceptions\IncompatibleWithGoogleAuthenticatorException::class); - Assert::assertEquals($size = 17, strlen($this->google2fa->setEnforceGoogleAuthenticatorCompatibility(true)->generateSecretKey($size))); + $this->expectException( + \PragmaRX\Google2FA\Exceptions\IncompatibleWithGoogleAuthenticatorException::class, + ); + Assert::assertEquals( + $size = 17, + strlen( + $this->google2fa + ->setEnforceGoogleAuthenticatorCompatibility(true) + ->generateSecretKey($size), + ), + ); - $this->expectException(\PragmaRX\Google2FA\Exceptions\IncompatibleWithGoogleAuthenticatorException::class); - Assert::assertEquals($size = 21, strlen($this->google2fa->setEnforceGoogleAuthenticatorCompatibility(true)->generateSecretKey($size))); + $this->expectException( + \PragmaRX\Google2FA\Exceptions\IncompatibleWithGoogleAuthenticatorException::class, + ); + Assert::assertEquals( + $size = 21, + strlen( + $this->google2fa + ->setEnforceGoogleAuthenticatorCompatibility(true) + ->generateSecretKey($size), + ), + ); } public function testConvertsInvalidCharsToBase32(): void { $converted = $this->google2fa->generateBase32RandomKey( 16, - '1234'. - chr(250). - chr(251). - chr(252). - chr(253). - chr(254). - chr(255) + '1234' . + chr(250) . + chr(251) . + chr(252) . + chr(253) . + chr(254) . + chr(255), ); $valid = preg_replace( - '/[^'.Google2FAConstants::VALID_FOR_B32.']/', + '/[^' . Google2FAConstants::VALID_FOR_B32 . ']/', '', - $converted + $converted, ); Assert::assertEquals($converted, $valid); @@ -122,20 +196,20 @@ public function testGetsValidTimestamps(): void public function testDecodesBase32Strings(): void { $result = - chr(0). - chr(232). - chr(196). - chr(187). - chr(190). - chr(223). - chr(26). - chr(241). - chr(145). + chr(0) . + chr(232) . + chr(196) . + chr(187) . + chr(190) . + chr(223) . + chr(26) . + chr(241) . + chr(145) . chr(86); Assert::assertEquals( $result, - $this->google2fa->base32Decode(Constants::SECRET) + $this->google2fa->base32Decode(Constants::SECRET), ); } @@ -143,7 +217,7 @@ public function testCreatesAOneTimePassword(): void { Assert::assertEquals( 6, - strlen($this->google2fa->getCurrentOtp(Constants::SECRET)) + strlen($this->google2fa->getCurrentOtp(Constants::SECRET)), ); } @@ -157,40 +231,40 @@ public function testVerifiesKeys(): void Constants::SECRET, '558854', 2, - 26213400 - ) + 26213400, + ), ); // 26213398 Assert::assertTrue( $this->google2fa->verifyKey( Constants::SECRET, '981084', 2, - 26213400 - ) + 26213400, + ), ); // 26213399 Assert::assertTrue( $this->google2fa->verifyKey( Constants::SECRET, '512396', 2, - 26213400 - ) + 26213400, + ), ); // 26213400 Assert::assertTrue( $this->google2fa->verifyKey( Constants::SECRET, '410272', 2, - 26213400 - ) + 26213400, + ), ); // 26213401 Assert::assertTrue( $this->google2fa->verifyKey( Constants::SECRET, '239815', 2, - 26213400 - ) + 26213400, + ), ); // 26213402 Assert::assertFalse( @@ -198,16 +272,16 @@ public function testVerifiesKeys(): void Constants::SECRET, '313366', 2, - 26213400 - ) + 26213400, + ), ); // 26213403 Assert::assertFalse( $this->google2fa->verifyKey( Constants::SECRET, '093183', 2, - 26213400 - ) + 26213400, + ), ); // 26213397 } @@ -217,8 +291,8 @@ public function testVerifiesKeysNewer(): void $this->google2fa->verifyKeyNewer( Constants::SECRET, '512396', - null /// first time user gets in - ) + null, /// first time user gets in + ), ); // 26213400 Assert::assertTrue( $this->google2fa->verifyKeyNewer( @@ -226,8 +300,8 @@ public function testVerifiesKeysNewer(): void '512396', null, /// first time user gets in 2, - 26213400 - ) + 26213400, + ), ); // 26213400 Assert::assertFalse( $this->google2fa->verifyKeyNewer( @@ -235,8 +309,8 @@ public function testVerifiesKeysNewer(): void '512396', 26213401, 2, - 26213400 - ) + 26213400, + ), ); // 26213400 Assert::assertFalse( $this->google2fa->verifyKeyNewer( @@ -244,8 +318,8 @@ public function testVerifiesKeysNewer(): void '410272', 26213401, 2, - 26213400 - ) + 26213400, + ), ); // 26213401 Assert::assertEquals( 26213402, @@ -254,8 +328,8 @@ public function testVerifiesKeysNewer(): void '239815', 26213401, 2, - 26213400 - ) + 26213400, + ), ); // 26213402 Assert::assertFalse( $this->google2fa->verifyKeyNewer( @@ -263,8 +337,8 @@ public function testVerifiesKeysNewer(): void '313366', 26213401, 2, - 26213400 - ) + 26213400, + ), ); // 26213403 Assert::assertEquals( @@ -274,8 +348,8 @@ public function testVerifiesKeysNewer(): void '512396', null, 2, - 26213400 - ) + 26213400, + ), ); // 26213400 Assert::assertEquals( 26213401, @@ -284,8 +358,8 @@ public function testVerifiesKeysNewer(): void '410272', null, 2, - 26213400 - ) + 26213400, + ), ); // 26213401 Assert::assertEquals( 26213402, @@ -294,8 +368,8 @@ public function testVerifiesKeysNewer(): void '239815', null, 2, - 26213400 - ) + 26213400, + ), ); // 26213402 Assert::assertFalse( $this->google2fa->verifyKeyNewer( @@ -303,8 +377,8 @@ public function testVerifiesKeysNewer(): void '313366', null, 2, - 26213400 - ) + 26213400, + ), ); // 26213403 } @@ -320,8 +394,8 @@ public function testVerifiesSha256Keys(): void Constants::SECRET, '230152', 2, - 26213400 - ) + 26213400, + ), ); // 26213398 Assert::assertTrue( @@ -329,8 +403,8 @@ public function testVerifiesSha256Keys(): void Constants::SECRET, '064978', 2, - 26213400 - ) + 26213400, + ), ); // 26213399 Assert::assertTrue( @@ -338,8 +412,8 @@ public function testVerifiesSha256Keys(): void Constants::SECRET, '758576', 2, - 26213400 - ) + 26213400, + ), ); // 26213400 Assert::assertTrue( @@ -347,8 +421,8 @@ public function testVerifiesSha256Keys(): void Constants::SECRET, '935741', 2, - 26213400 - ) + 26213400, + ), ); // 26213401 Assert::assertTrue( @@ -356,8 +430,8 @@ public function testVerifiesSha256Keys(): void Constants::SECRET, '044590', 2, - 26213400 - ) + 26213400, + ), ); // 26213402 Assert::assertFalse( @@ -365,8 +439,8 @@ public function testVerifiesSha256Keys(): void Constants::SECRET, '576276', 2, - 26213400 - ) + 26213400, + ), ); // 26213403 Assert::assertFalse( @@ -374,8 +448,8 @@ public function testVerifiesSha256Keys(): void Constants::SECRET, '152688', 2, - 26213400 - ) + 26213400, + ), ); // 26213397 } @@ -389,8 +463,8 @@ public function testVerifiesSha256KeysNewer(): void '758576', 26213401, 2, - 26213400 - ) + 26213400, + ), ); // 26213400 Assert::assertFalse( $this->google2fa->verifyKeyNewer( @@ -398,8 +472,8 @@ public function testVerifiesSha256KeysNewer(): void '935741', 26213401, 2, - 26213400 - ) + 26213400, + ), ); // 26213401 Assert::assertEquals( 26213402, @@ -408,8 +482,8 @@ public function testVerifiesSha256KeysNewer(): void '044590', 26213401, 2, - 26213400 - ) + 26213400, + ), ); // 26213402 Assert::assertFalse( $this->google2fa->verifyKeyNewer( @@ -417,8 +491,8 @@ public function testVerifiesSha256KeysNewer(): void '576276', 26213401, 2, - 26213400 - ) + 26213400, + ), ); // 26213403 Assert::assertEquals( @@ -428,8 +502,8 @@ public function testVerifiesSha256KeysNewer(): void '758576', null, 2, - 26213400 - ) + 26213400, + ), ); // 26213400 Assert::assertEquals( 26213401, @@ -438,8 +512,8 @@ public function testVerifiesSha256KeysNewer(): void '935741', null, 2, - 26213400 - ) + 26213400, + ), ); // 26213401 Assert::assertEquals( 26213402, @@ -448,8 +522,8 @@ public function testVerifiesSha256KeysNewer(): void '044590', null, 2, - 26213400 - ) + 26213400, + ), ); // 26213402 Assert::assertFalse( $this->google2fa->verifyKeyNewer( @@ -457,8 +531,8 @@ public function testVerifiesSha256KeysNewer(): void '576276', null, 2, - 26213400 - ) + 26213400, + ), ); // 26213403 } @@ -474,40 +548,40 @@ public function testVerifiesSha512Keys(): void Constants::SECRET, '772377', 2, - 26213400 - ) + 26213400, + ), ); // 26213398 Assert::assertTrue( $this->google2fa->verifyKey( Constants::SECRET, '048034', 2, - 26213400 - ) + 26213400, + ), ); // 26213399 Assert::assertTrue( $this->google2fa->verifyKey( Constants::SECRET, '752139', 2, - 26213400 - ) + 26213400, + ), ); // 26213400 Assert::assertTrue( $this->google2fa->verifyKey( Constants::SECRET, '572238', 2, - 26213400 - ) + 26213400, + ), ); // 26213401 Assert::assertTrue( $this->google2fa->verifyKey( Constants::SECRET, '424074', 2, - 26213400 - ) + 26213400, + ), ); // 26213402 Assert::assertFalse( @@ -515,16 +589,16 @@ public function testVerifiesSha512Keys(): void Constants::SECRET, '237162', 2, - 26213400 - ) + 26213400, + ), ); // 26213403 Assert::assertFalse( $this->google2fa->verifyKey( Constants::SECRET, '705476', 2, - 26213400 - ) + 26213400, + ), ); // 26213397 } @@ -538,8 +612,8 @@ public function testVerifiesSha512KeysNewer(): void '752139', 26213401, 2, - 26213400 - ) + 26213400, + ), ); // 26213400 Assert::assertFalse( $this->google2fa->verifyKeyNewer( @@ -547,8 +621,8 @@ public function testVerifiesSha512KeysNewer(): void '572238', 26213401, 2, - 26213400 - ) + 26213400, + ), ); // 26213401 Assert::assertEquals( 26213402, @@ -557,8 +631,8 @@ public function testVerifiesSha512KeysNewer(): void '424074', 26213401, 2, - 26213400 - ) + 26213400, + ), ); // 26213402 Assert::assertFalse( $this->google2fa->verifyKeyNewer( @@ -566,8 +640,8 @@ public function testVerifiesSha512KeysNewer(): void '237162', 26213401, 2, - 26213400 - ) + 26213400, + ), ); // 26213403 Assert::assertEquals( @@ -577,8 +651,8 @@ public function testVerifiesSha512KeysNewer(): void '752139', null, 2, - 26213400 - ) + 26213400, + ), ); // 26213400 Assert::assertEquals( 26213401, @@ -587,8 +661,8 @@ public function testVerifiesSha512KeysNewer(): void '572238', null, 2, - 26213400 - ) + 26213400, + ), ); // 26213401 Assert::assertEquals( 26213402, @@ -597,8 +671,8 @@ public function testVerifiesSha512KeysNewer(): void '424074', null, 2, - 26213400 - ) + 26213400, + ), ); // 26213402 Assert::assertFalse( $this->google2fa->verifyKeyNewer( @@ -606,8 +680,8 @@ public function testVerifiesSha512KeysNewer(): void '237162', null, 2, - 26213400 - ) + 26213400, + ), ); // 26213403 } @@ -615,7 +689,7 @@ public function testRemovesInvalidCharsFromSecret(): void { Assert::assertEquals( Constants::SECRET, - $this->google2fa->removeInvalidChars(Constants::SECRET.'!1-@@@') + $this->google2fa->removeInvalidChars(Constants::SECRET . '!1-@@@'), ); } @@ -623,7 +697,7 @@ public function testConvertsToBase32(): void { Assert::assertEquals( 'KBZGCZ3NMFJFQ', - $this->google2fa->toBase32('PragmaRX') + $this->google2fa->toBase32('PragmaRX'), ); } @@ -642,8 +716,8 @@ public function testSetsTheWindow(): void Constants::SECRET, '558854', null, - 26213400 - ) + 26213400, + ), ); $this->google2fa->setWindow(2); @@ -653,47 +727,47 @@ public function testSetsTheWindow(): void Constants::SECRET, '558854', null, - 26213400 - ) + 26213400, + ), ); Assert::assertTrue( $this->google2fa->verifyKey( Constants::SECRET, '558854', null, - 26213399 - ) + 26213399, + ), ); Assert::assertTrue( $this->google2fa->verifyKey( Constants::SECRET, '558854', null, - 26213398 - ) + 26213398, + ), ); Assert::assertTrue( $this->google2fa->verifyKey( Constants::SECRET, '558854', null, - 26213396 - ) + 26213396, + ), ); Assert::assertFalse( $this->google2fa->verifyKey( Constants::SECRET, '558854', null, - 26213395 - ) + 26213395, + ), ); } public function testSetsTheSecret(): void { Assert::assertFalse( - $this->google2fa->verify('558854', Constants::WRONG_SECRET) + $this->google2fa->verify('558854', Constants::WRONG_SECRET), ); $this->google2fa->setWindow(2); @@ -703,8 +777,8 @@ public function testSetsTheSecret(): void '558854', Constants::SECRET, null, - 26213400 - ) + 26213400, + ), ); $this->google2fa->setSecret(Constants::SECRET); @@ -715,27 +789,41 @@ public function testGetsAlgorithm(): void $this->google2fa->setAlgorithm('sha1'); Assert::assertEquals('sha1', $this->google2fa->getAlgorithm()); - Assert::assertEquals(Google2FAConstants::SHA1, $this->google2fa->getAlgorithm()); + Assert::assertEquals( + Google2FAConstants::SHA1, + $this->google2fa->getAlgorithm(), + ); $this->google2fa->setAlgorithm('sha256'); Assert::assertEquals('sha256', $this->google2fa->getAlgorithm()); - Assert::assertEquals(Google2FAConstants::SHA256, $this->google2fa->getAlgorithm()); + Assert::assertEquals( + Google2FAConstants::SHA256, + $this->google2fa->getAlgorithm(), + ); $this->google2fa->setAlgorithm('sha512'); Assert::assertEquals('sha512', $this->google2fa->getAlgorithm()); - Assert::assertEquals(Google2FAConstants::SHA512, $this->google2fa->getAlgorithm()); + Assert::assertEquals( + Google2FAConstants::SHA512, + $this->google2fa->getAlgorithm(), + ); } public function testSetWrongAlgorithm(): void { - $this->expectException(\PragmaRX\Google2FA\Exceptions\InvalidAlgorithmException::class); + $this->expectException( + \PragmaRX\Google2FA\Exceptions\InvalidAlgorithmException::class, + ); $this->google2fa->setAlgorithm('md5'); Assert::assertEquals('sha1', $this->google2fa->getAlgorithm()); - Assert::assertEquals(Google2FAConstants::SHA1, $this->google2fa->getAlgorithm()); + Assert::assertEquals( + Google2FAConstants::SHA1, + $this->google2fa->getAlgorithm(), + ); } public function testGetsKeyRegeneration(): void @@ -763,8 +851,8 @@ public function testGeneratesPasswordsInManyDifferentSizes(): void Constants::SECRET, '558854', null, - 26213400 - ) + 26213400, + ), ); $this->google2fa->setOneTimePasswordLength(7); @@ -774,14 +862,16 @@ public function testGeneratesPasswordsInManyDifferentSizes(): void Constants::SECRET, '8981084', null, - 26213400 - ) + 26213400, + ), ); } public function testShortSecretKey(): void { - $this->expectException(\PragmaRX\Google2FA\Exceptions\SecretKeyTooShortException::class); + $this->expectException( + \PragmaRX\Google2FA\Exceptions\SecretKeyTooShortException::class, + ); $this->google2fa->setEnforceGoogleAuthenticatorCompatibility(false); @@ -789,16 +879,18 @@ public function testShortSecretKey(): void Constants::SHORT_SECRET, '558854', null, - 26213400 + 26213400, ); } public function testValidateKey(): void { - $this->expectException(\PragmaRX\Google2FA\Exceptions\InvalidCharactersException::class); + $this->expectException( + \PragmaRX\Google2FA\Exceptions\InvalidCharactersException::class, + ); Assert::assertTrue( - is_numeric($this->google2fa->getCurrentOtp(Constants::SECRET)) + is_numeric($this->google2fa->getCurrentOtp(Constants::SECRET)), ); $this->google2fa->setEnforceGoogleAuthenticatorCompatibility(false); @@ -808,42 +900,54 @@ public function testValidateKey(): void public function testThrowsBaseException(): void { - $this->expectException(\PragmaRX\Google2FA\Exceptions\Google2FAException::class); + $this->expectException( + \PragmaRX\Google2FA\Exceptions\Google2FAException::class, + ); $this->throwSecretKeyTooShortException(); } public function testThrowsBaseExceptionContract(): void { - $this->expectException(\PragmaRX\Google2FA\Exceptions\Contracts\Google2FA::class); + $this->expectException( + \PragmaRX\Google2FA\Exceptions\Contracts\Google2FA::class, + ); $this->throwSecretKeyTooShortException(); } public function testThrowsSecretKeyTooShortException(): void { - $this->expectException(\PragmaRX\Google2FA\Exceptions\SecretKeyTooShortException::class); + $this->expectException( + \PragmaRX\Google2FA\Exceptions\SecretKeyTooShortException::class, + ); $this->throwSecretKeyTooShortException(); } public function testThrowsSecretKeyTooShortExceptionWhenVerifyingCode(): void { - $this->expectException(\PragmaRX\Google2FA\Exceptions\SecretKeyTooShortException::class); + $this->expectException( + \PragmaRX\Google2FA\Exceptions\SecretKeyTooShortException::class, + ); $this->google2fa->verify('558854', '', null, 26213400); } public function testThrowsSecretKeyTooShortExceptionContract(): void { - $this->expectException(\PragmaRX\Google2FA\Exceptions\Contracts\SecretKeyTooShort::class); + $this->expectException( + \PragmaRX\Google2FA\Exceptions\Contracts\SecretKeyTooShort::class, + ); $this->throwSecretKeyTooShortException(); } public function testThrowsIncompatibleWithGoogleAuthenticatorExceptionInterface(): void { - $this->expectException(\PragmaRX\Google2FA\Exceptions\Contracts\IncompatibleWithGoogleAuthenticator::class); + $this->expectException( + \PragmaRX\Google2FA\Exceptions\Contracts\IncompatibleWithGoogleAuthenticator::class, + ); $this->throwIncompatibleWithGoogleAuthenticatorException(); } @@ -856,7 +960,7 @@ public function throwSecretKeyTooShortException(): void Constants::SHORT_SECRET, // <------------- BUG '558854', null, - 26213400 + 26213400, ); } @@ -871,21 +975,25 @@ public function throwIncompatibleWithGoogleAuthenticatorException(): void strlen( $this->google2fa ->setEnforceGoogleAuthenticatorCompatibility(false) - ->generateSecretKey(17) - ) + ->generateSecretKey(17), + ), ); } public function testOoathTotpThrowsSecretKeyTooShortException(): void { - $this->expectException(\PragmaRX\Google2FA\Exceptions\SecretKeyTooShortException::class); + $this->expectException( + \PragmaRX\Google2FA\Exceptions\SecretKeyTooShortException::class, + ); $this->google2fa->oathTotp('', 0); } public function testOathTruncateThrowsInvalidHashException(): void { - $this->expectException(\PragmaRX\Google2FA\Exceptions\InvalidHashException::class); + $this->expectException( + \PragmaRX\Google2FA\Exceptions\InvalidHashException::class, + ); $this->google2fa->oathTruncate('foo'); } diff --git a/tools/linters.sh b/tools/linters.sh index 92b5f8f..e1c00d9 100755 --- a/tools/linters.sh +++ b/tools/linters.sh @@ -39,6 +39,10 @@ main() { psalm fi + if [ "$SERVICE" = "all" ] || [ "$SERVICE" = "phpunit" ]; then + phpunit + fi + checkStatus "$@" } @@ -74,6 +78,10 @@ init() { if [ -z ${PSALM+x} ]; then PSALM="vendor/bin/psalm" fi + + if [ -z ${PHPUNIT+x} ]; then + PHPUNIT="vendor/bin/phpunit" + fi } phpcsfixer() { @@ -274,6 +282,34 @@ psalm() { WAS_EXECUTED="$PSALM" } +phpunit() { + message "Running PHPUnit..." + + checkExecutable "PHPUnit" $PHPUNIT + + LOGFILE="$LOGS_PATH/phpunit.log" + PATHS_FILE="$TEMP_PATH/phpunit.files.txt" + + if [ "$FILES" = "." ]; then + if ! $PHPUNIT >>$LOGFILE; then + fatalError "PHPUnit finished with errors. Check the log file: $LOGFILE" + fi + else + if test -f "$PATHS_FILE"; then + \rm "$PATHS_FILE" + fi + + for FILE in $FILES; do + if ! $PHPUNIT analyse "$FILE" >$LOGFILE 2>&1; then + fatalError "PHPUnit finished with errors. Check the log file: $LOGFILE" + fi + done + + fi + + WAS_EXECUTED="$PHPUNIT" +} + buildArguments "$@" for COMMAND in $COMMANDS; do From 7a140a9ee06d454f62b5c3920bf86cebe27c8ec9 Mon Sep 17 00:00:00 2001 From: Antonio Ribeiro Date: Sun, 20 Oct 2024 13:55:46 +0200 Subject: [PATCH 2/2] Update instructions --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 96bfa6c..04bf08e 100644 --- a/README.md +++ b/README.md @@ -411,7 +411,9 @@ Pull requests and issues are more than welcome. ### Contribution setup -If you want to contribute, make sure code is checked, tested, linted and formatted before pushing it. Please install NPM packages to setup a husky pre-commit hook that will help you with that: +If you want to contribute, make sure code is checked, tested, linted and formatted before pushing it. + +Please install NPM packages to setup a husky pre-commit hook that will help you with that: ```bash npm install