Files
2meet-data-optimizer-hivepr…/tests/unit/AttributeBridgeTest.php
T

99 lines
3.7 KiB
PHP

<?php
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
/**
* Tests for WPDO_HivePress_Attribute_Bridge.
*
* @covers WPDO_HivePress_Attribute_Bridge
*/
class HivePressAttributeBridgeTest extends TestCase {
private WPDO_HivePress_Attribute_Bridge $bridge;
protected function setUp(): void {
$this->bridge = new WPDO_HivePress_Attribute_Bridge();
}
public function test_discover_returns_empty_when_no_attributes_registered(): void {
// apply_filters stub returns the value unchanged → empty array.
$this->assertSame( array(), $this->bridge->discover_attributes() );
}
public function test_suggest_zone_configs_returns_empty_when_no_attributes(): void {
$this->assertSame( array(), $this->bridge->suggest_zone_configs() );
}
/**
* Verify the SQL type mapping is sensible for known HivePress field types.
*
* Indirect test via reflection — the public API only returns full configs
* but we want fast feedback if the type table drifts.
*/
public function test_sql_type_mapping_via_reflection(): void {
$reflect = new ReflectionClass( $this->bridge );
$method = $reflect->getMethod( 'sql_type_for' );
$method->setAccessible( true );
$this->assertStringContainsString( 'int', $method->invoke( $this->bridge, 'number' ) );
$this->assertStringContainsString( 'decimal', $method->invoke( $this->bridge, 'price' ) );
$this->assertStringContainsString( 'tinyint', $method->invoke( $this->bridge, 'checkbox' ) );
$this->assertStringContainsString( 'date', $method->invoke( $this->bridge, 'date' ) );
$this->assertStringContainsString( 'varchar', $method->invoke( $this->bridge, 'text' ) );
// Unknown type → fallback to varchar.
$this->assertStringContainsString( 'varchar', $method->invoke( $this->bridge, 'unknown_type' ) );
}
/**
* Verify build_record normalises HP attribute config correctly.
*/
public function test_build_record_normalises_attribute_config(): void {
$reflect = new ReflectionClass( $this->bridge );
$method = $reflect->getMethod( 'build_record' );
$method->setAccessible( true );
$record = $method->invoke( $this->bridge, 'listing', 'beds', array(
'edit_field' => array( 'type' => 'integer' ),
'filterable' => true,
'sortable' => false,
'searchable' => true,
) );
$this->assertSame( 'listing', $record['model'] );
$this->assertSame( 'beds', $record['slug'] );
$this->assertSame( 'hp_beds', $record['meta_key'] );
$this->assertSame( 'integer', $record['type'] );
$this->assertTrue( $record['filterable'] );
$this->assertFalse( $record['sortable'] );
}
/**
* Promotion candidate logic: filterable OR sortable triggers promotion.
*/
public function test_promotion_candidate_logic(): void {
$reflect = new ReflectionClass( $this->bridge );
$method = $reflect->getMethod( 'is_promotion_candidate' );
$method->setAccessible( true );
// Filterable but not sortable → promote.
$this->assertTrue( $method->invoke( $this->bridge, array(
'model' => 'listing', 'slug' => 'x', 'meta_key' => 'hp_listing_x',
'type' => 'text', 'filterable' => true, 'sortable' => false, 'searchable' => true,
) ) );
// Sortable but not filterable → promote.
$this->assertTrue( $method->invoke( $this->bridge, array(
'model' => 'listing', 'slug' => 'x', 'meta_key' => 'hp_listing_x',
'type' => 'text', 'filterable' => false, 'sortable' => true, 'searchable' => false,
) ) );
// Searchable only (no filter, no sort) → don't promote (full-text would
// be a different optimization).
$this->assertFalse( $method->invoke( $this->bridge, array(
'model' => 'listing', 'slug' => 'x', 'meta_key' => 'hp_listing_x',
'type' => 'text', 'filterable' => false, 'sortable' => false, 'searchable' => true,
) ) );
}
}