d36bb954d1
Baseline before backporting wp-data-optimizer v3.0.1-v3.4.6. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TbG1keQQ7XBa7qMQY16KCY
132 lines
5.4 KiB
PHP
132 lines
5.4 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();
|
|
}
|
|
|
|
// ── 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'] );
|
|
}
|
|
}
|