51 lines
1.6 KiB
PHP
51 lines
1.6 KiB
PHP
<?php
|
|
// tests/Utils/ValidatorTest.php
|
|
namespace Tests\Utils;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
use App\Utils\Validator;
|
|
|
|
class ValidatorTest extends TestCase
|
|
{
|
|
public function testValidateEmail(): void
|
|
{
|
|
$this->assertTrue(Validator::validateEmail('test@example.com'));
|
|
$this->assertFalse(Validator::validateEmail('invalid-email'));
|
|
}
|
|
|
|
public function testValidateRequired(): void
|
|
{
|
|
$data = ['name' => 'John', 'email' => 'john@example.com'];
|
|
$required = ['name', 'email'];
|
|
|
|
$errors = Validator::validateRequired($data, $required);
|
|
$this->assertEmpty($errors);
|
|
|
|
$data = ['name' => 'John'];
|
|
$errors = Validator::validateRequired($data, $required);
|
|
$this->assertNotEmpty($errors);
|
|
$this->assertContains('email is required', $errors);
|
|
}
|
|
|
|
public function testSanitizeString(): void
|
|
{
|
|
$input = '<script>alert("xss")</script>Hello World';
|
|
$expected = '<script>alert("xss")</script>Hello World';
|
|
|
|
$result = Validator::sanitizeString($input);
|
|
$this->assertEquals($expected, $result);
|
|
}
|
|
|
|
public function testValidateUrl(): void
|
|
{
|
|
$this->assertTrue(Validator::validateUrl('https://example.com'));
|
|
$this->assertFalse(Validator::validateUrl('not-a-url'));
|
|
}
|
|
|
|
public function testValidateLength(): void
|
|
{
|
|
$this->assertTrue(Validator::validateLength('hello', 3, 10));
|
|
$this->assertFalse(Validator::validateLength('hi', 3, 10));
|
|
$this->assertFalse(Validator::validateLength('this string is too long', 3, 10));
|
|
}
|
|
} |