Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrate to PHPUnit 9 #29

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@
}
],
"require":{
"php":">=5.3.1"
"php":">=7.4"
},
"require-dev": {
"phpunit/phpunit": "4.3.2",
"codeclimate/php-test-reporter": "dev-master"
"phpunit/phpunit": "^9.5"
},
"scripts": {
"test": "phpunit tests",
"test-logic": "@php test.php"
},
"autoload":{
"psr-0":{
Expand Down
131 changes: 75 additions & 56 deletions tests/ValidateTest.php
Original file line number Diff line number Diff line change
@@ -1,179 +1,198 @@
<?php

class ValidateTest extends \PHPUnit_Framework_TestCase
use Yubikey\Validate;

/**
* @internal
*
* @coversNothing
*/
final class ValidateTest extends \PHPUnit\Framework\TestCase
{
private $validate = null;
private ?Validate $validate = null;

private $apiKey = 'dGVzdGluZzEyMzQ1Njc4OTA=';
private string $apiKey = 'dGVzdGluZzEyMzQ1Njc4OTA=';

private $clientId = 12345;
private int $clientId = 12345;

public function setUp()
protected function setUp(): void
{
$this->validate = new \Yubikey\Validate($this->apiKey, $this->clientId);
$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
* @expectedException \InvalidArgumentException
*/
public function testSetInvalidApiKey()
public function testSetInvalidApiKey(): void
{
$this->expectException(InvalidArgumentException::class);
$key = 'testing1234^%$#^#';
$this->validate->setApiKey($key);
}

/**
* 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 thta 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" valus
* is not boolean
* Test that an exception is thrown when the "use secure" values
* is not boolean.
*
* @expectedException \InvalidArgumentException
* @covers \Yubikey\Validate::setUseSecure
*/
public function testSetUseSecureInvalid()
public function testSetUseSecureInvalid(): void
{
$this->expectException(InvalidArgumentException::class);
$useSecure = 'invalid';
$this->validate->setUseSecure($useSecure);
}

/**
* 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
* @expectedException \InvalidArgumentException
*/
public function testSignatureGenerateNoApiKey()
public function testSignatureGenerateNoApiKey(): void
{
$this->expectException(InvalidArgumentException::class);
$key = null;
$data = array('foo' => 'bar');
$validate = new \Yubikey\Validate($key, $this->clientId);
$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))
);

$signature = $validate->generateSignature($data);
}

/**
* 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
);
}
}

2 changes: 1 addition & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

// custom autoloader...simple really :)
spl_autoload_register(function($className) {
spl_autoload_register(function($className): void {
$classPath = __DIR__.'/../src/'.str_replace('\\', '/', $className).'.php';
if (is_file($classPath)) {
require_once $classPath;
Expand Down