f4eee2338b
- ADR-003 step 4 (core side): TMDO_Schema_Registry::register() raises _doing_it_wrong() the first time each provider registers a zone=hot field in a request (zone omitted defaults to hot). Fields still register, so existing hot data stays readable. - Cold / warm / archive registrations are unaffected. - Test bootstrap's _doing_it_wrong stub now records calls. - Verify: core unit 658 / integration 459, HP 142/49, WC 10/22 green, phpcs clean - PR: PR-038
156 lines
6.7 KiB
PHP
156 lines
6.7 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
/**
|
|
* Tests for WPDO_Schema_Registry — field-to-zone mapping singleton.
|
|
*/
|
|
class SchemaRegistryTest extends TestCase {
|
|
|
|
private WPDO_Schema_Registry $registry;
|
|
|
|
protected function setUp(): void {
|
|
// Reset singleton via reflection.
|
|
$ref = new ReflectionClass( WPDO_Schema_Registry::class );
|
|
$instance = $ref->getProperty( 'instance' );
|
|
$instance->setAccessible( true );
|
|
$instance->setValue( null, null );
|
|
|
|
$this->registry = WPDO_Schema_Registry::instance();
|
|
$GLOBALS['_doing_it_wrong_calls'] = array();
|
|
}
|
|
|
|
// ── Hot zone deprecation (ADR-003) ──────────────────────────────────────
|
|
|
|
public function test_hot_registration_is_flagged_once_per_provider(): void {
|
|
foreach ( array( 'p1', 'p1', 'p2' ) as $i => $provider ) {
|
|
$this->registry->register( $provider, array( 'post_type' => 'x', 'meta_key' => "k{$i}", 'zone' => 'hot' ) );
|
|
}
|
|
$this->registry->register( 'p1', array( 'post_type' => 'x', 'meta_key' => 'k9' ) ); // zone defaults to hot.
|
|
|
|
$calls = $GLOBALS['_doing_it_wrong_calls'];
|
|
$this->assertCount( 2, $calls );
|
|
$this->assertStringContainsString( 'p1', $calls[0][1] );
|
|
$this->assertStringContainsString( 'p2', $calls[1][1] );
|
|
$this->assertSame( 'hot', $this->registry->get_field_zone( 'x', 'k9' ), 'still registered: frozen, not rejected' );
|
|
}
|
|
|
|
public function test_other_zones_are_not_flagged(): void {
|
|
$this->registry->register( 'p', array( 'post_type' => 'x', 'meta_key' => 'c', 'zone' => 'cold' ) );
|
|
$this->registry->register( 'p', array( 'post_type' => 'x', 'meta_key' => 'w', 'zone' => 'warm' ) );
|
|
$this->registry->register( 'p', array( 'post_type' => 'x', 'meta_key' => 'a', 'zone' => 'archive' ) );
|
|
|
|
$this->assertSame( array(), $GLOBALS['_doing_it_wrong_calls'] );
|
|
}
|
|
|
|
// ── register() ──────────────────────────────────────────────────────────
|
|
|
|
public function test_register_single_field(): void {
|
|
$this->registry->register( 'test', [
|
|
'post_type' => 'hp_listing',
|
|
'meta_key' => 'hp_price',
|
|
'zone' => 'hot',
|
|
'data_type' => 'decimal(10,2) NOT NULL DEFAULT 0',
|
|
'column' => 'hp_price',
|
|
'indexed' => true,
|
|
] );
|
|
|
|
$field = $this->registry->get_field( 'hp_listing', 'hp_price' );
|
|
$this->assertNotNull( $field );
|
|
$this->assertSame( 'hot', $field['zone'] );
|
|
$this->assertSame( 'hp_price', $field['column'] );
|
|
$this->assertTrue( $field['indexed'] );
|
|
}
|
|
|
|
public function test_register_many_registers_all_fields(): void {
|
|
$this->registry->register_many( 'test', [
|
|
[ 'post_type' => 'hp_listing', 'meta_key' => 'hp_featured', 'zone' => 'hot', 'data_type' => 'tinyint(1) NOT NULL DEFAULT 0', 'column' => 'hp_featured' ],
|
|
[ 'post_type' => 'hp_listing', 'meta_key' => 'hp_verified', 'zone' => 'hot', 'data_type' => 'tinyint(1) NOT NULL DEFAULT 0', 'column' => 'hp_verified' ],
|
|
[ 'post_type' => 'hp_vendor', 'meta_key' => 'hp_verified', 'zone' => 'hot', 'data_type' => 'tinyint(1) NOT NULL DEFAULT 0', 'column' => 'hp_verified' ],
|
|
] );
|
|
|
|
$listing_hot = $this->registry->get_zone_fields_for_type( 'hot', 'hp_listing' );
|
|
$this->assertCount( 2, $listing_hot );
|
|
|
|
$vendor_hot = $this->registry->get_zone_fields_for_type( 'hot', 'hp_vendor' );
|
|
$this->assertCount( 1, $vendor_hot );
|
|
}
|
|
|
|
// ── get_field() ─────────────────────────────────────────────────────────
|
|
|
|
public function test_get_field_returns_null_for_unknown_key(): void {
|
|
$this->assertNull( $this->registry->get_field( 'hp_listing', 'hp_nonexistent' ) );
|
|
}
|
|
|
|
// ── get_field_zone() ────────────────────────────────────────────────────
|
|
|
|
public function test_get_field_zone_returns_correct_zone(): void {
|
|
$this->registry->register( 'test', [
|
|
'post_type' => 'hp_vendor',
|
|
'meta_key' => 'hp_description',
|
|
'zone' => 'cold',
|
|
'cache_group' => 'wpdo_cold',
|
|
'cache_ttl' => 3600,
|
|
] );
|
|
|
|
$zone = $this->registry->get_field_zone( 'hp_vendor', 'hp_description' );
|
|
$this->assertSame( 'cold', $zone );
|
|
}
|
|
|
|
public function test_get_field_zone_returns_null_for_unknown(): void {
|
|
$this->assertNull( $this->registry->get_field_zone( 'hp_listing', 'hp_missing' ) );
|
|
}
|
|
|
|
// ── get_hot_columns() ───────────────────────────────────────────────────
|
|
|
|
public function test_get_hot_columns_returns_column_to_data_type_map(): void {
|
|
$this->registry->register( 'test', [
|
|
'post_type' => 'hp_listing',
|
|
'meta_key' => 'hp_price',
|
|
'zone' => 'hot',
|
|
'data_type' => 'decimal(10,2) NOT NULL DEFAULT 0',
|
|
'column' => 'hp_price',
|
|
] );
|
|
|
|
$cols = $this->registry->get_hot_columns( 'hp_listing' );
|
|
$this->assertArrayHasKey( 'hp_price', $cols );
|
|
$this->assertSame( 'decimal(10,2) NOT NULL DEFAULT 0', $cols['hp_price'] );
|
|
}
|
|
|
|
// ── Duplicate registration guard ────────────────────────────────────────
|
|
|
|
public function test_duplicate_registration_does_not_add_extra_entry(): void {
|
|
$field = [
|
|
'post_type' => 'hp_listing',
|
|
'meta_key' => 'hp_price',
|
|
'zone' => 'hot',
|
|
'data_type' => 'decimal(10,2) NOT NULL DEFAULT 0',
|
|
'column' => 'hp_price',
|
|
];
|
|
|
|
$this->registry->register( 'test', $field );
|
|
$this->registry->register( 'test', $field );
|
|
|
|
$hot = $this->registry->get_zone_fields_for_type( 'hot', 'hp_listing' );
|
|
$this->assertCount( 1, $hot );
|
|
}
|
|
|
|
// ── get_stats() ─────────────────────────────────────────────────────────
|
|
|
|
public function test_get_stats_reflects_registered_fields(): void {
|
|
$this->registry->register_many( 'test', [
|
|
[ 'post_type' => 'hp_listing', 'meta_key' => 'hp_price', 'zone' => 'hot', 'data_type' => 'decimal(10,2) NOT NULL DEFAULT 0', 'column' => 'hp_price' ],
|
|
[ 'post_type' => 'hp_listing', 'meta_key' => 'hp_featured', 'zone' => 'hot', 'data_type' => 'tinyint(1) NOT NULL DEFAULT 0', 'column' => 'hp_featured' ],
|
|
[ 'post_type' => 'hp_vendor', 'meta_key' => 'hp_desc', 'zone' => 'cold', 'cache_group' => 'g', 'cache_ttl' => 3600 ],
|
|
] );
|
|
|
|
$stats = $this->registry->get_stats();
|
|
|
|
$this->assertSame( 2, $stats['hot'] );
|
|
$this->assertSame( 1, $stats['cold'] );
|
|
$this->assertSame( 0, $stats['warm'] );
|
|
$this->assertSame( 0, $stats['archive'] );
|
|
}
|
|
}
|