From 6d356706ca972aa9bd49dab3ff8c481c94efa53d Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Mon, 24 Feb 2020 16:48:34 +0100 Subject: [PATCH 1/4] prepare for laravel-guzzle:2.0.0 --- .github/workflows/run-tests.yml | 2 ++ composer.json | 2 +- config/trashmail.php | 13 +------- src/Providers/DeadLetterProvider.php | 9 ++---- .../DisposableEmailDetectorProvider.php | 9 ++---- src/Providers/VerifierProvider.php | 17 +++++----- src/TrashmailRuleServiceProvider.php | 23 ++++++++++++++ tests/Rules/TrashmailRuleTest.php | 31 +++++++++++++++++++ tests/TestCase.php | 4 +-- 9 files changed, 73 insertions(+), 37 deletions(-) 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/composer.json b/composer.json index a08def5..8f352b2 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ ], "require": { "php": "^7.4", - "astrotomic/laravel-guzzle": "^1.0.1", + "astrotomic/laravel-guzzle": "dev-dev-v2", "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..5b99448 100644 --- a/tests/Rules/TrashmailRuleTest.php +++ b/tests/Rules/TrashmailRuleTest.php @@ -5,6 +5,7 @@ use Elbgoods\TrashmailRule\Rules\TrashmailRule; use Elbgoods\TrashmailRule\Tests\TestCase; use Illuminate\Support\Arr; +use Illuminate\Support\Str; final class TrashmailRuleTest extends TestCase { @@ -47,6 +48,36 @@ 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 + { + $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, ]; } From 5a02aa8e19a170a2ee3c318531e990d7f09cf0fe Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Mon, 24 Feb 2020 16:48:51 +0100 Subject: [PATCH 2/4] fix php cs --- tests/Rules/TrashmailRuleTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Rules/TrashmailRuleTest.php b/tests/Rules/TrashmailRuleTest.php index 5b99448..8ae0fba 100644 --- a/tests/Rules/TrashmailRuleTest.php +++ b/tests/Rules/TrashmailRuleTest.php @@ -5,7 +5,6 @@ use Elbgoods\TrashmailRule\Rules\TrashmailRule; use Elbgoods\TrashmailRule\Tests\TestCase; use Illuminate\Support\Arr; -use Illuminate\Support\Str; final class TrashmailRuleTest extends TestCase { From 5ba7f30a957b2dd05b2c3c20ee54c87df9ff1c46 Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Mon, 24 Feb 2020 17:16:25 +0100 Subject: [PATCH 3/4] use real v2.0.0 --- CHANGELOG.md | 4 ++++ composer.json | 2 +- tests/Rules/TrashmailRuleTest.php | 6 ++++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 85335d0..13ceb37 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 + + + ## v0.4.0 * add https://verifier.meetchopra.com provider `\Elbgoods\TrashmailRule\Providers\VerifierProvider` diff --git a/composer.json b/composer.json index 8f352b2..bfc149a 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ ], "require": { "php": "^7.4", - "astrotomic/laravel-guzzle": "dev-dev-v2", + "astrotomic/laravel-guzzle": "^2.0", "illuminate/cache": "^6.0", "illuminate/support": "^6.0" }, diff --git a/tests/Rules/TrashmailRuleTest.php b/tests/Rules/TrashmailRuleTest.php index 8ae0fba..a57d6a2 100644 --- a/tests/Rules/TrashmailRuleTest.php +++ b/tests/Rules/TrashmailRuleTest.php @@ -70,6 +70,12 @@ public function disposable_email_fails_with_disposable_email(): void /** @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(); From 02182c486c41507f69668ff8b7f947351b2f0d28 Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Mon, 24 Feb 2020 17:18:58 +0100 Subject: [PATCH 4/4] v0.5.0 --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 13ceb37..3883c8f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ 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