forked from FGRibreau/mailchecker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplatform.php.test.php
More file actions
102 lines (87 loc) · 3.3 KB
/
platform.php.test.php
File metadata and controls
102 lines (87 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
// Run tests from the repository root directory
// Linux:
// $ composer install && vendor/bin/phpunit test/platform.php.test.php
// Windows:
// > composer install && .\vendor\bin\phpunit.bat test\platform.php.test.php
require_once __DIR__ . '/../platform/php/MailChecker.php';
use Fgribreau\MailChecker;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
class Platform extends TestCase
{
public function assertIsValidResult($expected, $email)
{
$this->assertEquals($expected, MailChecker::isValid($email));
}
public function isValid($email)
{
$this->assertIsValidResult(true, $email);
}
public function isInvalid($email)
{
$this->assertIsValidResult(false, $email);
}
public function testReturnTrueIfValidEmail()
{
$this->isValid('plop@plop.com');
$this->isValid('my.ok@ok.plop.com');
$this->isValid('my+ok@ok.plop.com');
$this->isValid('my=ok@ok.plop.com');
$this->isValid('ok@gmail.com');
$this->isValid('ok@hotmail.com');
}
public function testReturnFalseIfEmailInvalid()
{
$this->isInvalid('');
$this->isInvalid(' ');
$this->isInvalid('plopplop.com');
$this->isInvalid('my+ok@ok=plop.com');
$this->isInvalid('my,ok@ok.plop.com');
$this->isInvalid(' ok@gmail.com ');
$this->isInvalid(' ok@gmail.com');
$this->isInvalid('ok@gmail.com ');
$this->isInvalid("\nok@gmail.com\n");
$this->isInvalid("\nok@gmail.com");
$this->isInvalid("ok@gmail.com\n");
}
public function testReturnFalseIfThrowableDomain()
{
$this->isInvalid('ok@tmail.com');
$this->isInvalid('ok@33mail.com');
//$this->isInvalid('ok@ok.33mail.com');
$this->isInvalid('ok@guerrillamailblock.com');
}
public static function provideBlackListTests()
{
foreach (array_rand(MailChecker::blacklist(), 1000) as $blacklistedDomain) {
yield [$blacklistedDomain];
}
}
#[DataProvider('provideBlackListTests')]
public function testReturnFalseForBlacklistedDomainsAndTheirSubdomains($blacklistedDomain)
{
$this->isInvalid('test@' . $blacklistedDomain);
$this->isInvalid('test@subdomain.' . $blacklistedDomain);
// Should not be invalid as a subdomain of a valid domain.
$this->isValid('test@' . $blacklistedDomain . '.gmail.com');
}
public function testAddCustomDomains()
{
$this->isValid('foo@youtube.com');
$this->isValid('foo@google.com');
$this->isValid('ok@gmail.com');
MailChecker::addCustomDomains(['youtube.com', 'google.com']);
$this->isInvalid('foo@youtube.com');
$this->isInvalid('foo@google.com');
$this->isValid('ok@gmail.com');
}
#[DataProvider('provideBlackListTests')]
public function testIsDomainBlocked($blacklistedDomain)
{
$this->assertEquals(true, MailChecker::isDomainBlocked($blacklistedDomain, true));
$this->assertEquals(true, MailChecker::isDomainBlocked('subdomain.'.$blacklistedDomain, true));
$this->assertEquals(true, MailChecker::isDomainBlocked($blacklistedDomain, false));
$this->assertEquals(false, MailChecker::isDomainBlocked('subdomain.'.$blacklistedDomain, false));
}
}