Skip to content

Commit 6757ea2

Browse files
IP field nullable (#93)
1 parent a5fc701 commit 6757ea2

2 files changed

Lines changed: 15 additions & 11 deletions

File tree

src/Model/Fields/Payer/IP.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@ class IP extends Field
1212
{
1313
protected $name = __CLASS__;
1414
protected $type = self::STRING;
15-
protected $minLength = 3;
1615
protected $maxLength = 255;
1716

1817
protected function initValidators()
1918
{
2019
$this->addValidator(function ($value) {
21-
if (false === filter_var($value, FILTER_VALIDATE_IP)) {
20+
if (!empty($value) && false === filter_var($value, FILTER_VALIDATE_IP)) {
2221
return new FieldValidationResult(false, 'Invalid IP address.');
2322
}
2423

tests/Model/Fields/Payer/IpTest.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,41 @@ public function testIpv4()
1313
$ip = new Ip();
1414
$ip->setValue('127.0.0.1');
1515

16-
$this->assertEquals('127.0.0.1', $ip->getValue());
16+
$this->assertSame('127.0.0.1', $ip->getValue());
1717
}
1818

1919
public function testIpv6()
2020
{
2121
$ip = new Ip();
2222
$ip->setValue('2001:db8::1');
2323

24-
$this->assertEquals('2001:db8::1', $ip->getValue());
24+
$this->assertSame('2001:db8::1', $ip->getValue());
2525
}
2626

27-
public function testInvalidIp()
27+
public function testEmptyString()
2828
{
2929
$ip = new Ip();
30+
$ip->setValue('');
3031

31-
$this->expectException(InvalidArgumentException::class);
32-
$this->expectExceptionMessage('Validation failed for field Tpay\OpenApi\Model\Fields\Payer\IP: Invalid IP address.');
33-
34-
$ip->setValue('TEST123');
32+
$this->assertSame('', $ip->getValue());
3533
}
3634

3735
public function testNull()
36+
{
37+
$ip = new Ip();
38+
$ip->setValue(null);
39+
40+
$this->assertSame(null, $ip->getValue());
41+
}
42+
43+
public function testInvalidIp()
3844
{
3945
$ip = new Ip();
4046

4147
$this->expectException(InvalidArgumentException::class);
42-
$this->expectExceptionMessage('Value of field Tpay\OpenApi\Model\Fields\Payer\IP is too short. Min required 3');
4348
$this->expectExceptionMessage('Validation failed for field Tpay\OpenApi\Model\Fields\Payer\IP: Invalid IP address.');
4449

45-
$ip->setValue(null);
50+
$ip->setValue('TEST123');
4651
}
4752

4853
public function testWrongType()

0 commit comments

Comments
 (0)