Files
moko-platform/tests/Unit/ConfigValidatorTest.php
Jonathan Miller b33623c731
Platform: moko-platform CI / CI Summary (push) Blocked by required conditions
Generic: Repo Health / Access control (push) Successful in 2s
Generic: Repo Health / Site Health (push) Has been skipped
Generic: Repo Health / Release configuration (push) Successful in 6s
Generic: Repo Health / Scripts governance (push) Successful in 6s
Generic: Repo Health / Repository health (push) Successful in 16s
Platform: moko-platform CI / CI Summary (pull_request) Blocked by required conditions
Generic: Repo Health / Site Health (pull_request) Has been skipped
Universal: PR Check / Branch Policy (pull_request) Successful in 3s
Generic: Repo Health / Access control (pull_request) Successful in 3s
Universal: Secret Scanning / Gitleaks Secret Scan (pull_request) Successful in 8s
Universal: PR Check / Validate PR (pull_request) Successful in 8s
Universal: PR Check / Build RC Package (pull_request) Successful in 3s
Generic: Repo Health / Release configuration (pull_request) Successful in 9s
Generic: Repo Health / Scripts governance (pull_request) Successful in 10s
Generic: Repo Health / Repository health (pull_request) Successful in 17s
Platform: moko-platform CI / Gate 1: Code Quality (push) Successful in 1m3s
Platform: moko-platform CI / Gate 1: Code Quality (pull_request) Successful in 1m0s
Platform: moko-platform CI / Gate 5: Template Integrity (push) Failing after 6s
Platform: moko-platform CI / Gate 2: Unit Tests (8.1) (push) Successful in 46s
Platform: moko-platform CI / Gate 2: Unit Tests (8.2) (push) Successful in 48s
Platform: moko-platform CI / Gate 3: Self-Health Check (push) Failing after 49s
Platform: moko-platform CI / Gate 4: Governance (push) Successful in 46s
Platform: moko-platform CI / Gate 2: Unit Tests (8.3) (push) Successful in 1m2s
Platform: moko-platform CI / Gate 5: Template Integrity (pull_request) Failing after 7s
Platform: moko-platform CI / Gate 2: Unit Tests (8.1) (pull_request) Successful in 49s
Platform: moko-platform CI / Gate 2: Unit Tests (8.2) (pull_request) Successful in 51s
Platform: moko-platform CI / Gate 3: Self-Health Check (pull_request) Failing after 53s
Platform: moko-platform CI / Gate 4: Governance (pull_request) Successful in 51s
Platform: moko-platform CI / Gate 2: Unit Tests (8.3) (pull_request) Successful in 59s
feat: add ConfigValidator for plugin JSON schema validation
Validates project config against plugin getConfigSchema() definitions.
Supports: type checking, required fields, enum values, nested objects,
arrays, string minLength/pattern, number min/max, unknown property warnings.

7 unit tests covering all validation paths.

Closes #105

Authored-by: Moko Consulting
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-05-25 21:50:37 -05:00

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));
}
}