diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 684d5b4..ed57201 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -39,6 +39,8 @@ jobs: - name: phpunit run: vendor/bin/phpunit + env: + VERIFIER_API_KEY: ${{ secrets.VERIFIER_API_KEY }} - name: php-cs-test run: vendor/bin/php-cs-test diff --git a/CHANGELOG.md b/CHANGELOG.md index 85335d0..3883c8f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to this package will be documented in this file. +## v0.5.0 + +* upgrade `astrotomic/laravel-guzzle` to v2.0.0 + ## v0.4.0 * add https://verifier.meetchopra.com provider `\Elbgoods\TrashmailRule\Providers\VerifierProvider` diff --git a/composer.json b/composer.json index a08def5..bfc149a 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ ], "require": { "php": "^7.4", - "astrotomic/laravel-guzzle": "^1.0.1", + "astrotomic/laravel-guzzle": "^2.0", "illuminate/cache": "^6.0", "illuminate/support": "^6.0" }, diff --git a/config/trashmail.php b/config/trashmail.php index 8d0a8ae..975da9d 100644 --- a/config/trashmail.php +++ b/config/trashmail.php @@ -1,7 +1,5 @@ 'elbgoods.trashmail.dead_letter', 'ttl' => 60 * 60 * 24, // one day ], - 'guzzle' => [ - RequestOptions::TIMEOUT => 10, - ], ], /* @@ -35,9 +30,6 @@ */ 'disposable_email_detector' => [ 'enabled' => false, - 'guzzle' => [ - RequestOptions::TIMEOUT => 5, - ], ], /* @@ -45,10 +37,7 @@ */ 'verifier' => [ 'enabled' => false, - 'api_key' => null, - 'guzzle' => [ - RequestOptions::TIMEOUT => 5, - ], + 'api_key' => env('VERIFIER_API_KEY'), ], /* diff --git a/src/Providers/DeadLetterProvider.php b/src/Providers/DeadLetterProvider.php index 44665ad..1aa9c4f 100644 --- a/src/Providers/DeadLetterProvider.php +++ b/src/Providers/DeadLetterProvider.php @@ -2,14 +2,13 @@ namespace Elbgoods\TrashmailRule\Providers; +use Astrotomic\LaravelGuzzle\Facades\Guzzle; use Elbgoods\TrashmailRule\Contracts\ProviderContract; use Illuminate\Contracts\Cache\Factory as CacheFactory; use Illuminate\Contracts\Cache\Repository as CacheRepository; class DeadLetterProvider implements ProviderContract { - protected const BLACKLIST_URL = 'https://www.dead-letter.email/blacklist_flat.json'; - protected array $config; protected CacheFactory $cache; @@ -59,10 +58,8 @@ protected function getBlacklist(): array protected function loadDeadLetter(): array { - $response = guzzle( - self::BLACKLIST_URL, - $this->config['guzzle'] - )->request('GET', ''); + $response = Guzzle::client('dead-letter.email') + ->request('GET', 'blacklist_flat.json'); $body = $response->getBody()->getContents(); diff --git a/src/Providers/DisposableEmailDetectorProvider.php b/src/Providers/DisposableEmailDetectorProvider.php index 2232350..acca7fd 100644 --- a/src/Providers/DisposableEmailDetectorProvider.php +++ b/src/Providers/DisposableEmailDetectorProvider.php @@ -2,12 +2,11 @@ namespace Elbgoods\TrashmailRule\Providers; +use Astrotomic\LaravelGuzzle\Facades\Guzzle; use Elbgoods\TrashmailRule\Contracts\ProviderContract; class DisposableEmailDetectorProvider implements ProviderContract { - protected const BASE_URL = 'https://api.disposable-email-detector.com/api/dea/v1/check/'; - protected array $config; public function __construct(array $config) @@ -21,10 +20,8 @@ public function isDisposable(string $domain): ?bool return null; } - $response = guzzle( - self::BASE_URL, - $this->config['guzzle'] - )->request('GET', $domain); + $response = Guzzle::client('api.disposable-email-detector.com') + ->request('GET', 'api/dea/v1/check/'.urlencode($domain)); $body = $response->getBody()->getContents(); diff --git a/src/Providers/VerifierProvider.php b/src/Providers/VerifierProvider.php index 341505f..07239da 100644 --- a/src/Providers/VerifierProvider.php +++ b/src/Providers/VerifierProvider.php @@ -2,12 +2,11 @@ namespace Elbgoods\TrashmailRule\Providers; +use Astrotomic\LaravelGuzzle\Facades\Guzzle; use Elbgoods\TrashmailRule\Contracts\ProviderContract; class VerifierProvider implements ProviderContract { - protected const BASE_URL = 'https://verifier.meetchopra.com/verify/'; - protected array $config; public function __construct(array $config) @@ -25,14 +24,12 @@ public function isDisposable(string $domain): ?bool return null; } - $response = guzzle( - self::BASE_URL, - $this->config['guzzle'] - )->request('GET', $domain, [ - 'query' => [ - 'token' => $this->config['api_key'], - ], - ]); + $response = Guzzle::client('verifier.meetchopra.com') + ->request('GET', 'verify/'.urlencode($domain), [ + 'query' => [ + 'token' => $this->config['api_key'], + ], + ]); $body = $response->getBody()->getContents(); diff --git a/src/TrashmailRuleServiceProvider.php b/src/TrashmailRuleServiceProvider.php index 3a27b70..cf5a1e8 100644 --- a/src/TrashmailRuleServiceProvider.php +++ b/src/TrashmailRuleServiceProvider.php @@ -2,6 +2,8 @@ namespace Elbgoods\TrashmailRule; +use Astrotomic\LaravelGuzzle\Facades\Guzzle; +use GuzzleHttp\RequestOptions; use Illuminate\Support\ServiceProvider; class TrashmailRuleServiceProvider extends ServiceProvider @@ -12,6 +14,27 @@ public function boot(): void $this->bootConfig(); $this->bootLang(); } + + Guzzle::register('dead-letter.email', [ + 'base_uri' => 'https://www.dead-letter.email', + RequestOptions::TIMEOUT => 10, + RequestOptions::ALLOW_REDIRECTS => true, + RequestOptions::HTTP_ERRORS => true, + ]); + + Guzzle::register('api.disposable-email-detector.com', [ + 'base_uri' => 'https://api.disposable-email-detector.com', + RequestOptions::TIMEOUT => 5, + RequestOptions::ALLOW_REDIRECTS => true, + RequestOptions::HTTP_ERRORS => true, + ]); + + Guzzle::register('verifier.meetchopra.com', [ + 'base_uri' => 'https://verifier.meetchopra.com', + RequestOptions::TIMEOUT => 5, + RequestOptions::ALLOW_REDIRECTS => true, + RequestOptions::HTTP_ERRORS => true, + ]); } public function register(): void diff --git a/tests/Rules/TrashmailRuleTest.php b/tests/Rules/TrashmailRuleTest.php index b6c3fe2..a57d6a2 100644 --- a/tests/Rules/TrashmailRuleTest.php +++ b/tests/Rules/TrashmailRuleTest.php @@ -47,6 +47,42 @@ public function it_passes_whitelist_addresses(): void $this->assertTrue($rule->passes('email', 'example@'.$domain)); } + /** @test */ + public function dead_letter_fails_with_disposable_email(): void + { + $this->app['config']->set('trashmail.dead_letter.enabled', true); + + $rule = new TrashmailRule(); + + $this->assertFalse($rule->passes('email', 'example@0815.ru')); + } + + /** @test */ + public function disposable_email_fails_with_disposable_email(): void + { + $this->app['config']->set('trashmail.disposable_email_detector.enabled', true); + + $rule = new TrashmailRule(); + + $this->assertFalse($rule->passes('email', 'example@0815.ru')); + } + + /** @test */ + public function verifier_fails_with_disposable_email(): void + { + if ($this->app['config']->get('trashmail.verifier.api_key') === null) { + $this->markTestSkipped('Verifier requires an API-Key'); + + return; + } + + $this->app['config']->set('trashmail.verifier.enabled', true); + + $rule = new TrashmailRule(); + + $this->assertFalse($rule->passes('email', 'example@0815.ru')); + } + public function provideTrashMailDomain(): array { return array_map(static function (string $domain): array { diff --git a/tests/TestCase.php b/tests/TestCase.php index c929e2c..faa5ed0 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,7 +2,7 @@ namespace Elbgoods\TrashmailRule\Tests; -use Astrotomic\LaravelGuzzle\LaravelGuzzleServiceProvider; +use Astrotomic\LaravelGuzzle\GuzzleServiceProvider; use Elbgoods\TrashmailRule\TrashmailRuleServiceProvider; use Orchestra\Testbench\TestCase as Orchestra; @@ -11,7 +11,7 @@ abstract class TestCase extends Orchestra protected function getPackageProviders($app) { return [ - LaravelGuzzleServiceProvider::class, + GuzzleServiceProvider::class, TrashmailRuleServiceProvider::class, ]; }