31 lines
832 B
PHP
31 lines
832 B
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Rules\ValidIpOrCidr;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Tests\TestCase;
|
|
|
|
class ValidIpOrCidrTest extends TestCase
|
|
{
|
|
private function passes(string $value): bool
|
|
{
|
|
return Validator::make(['v' => $value], ['v' => [new ValidIpOrCidr]])->passes();
|
|
}
|
|
|
|
public function test_accepts_ips_and_cidrs(): void
|
|
{
|
|
$this->assertTrue($this->passes('203.0.113.4'));
|
|
$this->assertTrue($this->passes('10.0.0.0/8'));
|
|
$this->assertTrue($this->passes('::1'));
|
|
$this->assertTrue($this->passes('2001:db8::/32'));
|
|
}
|
|
|
|
public function test_rejects_junk(): void
|
|
{
|
|
$this->assertFalse($this->passes('nope'));
|
|
$this->assertFalse($this->passes('10.0.0.0/99'));
|
|
$this->assertFalse($this->passes('1.2.3.4/'));
|
|
}
|
|
}
|