Skip to content

Commit

Permalink
- Issue #5: Static variables prevents running validation with differe…
Browse files Browse the repository at this point in the history
…nt configuration
  • Loading branch information
stymiee committed Apr 6, 2024
1 parent e80e6bb commit 3893f67
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 17 deletions.
7 changes: 3 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [1.1.4] - 2024-XX-XX

### Changed
- Issue #6: `googlemail.com` is now recognized as a Gmail address
- Issue #6: `.` are now removed when sanitizing Gmail addresses (to get to the root email address)

- CHANGELOG format

### Fixed
- Issue #5: Static variables prevents running validation with different configurations
- Issue #6: `googlemail.com` is now recognized as a Gmail address
- Issue #6: `.` are now removed when sanitizing Gmail addresses (to get to the root email address)

- Issue #6: `googlemail.com` is now considered a Gmail email address

## [1.1.3] - 2022-10-12

Expand Down
12 changes: 8 additions & 4 deletions src/EmailValidator/Validator/DisposableEmailValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

class DisposableEmailValidator extends AProviderValidator
{
/**
* @var array Array of client-provided disposable email providers.
*/
protected $disposableEmailListProviders = [];

/**
* @var array Array of URLs containing a list of disposable email addresses and the format of that list.
*/
Expand All @@ -33,15 +38,14 @@ public function validate(EmailAddress $email): bool
{
$valid = true;
if ($this->policy->checkDisposableEmail()) {
static $disposableEmailListProviders;
if ($disposableEmailListProviders === null) {
$disposableEmailListProviders = $this->getList(
if ($this->disposableEmailListProviders === []) {
$this->disposableEmailListProviders = $this->getList(
$this->policy->checkDisposableLocalListOnly(),
$this->policy->getDisposableList()
);
}
$domain = $email->getDomain();
$valid = !in_array($domain, $disposableEmailListProviders, true);
$valid = !in_array($domain, $this->disposableEmailListProviders, true);
}
return $valid;
}
Expand Down
12 changes: 8 additions & 4 deletions src/EmailValidator/Validator/FreeEmailValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

class FreeEmailValidator extends AProviderValidator
{
/**
* @var array Array of client-provided free email providers.
*/
protected $freeEmailListProviders = [];

/**
* @var array Array of URLs containing a list of free email addresses and the format of that list.
*/
Expand All @@ -29,15 +34,14 @@ public function validate(EmailAddress $email): bool
{
$valid = true;
if ($this->policy->checkFreeEmail()) {
static $freeEmailListProviders;
if ($freeEmailListProviders === null) {
$freeEmailListProviders = $this->getList(
if ($this->freeEmailListProviders === []) {
$this->freeEmailListProviders = $this->getList(
$this->policy->checkFreeLocalListOnly(),
$this->policy->getFreeList()
);
}
$domain = $email->getDomain();
$valid = !in_array($domain, $freeEmailListProviders, true);
$valid = !in_array($domain, $this->freeEmailListProviders, true);
}
return $valid;
}
Expand Down
16 changes: 14 additions & 2 deletions tests/EmailValidator/Validator/DisposableEmailValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,24 @@ public function testValidateExplicit(): void
'checkDisposableEmail' => false
];
$validator = new DisposableEmailValidator(new Policy($policy));
self::assertEquals(true, $validator->validate(new EmailAddress('[email protected]')));
self::assertTrue($validator->validate(new EmailAddress('[email protected]')));
}

public function testValidateDefault(): void
{
$validator = new DisposableEmailValidator(new Policy());
self::assertEquals(true, $validator->validate(new EmailAddress('[email protected]')));
self::assertTrue($validator->validate(new EmailAddress('[email protected]')));
}

public function testValidateClientProvidedDomain(): void
{
$policy = [
'checkDisposableEmail' => true,
'disposableList' => [
'example.com'
],
];
$validator = new DisposableEmailValidator(new Policy($policy));
self::assertFalse($validator->validate(new EmailAddress('[email protected]')));
}
}
16 changes: 14 additions & 2 deletions tests/EmailValidator/Validator/FreeEmailValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,24 @@ public function testValidateExplicit(): void
'checkFreeEmail' => false
];
$validator = new FreeEmailValidator(new Policy($policy));
self::assertEquals(true, $validator->validate(new EmailAddress('[email protected]')));
self::assertTrue($validator->validate(new EmailAddress('[email protected]')));
}

public function testValidateDefault(): void
{
$validator = new FreeEmailValidator(new Policy());
self::assertEquals(true, $validator->validate(new EmailAddress('[email protected]')));
self::assertTrue($validator->validate(new EmailAddress('[email protected]')));
}

public function testValidateClientProvidedDomain(): void
{
$policy = [
'checkFreeEmail' => true,
'freeList' => [
'example.com'
],
];
$validator = new FreeEmailValidator(new Policy($policy));
self::assertFalse($validator->validate(new EmailAddress('[email protected]')));
}
}
11 changes: 10 additions & 1 deletion tests/EmailValidator/Validator/MxValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ public function testValidate(): void
'checkMxRecords' => false
];
$validator = new MxValidator(new Policy($policy));
self::assertEquals(true, $validator->validate(new EmailAddress('[email protected]')));
self::assertTrue($validator->validate(new EmailAddress('[email protected]')));
}

public function testValidateDns(): void
{
$policy = [
'checkMxRecords' => true
];
$validator = new MxValidator(new Policy($policy));
self::assertTrue($validator->validate(new EmailAddress('[email protected]')));
}
}

0 comments on commit 3893f67

Please sign in to comment.