Skip to content

Commit 467f57e

Browse files
authored
Fix #120: Move check exists function "idn_to_ascii" from constructor of the url rule to method enableIDN
1 parent 82ca229 commit 467f57e

2 files changed

Lines changed: 33 additions & 10 deletions

File tree

src/Rule/Url.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use Yiisoft\Validator\Rule;
1010
use Yiisoft\Validator\ValidationContext;
1111

12-
use function function_exists;
1312
use function is_string;
1413
use function strlen;
1514

@@ -44,13 +43,6 @@ class Url extends Rule
4443

4544
private string $message = 'This value is not a valid URL.';
4645

47-
public function __construct()
48-
{
49-
if ($this->enableIDN && !function_exists('idn_to_ascii')) {
50-
throw new \RuntimeException('In order to use IDN validation intl extension must be installed and enabled.');
51-
}
52-
}
53-
5446
protected function validateValue($value, ValidationContext $context = null): Result
5547
{
5648
$result = new Result();
@@ -107,6 +99,10 @@ public function pattern(string $pattern): self
10799

108100
public function enableIDN(): self
109101
{
102+
if (!function_exists('idn_to_ascii')) {
103+
throw new \RuntimeException('In order to use IDN validation intl extension must be installed and enabled.');
104+
}
105+
110106
$new = clone $this;
111107
$new->enableIDN = true;
112108
return $new;

tests/Rule/UrlTest.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22

33
declare(strict_types=1);
44

5+
namespace Yiisoft\Validator\Rule;
6+
7+
use Yiisoft\Validator\Tests\Rule\UrlTest;
8+
9+
function function_exists($function)
10+
{
11+
if ($function === 'idn_to_ascii' && UrlTest::$idnFunctionException) {
12+
return false;
13+
}
14+
return \function_exists($function);
15+
}
16+
517
namespace Yiisoft\Validator\Tests\Rule;
618

719
use PHPUnit\Framework\TestCase;
@@ -13,6 +25,13 @@
1325
*/
1426
class UrlTest extends TestCase
1527
{
28+
public static bool $idnFunctionException = false;
29+
30+
protected function setUp(): void
31+
{
32+
static::$idnFunctionException = false;
33+
}
34+
1635
public function testValidate(): void
1736
{
1837
$val = new Url();
@@ -67,7 +86,7 @@ public function testValidateWithCustomScheme(): void
6786

6887
public function testValidateWithIdn(): void
6988
{
70-
if (!function_exists('idn_to_ascii')) {
89+
if (!\function_exists('idn_to_ascii')) {
7190
$this->markTestSkipped('intl package required');
7291
return;
7392
}
@@ -78,6 +97,14 @@ public function testValidateWithIdn(): void
7897
$this->assertTrue($val->validate('http://xn--zcack7ayc9a.de')->isValid());
7998
}
8099

100+
public function testEnableIdnException(): void
101+
{
102+
static::$idnFunctionException = true;
103+
104+
$this->expectException(\RuntimeException::class);
105+
(new Url())->enableIDN();
106+
}
107+
81108
public function testValidateLength(): void
82109
{
83110
$url = 'http://' . str_pad('base', 2000, 'url') . '.de';
@@ -87,7 +114,7 @@ public function testValidateLength(): void
87114

88115
public function testValidateWithIdnWithoutScheme(): void
89116
{
90-
if (!function_exists('idn_to_ascii')) {
117+
if (!\function_exists('idn_to_ascii')) {
91118
$this->markTestSkipped('intl package required');
92119
return;
93120
}

0 commit comments

Comments
 (0)