142 lines
3.8 KiB
PHP
142 lines
3.8 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
/* Copyright (C) 2026 Moko Consulting <hello@mokoconsulting.tech>
|
||
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
||
|
|
*/
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace MokoStandards\Tests\Unit;
|
||
|
|
|
||
|
|
use MokoEnterprise\ConfigValidator;
|
||
|
|
use PHPUnit\Framework\TestCase;
|
||
|
|
|
||
|
|
class ConfigValidatorTest extends TestCase
|
||
|
|
{
|
||
|
|
private ConfigValidator $validator;
|
||
|
|
|
||
|
|
protected function setUp(): void
|
||
|
|
{
|
||
|
|
$this->validator = new ConfigValidator();
|
||
|
|
}
|
||
|
|
|
||
|
|
public function testValidConfigPasses(): void
|
||
|
|
{
|
||
|
|
$schema = [
|
||
|
|
'type' => 'object',
|
||
|
|
'properties' => [
|
||
|
|
'name' => ['type' => 'string'],
|
||
|
|
'version' => ['type' => 'string'],
|
||
|
|
],
|
||
|
|
'required' => ['name'],
|
||
|
|
];
|
||
|
|
|
||
|
|
$config = ['name' => 'MyProject', 'version' => '1.0'];
|
||
|
|
|
||
|
|
$this->assertTrue($this->validator->validate($config, $schema));
|
||
|
|
$this->assertEmpty($this->validator->getErrors());
|
||
|
|
}
|
||
|
|
|
||
|
|
public function testMissingRequiredField(): void
|
||
|
|
{
|
||
|
|
$schema = [
|
||
|
|
'type' => 'object',
|
||
|
|
'properties' => [
|
||
|
|
'name' => ['type' => 'string'],
|
||
|
|
],
|
||
|
|
'required' => ['name'],
|
||
|
|
];
|
||
|
|
|
||
|
|
$this->assertFalse($this->validator->validate([], $schema));
|
||
|
|
$this->assertStringContainsString(
|
||
|
|
'required',
|
||
|
|
$this->validator->getErrors()[0]
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function testEnumValidation(): void
|
||
|
|
{
|
||
|
|
$schema = [
|
||
|
|
'type' => 'object',
|
||
|
|
'properties' => [
|
||
|
|
'type' => [
|
||
|
|
'type' => 'string',
|
||
|
|
'enum' => ['component', 'module', 'plugin'],
|
||
|
|
],
|
||
|
|
],
|
||
|
|
];
|
||
|
|
|
||
|
|
$valid = ['type' => 'component'];
|
||
|
|
$this->assertTrue($this->validator->validate($valid, $schema));
|
||
|
|
|
||
|
|
$invalid = ['type' => 'banana'];
|
||
|
|
$this->assertFalse($this->validator->validate($invalid, $schema));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function testNestedObjectValidation(): void
|
||
|
|
{
|
||
|
|
$schema = [
|
||
|
|
'type' => 'object',
|
||
|
|
'properties' => [
|
||
|
|
'db' => [
|
||
|
|
'type' => 'object',
|
||
|
|
'properties' => [
|
||
|
|
'host' => ['type' => 'string'],
|
||
|
|
'port' => ['type' => 'integer'],
|
||
|
|
],
|
||
|
|
'required' => ['host'],
|
||
|
|
],
|
||
|
|
],
|
||
|
|
];
|
||
|
|
|
||
|
|
$valid = ['db' => ['host' => 'localhost', 'port' => 3306]];
|
||
|
|
$this->assertTrue($this->validator->validate($valid, $schema));
|
||
|
|
|
||
|
|
$invalid = ['db' => ['port' => 3306]];
|
||
|
|
$this->assertFalse($this->validator->validate($invalid, $schema));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function testUnknownPropertiesWarn(): void
|
||
|
|
{
|
||
|
|
$schema = [
|
||
|
|
'type' => 'object',
|
||
|
|
'properties' => [
|
||
|
|
'name' => ['type' => 'string'],
|
||
|
|
],
|
||
|
|
];
|
||
|
|
|
||
|
|
$config = ['name' => 'ok', 'extra' => 'unknown'];
|
||
|
|
$this->assertTrue($this->validator->validate($config, $schema));
|
||
|
|
$this->assertNotEmpty($this->validator->getWarnings());
|
||
|
|
}
|
||
|
|
|
||
|
|
public function testTypeMismatch(): void
|
||
|
|
{
|
||
|
|
$schema = [
|
||
|
|
'type' => 'object',
|
||
|
|
'properties' => [
|
||
|
|
'count' => ['type' => 'integer'],
|
||
|
|
],
|
||
|
|
];
|
||
|
|
|
||
|
|
$invalid = ['count' => 'not-a-number'];
|
||
|
|
$this->assertFalse($this->validator->validate($invalid, $schema));
|
||
|
|
}
|
||
|
|
|
||
|
|
public function testStringMinLength(): void
|
||
|
|
{
|
||
|
|
$schema = [
|
||
|
|
'type' => 'object',
|
||
|
|
'properties' => [
|
||
|
|
'name' => ['type' => 'string', 'minLength' => 3],
|
||
|
|
],
|
||
|
|
];
|
||
|
|
|
||
|
|
$short = ['name' => 'ab'];
|
||
|
|
$this->assertFalse($this->validator->validate($short, $schema));
|
||
|
|
|
||
|
|
$ok = ['name' => 'abc'];
|
||
|
|
$this->assertTrue($this->validator->validate($ok, $schema));
|
||
|
|
}
|
||
|
|
}
|