test: add unit tests for Model methods #8

Open
opened 2026-02-20 02:36:18 +00:00 by Clotho · 0 comments
Member

Issue

Key model methods lack unit test coverage.

Missing Tests

Service Model (src/Mod/Hub/Models/Service.php)

Untested methods:

  • getDomainMappings() - Returns domain configuration from metadata
  • findByCode(string $code) - Static finder method
  • setMeta(string $key, mixed $value) - Metadata setter
  • getMeta(string $key, mixed $default = null) - Metadata getter
  • hasMeta(string $key) - Metadata existence check

Test scenarios:

  • Domain mapping retrieval with various metadata structures
  • Finding services by code (exists/not found)
  • Setting/getting/checking metadata
  • JSON metadata persistence
  • Edge cases: empty metadata, invalid keys

HoneypotHit Model (src/Mod/Hub/Models/HoneypotHit.php)

Untested static methods:

  • detectBot(string $path, string $userAgent, ?string $ip = null): bool - Bot detection logic
  • severityForPath(string $path): string - Severity classification
  • getSeverityWarning() - Constant getter
  • getSeverityCritical() - Constant getter

Untested scopes:

  • scopeRecent(Builder $query, int $hours = 24) - Recent hits filter
  • scopeFromIp(Builder $query, string $ip) - IP-based filter
  • scopeBots(Builder $query) - Bot hits filter

Untested complex method:

  • getStats(): array (lines 185-205) - Statistics aggregation

Test scenarios:

  • Bot detection with various user agents
  • Severity classification for different paths
  • Scope filtering correctness
  • Stats aggregation accuracy
  • Edge cases: null IPs, malformed user agents

Test File Locations

  • tests/Unit/Models/ServiceTest.php (create new)
  • tests/Unit/Models/HoneypotHitTest.php (create new)

Example Test

it("detects bots from user agent patterns", function () {
    expect(HoneypotHit::detectBot("/admin", "Mozilla/5.0 (compatible; Googlebot/2.1)"))
        ->toBeTrue();
    
    expect(HoneypotHit::detectBot("/admin", "Mozilla/5.0 (Windows NT 10.0)"))
        ->toBeFalse();
});

it("retrieves domain mappings from service metadata", function () {
    $service = Service::factory()->create([
        "metadata" => ["domains" => ["example.com" => "config"]]
    ]);
    
    expect($service->getDomainMappings())
        ->toHaveKey("example.com");
});

Priority

Medium - Models have some indirect coverage via feature tests, but unit tests needed for edge cases.

Discovered by

Automatic codebase scan (issue #3)

## Issue Key model methods lack unit test coverage. ## Missing Tests ### Service Model (src/Mod/Hub/Models/Service.php) **Untested methods:** - `getDomainMappings()` - Returns domain configuration from metadata - `findByCode(string $code)` - Static finder method - `setMeta(string $key, mixed $value)` - Metadata setter - `getMeta(string $key, mixed $default = null)` - Metadata getter - `hasMeta(string $key)` - Metadata existence check **Test scenarios:** - Domain mapping retrieval with various metadata structures - Finding services by code (exists/not found) - Setting/getting/checking metadata - JSON metadata persistence - Edge cases: empty metadata, invalid keys ### HoneypotHit Model (src/Mod/Hub/Models/HoneypotHit.php) **Untested static methods:** - `detectBot(string $path, string $userAgent, ?string $ip = null): bool` - Bot detection logic - `severityForPath(string $path): string` - Severity classification - `getSeverityWarning()` - Constant getter - `getSeverityCritical()` - Constant getter **Untested scopes:** - `scopeRecent(Builder $query, int $hours = 24)` - Recent hits filter - `scopeFromIp(Builder $query, string $ip)` - IP-based filter - `scopeBots(Builder $query)` - Bot hits filter **Untested complex method:** - `getStats(): array` (lines 185-205) - Statistics aggregation **Test scenarios:** - Bot detection with various user agents - Severity classification for different paths - Scope filtering correctness - Stats aggregation accuracy - Edge cases: null IPs, malformed user agents ## Test File Locations - `tests/Unit/Models/ServiceTest.php` (create new) - `tests/Unit/Models/HoneypotHitTest.php` (create new) ## Example Test ```php it("detects bots from user agent patterns", function () { expect(HoneypotHit::detectBot("/admin", "Mozilla/5.0 (compatible; Googlebot/2.1)")) ->toBeTrue(); expect(HoneypotHit::detectBot("/admin", "Mozilla/5.0 (Windows NT 10.0)")) ->toBeFalse(); }); it("retrieves domain mappings from service metadata", function () { $service = Service::factory()->create([ "metadata" => ["domains" => ["example.com" => "config"]] ]); expect($service->getDomainMappings()) ->toHaveKey("example.com"); }); ``` ## Priority **Medium** - Models have some indirect coverage via feature tests, but unit tests needed for edge cases. ## Discovered by Automatic codebase scan (issue #3)
Clotho added the
discovery
label 2026-02-20 02:36:18 +00:00
Charon added
PHP
testing
P2
and removed
discovery
labels 2026-02-20 12:16:55 +00:00
Clotho was assigned by Charon 2026-02-20 12:20:54 +00:00
Charon added the
agent-ready
label 2026-02-21 01:30:24 +00:00
Sign in to join this conversation.
No description provided.