Skip to content

Commit

Permalink
Rename exception
Browse files Browse the repository at this point in the history
  • Loading branch information
Stadly committed Dec 28, 2018
1 parent f37569e commit 732f204
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/Policy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
{
Expand All @@ -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
{
Expand Down
6 changes: 3 additions & 3 deletions src/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;

Expand All @@ -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;
}
4 changes: 2 additions & 2 deletions src/Rule/Dictionary.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions src/Rule/TestException.php → src/Rule/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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
{
Expand Down
4 changes: 2 additions & 2 deletions src/Rule/HaveIBeenPwned.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/Rule/DictionaryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ public function testTestThrowsExceptionWhenWordListThrowsException(): void

$rule = new Dictionary($wordList);

$this->expectException(TestException::class);
$this->expectException(Exception::class);

$rule->test('foo');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
use Stadly\PasswordPolice\Rule;

/**
* @coversDefaultClass \Stadly\PasswordPolice\Rule\TestException
* @coversDefaultClass \Stadly\PasswordPolice\Rule\Exception
* @covers ::<protected>
* @covers ::<private>
*/
final class TestExceptionTest extends TestCase
final class ExceptionTest extends TestCase
{
/**
* @var MockObject&Rule
Expand All @@ -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);
}

Expand All @@ -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());
}
Expand Down
6 changes: 3 additions & 3 deletions tests/Rule/HaveIBeenPwnedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -245,15 +245,15 @@ 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);

$rule = new HaveIBeenPwned(5, 0);
$rule->setClient($client);

$this->expectException(TestException::class);
$this->expectException(Exception::class);

$rule->test('291vnnzrvtu9');
}
Expand Down

0 comments on commit 732f204

Please sign in to comment.