Add validation for incorrect config data

This commit is contained in:
Oliver Davies 2020-06-18 01:12:18 +01:00
parent fae1e5b32a
commit b728750543
2 changed files with 54 additions and 0 deletions

View file

@ -4,6 +4,8 @@ declare(strict_types=1);
namespace Opdavies\Glassboxx;
use Assert\Assert;
class Config
{
/** @var int $vendorId */
@ -20,6 +22,12 @@ class Config
string $username,
string $password
) {
Assert::that($vendorId)
->greaterOrEqualThan(1, 'Vendor ID cannot be a negative number');
Assert::that($username)->notEmpty('Username cannot be empty');
Assert::that($password)->notEmpty('Password cannot be empty');
$this->password = $password;
$this->username = $username;
$this->vendorId = $vendorId;

View file

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Opdavies\Glassboxx\Tests;
use Assert\AssertionFailedException;
use Opdavies\Glassboxx\Config;
use PHPUnit\Framework\TestCase;
@ -21,4 +22,49 @@ class ConfigTest extends TestCase
$this->assertSame('opdavies', $config->getUsername());
$this->assertSame('secret', $config->getPassword());
}
public function badCreateDataProvider(): array
{
return [
'Negative vendor ID' => [
'vendor id' => -1,
'username' => 'user',
'password' => 'secret',
'message' => 'Vendor ID cannot be a negative number',
],
'Username is an empty string' => [
'vendor id' => 123,
'username' => '',
'password' => 'secret',
'message' => 'Username cannot be empty',
],
'Password is an empty string' => [
'vendor id' => 123,
'username' => 'user',
'password' => '',
'message' => 'Password cannot be empty',
],
];
}
/**
* @dataProvider badCreateDataProvider
*
* @param int $vendorId
* @param string $username
* @param string $password
* @param string $message
*/
public function testThatAnExceptionIsThrownWhenBadData(
int $vendorId,
string $username,
string $password,
string $message
) {
$this->expectException(AssertionFailedException::class);
$this->expectExceptionMessage($message);
new Config($vendorId, $username, $password);
}
}