From b72875054303d61758704aad045fe8c018c4340e Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Thu, 18 Jun 2020 01:12:18 +0100 Subject: [PATCH] Add validation for incorrect config data --- src/Config.php | 8 ++++++++ tests/ConfigTest.php | 46 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/src/Config.php b/src/Config.php index bd71c30..32124c1 100644 --- a/src/Config.php +++ b/src/Config.php @@ -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; diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php index 2196733..cb8622e 100644 --- a/tests/ConfigTest.php +++ b/tests/ConfigTest.php @@ -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); + } + }