From d1beaa9a7ca2a67a2e6202e8b0836eae8f156d6e Mon Sep 17 00:00:00 2001 From: Michael Bolli Date: Wed, 17 Aug 2022 17:53:59 +0300 Subject: [PATCH 1/2] migrate to PHPUnit 8 --- composer.json | 3 +-- tests/ValidateTest.php | 26 ++++++++++++++------------ 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/composer.json b/composer.json index 40c3746..c7f7227 100644 --- a/composer.json +++ b/composer.json @@ -16,8 +16,7 @@ "php":">=5.3.1" }, "require-dev": { - "phpunit/phpunit": "4.3.2", - "codeclimate/php-test-reporter": "dev-master" + "phpunit/phpunit": "8.*" }, "autoload":{ "psr-0":{ diff --git a/tests/ValidateTest.php b/tests/ValidateTest.php index 19f828b..cdbdb84 100644 --- a/tests/ValidateTest.php +++ b/tests/ValidateTest.php @@ -1,16 +1,18 @@ validate = new \Yubikey\Validate($this->apiKey, $this->clientId); + $this->validate = new Validate($this->apiKey, $this->clientId); } /** @@ -30,10 +32,10 @@ public function testGetSetApiKey() /** * Test the setting of a non-base64 encoded API key * @covers \Yubikey\Validate::setApiKey - * @expectedException \InvalidArgumentException */ public function testSetInvalidApiKey() { + $this->expectException(InvalidArgumentException::class); $key = 'testing1234^%$#^#'; $this->validate->setApiKey($key); } @@ -65,7 +67,7 @@ public function testGetSetClientId() } /** - * Test thta the getter/setter for the "use secure" setting works correctly + * Test that the getter/setter for the "use secure" setting works correctly * @covers \Yubikey\Validate::setUseSecure * @covers \Yubikey\Validate::getUseSecure */ @@ -78,14 +80,14 @@ public function testGetSetUseSecure() } /** - * Test that an exception is thrown when the "use secure" valus + * Test that an exception is thrown when the "use secure" values * is not boolean * - * @expectedException \InvalidArgumentException * @covers \Yubikey\Validate::setUseSecure */ public function testSetUseSecureInvalid() { + $this->expectException(InvalidArgumentException::class); $useSecure = 'invalid'; $this->validate->setUseSecure($useSecure); } @@ -133,13 +135,13 @@ public function testSignatureGenerate() /** * Test that an exception is thrown when the API is invalid (null or empty) * @covers \Yubikey\Validate::generateSignature - * @expectedException \InvalidArgumentException */ public function testSignatureGenerateNoApiKey() { + $this->expectException(InvalidArgumentException::class); $key = null; $data = array('foo' => 'bar'); - $validate = new \Yubikey\Validate($key, $this->clientId); + $validate = new Validate($key, $this->clientId); $hash = preg_replace( '/\+/', '%2B', base64_encode(hash_hmac('sha1', http_build_query($data), $key, true)) From 9f2c0b057274214218f7e8d5ad25505222e7a7be Mon Sep 17 00:00:00 2001 From: Michael Bolli Date: Mon, 9 Jan 2023 16:36:05 +0100 Subject: [PATCH 2/2] phpunit: migrate to 9.5 --- composer.json | 8 ++- tests/ValidateTest.php | 111 ++++++++++++++++++++++++----------------- tests/bootstrap.php | 2 +- 3 files changed, 71 insertions(+), 50 deletions(-) diff --git a/composer.json b/composer.json index c7f7227..c1b4c03 100644 --- a/composer.json +++ b/composer.json @@ -13,10 +13,14 @@ } ], "require":{ - "php":">=5.3.1" + "php":">=7.4" }, "require-dev": { - "phpunit/phpunit": "8.*" + "phpunit/phpunit": "^9.5" + }, + "scripts": { + "test": "phpunit tests", + "test-logic": "@php test.php" }, "autoload":{ "psr-0":{ diff --git a/tests/ValidateTest.php b/tests/ValidateTest.php index cdbdb84..3be268d 100644 --- a/tests/ValidateTest.php +++ b/tests/ValidateTest.php @@ -2,7 +2,12 @@ use Yubikey\Validate; -class ValidateTest extends \PHPUnit\Framework\TestCase +/** + * @internal + * + * @coversNothing + */ +final class ValidateTest extends \PHPUnit\Framework\TestCase { private ?Validate $validate = null; @@ -10,30 +15,32 @@ class ValidateTest extends \PHPUnit\Framework\TestCase private int $clientId = 12345; - public function setUp(): void + protected function setUp(): void { $this->validate = new Validate($this->apiKey, $this->clientId); } /** - * Test the getter and setter for the API key - * @covers \Yubikey\Validate::setApiKey + * Test the getter and setter for the API key. + * * @covers \Yubikey\Validate::getApiKey + * @covers \Yubikey\Validate::setApiKey */ - public function testGetSetApiKey() + public function testGetSetApiKey(): void { $preKey = 'testing1234567890'; $key = base64_encode($preKey); $this->validate->setApiKey($key); - $this->assertEquals($this->validate->getApiKey(), $preKey); + static::assertSame($this->validate->getApiKey(), $preKey); } /** - * Test the setting of a non-base64 encoded API key + * Test the setting of a non-base64 encoded API key. + * * @covers \Yubikey\Validate::setApiKey */ - public function testSetInvalidApiKey() + public function testSetInvalidApiKey(): void { $this->expectException(InvalidArgumentException::class); $key = 'testing1234^%$#^#'; @@ -41,51 +48,54 @@ public function testSetInvalidApiKey() } /** - * Test the getter and setter for the One-time password - * @covers \Yubikey\Validate::setOtp + * Test the getter and setter for the One-time password. + * * @covers \Yubikey\Validate::getOtp + * @covers \Yubikey\Validate::setOtp */ - public function testGetSetOtp() + public function testGetSetOtp(): void { $otp = base64_encode('testing1234567890'); $this->validate->setOtp($otp); - $this->assertEquals($this->validate->getOtp(), $otp); + static::assertSame($this->validate->getOtp(), $otp); } /** - * Test that the getter/setter for the Client ID works correctly + * Test that the getter/setter for the Client ID works correctly. + * * @covers \Yubikey\Validate::getClientId * @covers \Yubikey\Validate::setClientId */ - public function testGetSetClientId() + public function testGetSetClientId(): void { $clientId = 12345; $this->validate->setClientId($clientId); - $this->assertEquals($clientId, $this->validate->getClientId()); + static::assertSame($clientId, $this->validate->getClientId()); } /** - * Test that the getter/setter for the "use secure" setting works correctly - * @covers \Yubikey\Validate::setUseSecure + * Test that the getter/setter for the "use secure" setting works correctly. + * * @covers \Yubikey\Validate::getUseSecure + * @covers \Yubikey\Validate::setUseSecure */ - public function testGetSetUseSecure() + public function testGetSetUseSecure(): void { $useSecure = true; $this->validate->setUseSecure($useSecure); - $this->assertEquals($useSecure, $this->validate->getUseSecure()); + static::assertSame($useSecure, $this->validate->getUseSecure()); } /** * Test that an exception is thrown when the "use secure" values - * is not boolean + * is not boolean. * * @covers \Yubikey\Validate::setUseSecure */ - public function testSetUseSecureInvalid() + public function testSetUseSecureInvalid(): void { $this->expectException(InvalidArgumentException::class); $useSecure = 'invalid'; @@ -93,57 +103,63 @@ public function testSetUseSecureInvalid() } /** - * Test that the getter/setter for the host works correctly - * @covers \Yubikey\Validate::setHost + * Test that the getter/setter for the host works correctly. + * * @covers \Yubikey\Validate::getHost + * @covers \Yubikey\Validate::setHost */ - public function testGetSetHost() + public function testGetSetHost(): void { $host = 'test.foo.com'; $this->validate->setHost($host); - $this->assertEquals($this->validate->getHost(), $host); + static::assertSame($this->validate->getHost(), $host); } /** - * Test that a valid random host is selected if none was previously set + * Test that a valid random host is selected if none was previously set. + * * @covers \Yubikey\Validate::getHost */ - public function testGetRandomHost() + public function testGetRandomHost(): void { $host1 = $this->validate->getHost(); - $this->assertNotEquals($host1, null); + static::assertNotSame($host1, null); } /** - * Test that the signature generation is valid + * Test that the signature generation is valid. + * * @covers \Yubikey\Validate::generateSignature */ - public function testSignatureGenerate() + public function testSignatureGenerate(): void { - $data = array('foo' => 'bar'); + $data = ['foo' => 'bar']; $key = $this->validate->getApiKey(); $hash = preg_replace( - '/\+/', '%2B', + '/\+/', + '%2B', base64_encode(hash_hmac('sha1', http_build_query($data), $key, true)) ); $signature = $this->validate->generateSignature($data); - $this->assertEquals($hash, $signature); + static::assertSame($hash, $signature); } /** - * Test that an exception is thrown when the API is invalid (null or empty) + * Test that an exception is thrown when the API is invalid (null or empty). + * * @covers \Yubikey\Validate::generateSignature */ - public function testSignatureGenerateNoApiKey() + public function testSignatureGenerateNoApiKey(): void { $this->expectException(InvalidArgumentException::class); $key = null; - $data = array('foo' => 'bar'); + $data = ['foo' => 'bar']; $validate = new Validate($key, $this->clientId); $hash = preg_replace( - '/\+/', '%2B', + '/\+/', + '%2B', base64_encode(hash_hmac('sha1', http_build_query($data), $key, true)) ); @@ -151,31 +167,32 @@ public function testSignatureGenerateNoApiKey() } /** - * Add a new Host to the list + * Add a new Host to the list. + * * @covers \Yubikey\Validate::addHost */ - public function testAddNewHost() + public function testAddNewHost(): void { $this->validate->addHost('test.com'); - $this->assertTrue( - in_array('test.com', $this->validate->getHosts()) + static::assertTrue( + in_array('test.com', $this->validate->getHosts(), true) ); } /** - * Set the new Hosts list (override) - * @covers \Yubikey\Validate::setHosts + * Set the new Hosts list (override). + * * @covers \Yubikey\Validate::getHosts + * @covers \Yubikey\Validate::setHosts */ - public function testSetHosts() + public function testSetHosts(): void { - $hosts = array('foo.com'); + $hosts = ['foo.com']; $this->validate->setHosts($hosts); - $this->assertEquals( + static::assertSame( $this->validate->getHosts(), $hosts ); } } - diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 8012e03..ef01f53 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,7 +1,7 @@