Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/Model/Fields/Payer/IP.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ class IP extends Field
{
protected $name = __CLASS__;
protected $type = self::STRING;
protected $minLength = 3;
protected $maxLength = 255;

protected function initValidators()
{
$this->addValidator(function ($value) {
if (false === filter_var($value, FILTER_VALIDATE_IP)) {
if (!empty($value) && false === filter_var($value, FILTER_VALIDATE_IP)) {
return new FieldValidationResult(false, 'Invalid IP address.');
}

Expand Down
23 changes: 14 additions & 9 deletions tests/Model/Fields/Payer/IpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,41 @@ public function testIpv4()
$ip = new Ip();
$ip->setValue('127.0.0.1');

$this->assertEquals('127.0.0.1', $ip->getValue());
$this->assertSame('127.0.0.1', $ip->getValue());
}

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

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

public function testInvalidIp()
public function testEmptyString()
{
$ip = new Ip();
$ip->setValue('');

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Validation failed for field Tpay\OpenApi\Model\Fields\Payer\IP: Invalid IP address.');

$ip->setValue('TEST123');
$this->assertSame('', $ip->getValue());
}

public function testNull()
{
$ip = new Ip();
$ip->setValue(null);

$this->assertSame(null, $ip->getValue());
}

public function testInvalidIp()
{
$ip = new Ip();

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

$ip->setValue(null);
$ip->setValue('TEST123');
}

public function testWrongType()
Expand Down