2011-05-04 11:19:55 +02:00
|
|
|
<?php
|
2015-06-08 16:47:14 +02:00
|
|
|
|
2015-06-08 17:09:25 +02:00
|
|
|
/*
|
2023-02-17 05:13:51 +01:00
|
|
|
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
|
|
* SPDX-License-Identifier: MIT
|
2015-06-08 17:09:25 +02:00
|
|
|
*/
|
|
|
|
|
2017-11-19 19:01:31 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2011-05-04 11:19:55 +02:00
|
|
|
namespace Respect\Validation\Rules;
|
|
|
|
|
2016-10-29 15:37:09 +02:00
|
|
|
use Egulias\EmailValidator\EmailValidator;
|
|
|
|
use Egulias\EmailValidator\Validation\RFCValidation;
|
2024-01-28 01:46:21 +01:00
|
|
|
use PHPUnit\Framework\Attributes\CoversClass;
|
|
|
|
use PHPUnit\Framework\Attributes\Group;
|
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2018-06-07 02:04:29 +02:00
|
|
|
use Respect\Validation\Test\RuleTestCase;
|
2018-10-04 09:02:54 +02:00
|
|
|
use stdClass;
|
2020-07-21 22:54:41 +02:00
|
|
|
|
2018-12-05 08:36:38 +01:00
|
|
|
use function tmpfile;
|
2016-10-29 15:37:09 +02:00
|
|
|
|
2024-01-28 01:46:21 +01:00
|
|
|
#[Group(' rule')]
|
|
|
|
#[CoversClass(Email::class)]
|
2018-06-07 02:04:29 +02:00
|
|
|
final class EmailTest extends RuleTestCase
|
2011-05-04 11:19:55 +02:00
|
|
|
{
|
2024-01-28 01:46:21 +01:00
|
|
|
#[Test]
|
2018-08-19 18:46:09 +02:00
|
|
|
public function shouldUseEmailValidatorToValidate(): void
|
2015-02-15 23:36:31 +01:00
|
|
|
{
|
|
|
|
$input = 'example@example.com';
|
|
|
|
|
2018-08-19 18:46:09 +02:00
|
|
|
$emailValidator = $this->getMockBuilder(EmailValidator::class)
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2015-02-15 23:36:31 +01:00
|
|
|
$emailValidator
|
2018-07-23 20:35:14 +02:00
|
|
|
->expects(self::once())
|
2015-02-15 23:36:31 +01:00
|
|
|
->method('isValid')
|
2018-07-23 20:35:14 +02:00
|
|
|
->with($input, self::isInstanceOf(RFCValidation::class))
|
2024-01-28 01:46:21 +01:00
|
|
|
->willReturn(true);
|
2015-02-15 23:36:31 +01:00
|
|
|
|
2018-08-19 18:46:09 +02:00
|
|
|
$sut = new Email($emailValidator);
|
2015-02-15 23:36:31 +01:00
|
|
|
|
2024-03-25 09:18:05 +01:00
|
|
|
self::assertValidInput($sut, $input);
|
2015-02-15 23:36:31 +01:00
|
|
|
}
|
|
|
|
|
2024-02-04 19:23:04 +01:00
|
|
|
/** @return iterable<array{Email, mixed}> */
|
|
|
|
public static function providerForValidInput(): iterable
|
2011-05-04 11:19:55 +02:00
|
|
|
{
|
2024-01-28 00:54:26 +01:00
|
|
|
$sut = new Email(null);
|
2011-05-04 11:19:55 +02:00
|
|
|
|
2015-10-18 03:44:47 +02:00
|
|
|
return [
|
2018-08-19 18:46:09 +02:00
|
|
|
[$sut, 'test@test.com'],
|
|
|
|
[$sut, 'mail+mail@gmail.com'],
|
|
|
|
[$sut, 'mail.email@e.test.com'],
|
|
|
|
[$sut, 'a@a.a'],
|
2015-10-18 03:44:47 +02:00
|
|
|
];
|
2011-05-04 11:19:55 +02:00
|
|
|
}
|
|
|
|
|
2024-02-04 19:23:04 +01:00
|
|
|
/** @return iterable<array{Email, mixed}> */
|
|
|
|
public static function providerForInvalidInput(): iterable
|
2011-05-04 11:19:55 +02:00
|
|
|
{
|
2024-01-28 00:54:26 +01:00
|
|
|
$sut = new Email(null);
|
2018-06-07 02:04:29 +02:00
|
|
|
|
2015-10-18 03:44:47 +02:00
|
|
|
return [
|
2018-08-19 18:46:09 +02:00
|
|
|
[$sut, ''],
|
|
|
|
[$sut, 'test'],
|
|
|
|
[$sut, '@test.com'],
|
|
|
|
[$sut, 'mail@test@test.com'],
|
|
|
|
[$sut, 'test.test@'],
|
|
|
|
[$sut, 'test.@test.com'],
|
|
|
|
[$sut, 'test@.test.com'],
|
|
|
|
[$sut, 'test@test..com'],
|
|
|
|
[$sut, 'test@test.com.'],
|
|
|
|
[$sut, '.test@test.com'],
|
2018-12-05 08:36:38 +01:00
|
|
|
[$sut, []],
|
|
|
|
[$sut, new stdClass()],
|
|
|
|
[$sut, null],
|
|
|
|
[$sut, tmpfile()],
|
2015-10-18 03:44:47 +02:00
|
|
|
];
|
2011-05-04 11:19:55 +02:00
|
|
|
}
|
|
|
|
}
|