Skip to content

Commit

Permalink
add-last-updated-timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-tebiev committed May 29, 2024
1 parent ef3d760 commit 1232165
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 1 deletion.
1 change: 1 addition & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
PhpCsFixerCustomFixers\Fixer\PhpdocSelfAccessorFixer::name() => true,
// Calls to `PHPUnit\Framework\TestCase` static methods must all be of the same type, either `$this->`, `self::` or `static::`.
'php_unit_test_case_static_method_calls' => ['call_type' => 'self'],
'php_unit_strict' => false,
])
->setFinder(
PhpCsFixer\Finder::create()
Expand Down
3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
<testsuite name="Unit">
<directory>./tests/Unit/</directory>
</testsuite>
<testsuite name="Feature">
<directory>./tests/Feature/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
Expand Down
112 changes: 112 additions & 0 deletions tests/Feature/MainFunctionalityTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

declare(strict_types=1);

namespace Beeyev\DisposableEmailFilter\Tests\Feature;

use Beeyev\DisposableEmailFilter\DisposableEmailFilter;
use Beeyev\DisposableEmailFilter\Tests\AbstractTestCase;
use Beeyev\DisposableEmailFilter\Updater\Config;
use Beeyev\DisposableEmailFilter\Updater\Support\DomainsExtractor;

/**
* @internal
* @coversNothing
*/
final class MainFunctionalityTest extends AbstractTestCase
{
/** @var non-empty-string */
private $randomBlackListedEmail;

/** @var non-empty-string */
private $randomWhitelistedEmail;

/** @var non-empty-string */
private $randomDisposableEmail;

/** @var DisposableEmailFilter */
private $disposableEmailFilter;

protected function setUp(): void
{
$this->randomBlackListedEmail = 'abc@' . $this->getRandomDomain(Config::LOCAL_BLACKLIST_PATH);
$this->randomWhitelistedEmail = 'abc@' . $this->getRandomDomain(Config::WHITELIST_PATH);
$this->randomDisposableEmail = 'abc@' . $this->getRandomDomain(Config::TXT_BLACKLIST_PATH);

$this->disposableEmailFilter = new DisposableEmailFilter();
}

public function testWhitelistedDomain(): void
{
$result = $this->disposableEmailFilter->isDisposableEmailAddress($this->randomWhitelistedEmail);

self::assertFalse($result);
}

public function testBlacklistedDomain(): void
{
$result = $this->disposableEmailFilter->isDisposableEmailAddress($this->randomBlackListedEmail);

self::assertTrue($result);
}

public function testDisposableDomainIsDisposable(): void
{
$result = $this->disposableEmailFilter->isDisposableEmailAddress($this->randomDisposableEmail);

self::assertTrue($result);
}

public function testNonDisposableDomain(): void
{
$result = $this->disposableEmailFilter->isDisposableEmailAddress('[email protected]');

self::assertFalse($result);
}

/**
* @param non-empty-string $filePath
*
* @return non-empty-string
*/
private function getRandomDomain(string $filePath): string
{
$fileContents = $this->loafFirstLinesOfFile($filePath, 50);
$domains = DomainsExtractor::toArray($fileContents);

$result = $domains[array_rand($domains)];
assert(is_string($result) && $result !== '');

return $result;
}

/**
* @param non-empty-string $filePath
* @param positive-int $linesCount
*
* @return non-empty-string
*/
private function loafFirstLinesOfFile(string $filePath, int $linesCount): string
{
$lines = [];
$fh = fopen($filePath, 'r');
assert($fh !== false, 'Could not open file: ' . $filePath);

for ($i = 0; $i < $linesCount; ++$i) {
$line = fgets($fh);
if ($line === false) {
break;
}

$lines[] = str_replace(["\r", "\n"], '', $line);
}

if (fclose($fh) === false) {
throw new \RuntimeException('Could not close file: ' . $filePath);
}

assert(count($lines) > 0);

return implode(PHP_EOL, $lines);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function testUpdatedDateTimeCanBeRetrieved(): void
$disposableEmailDomains = new DisposableEmailDomains(self::DISPOSABLE_EMAIL_DOMAINS_FILE_PATH);

$currentDateTime = new \DateTimeImmutable('2024-05-29 12:24:39.562100', new \DateTimeZone('UTC'));
self::assertSame($currentDateTime, $disposableEmailDomains->getUpdatedDateTime());
self::assertEquals($currentDateTime, $disposableEmailDomains->getUpdatedDateTime());
self::assertSame('UTC', $disposableEmailDomains->getUpdatedDateTime()->getTimezone()->getName());
}

Expand Down

0 comments on commit 1232165

Please sign in to comment.