Files
wpdev d36bb954d1 chore: initial snapshot of 2meet-data-optimizer v0.1.0
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
2026-07-31 05:06:36 +08:00

127 lines
5.0 KiB
PHP

<?php
declare(strict_types=1);
use PHPUnit\Framework\TestCase;
/**
* Integration tests for WPDO_Zone_Hot against real MariaDB.
*
* Creates a dedicated test table (wp_itest_wpdo_hot_hp_listing) in
* setUpBeforeClass() and drops it in tearDownAfterClass(), so the real
* DB is never polluted with test data.
*/
class ZoneHotIntegrationTest extends TestCase {
private const POST_TYPE = 'hp_listing';
private const TABLE = 'wp_itest_wpdo_hot_hp_listing';
// ── Fixture lifecycle ─────────────────────────────────────────────────
public static function setUpBeforeClass(): void {
global $wpdb;
// DROP + CREATE ensures clean schema even after interrupted prior runs.
$wpdb->query( 'DROP TABLE IF EXISTS `' . self::TABLE . '`' );
$wpdb->query(
'CREATE TABLE `' . self::TABLE . '` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`post_id` bigint(20) unsigned NOT NULL DEFAULT 0,
`hp_price` decimal(10,2) NOT NULL DEFAULT 0,
`hp_featured` tinyint(1) NOT NULL DEFAULT 0,
`updated_at` datetime NOT NULL DEFAULT \'0000-00-00 00:00:00\',
PRIMARY KEY (`id`),
UNIQUE KEY `post_id` (`post_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4'
);
}
public static function tearDownAfterClass(): void {
global $wpdb;
$wpdb->query( 'DROP TABLE IF EXISTS `' . self::TABLE . '`' );
}
protected function setUp(): void {
global $wpdb;
$wpdb->query( 'TRUNCATE TABLE `' . self::TABLE . '`' );
}
// ── set / get ────────────────────────────────────────────────────────
public function test_set_and_get_single_field(): void {
WPDO_Zone_Hot::set( 1, self::POST_TYPE, 'hp_price', '199.99' );
$val = WPDO_Zone_Hot::get( 1, self::POST_TYPE, 'hp_price' );
$this->assertSame( '199.99', $val );
}
public function test_get_returns_null_for_missing_post(): void {
$val = WPDO_Zone_Hot::get( 9999, self::POST_TYPE, 'hp_price' );
$this->assertNull( $val );
}
public function test_set_overwrites_existing_value(): void {
WPDO_Zone_Hot::set( 2, self::POST_TYPE, 'hp_price', '50.00' );
WPDO_Zone_Hot::set( 2, self::POST_TYPE, 'hp_price', '75.00' );
$val = WPDO_Zone_Hot::get( 2, self::POST_TYPE, 'hp_price' );
$this->assertSame( '75.00', $val );
}
public function test_set_featured_integer_field(): void {
WPDO_Zone_Hot::set( 3, self::POST_TYPE, 'hp_featured', '1' );
$val = WPDO_Zone_Hot::get( 3, self::POST_TYPE, 'hp_featured' );
$this->assertSame( '1', $val );
}
// ── set_many / get_row ───────────────────────────────────────────────
public function test_set_many_stores_multiple_columns(): void {
WPDO_Zone_Hot::set_many( 4, self::POST_TYPE, [
'hp_price' => '299.00',
'hp_featured' => '1',
] );
$row = WPDO_Zone_Hot::get_row( 4, self::POST_TYPE );
$this->assertIsArray( $row );
$this->assertSame( '299.00', $row['hp_price'] );
$this->assertSame( '1', $row['hp_featured'] );
}
public function test_set_many_overwrites_on_second_call(): void {
WPDO_Zone_Hot::set_many( 5, self::POST_TYPE, [ 'hp_price' => '100.00', 'hp_featured' => '0' ] );
WPDO_Zone_Hot::set_many( 5, self::POST_TYPE, [ 'hp_price' => '200.00', 'hp_featured' => '1' ] );
$row = WPDO_Zone_Hot::get_row( 5, self::POST_TYPE );
$this->assertSame( '200.00', $row['hp_price'] );
$this->assertSame( '1', $row['hp_featured'] );
}
public function test_get_row_returns_null_for_missing_post(): void {
$row = WPDO_Zone_Hot::get_row( 9998, self::POST_TYPE );
$this->assertNull( $row );
}
// ── delete ───────────────────────────────────────────────────────────
public function test_delete_removes_row(): void {
WPDO_Zone_Hot::set( 6, self::POST_TYPE, 'hp_price', '42.00' );
$this->assertNotNull( WPDO_Zone_Hot::get( 6, self::POST_TYPE, 'hp_price' ) );
WPDO_Zone_Hot::delete( 6, self::POST_TYPE );
$this->assertNull( WPDO_Zone_Hot::get( 6, self::POST_TYPE, 'hp_price' ) );
}
public function test_delete_nonexistent_post_does_not_error(): void {
// Should complete without throwing.
WPDO_Zone_Hot::delete( 9997, self::POST_TYPE );
$this->assertTrue( true );
}
// ── isolation ────────────────────────────────────────────────────────
public function test_different_post_ids_are_independent(): void {
WPDO_Zone_Hot::set( 10, self::POST_TYPE, 'hp_price', '10.00' );
WPDO_Zone_Hot::set( 11, self::POST_TYPE, 'hp_price', '11.00' );
$this->assertSame( '10.00', WPDO_Zone_Hot::get( 10, self::POST_TYPE, 'hp_price' ) );
$this->assertSame( '11.00', WPDO_Zone_Hot::get( 11, self::POST_TYPE, 'hp_price' ) );
}
}