From 732f2042dd35907b0b477147adf04a945af2b004 Mon Sep 17 00:00:00 2001 From: Stadly Date: Fri, 28 Dec 2018 10:45:01 +0100 Subject: [PATCH] Rename exception --- CHANGELOG.md | 1 + src/Policy.php | 6 +++--- src/Rule.php | 6 +++--- src/Rule/Dictionary.php | 4 ++-- src/Rule/{TestException.php => Exception.php} | 10 +++++----- src/Rule/HaveIBeenPwned.php | 4 ++-- tests/Rule/DictionaryTest.php | 2 +- .../Rule/{TestExceptionTest.php => ExceptionTest.php} | 10 +++++----- tests/Rule/HaveIBeenPwnedTest.php | 6 +++--- 9 files changed, 25 insertions(+), 24 deletions(-) rename src/Rule/{TestException.php => Exception.php} (66%) rename tests/Rule/{TestExceptionTest.php => ExceptionTest.php} (72%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 99a795d..eb0812e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip ### Changed - Remove `Interface` suffix from interfaces. - Move interfaces one level up in the namespace hierarchy. +- Rename `TestException` to `Exception`. ### Fixed - Nothing diff --git a/src/Policy.php b/src/Policy.php index 680706b..705ed5a 100644 --- a/src/Policy.php +++ b/src/Policy.php @@ -4,7 +4,7 @@ namespace Stadly\PasswordPolice; -use Stadly\PasswordPolice\Rule\TestException; +use Stadly\PasswordPolice\Rule\Exception; use Symfony\Component\Translation\Translator; use Symfony\Contracts\Translation\LocaleAwareInterface; use Symfony\Contracts\Translation\TranslatorInterface; @@ -44,7 +44,7 @@ public function addRules(Rule... $rules): void * * @param Password|string $password Password to check. * @return bool Whether the password is in compliance with the policy. - * @throws TestException If an error occurred while testing the policy. + * @throws Exception If an error occurred. */ public function test($password): bool { @@ -62,7 +62,7 @@ public function test($password): bool * * @param Password|string $password Password to validate. * @return ValidationError[] Validation errors describing why the password is not in compliance with the policy. - * @throws TestException If an error occurred while testing the policy. + * @throws Exception If an error occurred. */ public function validate($password): array { diff --git a/src/Rule.php b/src/Rule.php index ec637df..27c57c6 100644 --- a/src/Rule.php +++ b/src/Rule.php @@ -4,7 +4,7 @@ namespace Stadly\PasswordPolice; -use Stadly\PasswordPolice\Rule\TestException; +use Stadly\PasswordPolice\Rule\Exception; /** * Interface that must be implemented by all rules. @@ -17,7 +17,7 @@ interface Rule * @param Password|string $password Password to check. * @param int|null $weight Don't consider constraints with lower weights. * @return bool Whether the password is in compliance with the rule. - * @throws TestException If an error occurred while testing the rule. + * @throws Exception If an error occurred. */ public function test($password, ?int $weight = 1): bool; @@ -26,7 +26,7 @@ public function test($password, ?int $weight = 1): bool; * * @param Password|string $password Password to validate. * @return ValidationError|null Validation error describing why the password is not in compliance with the rule. - * @throws TestException If an error occurred while testing the rule. + * @throws Exception If an error occurred. */ public function validate($password): ?ValidationError; } diff --git a/src/Rule/Dictionary.php b/src/Rule/Dictionary.php index db43a08..52adbfa 100644 --- a/src/Rule/Dictionary.php +++ b/src/Rule/Dictionary.php @@ -136,7 +136,7 @@ public function validate($password): ?ValidationError /** * @param string $password Password to find dictionary words in. * @return string|null Dictionary word in the password. - * @throws TestException If an error occurred while using the word list. + * @throws Exception If an error occurred. */ private function getDictionaryWord(string $password): ?string { @@ -146,7 +146,7 @@ private function getDictionaryWord(string $password): ?string return $word; } } catch (RuntimeException $exception) { - throw new TestException( + throw new Exception( $this, 'An error occurred while using the word list: '.$exception->getMessage(), $exception diff --git a/src/Rule/TestException.php b/src/Rule/Exception.php similarity index 66% rename from src/Rule/TestException.php rename to src/Rule/Exception.php index 6acc7c5..ba380c5 100644 --- a/src/Rule/TestException.php +++ b/src/Rule/Exception.php @@ -9,17 +9,17 @@ use Throwable; /** - * Exception thrown if an error occurred while testing a rule. + * Exception thrown if a rule caused an error. */ -final class TestException extends RuntimeException +final class Exception extends RuntimeException { /** - * @var Rule Rule that was tested when the error occurred. + * @var Rule Rule that caused the error. */ private $rule; /** - * @param Rule $rule Rule that was tested when the error occurred. + * @param Rule $rule Rule that caused the error. * @param string $message Exception message. * @param Throwable|null $previous Previous exception, used for exception chaining. */ @@ -31,7 +31,7 @@ public function __construct(Rule $rule, string $message, ?Throwable $previous = } /** - * @return Rule Rule that was tested when the error occurred. + * @return Rule Rule that caused the error. */ public function getRule(): Rule { diff --git a/src/Rule/HaveIBeenPwned.php b/src/Rule/HaveIBeenPwned.php index 4442aa3..f63a61b 100644 --- a/src/Rule/HaveIBeenPwned.php +++ b/src/Rule/HaveIBeenPwned.php @@ -154,7 +154,7 @@ private function getViolation(int $count, ?int $weight = null): ?Count /** * @param string $password Password to check in breaches. * @return int Number of appearances in breaches. - * @throws TestException If an error occurred while using the Have I Been Pwned? service. + * @throws Exception If an error occurred. */ private function getCount(string $password): int { @@ -179,7 +179,7 @@ private function getCount(string $password): int } return 0; } catch (ClientExceptionInterface | RuntimeException $exception) { - throw new TestException( + throw new Exception( $this, 'An error occurred while using the Have I Been Pwned? service: '.$exception->getMessage(), $exception diff --git a/tests/Rule/DictionaryTest.php b/tests/Rule/DictionaryTest.php index 6c390c6..742d106 100644 --- a/tests/Rule/DictionaryTest.php +++ b/tests/Rule/DictionaryTest.php @@ -309,7 +309,7 @@ public function testTestThrowsExceptionWhenWordListThrowsException(): void $rule = new Dictionary($wordList); - $this->expectException(TestException::class); + $this->expectException(Exception::class); $rule->test('foo'); } diff --git a/tests/Rule/TestExceptionTest.php b/tests/Rule/ExceptionTest.php similarity index 72% rename from tests/Rule/TestExceptionTest.php rename to tests/Rule/ExceptionTest.php index b97ef49..1eec33f 100644 --- a/tests/Rule/TestExceptionTest.php +++ b/tests/Rule/ExceptionTest.php @@ -9,11 +9,11 @@ use Stadly\PasswordPolice\Rule; /** - * @coversDefaultClass \Stadly\PasswordPolice\Rule\TestException + * @coversDefaultClass \Stadly\PasswordPolice\Rule\Exception * @covers :: * @covers :: */ -final class TestExceptionTest extends TestCase +final class ExceptionTest extends TestCase { /** * @var MockObject&Rule @@ -30,10 +30,10 @@ protected function setUp(): void */ public function testCanConstructException(): void { - $exception = new TestException($this->rule, 'foo'); + $exception = new Exception($this->rule, 'foo'); // Force generation of code coverage - $exceptionConstruct = new TestException($this->rule, 'foo'); + $exceptionConstruct = new Exception($this->rule, 'foo'); self::assertEquals($exception, $exceptionConstruct); } @@ -42,7 +42,7 @@ public function testCanConstructException(): void */ public function testCanGetRule(): void { - $exception = new TestException($this->rule, 'foo'); + $exception = new Exception($this->rule, 'foo'); self::assertSame($this->rule, $exception->getRule()); } diff --git a/tests/Rule/HaveIBeenPwnedTest.php b/tests/Rule/HaveIBeenPwnedTest.php index 5d6a27d..f638b17 100644 --- a/tests/Rule/HaveIBeenPwnedTest.php +++ b/tests/Rule/HaveIBeenPwnedTest.php @@ -7,7 +7,7 @@ use Http\Factory\Discovery\ClientLocator; use Http\Factory\Discovery\FactoryLocator; use InvalidArgumentException; -use PHPUnit\Framework\MockObject\Stub\Exception; +use PHPUnit\Framework\MockObject\Stub\Exception as StubException; use PHPUnit\Framework\TestCase; use Psr\Http\Client\ClientExceptionInterface; use Psr\Http\Client\ClientInterface; @@ -245,7 +245,7 @@ public function testRuleIsSatisfiedWhenConstraintWeightIsLowerThanTestWeight(): public function testErrorsWhenCalculatingCountAreHandled(): void { $exception = $this->createMock(ClientExceptionInterface::class); - $stubException = new Exception($exception); + $stubException = new StubException($exception); $client = $this->createMock(ClientInterface::class); $client->method('sendRequest')->will($stubException); @@ -253,7 +253,7 @@ public function testErrorsWhenCalculatingCountAreHandled(): void $rule = new HaveIBeenPwned(5, 0); $rule->setClient($client); - $this->expectException(TestException::class); + $this->expectException(Exception::class); $rule->test('291vnnzrvtu9'); }